Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
26 :
27 : #define ENTER_MOD_METHOD() \
28 : ::osl::MutexGuard aGuard(s_aMutex); \
29 : ensureImpl()
30 :
31 :
32 : namespace COMPMOD_NAMESPACE
33 : {
34 :
35 :
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;
41 :
42 :
43 : //= OModuleImpl
44 :
45 : /** implementation for <type>OModule</type>. not threadsafe, has to be guarded by its owner
46 : */
47 : class OModuleImpl
48 : {
49 : ResMgr* m_pResources;
50 : sal_Bool m_bInitialized;
51 : OString m_sFilePrefix;
52 :
53 : public:
54 : /// ctor
55 : OModuleImpl();
56 : ~OModuleImpl();
57 :
58 : /// get the manager for the resources of the module
59 : ResMgr* getResManager();
60 0 : void setResourceFilePrefix(const OString& _rPrefix) { m_sFilePrefix = _rPrefix; }
61 : };
62 :
63 :
64 0 : OModuleImpl::OModuleImpl()
65 : :m_pResources(NULL)
66 0 : ,m_bInitialized(sal_False)
67 : {
68 0 : }
69 :
70 :
71 0 : OModuleImpl::~OModuleImpl()
72 : {
73 0 : if (m_pResources)
74 0 : delete m_pResources;
75 0 : }
76 :
77 :
78 0 : ResMgr* OModuleImpl::getResManager()
79 : {
80 : // note that this method is not threadsafe, which counts for the whole class !
81 0 : if (!m_pResources && !m_bInitialized)
82 : {
83 : DBG_ASSERT(!m_sFilePrefix.isEmpty(), "OModuleImpl::getResManager: no resource file prefix!");
84 : // create a manager with a fixed prefix
85 0 : m_pResources = ResMgr::CreateResMgr(m_sFilePrefix.getStr());
86 : DBG_ASSERT(m_pResources,
87 : OStringBuffer("OModuleImpl::getResManager: could not create the resource manager (file name: ")
88 : .append(m_sFilePrefix)
89 : .append(")!").getStr());
90 :
91 0 : m_bInitialized = sal_True;
92 : }
93 0 : return m_pResources;
94 : }
95 :
96 :
97 : //= OModule
98 :
99 0 : ::osl::Mutex OModule::s_aMutex;
100 : sal_Int32 OModule::s_nClients = 0;
101 : OModuleImpl* OModule::s_pImpl = NULL;
102 0 : OString OModule::s_sResPrefix;
103 :
104 0 : ResMgr* OModule::getResManager()
105 : {
106 0 : ENTER_MOD_METHOD();
107 0 : return s_pImpl->getResManager();
108 : }
109 :
110 :
111 0 : void OModule::setResourceFilePrefix(const OString& _rPrefix)
112 : {
113 0 : ::osl::MutexGuard aGuard(s_aMutex);
114 0 : s_sResPrefix = _rPrefix;
115 0 : if (s_pImpl)
116 0 : s_pImpl->setResourceFilePrefix(_rPrefix);
117 0 : }
118 :
119 :
120 0 : void OModule::registerClient()
121 : {
122 0 : ::osl::MutexGuard aGuard(s_aMutex);
123 0 : ++s_nClients;
124 0 : }
125 :
126 :
127 0 : void OModule::revokeClient()
128 : {
129 0 : ::osl::MutexGuard aGuard(s_aMutex);
130 0 : if (!--s_nClients && s_pImpl)
131 : {
132 0 : delete s_pImpl;
133 0 : s_pImpl = NULL;
134 0 : }
135 0 : }
136 :
137 :
138 0 : void OModule::ensureImpl()
139 : {
140 0 : if (s_pImpl)
141 0 : return;
142 0 : s_pImpl = new OModuleImpl();
143 0 : s_pImpl->setResourceFilePrefix(s_sResPrefix);
144 : }
145 :
146 :
147 : //- registration helper
148 :
149 :
150 : Sequence< OUString >* OModule::s_pImplementationNames = NULL;
151 : Sequence< Sequence< OUString > >* OModule::s_pSupportedServices = NULL;
152 : Sequence< sal_Int64 >* OModule::s_pCreationFunctionPointers = NULL;
153 : Sequence< sal_Int64 >* OModule::s_pFactoryFunctionPointers = NULL;
154 :
155 :
156 0 : void OModule::registerComponent(
157 : const OUString& _rImplementationName,
158 : const Sequence< OUString >& _rServiceNames,
159 : ComponentInstantiation _pCreateFunction,
160 : FactoryInstantiation _pFactoryFunction)
161 : {
162 0 : if (!s_pImplementationNames)
163 : {
164 : OSL_ENSURE(!s_pSupportedServices && !s_pCreationFunctionPointers && !s_pFactoryFunctionPointers,
165 : "OModule::registerComponent : inconsistent state (the pointers (1)) !");
166 0 : s_pImplementationNames = new Sequence< OUString >;
167 0 : s_pSupportedServices = new Sequence< Sequence< OUString > >;
168 0 : s_pCreationFunctionPointers = new Sequence< sal_Int64 >;
169 0 : s_pFactoryFunctionPointers = new Sequence< sal_Int64 >;
170 : }
171 : OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
172 : "OModule::registerComponent : inconsistent state (the pointers (2)) !");
173 :
174 : OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
175 : && (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
176 : && (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
177 : "OModule::registerComponent : inconsistent state !");
178 :
179 0 : sal_Int32 nOldLen = s_pImplementationNames->getLength();
180 0 : s_pImplementationNames->realloc(nOldLen + 1);
181 0 : s_pSupportedServices->realloc(nOldLen + 1);
182 0 : s_pCreationFunctionPointers->realloc(nOldLen + 1);
183 0 : s_pFactoryFunctionPointers->realloc(nOldLen + 1);
184 :
185 0 : s_pImplementationNames->getArray()[nOldLen] = _rImplementationName;
186 0 : s_pSupportedServices->getArray()[nOldLen] = _rServiceNames;
187 0 : s_pCreationFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pCreateFunction);
188 0 : s_pFactoryFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pFactoryFunction);
189 0 : }
190 :
191 :
192 0 : void OModule::revokeComponent(const OUString& _rImplementationName)
193 : {
194 0 : if (!s_pImplementationNames)
195 : {
196 : OSL_FAIL("OModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
197 0 : return;
198 : }
199 : OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
200 : "OModule::revokeComponent : inconsistent state (the pointers) !");
201 : OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
202 : && (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
203 : && (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
204 : "OModule::revokeComponent : inconsistent state !");
205 :
206 0 : sal_Int32 nLen = s_pImplementationNames->getLength();
207 0 : const OUString* pImplNames = s_pImplementationNames->getConstArray();
208 0 : for (sal_Int32 i=0; i<nLen; ++i, ++pImplNames)
209 : {
210 0 : if (pImplNames->equals(_rImplementationName))
211 : {
212 0 : removeElementAt(*s_pImplementationNames, i);
213 0 : removeElementAt(*s_pSupportedServices, i);
214 0 : removeElementAt(*s_pCreationFunctionPointers, i);
215 0 : removeElementAt(*s_pFactoryFunctionPointers, i);
216 0 : break;
217 : }
218 : }
219 :
220 0 : if (s_pImplementationNames->getLength() == 0)
221 : {
222 0 : delete s_pImplementationNames; s_pImplementationNames = NULL;
223 0 : delete s_pSupportedServices; s_pSupportedServices = NULL;
224 0 : delete s_pCreationFunctionPointers; s_pCreationFunctionPointers = NULL;
225 0 : delete s_pFactoryFunctionPointers; s_pFactoryFunctionPointers = NULL;
226 : }
227 : }
228 :
229 :
230 0 : Reference< XInterface > OModule::getComponentFactory(
231 : const OUString& _rImplementationName,
232 : const Reference< XMultiServiceFactory >& _rxServiceManager)
233 : {
234 : OSL_ENSURE(_rxServiceManager.is(), "OModule::getComponentFactory : invalid argument (service manager) !");
235 : OSL_ENSURE(!_rImplementationName.isEmpty(), "OModule::getComponentFactory : invalid argument (implementation name) !");
236 :
237 0 : if (!s_pImplementationNames)
238 : {
239 : OSL_FAIL("OModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
240 0 : return NULL;
241 : }
242 : OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
243 : "OModule::getComponentFactory : inconsistent state (the pointers) !");
244 : OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
245 : && (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
246 : && (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
247 : "OModule::getComponentFactory : inconsistent state !");
248 :
249 :
250 0 : Reference< XInterface > xReturn;
251 :
252 :
253 0 : sal_Int32 nLen = s_pImplementationNames->getLength();
254 0 : const OUString* pImplName = s_pImplementationNames->getConstArray();
255 0 : const Sequence< OUString >* pServices = s_pSupportedServices->getConstArray();
256 0 : const sal_Int64* pComponentFunction = s_pCreationFunctionPointers->getConstArray();
257 0 : const sal_Int64* pFactoryFunction = s_pFactoryFunctionPointers->getConstArray();
258 :
259 0 : for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices, ++pComponentFunction, ++pFactoryFunction)
260 : {
261 0 : if (pImplName->equals(_rImplementationName))
262 : {
263 0 : const FactoryInstantiation FactoryInstantiationFunction = reinterpret_cast<const FactoryInstantiation>(*pFactoryFunction);
264 0 : const ComponentInstantiation ComponentInstantiationFunction = reinterpret_cast<const ComponentInstantiation>(*pComponentFunction);
265 :
266 0 : xReturn = FactoryInstantiationFunction( _rxServiceManager, *pImplName, ComponentInstantiationFunction, *pServices, NULL);
267 0 : if (xReturn.is())
268 : {
269 0 : xReturn->acquire();
270 0 : return xReturn.get();
271 : }
272 : }
273 : }
274 :
275 0 : return NULL;
276 : }
277 :
278 :
279 :
280 0 : } // namespace COMPMOD_NAMESPACE
281 :
282 :
283 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|