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