1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Acomponentmodule.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #ifndef _EXTENSIONS_COMPONENT_MODULE_HXX_
33 #include "Acomponentmodule.hxx"
36 #ifndef _TOOLS_RESMGR_HXX
37 #include <tools/resmgr.hxx>
40 #include <svtools/solar.hrc>
42 #ifndef _COMPHELPER_SEQUENCE_HXX_
43 #include <comphelper/sequence.hxx>
45 #ifndef _TOOLS_DEBUG_HXX
46 #include <tools/debug.hxx>
49 #define ENTER_MOD_METHOD() \
50 ::osl::MutexGuard aGuard(s_aMutex); \
53 //.........................................................................
54 namespace COMPMOD_NAMESPACE
56 //.........................................................................
58 using namespace ::com::sun::star::uno
;
59 using namespace ::com::sun::star::lang
;
60 using namespace ::com::sun::star::registry
;
61 using namespace ::comphelper
;
62 using namespace ::cppu
;
64 //=========================================================================
66 //=========================================================================
67 /** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
71 ResMgr
* m_pRessources
;
72 sal_Bool m_bInitialized
;
73 ByteString m_sFilePrefix
;
80 /// get the manager for the ressources of the module
81 ResMgr
* getResManager();
82 void setResourceFilePrefix(const ::rtl::OString
& _rPrefix
) { m_sFilePrefix
= _rPrefix
; }
85 //-------------------------------------------------------------------------
86 OModuleImpl::OModuleImpl()
88 ,m_bInitialized(sal_False
)
92 //-------------------------------------------------------------------------
93 OModuleImpl::~OModuleImpl()
99 //-------------------------------------------------------------------------
100 ResMgr
* OModuleImpl::getResManager()
102 // note that this method is not threadsafe, which counts for the whole class !
103 if (!m_pRessources
&& !m_bInitialized
)
105 DBG_ASSERT(m_sFilePrefix
.Len(), "OModuleImpl::getResManager: no resource file prefix!");
106 // create a manager with a fixed prefix
107 ByteString aMgrName
= m_sFilePrefix
;
109 m_pRessources
= ResMgr::CreateResMgr(aMgrName
.GetBuffer());
110 DBG_ASSERT(m_pRessources
,
111 (ByteString("OModuleImpl::getResManager: could not create the resource manager (file name: ")
113 += ByteString(")!")).GetBuffer());
115 m_bInitialized
= sal_True
;
117 return m_pRessources
;
120 //=========================================================================
122 //=========================================================================
123 ::osl::Mutex
OModule::s_aMutex
;
124 sal_Int32
OModule::s_nClients
= 0;
125 OModuleImpl
* OModule::s_pImpl
= NULL
;
126 ::rtl::OString
OModule::s_sResPrefix
;
127 //-------------------------------------------------------------------------
128 ResMgr
* OModule::getResManager()
131 return s_pImpl
->getResManager();
134 //-------------------------------------------------------------------------
135 void OModule::setResourceFilePrefix(const ::rtl::OString
& _rPrefix
)
137 ::osl::MutexGuard
aGuard(s_aMutex
);
138 s_sResPrefix
= _rPrefix
;
140 s_pImpl
->setResourceFilePrefix(_rPrefix
);
143 //-------------------------------------------------------------------------
144 void OModule::registerClient()
146 ::osl::MutexGuard
aGuard(s_aMutex
);
150 //-------------------------------------------------------------------------
151 void OModule::revokeClient()
153 ::osl::MutexGuard
aGuard(s_aMutex
);
154 if (!--s_nClients
&& s_pImpl
)
161 //-------------------------------------------------------------------------
162 void OModule::ensureImpl()
166 s_pImpl
= new OModuleImpl();
167 s_pImpl
->setResourceFilePrefix(s_sResPrefix
);
170 //--------------------------------------------------------------------------
171 //- registration helper
172 //--------------------------------------------------------------------------
174 Sequence
< ::rtl::OUString
>* OModule::s_pImplementationNames
= NULL
;
175 Sequence
< Sequence
< ::rtl::OUString
> >* OModule::s_pSupportedServices
= NULL
;
176 Sequence
< sal_Int64
>* OModule::s_pCreationFunctionPointers
= NULL
;
177 Sequence
< sal_Int64
>* OModule::s_pFactoryFunctionPointers
= NULL
;
179 //--------------------------------------------------------------------------
180 void OModule::registerComponent(
181 const ::rtl::OUString
& _rImplementationName
,
182 const Sequence
< ::rtl::OUString
>& _rServiceNames
,
183 ComponentInstantiation _pCreateFunction
,
184 FactoryInstantiation _pFactoryFunction
)
186 if (!s_pImplementationNames
)
188 OSL_ENSURE(!s_pSupportedServices
&& !s_pCreationFunctionPointers
&& !s_pFactoryFunctionPointers
,
189 "OModule::registerComponent : inconsistent state (the pointers (1)) !");
190 s_pImplementationNames
= new Sequence
< ::rtl::OUString
>;
191 s_pSupportedServices
= new Sequence
< Sequence
< ::rtl::OUString
> >;
192 s_pCreationFunctionPointers
= new Sequence
< sal_Int64
>;
193 s_pFactoryFunctionPointers
= new Sequence
< sal_Int64
>;
195 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
196 "OModule::registerComponent : inconsistent state (the pointers (2)) !");
198 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
199 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
200 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
201 "OModule::registerComponent : inconsistent state !");
203 sal_Int32 nOldLen
= s_pImplementationNames
->getLength();
204 s_pImplementationNames
->realloc(nOldLen
+ 1);
205 s_pSupportedServices
->realloc(nOldLen
+ 1);
206 s_pCreationFunctionPointers
->realloc(nOldLen
+ 1);
207 s_pFactoryFunctionPointers
->realloc(nOldLen
+ 1);
209 s_pImplementationNames
->getArray()[nOldLen
] = _rImplementationName
;
210 s_pSupportedServices
->getArray()[nOldLen
] = _rServiceNames
;
211 s_pCreationFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pCreateFunction
);
212 s_pFactoryFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pFactoryFunction
);
215 //--------------------------------------------------------------------------
216 void OModule::revokeComponent(const ::rtl::OUString
& _rImplementationName
)
218 if (!s_pImplementationNames
)
220 OSL_ASSERT("OModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
223 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
224 "OModule::revokeComponent : inconsistent state (the pointers) !");
225 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
226 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
227 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
228 "OModule::revokeComponent : inconsistent state !");
230 sal_Int32 nLen
= s_pImplementationNames
->getLength();
231 const ::rtl::OUString
* pImplNames
= s_pImplementationNames
->getConstArray();
232 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplNames
)
234 if (pImplNames
->equals(_rImplementationName
))
236 removeElementAt(*s_pImplementationNames
, i
);
237 removeElementAt(*s_pSupportedServices
, i
);
238 removeElementAt(*s_pCreationFunctionPointers
, i
);
239 removeElementAt(*s_pFactoryFunctionPointers
, i
);
244 if (s_pImplementationNames
->getLength() == 0)
246 delete s_pImplementationNames
; s_pImplementationNames
= NULL
;
247 delete s_pSupportedServices
; s_pSupportedServices
= NULL
;
248 delete s_pCreationFunctionPointers
; s_pCreationFunctionPointers
= NULL
;
249 delete s_pFactoryFunctionPointers
; s_pFactoryFunctionPointers
= NULL
;
253 //--------------------------------------------------------------------------
254 sal_Bool
OModule::writeComponentInfos(
255 const Reference
< XMultiServiceFactory
>& /*_rxServiceManager*/,
256 const Reference
< XRegistryKey
>& _rxRootKey
)
258 OSL_ENSURE(_rxRootKey
.is(), "OModule::writeComponentInfos : invalid argument !");
260 if (!s_pImplementationNames
)
262 OSL_ASSERT("OModule::writeComponentInfos : have no class infos ! Are you sure called this method at the right time ?");
265 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
266 "OModule::writeComponentInfos : inconsistent state (the pointers) !");
267 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
268 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
269 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
270 "OModule::writeComponentInfos : inconsistent state !");
272 sal_Int32 nLen
= s_pImplementationNames
->getLength();
273 const ::rtl::OUString
* pImplName
= s_pImplementationNames
->getConstArray();
274 const Sequence
< ::rtl::OUString
>* pServices
= s_pSupportedServices
->getConstArray();
276 ::rtl::OUString
sRootKey("/", 1, RTL_TEXTENCODING_ASCII_US
);
277 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplName
, ++pServices
)
279 ::rtl::OUString
aMainKeyName(sRootKey
);
280 aMainKeyName
+= *pImplName
;
281 aMainKeyName
+= ::rtl::OUString::createFromAscii("/UNO/SERVICES");
285 Reference
< XRegistryKey
> xNewKey( _rxRootKey
->createKey(aMainKeyName
) );
287 const ::rtl::OUString
* pService
= pServices
->getConstArray();
288 for (sal_Int32 j
=0; j
<pServices
->getLength(); ++j
, ++pService
)
289 xNewKey
->createKey(*pService
);
293 OSL_ASSERT("OModule::writeComponentInfos : something went wrong while creating the keys !");
301 //--------------------------------------------------------------------------
302 Reference
< XInterface
> OModule::getComponentFactory(
303 const ::rtl::OUString
& _rImplementationName
,
304 const Reference
< XMultiServiceFactory
>& _rxServiceManager
)
306 OSL_ENSURE(_rxServiceManager
.is(), "OModule::getComponentFactory : invalid argument (service manager) !");
307 OSL_ENSURE(_rImplementationName
.getLength(), "OModule::getComponentFactory : invalid argument (implementation name) !");
309 if (!s_pImplementationNames
)
311 OSL_ASSERT("OModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
314 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
315 "OModule::getComponentFactory : inconsistent state (the pointers) !");
316 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
317 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
318 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
319 "OModule::getComponentFactory : inconsistent state !");
322 Reference
< XInterface
> xReturn
;
325 sal_Int32 nLen
= s_pImplementationNames
->getLength();
326 const ::rtl::OUString
* pImplName
= s_pImplementationNames
->getConstArray();
327 const Sequence
< ::rtl::OUString
>* pServices
= s_pSupportedServices
->getConstArray();
328 const sal_Int64
* pComponentFunction
= s_pCreationFunctionPointers
->getConstArray();
329 const sal_Int64
* pFactoryFunction
= s_pFactoryFunctionPointers
->getConstArray();
331 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplName
, ++pServices
, ++pComponentFunction
, ++pFactoryFunction
)
333 if (pImplName
->equals(_rImplementationName
))
335 const FactoryInstantiation FactoryInstantiationFunction
= reinterpret_cast<const FactoryInstantiation
>(*pFactoryFunction
);
336 const ComponentInstantiation ComponentInstantiationFunction
= reinterpret_cast<const ComponentInstantiation
>(*pComponentFunction
);
338 xReturn
= FactoryInstantiationFunction( _rxServiceManager
, *pImplName
, ComponentInstantiationFunction
, *pServices
, NULL
);
342 return xReturn
.get();
351 //.........................................................................
352 } // namespace COMPMOD_NAMESPACE
353 //.........................................................................