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