1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include "componentmodule.hxx"
29 #include <tools/resmgr.hxx>
31 #include <svl/solar.hrc>
33 #include <comphelper/sequence.hxx>
34 #include <tools/debug.hxx>
36 #define ENTER_MOD_METHOD() \
37 ::osl::MutexGuard aGuard(s_aMutex); \
40 //.........................................................................
41 namespace COMPMOD_NAMESPACE
43 //.........................................................................
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::lang
;
47 using namespace ::com::sun::star::registry
;
48 using namespace ::comphelper
;
49 using namespace ::cppu
;
51 //=========================================================================
53 //=========================================================================
54 /** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
58 ResMgr
* m_pRessources
;
59 sal_Bool m_bInitialized
;
60 ByteString m_sFilePrefix
;
67 /// get the manager for the ressources of the module
68 ResMgr
* getResManager();
69 void setResourceFilePrefix(const ::rtl::OString
& _rPrefix
) { m_sFilePrefix
= _rPrefix
; }
72 //-------------------------------------------------------------------------
73 OModuleImpl::OModuleImpl()
75 ,m_bInitialized(sal_False
)
79 //-------------------------------------------------------------------------
80 OModuleImpl::~OModuleImpl()
86 //-------------------------------------------------------------------------
87 ResMgr
* OModuleImpl::getResManager()
89 // note that this method is not threadsafe, which counts for the whole class !
90 if (!m_pRessources
&& !m_bInitialized
)
92 DBG_ASSERT(m_sFilePrefix
.Len(), "OModuleImpl::getResManager: no resource file prefix!");
93 // create a manager with a fixed prefix
94 ByteString aMgrName
= m_sFilePrefix
;
96 m_pRessources
= ResMgr::CreateResMgr(aMgrName
.GetBuffer());
97 DBG_ASSERT(m_pRessources
,
98 (ByteString("OModuleImpl::getResManager: could not create the resource manager (file name: ")
100 += ByteString(")!")).GetBuffer());
102 m_bInitialized
= sal_True
;
104 return m_pRessources
;
107 //=========================================================================
109 //=========================================================================
110 ::osl::Mutex
OModule::s_aMutex
;
111 sal_Int32
OModule::s_nClients
= 0;
112 OModuleImpl
* OModule::s_pImpl
= NULL
;
113 ::rtl::OString
OModule::s_sResPrefix
;
114 //-------------------------------------------------------------------------
115 ResMgr
* OModule::getResManager()
118 return s_pImpl
->getResManager();
121 //-------------------------------------------------------------------------
122 void OModule::setResourceFilePrefix(const ::rtl::OString
& _rPrefix
)
124 ::osl::MutexGuard
aGuard(s_aMutex
);
125 s_sResPrefix
= _rPrefix
;
127 s_pImpl
->setResourceFilePrefix(_rPrefix
);
130 //-------------------------------------------------------------------------
131 void OModule::registerClient()
133 ::osl::MutexGuard
aGuard(s_aMutex
);
137 //-------------------------------------------------------------------------
138 void OModule::revokeClient()
140 ::osl::MutexGuard
aGuard(s_aMutex
);
141 if (!--s_nClients
&& s_pImpl
)
148 //-------------------------------------------------------------------------
149 void OModule::ensureImpl()
153 s_pImpl
= new OModuleImpl();
154 s_pImpl
->setResourceFilePrefix(s_sResPrefix
);
157 //--------------------------------------------------------------------------
158 //- registration helper
159 //--------------------------------------------------------------------------
161 Sequence
< ::rtl::OUString
>* OModule::s_pImplementationNames
= NULL
;
162 Sequence
< Sequence
< ::rtl::OUString
> >* OModule::s_pSupportedServices
= NULL
;
163 Sequence
< sal_Int64
>* OModule::s_pCreationFunctionPointers
= NULL
;
164 Sequence
< sal_Int64
>* OModule::s_pFactoryFunctionPointers
= NULL
;
166 //--------------------------------------------------------------------------
167 void OModule::registerComponent(
168 const ::rtl::OUString
& _rImplementationName
,
169 const Sequence
< ::rtl::OUString
>& _rServiceNames
,
170 ComponentInstantiation _pCreateFunction
,
171 FactoryInstantiation _pFactoryFunction
)
173 if (!s_pImplementationNames
)
175 OSL_ENSURE(!s_pSupportedServices
&& !s_pCreationFunctionPointers
&& !s_pFactoryFunctionPointers
,
176 "OModule::registerComponent : inconsistent state (the pointers (1)) !");
177 s_pImplementationNames
= new Sequence
< ::rtl::OUString
>;
178 s_pSupportedServices
= new Sequence
< Sequence
< ::rtl::OUString
> >;
179 s_pCreationFunctionPointers
= new Sequence
< sal_Int64
>;
180 s_pFactoryFunctionPointers
= new Sequence
< sal_Int64
>;
182 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
183 "OModule::registerComponent : inconsistent state (the pointers (2)) !");
185 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
186 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
187 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
188 "OModule::registerComponent : inconsistent state !");
190 sal_Int32 nOldLen
= s_pImplementationNames
->getLength();
191 s_pImplementationNames
->realloc(nOldLen
+ 1);
192 s_pSupportedServices
->realloc(nOldLen
+ 1);
193 s_pCreationFunctionPointers
->realloc(nOldLen
+ 1);
194 s_pFactoryFunctionPointers
->realloc(nOldLen
+ 1);
196 s_pImplementationNames
->getArray()[nOldLen
] = _rImplementationName
;
197 s_pSupportedServices
->getArray()[nOldLen
] = _rServiceNames
;
198 s_pCreationFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pCreateFunction
);
199 s_pFactoryFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pFactoryFunction
);
202 //--------------------------------------------------------------------------
203 void OModule::revokeComponent(const ::rtl::OUString
& _rImplementationName
)
205 if (!s_pImplementationNames
)
207 OSL_ASSERT("OModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
210 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
211 "OModule::revokeComponent : inconsistent state (the pointers) !");
212 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
213 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
214 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
215 "OModule::revokeComponent : inconsistent state !");
217 sal_Int32 nLen
= s_pImplementationNames
->getLength();
218 const ::rtl::OUString
* pImplNames
= s_pImplementationNames
->getConstArray();
219 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplNames
)
221 if (pImplNames
->equals(_rImplementationName
))
223 removeElementAt(*s_pImplementationNames
, i
);
224 removeElementAt(*s_pSupportedServices
, i
);
225 removeElementAt(*s_pCreationFunctionPointers
, i
);
226 removeElementAt(*s_pFactoryFunctionPointers
, i
);
231 if (s_pImplementationNames
->getLength() == 0)
233 delete s_pImplementationNames
; s_pImplementationNames
= NULL
;
234 delete s_pSupportedServices
; s_pSupportedServices
= NULL
;
235 delete s_pCreationFunctionPointers
; s_pCreationFunctionPointers
= NULL
;
236 delete s_pFactoryFunctionPointers
; s_pFactoryFunctionPointers
= NULL
;
240 //--------------------------------------------------------------------------
241 sal_Bool
OModule::writeComponentInfos(
242 const Reference
< XMultiServiceFactory
>& /*_rxServiceManager*/,
243 const Reference
< XRegistryKey
>& _rxRootKey
)
245 OSL_ENSURE(_rxRootKey
.is(), "OModule::writeComponentInfos : invalid argument !");
247 if (!s_pImplementationNames
)
249 OSL_ASSERT("OModule::writeComponentInfos : have no class infos ! Are you sure called this method at the right time ?");
252 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
253 "OModule::writeComponentInfos : inconsistent state (the pointers) !");
254 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
255 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
256 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
257 "OModule::writeComponentInfos : inconsistent state !");
259 sal_Int32 nLen
= s_pImplementationNames
->getLength();
260 const ::rtl::OUString
* pImplName
= s_pImplementationNames
->getConstArray();
261 const Sequence
< ::rtl::OUString
>* pServices
= s_pSupportedServices
->getConstArray();
263 ::rtl::OUString
sRootKey("/", 1, RTL_TEXTENCODING_ASCII_US
);
264 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplName
, ++pServices
)
266 ::rtl::OUString
aMainKeyName(sRootKey
);
267 aMainKeyName
+= *pImplName
;
268 aMainKeyName
+= ::rtl::OUString::createFromAscii("/UNO/SERVICES");
272 Reference
< XRegistryKey
> xNewKey( _rxRootKey
->createKey(aMainKeyName
) );
274 const ::rtl::OUString
* pService
= pServices
->getConstArray();
275 for (sal_Int32 j
=0; j
<pServices
->getLength(); ++j
, ++pService
)
276 xNewKey
->createKey(*pService
);
280 OSL_ASSERT("OModule::writeComponentInfos : something went wrong while creating the keys !");
288 //--------------------------------------------------------------------------
289 Reference
< XInterface
> OModule::getComponentFactory(
290 const ::rtl::OUString
& _rImplementationName
,
291 const Reference
< XMultiServiceFactory
>& _rxServiceManager
)
293 OSL_ENSURE(_rxServiceManager
.is(), "OModule::getComponentFactory : invalid argument (service manager) !");
294 OSL_ENSURE(_rImplementationName
.getLength(), "OModule::getComponentFactory : invalid argument (implementation name) !");
296 if (!s_pImplementationNames
)
298 OSL_ASSERT("OModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
301 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
302 "OModule::getComponentFactory : inconsistent state (the pointers) !");
303 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
304 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
305 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
306 "OModule::getComponentFactory : inconsistent state !");
309 Reference
< XInterface
> xReturn
;
312 sal_Int32 nLen
= s_pImplementationNames
->getLength();
313 const ::rtl::OUString
* pImplName
= s_pImplementationNames
->getConstArray();
314 const Sequence
< ::rtl::OUString
>* pServices
= s_pSupportedServices
->getConstArray();
315 const sal_Int64
* pComponentFunction
= s_pCreationFunctionPointers
->getConstArray();
316 const sal_Int64
* pFactoryFunction
= s_pFactoryFunctionPointers
->getConstArray();
318 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplName
, ++pServices
, ++pComponentFunction
, ++pFactoryFunction
)
320 if (pImplName
->equals(_rImplementationName
))
322 const FactoryInstantiation FactoryInstantiationFunction
= reinterpret_cast<const FactoryInstantiation
>(*pFactoryFunction
);
323 const ComponentInstantiation ComponentInstantiationFunction
= reinterpret_cast<const ComponentInstantiation
>(*pComponentFunction
);
325 xReturn
= FactoryInstantiationFunction( _rxServiceManager
, *pImplName
, ComponentInstantiationFunction
, *pServices
, NULL
);
329 return xReturn
.get();
338 //.........................................................................
339 } // namespace COMPMOD_NAMESPACE
340 //.........................................................................