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: componentmodule.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 ************************************************************************/
31 #include "componentmodule.hxx"
32 #include <tools/resmgr.hxx>
34 #include <svtools/solar.hrc>
36 #include <comphelper/sequence.hxx>
37 #include <tools/debug.hxx>
39 #define ENTER_MOD_METHOD() \
40 ::osl::MutexGuard aGuard(s_aMutex); \
43 //.........................................................................
44 namespace COMPMOD_NAMESPACE
46 //.........................................................................
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::lang
;
50 using namespace ::com::sun::star::registry
;
51 using namespace ::comphelper
;
52 using namespace ::cppu
;
54 //=========================================================================
56 //=========================================================================
57 /** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
61 ResMgr
* m_pRessources
;
62 sal_Bool m_bInitialized
;
63 ByteString m_sFilePrefix
;
70 /// get the manager for the ressources of the module
71 ResMgr
* getResManager();
72 void setResourceFilePrefix(const ::rtl::OString
& _rPrefix
) { m_sFilePrefix
= _rPrefix
; }
75 //-------------------------------------------------------------------------
76 OModuleImpl::OModuleImpl()
78 ,m_bInitialized(sal_False
)
82 //-------------------------------------------------------------------------
83 OModuleImpl::~OModuleImpl()
89 //-------------------------------------------------------------------------
90 ResMgr
* OModuleImpl::getResManager()
92 // note that this method is not threadsafe, which counts for the whole class !
93 if (!m_pRessources
&& !m_bInitialized
)
95 DBG_ASSERT(m_sFilePrefix
.Len(), "OModuleImpl::getResManager: no resource file prefix!");
96 // create a manager with a fixed prefix
97 ByteString aMgrName
= m_sFilePrefix
;
99 m_pRessources
= ResMgr::CreateResMgr(aMgrName
.GetBuffer());
100 DBG_ASSERT(m_pRessources
,
101 (ByteString("OModuleImpl::getResManager: could not create the resource manager (file name: ")
103 += ByteString(")!")).GetBuffer());
105 m_bInitialized
= sal_True
;
107 return m_pRessources
;
110 //=========================================================================
112 //=========================================================================
113 ::osl::Mutex
OModule::s_aMutex
;
114 sal_Int32
OModule::s_nClients
= 0;
115 OModuleImpl
* OModule::s_pImpl
= NULL
;
116 ::rtl::OString
OModule::s_sResPrefix
;
117 //-------------------------------------------------------------------------
118 ResMgr
* OModule::getResManager()
121 return s_pImpl
->getResManager();
124 //-------------------------------------------------------------------------
125 void OModule::setResourceFilePrefix(const ::rtl::OString
& _rPrefix
)
127 ::osl::MutexGuard
aGuard(s_aMutex
);
128 s_sResPrefix
= _rPrefix
;
130 s_pImpl
->setResourceFilePrefix(_rPrefix
);
133 //-------------------------------------------------------------------------
134 void OModule::registerClient()
136 ::osl::MutexGuard
aGuard(s_aMutex
);
140 //-------------------------------------------------------------------------
141 void OModule::revokeClient()
143 ::osl::MutexGuard
aGuard(s_aMutex
);
144 if (!--s_nClients
&& s_pImpl
)
151 //-------------------------------------------------------------------------
152 void OModule::ensureImpl()
156 s_pImpl
= new OModuleImpl();
157 s_pImpl
->setResourceFilePrefix(s_sResPrefix
);
160 //--------------------------------------------------------------------------
161 //- registration helper
162 //--------------------------------------------------------------------------
164 Sequence
< ::rtl::OUString
>* OModule::s_pImplementationNames
= NULL
;
165 Sequence
< Sequence
< ::rtl::OUString
> >* OModule::s_pSupportedServices
= NULL
;
166 Sequence
< sal_Int64
>* OModule::s_pCreationFunctionPointers
= NULL
;
167 Sequence
< sal_Int64
>* OModule::s_pFactoryFunctionPointers
= NULL
;
169 //--------------------------------------------------------------------------
170 void OModule::registerComponent(
171 const ::rtl::OUString
& _rImplementationName
,
172 const Sequence
< ::rtl::OUString
>& _rServiceNames
,
173 ComponentInstantiation _pCreateFunction
,
174 FactoryInstantiation _pFactoryFunction
)
176 if (!s_pImplementationNames
)
178 OSL_ENSURE(!s_pSupportedServices
&& !s_pCreationFunctionPointers
&& !s_pFactoryFunctionPointers
,
179 "OModule::registerComponent : inconsistent state (the pointers (1)) !");
180 s_pImplementationNames
= new Sequence
< ::rtl::OUString
>;
181 s_pSupportedServices
= new Sequence
< Sequence
< ::rtl::OUString
> >;
182 s_pCreationFunctionPointers
= new Sequence
< sal_Int64
>;
183 s_pFactoryFunctionPointers
= new Sequence
< sal_Int64
>;
185 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
186 "OModule::registerComponent : inconsistent state (the pointers (2)) !");
188 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
189 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
190 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
191 "OModule::registerComponent : inconsistent state !");
193 sal_Int32 nOldLen
= s_pImplementationNames
->getLength();
194 s_pImplementationNames
->realloc(nOldLen
+ 1);
195 s_pSupportedServices
->realloc(nOldLen
+ 1);
196 s_pCreationFunctionPointers
->realloc(nOldLen
+ 1);
197 s_pFactoryFunctionPointers
->realloc(nOldLen
+ 1);
199 s_pImplementationNames
->getArray()[nOldLen
] = _rImplementationName
;
200 s_pSupportedServices
->getArray()[nOldLen
] = _rServiceNames
;
201 s_pCreationFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pCreateFunction
);
202 s_pFactoryFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pFactoryFunction
);
205 //--------------------------------------------------------------------------
206 void OModule::revokeComponent(const ::rtl::OUString
& _rImplementationName
)
208 if (!s_pImplementationNames
)
210 OSL_ASSERT("OModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
213 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
214 "OModule::revokeComponent : inconsistent state (the pointers) !");
215 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
216 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
217 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
218 "OModule::revokeComponent : inconsistent state !");
220 sal_Int32 nLen
= s_pImplementationNames
->getLength();
221 const ::rtl::OUString
* pImplNames
= s_pImplementationNames
->getConstArray();
222 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplNames
)
224 if (pImplNames
->equals(_rImplementationName
))
226 removeElementAt(*s_pImplementationNames
, i
);
227 removeElementAt(*s_pSupportedServices
, i
);
228 removeElementAt(*s_pCreationFunctionPointers
, i
);
229 removeElementAt(*s_pFactoryFunctionPointers
, i
);
234 if (s_pImplementationNames
->getLength() == 0)
236 delete s_pImplementationNames
; s_pImplementationNames
= NULL
;
237 delete s_pSupportedServices
; s_pSupportedServices
= NULL
;
238 delete s_pCreationFunctionPointers
; s_pCreationFunctionPointers
= NULL
;
239 delete s_pFactoryFunctionPointers
; s_pFactoryFunctionPointers
= NULL
;
243 //--------------------------------------------------------------------------
244 sal_Bool
OModule::writeComponentInfos(
245 const Reference
< XMultiServiceFactory
>& /*_rxServiceManager*/,
246 const Reference
< XRegistryKey
>& _rxRootKey
)
248 OSL_ENSURE(_rxRootKey
.is(), "OModule::writeComponentInfos : invalid argument !");
250 if (!s_pImplementationNames
)
252 OSL_ASSERT("OModule::writeComponentInfos : have no class infos ! Are you sure called this method at the right time ?");
255 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
256 "OModule::writeComponentInfos : inconsistent state (the pointers) !");
257 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
258 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
259 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
260 "OModule::writeComponentInfos : inconsistent state !");
262 sal_Int32 nLen
= s_pImplementationNames
->getLength();
263 const ::rtl::OUString
* pImplName
= s_pImplementationNames
->getConstArray();
264 const Sequence
< ::rtl::OUString
>* pServices
= s_pSupportedServices
->getConstArray();
266 ::rtl::OUString
sRootKey("/", 1, RTL_TEXTENCODING_ASCII_US
);
267 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplName
, ++pServices
)
269 ::rtl::OUString
aMainKeyName(sRootKey
);
270 aMainKeyName
+= *pImplName
;
271 aMainKeyName
+= ::rtl::OUString::createFromAscii("/UNO/SERVICES");
275 Reference
< XRegistryKey
> xNewKey( _rxRootKey
->createKey(aMainKeyName
) );
277 const ::rtl::OUString
* pService
= pServices
->getConstArray();
278 for (sal_Int32 j
=0; j
<pServices
->getLength(); ++j
, ++pService
)
279 xNewKey
->createKey(*pService
);
283 OSL_ASSERT("OModule::writeComponentInfos : something went wrong while creating the keys !");
291 //--------------------------------------------------------------------------
292 Reference
< XInterface
> OModule::getComponentFactory(
293 const ::rtl::OUString
& _rImplementationName
,
294 const Reference
< XMultiServiceFactory
>& _rxServiceManager
)
296 OSL_ENSURE(_rxServiceManager
.is(), "OModule::getComponentFactory : invalid argument (service manager) !");
297 OSL_ENSURE(_rImplementationName
.getLength(), "OModule::getComponentFactory : invalid argument (implementation name) !");
299 if (!s_pImplementationNames
)
301 OSL_ASSERT("OModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
304 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
305 "OModule::getComponentFactory : inconsistent state (the pointers) !");
306 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
307 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
308 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
309 "OModule::getComponentFactory : inconsistent state !");
312 Reference
< XInterface
> xReturn
;
315 sal_Int32 nLen
= s_pImplementationNames
->getLength();
316 const ::rtl::OUString
* pImplName
= s_pImplementationNames
->getConstArray();
317 const Sequence
< ::rtl::OUString
>* pServices
= s_pSupportedServices
->getConstArray();
318 const sal_Int64
* pComponentFunction
= s_pCreationFunctionPointers
->getConstArray();
319 const sal_Int64
* pFactoryFunction
= s_pFactoryFunctionPointers
->getConstArray();
321 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplName
, ++pServices
, ++pComponentFunction
, ++pFactoryFunction
)
323 if (pImplName
->equals(_rImplementationName
))
325 const FactoryInstantiation FactoryInstantiationFunction
= reinterpret_cast<const FactoryInstantiation
>(*pFactoryFunction
);
326 const ComponentInstantiation ComponentInstantiationFunction
= reinterpret_cast<const ComponentInstantiation
>(*pComponentFunction
);
328 xReturn
= FactoryInstantiationFunction( _rxServiceManager
, *pImplName
, ComponentInstantiationFunction
, *pServices
, NULL
);
332 return xReturn
.get();
341 //.........................................................................
342 } // namespace COMPMOD_NAMESPACE
343 //.........................................................................