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 #include <comphelper/componentmodule.hxx>
27 
28 /** === begin UNO includes === **/
29 /** === end UNO includes === **/
30 #include <comphelper/sequence.hxx>
31 #include <osl/diagnose.h>
32 
33 #include <vector>
34 
35 //........................................................................
36 namespace comphelper
37 {
38 //........................................................................
39 
40     using namespace ::cppu;
41     /** === being UNO using === **/
42     using ::com::sun::star::uno::Sequence;
43     using ::com::sun::star::uno::RuntimeException;
44     using ::com::sun::star::uno::Reference;
45     using ::com::sun::star::lang::XMultiServiceFactory;
46     using ::com::sun::star::registry::XRegistryKey;
47     using ::com::sun::star::uno::Exception;
48     using ::com::sun::star::uno::XInterface;
49     /** === end UNO using === **/
50 
51     typedef ::std::vector< ComponentDescription >   ComponentDescriptions;
52 
53 	//=========================================================================
54 	//= OModuleImpl
55 	//=========================================================================
56 	/** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
57 	*/
58 	class OModuleImpl
59 	{
60     public:
61         ComponentDescriptions                           m_aRegisteredComponents;
62 
63 		OModuleImpl();
64 		~OModuleImpl();
65 	};
66 
67 	//-------------------------------------------------------------------------
OModuleImpl()68 	OModuleImpl::OModuleImpl()
69 	{
70 	}
71 
72 	//-------------------------------------------------------------------------
~OModuleImpl()73 	OModuleImpl::~OModuleImpl()
74 	{
75 	}
76 
77 	//=========================================================================
78 	//= OModule
79 	//=========================================================================
80 	//-------------------------------------------------------------------------
OModule()81     OModule::OModule()
82         :m_nClients( 0 )
83 	    ,m_pImpl( new OModuleImpl )
84     {
85     }
86 
~OModule()87     OModule::~OModule() {}
88 
89 	//-------------------------------------------------------------------------
registerClient(OModule::ClientAccess)90     void OModule::registerClient( OModule::ClientAccess )
91 	{
92 		::osl::MutexGuard aGuard(m_aMutex);
93         if ( 1 == osl_incrementInterlockedCount( &m_nClients ) )
94             onFirstClient();
95 	}
96 
97 	//-------------------------------------------------------------------------
revokeClient(OModule::ClientAccess)98 	void OModule::revokeClient( OModule::ClientAccess )
99 	{
100 		::osl::MutexGuard aGuard(m_aMutex);
101         if ( 0 == osl_decrementInterlockedCount( &m_nClients ) )
102             onLastClient();
103 	}
104 
105 	//--------------------------------------------------------------------------
onFirstClient()106     void OModule::onFirstClient()
107     {
108     }
109 
110 	//--------------------------------------------------------------------------
onLastClient()111     void OModule::onLastClient()
112     {
113     }
114 
115     //--------------------------------------------------------------------------
registerImplementation(const ComponentDescription & _rComp)116     void OModule::registerImplementation( const ComponentDescription& _rComp )
117     {
118         ::osl::MutexGuard aGuard( m_aMutex );
119         if ( !m_pImpl )
120             throw RuntimeException();
121 
122         m_pImpl->m_aRegisteredComponents.push_back( _rComp );
123     }
124 
125 	//--------------------------------------------------------------------------
registerImplementation(const::rtl::OUString & _rImplementationName,const::com::sun::star::uno::Sequence<::rtl::OUString> & _rServiceNames,::cppu::ComponentFactoryFunc _pCreateFunction,FactoryInstantiation _pFactoryFunction)126     void OModule::registerImplementation( const ::rtl::OUString& _rImplementationName, const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
127         ::cppu::ComponentFactoryFunc _pCreateFunction, FactoryInstantiation _pFactoryFunction )
128 	{
129         ComponentDescription aComponent( _rImplementationName, _rServiceNames, ::rtl::OUString(), _pCreateFunction, _pFactoryFunction );
130         registerImplementation( aComponent );
131 	}
132 
133 	//--------------------------------------------------------------------------
getComponentFactory(const sal_Char * _pImplementationName,void * _pServiceManager,void *)134 	void* OModule::getComponentFactory( const sal_Char* _pImplementationName, void* _pServiceManager, void* /*_pRegistryKey*/ )
135 	{
136         Reference< XInterface > xFactory( getComponentFactory(
137             ::rtl::OUString::createFromAscii( _pImplementationName ),
138             Reference< XMultiServiceFactory >( static_cast< XMultiServiceFactory* >( _pServiceManager ) )
139         ) );
140         return xFactory.get();
141     }
142 
143 	//--------------------------------------------------------------------------
getComponentFactory(const::rtl::OUString & _rImplementationName,const Reference<XMultiServiceFactory> &)144 	Reference< XInterface > OModule::getComponentFactory( const ::rtl::OUString& _rImplementationName,
145         const Reference< XMultiServiceFactory >& /* _rxServiceManager */ )
146 	{
147 		Reference< XInterface > xReturn;
148 
149         for (   ComponentDescriptions::const_iterator component = m_pImpl->m_aRegisteredComponents.begin();
150                 component != m_pImpl->m_aRegisteredComponents.end();
151                 ++component
152             )
153 		{
154 			if ( component->sImplementationName == _rImplementationName )
155 			{
156 				xReturn = component->pFactoryCreationFunc(
157                     component->pComponentCreationFunc,
158                     component->sImplementationName,
159                     component->aSupportedServices,
160                     NULL
161                 );
162 				if ( xReturn.is() )
163 				{
164 					xReturn->acquire();
165 					return xReturn.get();
166 				}
167 			}
168 		}
169 
170 		return NULL;
171 	}
172 
173 //........................................................................
174 } // namespace comphelper
175 //........................................................................
176