1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "componentmodule.hxx"
21 #include <tools/resmgr.hxx>
22 #include <svl/solar.hrc>
23 #include <comphelper/sequence.hxx>
24 #include <tools/debug.hxx>
25 #include <rtl/strbuf.hxx>
27 #define ENTER_MOD_METHOD() \
28 ::osl::MutexGuard aGuard(s_aMutex); \
32 namespace COMPMOD_NAMESPACE
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::registry
;
39 using namespace ::comphelper
;
40 using namespace ::cppu
;
42 // implementation for <type>OModule</type>. not threadsafe, has to be guarded by its owner
47 OString m_sFilePrefix
;
54 /// get the manager for the resources of the module
55 ResMgr
* getResManager();
56 void setResourceFilePrefix(const OString
& _rPrefix
) { m_sFilePrefix
= _rPrefix
; }
60 OModuleImpl::OModuleImpl()
62 ,m_bInitialized(false)
67 OModuleImpl::~OModuleImpl()
74 ResMgr
* OModuleImpl::getResManager()
76 // note that this method is not threadsafe, which counts for the whole class !
77 if (!m_pResources
&& !m_bInitialized
)
79 DBG_ASSERT(!m_sFilePrefix
.isEmpty(), "OModuleImpl::getResManager: no resource file prefix!");
80 // create a manager with a fixed prefix
81 m_pResources
= ResMgr::CreateResMgr(m_sFilePrefix
.getStr());
82 DBG_ASSERT(m_pResources
,
83 OStringBuffer("OModuleImpl::getResManager: could not create the resource manager (file name: ")
84 .append(m_sFilePrefix
)
85 .append(")!").getStr());
87 m_bInitialized
= true;
93 ::osl::Mutex
OModule::s_aMutex
;
94 sal_Int32
OModule::s_nClients
= 0;
95 OModuleImpl
* OModule::s_pImpl
= NULL
;
96 OString
OModule::s_sResPrefix
;
98 ResMgr
* OModule::getResManager()
101 return s_pImpl
->getResManager();
105 void OModule::setResourceFilePrefix(const OString
& _rPrefix
)
107 ::osl::MutexGuard
aGuard(s_aMutex
);
108 s_sResPrefix
= _rPrefix
;
110 s_pImpl
->setResourceFilePrefix(_rPrefix
);
114 void OModule::registerClient()
116 ::osl::MutexGuard
aGuard(s_aMutex
);
121 void OModule::revokeClient()
123 ::osl::MutexGuard
aGuard(s_aMutex
);
124 if (!--s_nClients
&& s_pImpl
)
132 void OModule::ensureImpl()
136 s_pImpl
= new OModuleImpl();
137 s_pImpl
->setResourceFilePrefix(s_sResPrefix
);
141 //- registration helper
144 Sequence
< OUString
>* OModule::s_pImplementationNames
= NULL
;
145 Sequence
< Sequence
< OUString
> >* OModule::s_pSupportedServices
= NULL
;
146 Sequence
< sal_Int64
>* OModule::s_pCreationFunctionPointers
= NULL
;
147 Sequence
< sal_Int64
>* OModule::s_pFactoryFunctionPointers
= NULL
;
150 void OModule::registerComponent(
151 const OUString
& _rImplementationName
,
152 const Sequence
< OUString
>& _rServiceNames
,
153 ComponentInstantiation _pCreateFunction
,
154 FactoryInstantiation _pFactoryFunction
)
156 if (!s_pImplementationNames
)
158 OSL_ENSURE(!s_pSupportedServices
&& !s_pCreationFunctionPointers
&& !s_pFactoryFunctionPointers
,
159 "OModule::registerComponent : inconsistent state (the pointers (1)) !");
160 s_pImplementationNames
= new Sequence
< OUString
>;
161 s_pSupportedServices
= new Sequence
< Sequence
< OUString
> >;
162 s_pCreationFunctionPointers
= new Sequence
< sal_Int64
>;
163 s_pFactoryFunctionPointers
= new Sequence
< sal_Int64
>;
165 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
166 "OModule::registerComponent : inconsistent state (the pointers (2)) !");
168 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
169 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
170 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
171 "OModule::registerComponent : inconsistent state !");
173 sal_Int32 nOldLen
= s_pImplementationNames
->getLength();
174 s_pImplementationNames
->realloc(nOldLen
+ 1);
175 s_pSupportedServices
->realloc(nOldLen
+ 1);
176 s_pCreationFunctionPointers
->realloc(nOldLen
+ 1);
177 s_pFactoryFunctionPointers
->realloc(nOldLen
+ 1);
179 s_pImplementationNames
->getArray()[nOldLen
] = _rImplementationName
;
180 s_pSupportedServices
->getArray()[nOldLen
] = _rServiceNames
;
181 s_pCreationFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pCreateFunction
);
182 s_pFactoryFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pFactoryFunction
);
186 void OModule::revokeComponent(const OUString
& _rImplementationName
)
188 if (!s_pImplementationNames
)
190 OSL_FAIL("OModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
193 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
194 "OModule::revokeComponent : inconsistent state (the pointers) !");
195 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
196 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
197 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
198 "OModule::revokeComponent : inconsistent state !");
200 sal_Int32 nLen
= s_pImplementationNames
->getLength();
201 const OUString
* pImplNames
= s_pImplementationNames
->getConstArray();
202 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplNames
)
204 if (pImplNames
->equals(_rImplementationName
))
206 removeElementAt(*s_pImplementationNames
, i
);
207 removeElementAt(*s_pSupportedServices
, i
);
208 removeElementAt(*s_pCreationFunctionPointers
, i
);
209 removeElementAt(*s_pFactoryFunctionPointers
, i
);
214 if (s_pImplementationNames
->getLength() == 0)
216 delete s_pImplementationNames
; s_pImplementationNames
= NULL
;
217 delete s_pSupportedServices
; s_pSupportedServices
= NULL
;
218 delete s_pCreationFunctionPointers
; s_pCreationFunctionPointers
= NULL
;
219 delete s_pFactoryFunctionPointers
; s_pFactoryFunctionPointers
= NULL
;
224 Reference
< XInterface
> OModule::getComponentFactory(
225 const OUString
& _rImplementationName
,
226 const Reference
< XMultiServiceFactory
>& _rxServiceManager
)
228 OSL_ENSURE(_rxServiceManager
.is(), "OModule::getComponentFactory : invalid argument (service manager) !");
229 OSL_ENSURE(!_rImplementationName
.isEmpty(), "OModule::getComponentFactory : invalid argument (implementation name) !");
231 if (!s_pImplementationNames
)
233 OSL_FAIL("OModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
236 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
237 "OModule::getComponentFactory : inconsistent state (the pointers) !");
238 OSL_ENSURE( (s_pImplementationNames
->getLength() == s_pSupportedServices
->getLength())
239 && (s_pImplementationNames
->getLength() == s_pCreationFunctionPointers
->getLength())
240 && (s_pImplementationNames
->getLength() == s_pFactoryFunctionPointers
->getLength()),
241 "OModule::getComponentFactory : inconsistent state !");
244 Reference
< XInterface
> xReturn
;
247 sal_Int32 nLen
= s_pImplementationNames
->getLength();
248 const OUString
* pImplName
= s_pImplementationNames
->getConstArray();
249 const Sequence
< OUString
>* pServices
= s_pSupportedServices
->getConstArray();
250 const sal_Int64
* pComponentFunction
= s_pCreationFunctionPointers
->getConstArray();
251 const sal_Int64
* pFactoryFunction
= s_pFactoryFunctionPointers
->getConstArray();
253 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplName
, ++pServices
, ++pComponentFunction
, ++pFactoryFunction
)
255 if (pImplName
->equals(_rImplementationName
))
257 const FactoryInstantiation FactoryInstantiationFunction
= reinterpret_cast<const FactoryInstantiation
>(*pFactoryFunction
);
258 const ComponentInstantiation ComponentInstantiationFunction
= reinterpret_cast<const ComponentInstantiation
>(*pComponentFunction
);
260 xReturn
= FactoryInstantiationFunction( _rxServiceManager
, *pImplName
, ComponentInstantiationFunction
, *pServices
, NULL
);
264 return xReturn
.get();
274 } // namespace COMPMOD_NAMESPACE
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */