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