1*dde7d3faSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*dde7d3faSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*dde7d3faSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*dde7d3faSAndrew Rist * distributed with this work for additional information 6*dde7d3faSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*dde7d3faSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*dde7d3faSAndrew Rist * "License"); you may not use this file except in compliance 9*dde7d3faSAndrew Rist * with the License. You may obtain a copy of the License at 10*dde7d3faSAndrew Rist * 11*dde7d3faSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*dde7d3faSAndrew Rist * 13*dde7d3faSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*dde7d3faSAndrew Rist * software distributed under the License is distributed on an 15*dde7d3faSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*dde7d3faSAndrew Rist * KIND, either express or implied. See the License for the 17*dde7d3faSAndrew Rist * specific language governing permissions and limitations 18*dde7d3faSAndrew Rist * under the License. 19*dde7d3faSAndrew Rist * 20*dde7d3faSAndrew Rist *************************************************************/ 21*dde7d3faSAndrew Rist 22*dde7d3faSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "comphelper/legacysingletonfactory.hxx" 28cdf0e10cSrcweir 29cdf0e10cSrcweir /** === begin UNO includes === **/ 30cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 31cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp> 32cdf0e10cSrcweir /** === end UNO includes === **/ 33cdf0e10cSrcweir 34cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx> 35cdf0e10cSrcweir 36cdf0e10cSrcweir #include <algorithm> 37cdf0e10cSrcweir 38cdf0e10cSrcweir //........................................................................ 39cdf0e10cSrcweir namespace comphelper 40cdf0e10cSrcweir { 41cdf0e10cSrcweir //........................................................................ 42cdf0e10cSrcweir 43cdf0e10cSrcweir /** === begin UNO using === **/ 44cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 45cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 46cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 47cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 48cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW; 49cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 50cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 51cdf0e10cSrcweir using ::com::sun::star::uno::Any; 52cdf0e10cSrcweir using ::com::sun::star::uno::makeAny; 53cdf0e10cSrcweir using ::com::sun::star::lang::XSingleComponentFactory; 54cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 55cdf0e10cSrcweir using ::com::sun::star::uno::XComponentContext; 56cdf0e10cSrcweir using ::com::sun::star::lang::XServiceInfo; 57cdf0e10cSrcweir using ::com::sun::star::lang::XInitialization; 58cdf0e10cSrcweir /** === end UNO using === **/ 59cdf0e10cSrcweir 60cdf0e10cSrcweir //==================================================================== 61cdf0e10cSrcweir //= LegacySingletonFactory 62cdf0e10cSrcweir //==================================================================== 63cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2 < XServiceInfo 64cdf0e10cSrcweir , XSingleComponentFactory 65cdf0e10cSrcweir > LegacySingletonFactory_Base; 66cdf0e10cSrcweir 67cdf0e10cSrcweir class COMPHELPER_DLLPRIVATE LegacySingletonFactory : public LegacySingletonFactory_Base 68cdf0e10cSrcweir { 69cdf0e10cSrcweir public: 70cdf0e10cSrcweir LegacySingletonFactory( 71cdf0e10cSrcweir ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName, 72cdf0e10cSrcweir const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount 73cdf0e10cSrcweir ); 74cdf0e10cSrcweir 75cdf0e10cSrcweir // XServiceInfo 76cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (RuntimeException); 77cdf0e10cSrcweir virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException); 78cdf0e10cSrcweir virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException); 79cdf0e10cSrcweir 80cdf0e10cSrcweir // XSingleComponentFactory 81cdf0e10cSrcweir virtual Reference< XInterface > SAL_CALL createInstanceWithContext( const Reference< XComponentContext >& Context ) throw (Exception, RuntimeException); 82cdf0e10cSrcweir virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext( const Sequence< Any >& Arguments, const Reference< XComponentContext >& Context ) throw (Exception, RuntimeException); 83cdf0e10cSrcweir 84cdf0e10cSrcweir protected: 85cdf0e10cSrcweir ~LegacySingletonFactory(); 86cdf0e10cSrcweir 87cdf0e10cSrcweir private: 88cdf0e10cSrcweir /** creates m_xInstance, returns whether it actually was created (<TRUE/>) or existed before (<FALSE/> 89cdf0e10cSrcweir */ 90cdf0e10cSrcweir bool impl_nts_ensureInstance( const Reference< XComponentContext >& _rxContext ); 91cdf0e10cSrcweir 92cdf0e10cSrcweir private: 93cdf0e10cSrcweir ::osl::Mutex m_aMutex; 94cdf0e10cSrcweir ::cppu::ComponentFactoryFunc m_componentFactoryFunc; 95cdf0e10cSrcweir ::rtl::OUString m_sImplementationName; 96cdf0e10cSrcweir Sequence< ::rtl::OUString > m_aServiceNames; 97cdf0e10cSrcweir rtl_ModuleCount* m_pModuleCount; 98cdf0e10cSrcweir Reference< XInterface > m_xTheInstance; 99cdf0e10cSrcweir }; 100cdf0e10cSrcweir 101cdf0e10cSrcweir //-------------------------------------------------------------------- LegacySingletonFactory(::cppu::ComponentFactoryFunc _componentFactoryFunc,const::rtl::OUString & _rImplementationName,const Sequence<::rtl::OUString> & _rServiceNames,rtl_ModuleCount * _pModCount)102cdf0e10cSrcweir LegacySingletonFactory::LegacySingletonFactory( ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName, 103cdf0e10cSrcweir const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount ) 104cdf0e10cSrcweir :m_componentFactoryFunc ( _componentFactoryFunc ) 105cdf0e10cSrcweir ,m_sImplementationName ( _rImplementationName ) 106cdf0e10cSrcweir ,m_aServiceNames ( _rServiceNames ) 107cdf0e10cSrcweir ,m_pModuleCount ( _pModCount ) 108cdf0e10cSrcweir ,m_xTheInstance ( ) 109cdf0e10cSrcweir { 110cdf0e10cSrcweir if ( m_pModuleCount ) 111cdf0e10cSrcweir m_pModuleCount->acquire( m_pModuleCount ); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir //-------------------------------------------------------------------- ~LegacySingletonFactory()115cdf0e10cSrcweir LegacySingletonFactory::~LegacySingletonFactory() 116cdf0e10cSrcweir { 117cdf0e10cSrcweir if ( m_pModuleCount ) 118cdf0e10cSrcweir m_pModuleCount->release( m_pModuleCount ); 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir //-------------------------------------------------------------------- getImplementationName()122cdf0e10cSrcweir ::rtl::OUString SAL_CALL LegacySingletonFactory::getImplementationName( ) throw (RuntimeException) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir return m_sImplementationName; 125cdf0e10cSrcweir } 126cdf0e10cSrcweir 127cdf0e10cSrcweir //-------------------------------------------------------------------- supportsService(const::rtl::OUString & _rServiceName)128cdf0e10cSrcweir ::sal_Bool SAL_CALL LegacySingletonFactory::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir Sequence< ::rtl::OUString > aServices( getSupportedServiceNames() ); 131cdf0e10cSrcweir const ::rtl::OUString* pStart = aServices.getConstArray(); 132cdf0e10cSrcweir const ::rtl::OUString* pEnd = aServices.getConstArray() + aServices.getLength(); 133cdf0e10cSrcweir return ::std::find( pStart, pEnd, _rServiceName ) != pEnd; 134cdf0e10cSrcweir } 135cdf0e10cSrcweir 136cdf0e10cSrcweir //-------------------------------------------------------------------- getSupportedServiceNames()137cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL LegacySingletonFactory::getSupportedServiceNames( ) throw (RuntimeException) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir return m_aServiceNames; 140cdf0e10cSrcweir } 141cdf0e10cSrcweir 142cdf0e10cSrcweir //-------------------------------------------------------------------- impl_nts_ensureInstance(const Reference<XComponentContext> & _rxContext)143cdf0e10cSrcweir bool LegacySingletonFactory::impl_nts_ensureInstance( const Reference< XComponentContext >& _rxContext ) 144cdf0e10cSrcweir { 145cdf0e10cSrcweir if ( m_xTheInstance.is() ) 146cdf0e10cSrcweir return false; 147cdf0e10cSrcweir 148cdf0e10cSrcweir m_xTheInstance = (*m_componentFactoryFunc)( _rxContext ); 149cdf0e10cSrcweir if ( !m_xTheInstance.is() ) 150cdf0e10cSrcweir throw RuntimeException(); 151cdf0e10cSrcweir 152cdf0e10cSrcweir return true; // true -> "was newly created" 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir //-------------------------------------------------------------------- createInstanceWithContext(const Reference<XComponentContext> & _rxContext)156cdf0e10cSrcweir Reference< XInterface > SAL_CALL LegacySingletonFactory::createInstanceWithContext( const Reference< XComponentContext >& _rxContext ) throw (Exception, RuntimeException) 157cdf0e10cSrcweir { 158cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 159cdf0e10cSrcweir impl_nts_ensureInstance( _rxContext ); 160cdf0e10cSrcweir 161cdf0e10cSrcweir return m_xTheInstance; 162cdf0e10cSrcweir } 163cdf0e10cSrcweir 164cdf0e10cSrcweir //-------------------------------------------------------------------- createInstanceWithArgumentsAndContext(const Sequence<Any> & _rArguments,const Reference<XComponentContext> & _rxContext)165cdf0e10cSrcweir Reference< XInterface > SAL_CALL LegacySingletonFactory::createInstanceWithArgumentsAndContext( const Sequence< Any >& _rArguments, const Reference< XComponentContext >& _rxContext ) throw (Exception, RuntimeException) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 168cdf0e10cSrcweir if ( !impl_nts_ensureInstance( _rxContext ) ) 169cdf0e10cSrcweir throw RuntimeException( 170cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Instance already created before, unable to initialize it." ) ), 171cdf0e10cSrcweir *this 172cdf0e10cSrcweir ); 173cdf0e10cSrcweir 174cdf0e10cSrcweir Reference< XInitialization > xInit( m_xTheInstance, UNO_QUERY_THROW ); 175cdf0e10cSrcweir xInit->initialize( _rArguments ); 176cdf0e10cSrcweir 177cdf0e10cSrcweir return m_xTheInstance; 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir //==================================================================== 181cdf0e10cSrcweir //= createLegacySingletonFactory 182cdf0e10cSrcweir //==================================================================== createLegacySingletonFactory(::cppu::ComponentFactoryFunc _componentFactoryFunc,const::rtl::OUString & _rImplementationName,const Sequence<::rtl::OUString> & _rServiceNames,rtl_ModuleCount * _pModCount)183cdf0e10cSrcweir Reference< XSingleComponentFactory > createLegacySingletonFactory( 184cdf0e10cSrcweir ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName, 185cdf0e10cSrcweir const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount ) 186cdf0e10cSrcweir { 187cdf0e10cSrcweir return new LegacySingletonFactory( _componentFactoryFunc, _rImplementationName, _rServiceNames, _pModCount ); 188cdf0e10cSrcweir } 189cdf0e10cSrcweir 190cdf0e10cSrcweir 191cdf0e10cSrcweir //........................................................................ 192cdf0e10cSrcweir } // namespace comphelper 193cdf0e10cSrcweir //........................................................................ 194