1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #ifndef _EXTENSIONS_COMPONENT_MODULE_HXX_
26cdf0e10cSrcweir #include "Acomponentmodule.hxx"
27cdf0e10cSrcweir #endif
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifndef _TOOLS_RESMGR_HXX
30cdf0e10cSrcweir #include <tools/resmgr.hxx>
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #ifndef _SOLAR_HRC
33cdf0e10cSrcweir #include <svl/solar.hrc>
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir #ifndef _COMPHELPER_SEQUENCE_HXX_
36cdf0e10cSrcweir #include <comphelper/sequence.hxx>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #ifndef _TOOLS_DEBUG_HXX
39cdf0e10cSrcweir #include <tools/debug.hxx>
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #define ENTER_MOD_METHOD()	\
43cdf0e10cSrcweir 	::osl::MutexGuard aGuard(s_aMutex);	\
44cdf0e10cSrcweir 	ensureImpl()
45cdf0e10cSrcweir 
46cdf0e10cSrcweir //.........................................................................
47cdf0e10cSrcweir namespace COMPMOD_NAMESPACE
48cdf0e10cSrcweir {
49cdf0e10cSrcweir //.........................................................................
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
52cdf0e10cSrcweir 	using namespace ::com::sun::star::lang;
53cdf0e10cSrcweir 	using namespace ::com::sun::star::registry;
54cdf0e10cSrcweir 	using namespace ::comphelper;
55cdf0e10cSrcweir 	using namespace ::cppu;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	//=========================================================================
58cdf0e10cSrcweir 	//= OModuleImpl
59cdf0e10cSrcweir 	//=========================================================================
60cdf0e10cSrcweir 	/** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
61cdf0e10cSrcweir 	*/
62cdf0e10cSrcweir 	class OModuleImpl
63cdf0e10cSrcweir 	{
64cdf0e10cSrcweir 		ResMgr*		m_pRessources;
65cdf0e10cSrcweir 		sal_Bool	m_bInitialized;
66cdf0e10cSrcweir 		ByteString	m_sFilePrefix;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	public:
69cdf0e10cSrcweir 		/// ctor
70cdf0e10cSrcweir 		OModuleImpl();
71cdf0e10cSrcweir 		~OModuleImpl();
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 		/// get the manager for the ressources of the module
74cdf0e10cSrcweir 		ResMgr*	getResManager();
setResourceFilePrefix(const::rtl::OString & _rPrefix)75cdf0e10cSrcweir 		void	setResourceFilePrefix(const ::rtl::OString& _rPrefix) { m_sFilePrefix = _rPrefix; }
76cdf0e10cSrcweir 	};
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	//-------------------------------------------------------------------------
OModuleImpl()79cdf0e10cSrcweir 	OModuleImpl::OModuleImpl()
80cdf0e10cSrcweir 		:m_pRessources(NULL)
81cdf0e10cSrcweir 		,m_bInitialized(sal_False)
82cdf0e10cSrcweir 	{
83cdf0e10cSrcweir 	}
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	//-------------------------------------------------------------------------
~OModuleImpl()86cdf0e10cSrcweir 	OModuleImpl::~OModuleImpl()
87cdf0e10cSrcweir 	{
88cdf0e10cSrcweir 		if (m_pRessources)
89cdf0e10cSrcweir 			delete m_pRessources;
90cdf0e10cSrcweir 	}
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	//-------------------------------------------------------------------------
getResManager()93cdf0e10cSrcweir 	ResMgr*	OModuleImpl::getResManager()
94cdf0e10cSrcweir 	{
95cdf0e10cSrcweir 		// note that this method is not threadsafe, which counts for the whole class !
96cdf0e10cSrcweir 		if (!m_pRessources && !m_bInitialized)
97cdf0e10cSrcweir 		{
98cdf0e10cSrcweir 			DBG_ASSERT(m_sFilePrefix.Len(), "OModuleImpl::getResManager: no resource file prefix!");
99cdf0e10cSrcweir 			// create a manager with a fixed prefix
100cdf0e10cSrcweir 			ByteString aMgrName = m_sFilePrefix;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 			m_pRessources = ResMgr::CreateResMgr(aMgrName.GetBuffer());
103cdf0e10cSrcweir 			DBG_ASSERT(m_pRessources,
104cdf0e10cSrcweir 					(ByteString("OModuleImpl::getResManager: could not create the resource manager (file name: ")
105cdf0e10cSrcweir 				+=	aMgrName
106cdf0e10cSrcweir 				+=	ByteString(")!")).GetBuffer());
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 			m_bInitialized = sal_True;
109cdf0e10cSrcweir 		}
110cdf0e10cSrcweir 		return m_pRessources;
111cdf0e10cSrcweir 	}
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	//=========================================================================
114cdf0e10cSrcweir 	//= OModule
115cdf0e10cSrcweir 	//=========================================================================
116cdf0e10cSrcweir 	::osl::Mutex	OModule::s_aMutex;
117cdf0e10cSrcweir 	sal_Int32		OModule::s_nClients = 0;
118cdf0e10cSrcweir 	OModuleImpl*	OModule::s_pImpl = NULL;
119cdf0e10cSrcweir 	::rtl::OString	OModule::s_sResPrefix;
120cdf0e10cSrcweir 	//-------------------------------------------------------------------------
getResManager()121cdf0e10cSrcweir 	ResMgr*	OModule::getResManager()
122cdf0e10cSrcweir 	{
123cdf0e10cSrcweir 		ENTER_MOD_METHOD();
124cdf0e10cSrcweir 		return s_pImpl->getResManager();
125cdf0e10cSrcweir 	}
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	//-------------------------------------------------------------------------
setResourceFilePrefix(const::rtl::OString & _rPrefix)128cdf0e10cSrcweir 	void OModule::setResourceFilePrefix(const ::rtl::OString& _rPrefix)
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		::osl::MutexGuard aGuard(s_aMutex);
131cdf0e10cSrcweir 		s_sResPrefix = _rPrefix;
132cdf0e10cSrcweir 		if (s_pImpl)
133cdf0e10cSrcweir 			s_pImpl->setResourceFilePrefix(_rPrefix);
134cdf0e10cSrcweir 	}
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	//-------------------------------------------------------------------------
registerClient()137cdf0e10cSrcweir 	void OModule::registerClient()
138cdf0e10cSrcweir 	{
139cdf0e10cSrcweir 		::osl::MutexGuard aGuard(s_aMutex);
140cdf0e10cSrcweir 		++s_nClients;
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	//-------------------------------------------------------------------------
revokeClient()144cdf0e10cSrcweir 	void OModule::revokeClient()
145cdf0e10cSrcweir 	{
146cdf0e10cSrcweir 		::osl::MutexGuard aGuard(s_aMutex);
147cdf0e10cSrcweir 		if (!--s_nClients && s_pImpl)
148cdf0e10cSrcweir 		{
149cdf0e10cSrcweir 			delete s_pImpl;
150cdf0e10cSrcweir 			s_pImpl = NULL;
151cdf0e10cSrcweir 		}
152cdf0e10cSrcweir 	}
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	//-------------------------------------------------------------------------
ensureImpl()155cdf0e10cSrcweir 	void OModule::ensureImpl()
156cdf0e10cSrcweir 	{
157cdf0e10cSrcweir 		if (s_pImpl)
158cdf0e10cSrcweir 			return;
159cdf0e10cSrcweir 		s_pImpl = new OModuleImpl();
160cdf0e10cSrcweir 		s_pImpl->setResourceFilePrefix(s_sResPrefix);
161cdf0e10cSrcweir 	}
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 	//--------------------------------------------------------------------------
164cdf0e10cSrcweir 	//- registration helper
165cdf0e10cSrcweir 	//--------------------------------------------------------------------------
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 	Sequence< ::rtl::OUString >*				OModule::s_pImplementationNames = NULL;
168cdf0e10cSrcweir 	Sequence< Sequence< ::rtl::OUString > >*	OModule::s_pSupportedServices = NULL;
169cdf0e10cSrcweir 	Sequence< sal_Int64 >*						OModule::s_pCreationFunctionPointers = NULL;
170cdf0e10cSrcweir 	Sequence< sal_Int64 >*						OModule::s_pFactoryFunctionPointers = NULL;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	//--------------------------------------------------------------------------
registerComponent(const::rtl::OUString & _rImplementationName,const Sequence<::rtl::OUString> & _rServiceNames,ComponentInstantiation _pCreateFunction,FactoryInstantiation _pFactoryFunction)173cdf0e10cSrcweir 	void OModule::registerComponent(
174cdf0e10cSrcweir 		const ::rtl::OUString& _rImplementationName,
175cdf0e10cSrcweir 		const Sequence< ::rtl::OUString >& _rServiceNames,
176cdf0e10cSrcweir 		ComponentInstantiation _pCreateFunction,
177cdf0e10cSrcweir 		FactoryInstantiation _pFactoryFunction)
178cdf0e10cSrcweir 	{
179cdf0e10cSrcweir 		if (!s_pImplementationNames)
180cdf0e10cSrcweir 		{
181cdf0e10cSrcweir 			OSL_ENSURE(!s_pSupportedServices && !s_pCreationFunctionPointers && !s_pFactoryFunctionPointers,
182cdf0e10cSrcweir 				"OModule::registerComponent : inconsistent state (the pointers (1)) !");
183cdf0e10cSrcweir 			s_pImplementationNames = new Sequence< ::rtl::OUString >;
184cdf0e10cSrcweir 			s_pSupportedServices = new Sequence< Sequence< ::rtl::OUString > >;
185cdf0e10cSrcweir 			s_pCreationFunctionPointers = new Sequence< sal_Int64 >;
186cdf0e10cSrcweir 			s_pFactoryFunctionPointers = new Sequence< sal_Int64 >;
187cdf0e10cSrcweir 		}
188cdf0e10cSrcweir 		OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
189cdf0e10cSrcweir 			"OModule::registerComponent : inconsistent state (the pointers (2)) !");
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 		OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
192cdf0e10cSrcweir 					&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
193cdf0e10cSrcweir 					&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
194cdf0e10cSrcweir 			"OModule::registerComponent : inconsistent state !");
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 		sal_Int32 nOldLen = s_pImplementationNames->getLength();
197cdf0e10cSrcweir 		s_pImplementationNames->realloc(nOldLen + 1);
198cdf0e10cSrcweir 		s_pSupportedServices->realloc(nOldLen + 1);
199cdf0e10cSrcweir 		s_pCreationFunctionPointers->realloc(nOldLen + 1);
200cdf0e10cSrcweir 		s_pFactoryFunctionPointers->realloc(nOldLen + 1);
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 		s_pImplementationNames->getArray()[nOldLen] = _rImplementationName;
203cdf0e10cSrcweir 		s_pSupportedServices->getArray()[nOldLen] = _rServiceNames;
204cdf0e10cSrcweir 		s_pCreationFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pCreateFunction);
205cdf0e10cSrcweir 		s_pFactoryFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pFactoryFunction);
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 	//--------------------------------------------------------------------------
revokeComponent(const::rtl::OUString & _rImplementationName)209cdf0e10cSrcweir 	void OModule::revokeComponent(const ::rtl::OUString& _rImplementationName)
210cdf0e10cSrcweir 	{
211cdf0e10cSrcweir 		if (!s_pImplementationNames)
212cdf0e10cSrcweir 		{
213cdf0e10cSrcweir 			OSL_ASSERT("OModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
214cdf0e10cSrcweir 			return;
215cdf0e10cSrcweir 		}
216cdf0e10cSrcweir 		OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
217cdf0e10cSrcweir 			"OModule::revokeComponent : inconsistent state (the pointers) !");
218cdf0e10cSrcweir 		OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
219cdf0e10cSrcweir 					&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
220cdf0e10cSrcweir 					&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
221cdf0e10cSrcweir 			"OModule::revokeComponent : inconsistent state !");
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 		sal_Int32 nLen = s_pImplementationNames->getLength();
224cdf0e10cSrcweir 		const ::rtl::OUString* pImplNames = s_pImplementationNames->getConstArray();
225cdf0e10cSrcweir 		for (sal_Int32 i=0; i<nLen; ++i, ++pImplNames)
226cdf0e10cSrcweir 		{
227cdf0e10cSrcweir 			if (pImplNames->equals(_rImplementationName))
228cdf0e10cSrcweir 			{
229cdf0e10cSrcweir 				removeElementAt(*s_pImplementationNames, i);
230cdf0e10cSrcweir 				removeElementAt(*s_pSupportedServices, i);
231cdf0e10cSrcweir 				removeElementAt(*s_pCreationFunctionPointers, i);
232cdf0e10cSrcweir 				removeElementAt(*s_pFactoryFunctionPointers, i);
233cdf0e10cSrcweir 				break;
234cdf0e10cSrcweir 			}
235cdf0e10cSrcweir 		}
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 		if (s_pImplementationNames->getLength() == 0)
238cdf0e10cSrcweir 		{
239cdf0e10cSrcweir 			delete s_pImplementationNames; s_pImplementationNames = NULL;
240cdf0e10cSrcweir 			delete s_pSupportedServices; s_pSupportedServices = NULL;
241cdf0e10cSrcweir 			delete s_pCreationFunctionPointers; s_pCreationFunctionPointers = NULL;
242cdf0e10cSrcweir 			delete s_pFactoryFunctionPointers; s_pFactoryFunctionPointers = NULL;
243cdf0e10cSrcweir 		}
244cdf0e10cSrcweir 	}
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	//--------------------------------------------------------------------------
getComponentFactory(const::rtl::OUString & _rImplementationName,const Reference<XMultiServiceFactory> & _rxServiceManager)247cdf0e10cSrcweir 	Reference< XInterface > OModule::getComponentFactory(
248cdf0e10cSrcweir 		const ::rtl::OUString& _rImplementationName,
249cdf0e10cSrcweir 		const Reference< XMultiServiceFactory >& _rxServiceManager)
250cdf0e10cSrcweir 	{
251cdf0e10cSrcweir 		OSL_ENSURE(_rxServiceManager.is(), "OModule::getComponentFactory : invalid argument (service manager) !");
252cdf0e10cSrcweir 		OSL_ENSURE(_rImplementationName.getLength(), "OModule::getComponentFactory : invalid argument (implementation name) !");
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 		if (!s_pImplementationNames)
255cdf0e10cSrcweir 		{
256cdf0e10cSrcweir 			OSL_ASSERT("OModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
257cdf0e10cSrcweir 			return NULL;
258cdf0e10cSrcweir 		}
259cdf0e10cSrcweir 		OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
260cdf0e10cSrcweir 			"OModule::getComponentFactory : inconsistent state (the pointers) !");
261cdf0e10cSrcweir 		OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
262cdf0e10cSrcweir 					&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
263cdf0e10cSrcweir 					&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
264cdf0e10cSrcweir 			"OModule::getComponentFactory : inconsistent state !");
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 		Reference< XInterface > xReturn;
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 		sal_Int32 nLen = s_pImplementationNames->getLength();
271cdf0e10cSrcweir 		const ::rtl::OUString* pImplName = s_pImplementationNames->getConstArray();
272cdf0e10cSrcweir 		const Sequence< ::rtl::OUString >* pServices = s_pSupportedServices->getConstArray();
273cdf0e10cSrcweir 		const sal_Int64* pComponentFunction = s_pCreationFunctionPointers->getConstArray();
274cdf0e10cSrcweir 		const sal_Int64* pFactoryFunction = s_pFactoryFunctionPointers->getConstArray();
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 		for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices, ++pComponentFunction, ++pFactoryFunction)
277cdf0e10cSrcweir 		{
278cdf0e10cSrcweir 			if (pImplName->equals(_rImplementationName))
279cdf0e10cSrcweir 			{
280cdf0e10cSrcweir 				const FactoryInstantiation FactoryInstantiationFunction = reinterpret_cast<const FactoryInstantiation>(*pFactoryFunction);
281cdf0e10cSrcweir 				const ComponentInstantiation ComponentInstantiationFunction = reinterpret_cast<const ComponentInstantiation>(*pComponentFunction);
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 				xReturn = FactoryInstantiationFunction( _rxServiceManager, *pImplName, ComponentInstantiationFunction, *pServices, NULL);
284cdf0e10cSrcweir 				if (xReturn.is())
285cdf0e10cSrcweir 				{
286cdf0e10cSrcweir 					xReturn->acquire();
287cdf0e10cSrcweir 					return xReturn.get();
288cdf0e10cSrcweir 				}
289cdf0e10cSrcweir 			}
290cdf0e10cSrcweir 		}
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 		return NULL;
293cdf0e10cSrcweir 	}
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 
296cdf0e10cSrcweir //.........................................................................
297cdf0e10cSrcweir }	// namespace COMPMOD_NAMESPACE
298cdf0e10cSrcweir //.........................................................................
299cdf0e10cSrcweir 
300