1*34dd1e25SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include <osl/interlck.h> 25cdf0e10cSrcweir #include <osl/mutex.hxx> 26cdf0e10cSrcweir #include <rtl/uuid.h> 27cdf0e10cSrcweir #include <cppuhelper/factory.hxx> 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 30cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp> 31cdf0e10cSrcweir #include <my_module/XSomething.hpp> 32cdf0e10cSrcweir 33cdf0e10cSrcweir 34cdf0e10cSrcweir using namespace ::rtl; // for OUString 35cdf0e10cSrcweir using namespace ::com::sun::star; // for odk interfaces 36cdf0e10cSrcweir using namespace ::com::sun::star::uno; // for basic types 37cdf0e10cSrcweir 38cdf0e10cSrcweir namespace my_sc_impl 39cdf0e10cSrcweir { 40cdf0e10cSrcweir 41cdf0e10cSrcweir Sequence< OUString > SAL_CALL getSupportedServiceNames_MyService1Impl() 42cdf0e10cSrcweir { 43cdf0e10cSrcweir Sequence< OUString > names(1); 44cdf0e10cSrcweir names[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1")); 45cdf0e10cSrcweir return names; 46cdf0e10cSrcweir } 47cdf0e10cSrcweir 48cdf0e10cSrcweir OUString SAL_CALL getImplementationName_MyService1Impl() 49cdf0e10cSrcweir { 50cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( 51cdf0e10cSrcweir "my_module.my_sc_implementation.MyService1") ); 52cdf0e10cSrcweir } 53cdf0e10cSrcweir 54cdf0e10cSrcweir 55cdf0e10cSrcweir class MyService1Impl 56cdf0e10cSrcweir : public ::my_module::XSomething 57cdf0e10cSrcweir , public lang::XServiceInfo 58cdf0e10cSrcweir , public lang::XTypeProvider 59cdf0e10cSrcweir { 60cdf0e10cSrcweir oslInterlockedCount m_refcount; 61cdf0e10cSrcweir OUString m_sData; 62cdf0e10cSrcweir // it's good practise to store the context for further use when you use 63cdf0e10cSrcweir // other UNO API's in your implementation 64cdf0e10cSrcweir Reference< XComponentContext > m_xContext; 65cdf0e10cSrcweir public: 66cdf0e10cSrcweir inline MyService1Impl(Reference< XComponentContext > const & xContext) throw () 67cdf0e10cSrcweir : m_refcount( 0 ), 68cdf0e10cSrcweir m_xContext(xContext) 69cdf0e10cSrcweir {} 70cdf0e10cSrcweir 71cdf0e10cSrcweir virtual ~MyService1Impl() {} 72cdf0e10cSrcweir 73cdf0e10cSrcweir // XInterface 74cdf0e10cSrcweir virtual Any SAL_CALL queryInterface( Type const & type ) 75cdf0e10cSrcweir throw (RuntimeException); 76cdf0e10cSrcweir virtual void SAL_CALL acquire() 77cdf0e10cSrcweir throw (); 78cdf0e10cSrcweir virtual void SAL_CALL release() 79cdf0e10cSrcweir throw (); 80cdf0e10cSrcweir // XTypeProvider 81cdf0e10cSrcweir virtual Sequence< Type > SAL_CALL getTypes() 82cdf0e10cSrcweir throw (RuntimeException); 83cdf0e10cSrcweir virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() 84cdf0e10cSrcweir throw (RuntimeException); 85cdf0e10cSrcweir // XSomething 86cdf0e10cSrcweir virtual OUString SAL_CALL methodOne( OUString const & str ) 87cdf0e10cSrcweir throw (RuntimeException); 88cdf0e10cSrcweir virtual OUString SAL_CALL methodTwo( ) 89cdf0e10cSrcweir throw (RuntimeException); 90cdf0e10cSrcweir // XServiceInfo 91cdf0e10cSrcweir virtual OUString SAL_CALL getImplementationName() 92cdf0e10cSrcweir throw (RuntimeException); 93cdf0e10cSrcweir virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) 94cdf0e10cSrcweir throw (RuntimeException); 95cdf0e10cSrcweir virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() 96cdf0e10cSrcweir throw (RuntimeException); 97cdf0e10cSrcweir }; 98cdf0e10cSrcweir 99cdf0e10cSrcweir // XInterface implementation 100cdf0e10cSrcweir Any MyService1Impl::queryInterface( Type const & type ) 101cdf0e10cSrcweir throw (RuntimeException) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir if (type.equals(::cppu::UnoType< Reference< XInterface > >::get())) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir // return XInterface interface 106cdf0e10cSrcweir // (resolve ambiguity by casting to lang::XTypeProvider) 107cdf0e10cSrcweir Reference< XInterface > x( 108cdf0e10cSrcweir static_cast< lang::XTypeProvider * >( this ) ); 109cdf0e10cSrcweir return makeAny( x ); 110cdf0e10cSrcweir } 111cdf0e10cSrcweir if (type.equals(::cppu::UnoType< Reference< lang::XTypeProvider > >::get())) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir // return XInterface interface 114cdf0e10cSrcweir Reference< XInterface > x( 115cdf0e10cSrcweir static_cast< lang::XTypeProvider * >( this ) ); 116cdf0e10cSrcweir return makeAny( x ); 117cdf0e10cSrcweir } 118cdf0e10cSrcweir if (type.equals(::cppu::UnoType< Reference< lang::XServiceInfo > >::get())) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir // return XServiceInfo interface 121cdf0e10cSrcweir Reference< lang::XServiceInfo > x( 122cdf0e10cSrcweir static_cast< lang::XServiceInfo * >( this ) ); 123cdf0e10cSrcweir return makeAny( x ); 124cdf0e10cSrcweir } 125cdf0e10cSrcweir if (type.equals(::cppu::UnoType< Reference< ::my_module::XSomething > >::get())) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir // return sample interface 128cdf0e10cSrcweir Reference< ::my_module::XSomething > x( 129cdf0e10cSrcweir static_cast< ::my_module::XSomething * >( this ) ); 130cdf0e10cSrcweir return makeAny( x ); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir // querying for unsupported type 133cdf0e10cSrcweir return Any(); 134cdf0e10cSrcweir } 135cdf0e10cSrcweir 136cdf0e10cSrcweir void MyService1Impl::acquire() 137cdf0e10cSrcweir throw () 138cdf0e10cSrcweir { 139cdf0e10cSrcweir // thread-safe incrementation of reference count 140cdf0e10cSrcweir ::osl_incrementInterlockedCount( &m_refcount ); 141cdf0e10cSrcweir } 142cdf0e10cSrcweir 143cdf0e10cSrcweir void MyService1Impl::release() 144cdf0e10cSrcweir throw () 145cdf0e10cSrcweir { 146cdf0e10cSrcweir // thread-safe decrementation of reference count 147cdf0e10cSrcweir if (0 == ::osl_decrementInterlockedCount( &m_refcount )) 148cdf0e10cSrcweir { 149cdf0e10cSrcweir delete this; // shutdown this object 150cdf0e10cSrcweir } 151cdf0e10cSrcweir } 152cdf0e10cSrcweir 153cdf0e10cSrcweir // XTypeProvider implementation 154cdf0e10cSrcweir Sequence< Type > MyService1Impl::getTypes() 155cdf0e10cSrcweir throw (RuntimeException) 156cdf0e10cSrcweir { 157cdf0e10cSrcweir Sequence< Type > seq( 3 ); 158cdf0e10cSrcweir seq[ 0 ] = ::cppu::UnoType< Reference< lang::XTypeProvider > >::get(); 159cdf0e10cSrcweir seq[ 1 ] = ::cppu::UnoType< Reference< lang::XServiceInfo > >::get(); 160cdf0e10cSrcweir seq[ 2 ] = ::cppu::UnoType< Reference< ::my_module::XSomething > >::get(); 161cdf0e10cSrcweir return seq; 162cdf0e10cSrcweir } 163cdf0e10cSrcweir Sequence< sal_Int8 > MyService1Impl::getImplementationId() 164cdf0e10cSrcweir throw (RuntimeException) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir static Sequence< sal_Int8 > * s_pId = 0; 167cdf0e10cSrcweir if (! s_pId) 168cdf0e10cSrcweir { 169cdf0e10cSrcweir // create unique id 170cdf0e10cSrcweir Sequence< sal_Int8 > id( 16 ); 171cdf0e10cSrcweir ::rtl_createUuid( (sal_uInt8 *)id.getArray(), 0, sal_True ); 172cdf0e10cSrcweir // guard initialization with some mutex 173cdf0e10cSrcweir ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() ); 174cdf0e10cSrcweir if (! s_pId) 175cdf0e10cSrcweir { 176cdf0e10cSrcweir static Sequence< sal_Int8 > s_id( id ); 177cdf0e10cSrcweir s_pId = &s_id; 178cdf0e10cSrcweir } 179cdf0e10cSrcweir } 180cdf0e10cSrcweir return *s_pId; 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir // XSomething implementation 184cdf0e10cSrcweir OUString MyService1Impl::methodOne( OUString const & str ) 185cdf0e10cSrcweir throw (RuntimeException) 186cdf0e10cSrcweir { 187cdf0e10cSrcweir m_sData = str; 188cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( 189cdf0e10cSrcweir "called methodOne() of MyService1 implementation: ") ) + m_sData; 190cdf0e10cSrcweir } 191cdf0e10cSrcweir 192cdf0e10cSrcweir OUString MyService1Impl::methodTwo( ) 193cdf0e10cSrcweir throw (RuntimeException) 194cdf0e10cSrcweir { 195cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( 196cdf0e10cSrcweir "called methodTwo() of MyService1 implementation: ") ) + m_sData; 197cdf0e10cSrcweir } 198cdf0e10cSrcweir 199cdf0e10cSrcweir // XServiceInfo implementation 200cdf0e10cSrcweir OUString MyService1Impl::getImplementationName() 201cdf0e10cSrcweir throw (RuntimeException) 202cdf0e10cSrcweir { 203cdf0e10cSrcweir // unique implementation name 204cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( 205cdf0e10cSrcweir "my_module.my_sc_implementation.MyService1") ); 206cdf0e10cSrcweir } 207cdf0e10cSrcweir sal_Bool MyService1Impl::supportsService( OUString const & serviceName ) 208cdf0e10cSrcweir throw (RuntimeException) 209cdf0e10cSrcweir { 210cdf0e10cSrcweir // this object only supports one service, so the test is simple 211cdf0e10cSrcweir return serviceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 212cdf0e10cSrcweir "my_module.MyService1") ); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir Sequence< OUString > MyService1Impl::getSupportedServiceNames() 215cdf0e10cSrcweir throw (RuntimeException) 216cdf0e10cSrcweir { 217cdf0e10cSrcweir // this object only supports one service 218cdf0e10cSrcweir OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1") ); 219cdf0e10cSrcweir return Sequence< OUString >( &serviceName, 1 ); 220cdf0e10cSrcweir } 221cdf0e10cSrcweir 222cdf0e10cSrcweir Reference< XInterface > SAL_CALL create_MyService1Impl( 223cdf0e10cSrcweir Reference< XComponentContext > const & xContext ) 224cdf0e10cSrcweir SAL_THROW( () ) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir return static_cast< lang::XTypeProvider * >( new MyService1Impl( xContext) ); 227cdf0e10cSrcweir } 228cdf0e10cSrcweir 229cdf0e10cSrcweir // forward decl: implemented in service2_impl.cxx 230cdf0e10cSrcweir Reference< XInterface > SAL_CALL create_MyService2Impl( 231cdf0e10cSrcweir Reference< XComponentContext > const & ) SAL_THROW( () ); 232cdf0e10cSrcweir 233cdf0e10cSrcweir } 234cdf0e10cSrcweir 235cdf0e10cSrcweir /* 236cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment( 237cdf0e10cSrcweir sal_Char const ** ppEnvTypeName, uno_Environment ** ) 238cdf0e10cSrcweir { 239cdf0e10cSrcweir *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; 240cdf0e10cSrcweir } 241cdf0e10cSrcweir 242cdf0e10cSrcweir // This method not longer necessary since OOo 3.4 where the component registration was 243cdf0e10cSrcweir // was changed to passive component registration. For more details see 244cdf0e10cSrcweir // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration 245cdf0e10cSrcweir // 246cdf0e10cSrcweir // extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo( 247cdf0e10cSrcweir // lang::XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry ) 248cdf0e10cSrcweir // { 249cdf0e10cSrcweir // if (xRegistry) 250cdf0e10cSrcweir // { 251cdf0e10cSrcweir // try 252cdf0e10cSrcweir // { 253cdf0e10cSrcweir // // implementation of MyService1A 254cdf0e10cSrcweir // Reference< registry::XRegistryKey > xKey( 255cdf0e10cSrcweir // xRegistry->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( 256cdf0e10cSrcweir // "my_module.my_sc_implementation.MyService1/UNO/SERVICES") ) ) ); 257cdf0e10cSrcweir // // subkeys denote implemented services of implementation 258cdf0e10cSrcweir // xKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( 259cdf0e10cSrcweir // "my_module.MyService1") ) ); 260cdf0e10cSrcweir // // implementation of MyService1B 261cdf0e10cSrcweir // xKey = xRegistry->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( 262cdf0e10cSrcweir // "my_module.my_sc_implementation.MyService2/UNO/SERVICES") ) ); 263cdf0e10cSrcweir // // subkeys denote implemented services of implementation 264cdf0e10cSrcweir // xKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM( 265cdf0e10cSrcweir // "my_module.MyService2") ) ); 266cdf0e10cSrcweir // return sal_True; // success 267cdf0e10cSrcweir // } 268cdf0e10cSrcweir // catch (registry::InvalidRegistryException &) 269cdf0e10cSrcweir // { 270cdf0e10cSrcweir // // function fails if exception caught 271cdf0e10cSrcweir // } 272cdf0e10cSrcweir // } 273cdf0e10cSrcweir // return sal_False; 274cdf0e10cSrcweir // } 275cdf0e10cSrcweir 276cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory( 277cdf0e10cSrcweir sal_Char const * implName, lang::XMultiServiceFactory * xMgr, void * ) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir Reference< lang::XSingleComponentFactory > xFactory; 280cdf0e10cSrcweir if (0 == ::rtl_str_compare( implName, "my_module.my_sc_implementation.MyService1" )) 281cdf0e10cSrcweir { 282cdf0e10cSrcweir // create component factory for MyService1 implementation 283cdf0e10cSrcweir OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1") ); 284cdf0e10cSrcweir xFactory = ::cppu::createSingleComponentFactory( 285cdf0e10cSrcweir ::my_sc_impl::create_MyService1Impl, 286cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_implementation.MyService1") ), 287cdf0e10cSrcweir Sequence< OUString >( &serviceName, 1 ) ); 288cdf0e10cSrcweir } 289cdf0e10cSrcweir else if (0 == ::rtl_str_compare( implName, "my_module.my_sc_implementation.MyService2" )) 290cdf0e10cSrcweir { 291cdf0e10cSrcweir // create component factory for MyService12 implementation 292cdf0e10cSrcweir OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService2") ); 293cdf0e10cSrcweir xFactory = ::cppu::createSingleComponentFactory( 294cdf0e10cSrcweir ::my_sc_impl::create_MyService2Impl, 295cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_implementation.MyService2") ), 296cdf0e10cSrcweir Sequence< OUString >( &serviceName, 1 ) ); 297cdf0e10cSrcweir } 298cdf0e10cSrcweir if (xFactory.is()) 299cdf0e10cSrcweir xFactory->acquire(); 300cdf0e10cSrcweir return xFactory.get(); // return acquired interface pointer or null 301cdf0e10cSrcweir } 302cdf0e10cSrcweir */ 303cdf0e10cSrcweir 304