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