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); \
31 //.........................................................................
32 namespace COMPMOD_NAMESPACE
34 //.........................................................................
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 //=========================================================================
44 //=========================================================================
45 /** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
50 sal_Bool m_bInitialized
;
51 rtl::OString m_sFilePrefix
;
58 /// get the manager for the resources of the module
59 ResMgr
* getResManager();
60 void setResourceFilePrefix(const ::rtl::OString
& _rPrefix
) { m_sFilePrefix
= _rPrefix
; }
63 //-------------------------------------------------------------------------
64 OModuleImpl::OModuleImpl()
66 ,m_bInitialized(sal_False
)
70 //-------------------------------------------------------------------------
71 OModuleImpl::~OModuleImpl()
77 //-------------------------------------------------------------------------
78 ResMgr
* OModuleImpl::getResManager()
80 // note that this method is not threadsafe, which counts for the whole class !
81 if (!m_pResources
&& !m_bInitialized
)
83 DBG_ASSERT(!m_sFilePrefix
.isEmpty(), "OModuleImpl::getResManager: no resource file prefix!");
84 // create a manager with a fixed prefix
85 m_pResources
= ResMgr::CreateResMgr(m_sFilePrefix
.getStr());
86 DBG_ASSERT(m_pResources
,
87 rtl::OStringBuffer("OModuleImpl::getResManager: could not create the resource manager (file name: ")
88 .append(m_sFilePrefix
)
89 .append(")!").getStr());
91 m_bInitialized
= sal_True
;
96 //=========================================================================
98 //=========================================================================
99 ::osl::Mutex
OModule::s_aMutex
;
100 sal_Int32
OModule::s_nClients
= 0;
101 OModuleImpl
* OModule::s_pImpl
= NULL
;
102 ::rtl::OString
OModule::s_sResPrefix
;
103 //-------------------------------------------------------------------------
104 ResMgr
* OModule::getResManager()
107 return s_pImpl
->getResManager();
110 //-------------------------------------------------------------------------
111 void OModule::setResourceFilePrefix(const ::rtl::OString
& _rPrefix
)
113 ::osl::MutexGuard
aGuard(s_aMutex
);
114 s_sResPrefix
= _rPrefix
;
116 s_pImpl
->setResourceFilePrefix(_rPrefix
);
119 //-------------------------------------------------------------------------
120 void OModule::registerClient()
122 ::osl::MutexGuard
aGuard(s_aMutex
);
126 //-------------------------------------------------------------------------
127 void OModule::revokeClient()
129 ::osl::MutexGuard
aGuard(s_aMutex
);
130 if (!--s_nClients
&& s_pImpl
)
137 //-------------------------------------------------------------------------
138 void OModule::ensureImpl()
142 s_pImpl
= new OModuleImpl();
143 s_pImpl
->setResourceFilePrefix(s_sResPrefix
);
146 //--------------------------------------------------------------------------
147 //- registration helper
148 //--------------------------------------------------------------------------
150 Sequence
< ::rtl::OUString
>* OModule::s_pImplementationNames
= NULL
;
151 Sequence
< Sequence
< ::rtl::OUString
> >* OModule::s_pSupportedServices
= NULL
;
152 Sequence
< sal_Int64
>* OModule::s_pCreationFunctionPointers
= NULL
;
153 Sequence
< sal_Int64
>* OModule::s_pFactoryFunctionPointers
= NULL
;
155 //--------------------------------------------------------------------------
156 void OModule::registerComponent(
157 const ::rtl::OUString
& _rImplementationName
,
158 const Sequence
< ::rtl::OUString
>& _rServiceNames
,
159 ComponentInstantiation _pCreateFunction
,
160 FactoryInstantiation _pFactoryFunction
)
162 if (!s_pImplementationNames
)
164 OSL_ENSURE(!s_pSupportedServices
&& !s_pCreationFunctionPointers
&& !s_pFactoryFunctionPointers
,
165 "OModule::registerComponent : inconsistent state (the pointers (1)) !");
166 s_pImplementationNames
= new Sequence
< ::rtl::OUString
>;
167 s_pSupportedServices
= new Sequence
< Sequence
< ::rtl::OUString
> >;
168 s_pCreationFunctionPointers
= new Sequence
< sal_Int64
>;
169 s_pFactoryFunctionPointers
= new Sequence
< sal_Int64
>;
171 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
172 "OModule::registerComponent : inconsistent state (the pointers (2)) !");
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 !");
179 sal_Int32 nOldLen
= s_pImplementationNames
->getLength();
180 s_pImplementationNames
->realloc(nOldLen
+ 1);
181 s_pSupportedServices
->realloc(nOldLen
+ 1);
182 s_pCreationFunctionPointers
->realloc(nOldLen
+ 1);
183 s_pFactoryFunctionPointers
->realloc(nOldLen
+ 1);
185 s_pImplementationNames
->getArray()[nOldLen
] = _rImplementationName
;
186 s_pSupportedServices
->getArray()[nOldLen
] = _rServiceNames
;
187 s_pCreationFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pCreateFunction
);
188 s_pFactoryFunctionPointers
->getArray()[nOldLen
] = reinterpret_cast<sal_Int64
>(_pFactoryFunction
);
191 //--------------------------------------------------------------------------
192 void OModule::revokeComponent(const ::rtl::OUString
& _rImplementationName
)
194 if (!s_pImplementationNames
)
196 OSL_FAIL("OModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
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 !");
206 sal_Int32 nLen
= s_pImplementationNames
->getLength();
207 const ::rtl::OUString
* pImplNames
= s_pImplementationNames
->getConstArray();
208 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplNames
)
210 if (pImplNames
->equals(_rImplementationName
))
212 removeElementAt(*s_pImplementationNames
, i
);
213 removeElementAt(*s_pSupportedServices
, i
);
214 removeElementAt(*s_pCreationFunctionPointers
, i
);
215 removeElementAt(*s_pFactoryFunctionPointers
, i
);
220 if (s_pImplementationNames
->getLength() == 0)
222 delete s_pImplementationNames
; s_pImplementationNames
= NULL
;
223 delete s_pSupportedServices
; s_pSupportedServices
= NULL
;
224 delete s_pCreationFunctionPointers
; s_pCreationFunctionPointers
= NULL
;
225 delete s_pFactoryFunctionPointers
; s_pFactoryFunctionPointers
= NULL
;
229 //--------------------------------------------------------------------------
230 Reference
< XInterface
> OModule::getComponentFactory(
231 const ::rtl::OUString
& _rImplementationName
,
232 const Reference
< XMultiServiceFactory
>& _rxServiceManager
)
234 OSL_ENSURE(_rxServiceManager
.is(), "OModule::getComponentFactory : invalid argument (service manager) !");
235 OSL_ENSURE(!_rImplementationName
.isEmpty(), "OModule::getComponentFactory : invalid argument (implementation name) !");
237 if (!s_pImplementationNames
)
239 OSL_FAIL("OModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
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 !");
250 Reference
< XInterface
> xReturn
;
253 sal_Int32 nLen
= s_pImplementationNames
->getLength();
254 const ::rtl::OUString
* pImplName
= s_pImplementationNames
->getConstArray();
255 const Sequence
< ::rtl::OUString
>* pServices
= s_pSupportedServices
->getConstArray();
256 const sal_Int64
* pComponentFunction
= s_pCreationFunctionPointers
->getConstArray();
257 const sal_Int64
* pFactoryFunction
= s_pFactoryFunctionPointers
->getConstArray();
259 for (sal_Int32 i
=0; i
<nLen
; ++i
, ++pImplName
, ++pServices
, ++pComponentFunction
, ++pFactoryFunction
)
261 if (pImplName
->equals(_rImplementationName
))
263 const FactoryInstantiation FactoryInstantiationFunction
= reinterpret_cast<const FactoryInstantiation
>(*pFactoryFunction
);
264 const ComponentInstantiation ComponentInstantiationFunction
= reinterpret_cast<const ComponentInstantiation
>(*pComponentFunction
);
266 xReturn
= FactoryInstantiationFunction( _rxServiceManager
, *pImplName
, ComponentInstantiationFunction
, *pServices
, NULL
);
270 return xReturn
.get();
279 //.........................................................................
280 } // namespace COMPMOD_NAMESPACE
281 //.........................................................................
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */