1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "comphelper/legacysingletonfactory.hxx" 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir /** === begin UNO includes === **/ 34*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 35*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp> 36*cdf0e10cSrcweir /** === end UNO includes === **/ 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx> 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include <algorithm> 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir //........................................................................ 43*cdf0e10cSrcweir namespace comphelper 44*cdf0e10cSrcweir { 45*cdf0e10cSrcweir //........................................................................ 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir /** === begin UNO using === **/ 48*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 49*cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 50*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 51*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 52*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW; 53*cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 54*cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 55*cdf0e10cSrcweir using ::com::sun::star::uno::Any; 56*cdf0e10cSrcweir using ::com::sun::star::uno::makeAny; 57*cdf0e10cSrcweir using ::com::sun::star::lang::XSingleComponentFactory; 58*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 59*cdf0e10cSrcweir using ::com::sun::star::uno::XComponentContext; 60*cdf0e10cSrcweir using ::com::sun::star::lang::XServiceInfo; 61*cdf0e10cSrcweir using ::com::sun::star::lang::XInitialization; 62*cdf0e10cSrcweir /** === end UNO using === **/ 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir //==================================================================== 65*cdf0e10cSrcweir //= LegacySingletonFactory 66*cdf0e10cSrcweir //==================================================================== 67*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2 < XServiceInfo 68*cdf0e10cSrcweir , XSingleComponentFactory 69*cdf0e10cSrcweir > LegacySingletonFactory_Base; 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir class COMPHELPER_DLLPRIVATE LegacySingletonFactory : public LegacySingletonFactory_Base 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir public: 74*cdf0e10cSrcweir LegacySingletonFactory( 75*cdf0e10cSrcweir ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName, 76*cdf0e10cSrcweir const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount 77*cdf0e10cSrcweir ); 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir // XServiceInfo 80*cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (RuntimeException); 81*cdf0e10cSrcweir virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException); 82*cdf0e10cSrcweir virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException); 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir // XSingleComponentFactory 85*cdf0e10cSrcweir virtual Reference< XInterface > SAL_CALL createInstanceWithContext( const Reference< XComponentContext >& Context ) throw (Exception, RuntimeException); 86*cdf0e10cSrcweir virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext( const Sequence< Any >& Arguments, const Reference< XComponentContext >& Context ) throw (Exception, RuntimeException); 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir protected: 89*cdf0e10cSrcweir ~LegacySingletonFactory(); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir private: 92*cdf0e10cSrcweir /** creates m_xInstance, returns whether it actually was created (<TRUE/>) or existed before (<FALSE/> 93*cdf0e10cSrcweir */ 94*cdf0e10cSrcweir bool impl_nts_ensureInstance( const Reference< XComponentContext >& _rxContext ); 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir private: 97*cdf0e10cSrcweir ::osl::Mutex m_aMutex; 98*cdf0e10cSrcweir ::cppu::ComponentFactoryFunc m_componentFactoryFunc; 99*cdf0e10cSrcweir ::rtl::OUString m_sImplementationName; 100*cdf0e10cSrcweir Sequence< ::rtl::OUString > m_aServiceNames; 101*cdf0e10cSrcweir rtl_ModuleCount* m_pModuleCount; 102*cdf0e10cSrcweir Reference< XInterface > m_xTheInstance; 103*cdf0e10cSrcweir }; 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir //-------------------------------------------------------------------- 106*cdf0e10cSrcweir LegacySingletonFactory::LegacySingletonFactory( ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName, 107*cdf0e10cSrcweir const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount ) 108*cdf0e10cSrcweir :m_componentFactoryFunc ( _componentFactoryFunc ) 109*cdf0e10cSrcweir ,m_sImplementationName ( _rImplementationName ) 110*cdf0e10cSrcweir ,m_aServiceNames ( _rServiceNames ) 111*cdf0e10cSrcweir ,m_pModuleCount ( _pModCount ) 112*cdf0e10cSrcweir ,m_xTheInstance ( ) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir if ( m_pModuleCount ) 115*cdf0e10cSrcweir m_pModuleCount->acquire( m_pModuleCount ); 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir //-------------------------------------------------------------------- 119*cdf0e10cSrcweir LegacySingletonFactory::~LegacySingletonFactory() 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir if ( m_pModuleCount ) 122*cdf0e10cSrcweir m_pModuleCount->release( m_pModuleCount ); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir //-------------------------------------------------------------------- 126*cdf0e10cSrcweir ::rtl::OUString SAL_CALL LegacySingletonFactory::getImplementationName( ) throw (RuntimeException) 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir return m_sImplementationName; 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir //-------------------------------------------------------------------- 132*cdf0e10cSrcweir ::sal_Bool SAL_CALL LegacySingletonFactory::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException) 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir Sequence< ::rtl::OUString > aServices( getSupportedServiceNames() ); 135*cdf0e10cSrcweir const ::rtl::OUString* pStart = aServices.getConstArray(); 136*cdf0e10cSrcweir const ::rtl::OUString* pEnd = aServices.getConstArray() + aServices.getLength(); 137*cdf0e10cSrcweir return ::std::find( pStart, pEnd, _rServiceName ) != pEnd; 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir //-------------------------------------------------------------------- 141*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL LegacySingletonFactory::getSupportedServiceNames( ) throw (RuntimeException) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir return m_aServiceNames; 144*cdf0e10cSrcweir } 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir //-------------------------------------------------------------------- 147*cdf0e10cSrcweir bool LegacySingletonFactory::impl_nts_ensureInstance( const Reference< XComponentContext >& _rxContext ) 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir if ( m_xTheInstance.is() ) 150*cdf0e10cSrcweir return false; 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir m_xTheInstance = (*m_componentFactoryFunc)( _rxContext ); 153*cdf0e10cSrcweir if ( !m_xTheInstance.is() ) 154*cdf0e10cSrcweir throw RuntimeException(); 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir return true; // true -> "was newly created" 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir //-------------------------------------------------------------------- 160*cdf0e10cSrcweir Reference< XInterface > SAL_CALL LegacySingletonFactory::createInstanceWithContext( const Reference< XComponentContext >& _rxContext ) throw (Exception, RuntimeException) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 163*cdf0e10cSrcweir impl_nts_ensureInstance( _rxContext ); 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir return m_xTheInstance; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir //-------------------------------------------------------------------- 169*cdf0e10cSrcweir Reference< XInterface > SAL_CALL LegacySingletonFactory::createInstanceWithArgumentsAndContext( const Sequence< Any >& _rArguments, const Reference< XComponentContext >& _rxContext ) throw (Exception, RuntimeException) 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 172*cdf0e10cSrcweir if ( !impl_nts_ensureInstance( _rxContext ) ) 173*cdf0e10cSrcweir throw RuntimeException( 174*cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Instance already created before, unable to initialize it." ) ), 175*cdf0e10cSrcweir *this 176*cdf0e10cSrcweir ); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir Reference< XInitialization > xInit( m_xTheInstance, UNO_QUERY_THROW ); 179*cdf0e10cSrcweir xInit->initialize( _rArguments ); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir return m_xTheInstance; 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir //==================================================================== 185*cdf0e10cSrcweir //= createLegacySingletonFactory 186*cdf0e10cSrcweir //==================================================================== 187*cdf0e10cSrcweir Reference< XSingleComponentFactory > createLegacySingletonFactory( 188*cdf0e10cSrcweir ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName, 189*cdf0e10cSrcweir const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount ) 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir return new LegacySingletonFactory( _componentFactoryFunc, _rImplementationName, _rServiceNames, _pModCount ); 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir //........................................................................ 196*cdf0e10cSrcweir } // namespace comphelper 197*cdf0e10cSrcweir //........................................................................ 198