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