xref: /trunk/main/extensions/source/inc/componentmodule.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "componentmodule.hxx"
29*cdf0e10cSrcweir #include <tools/resmgr.hxx>
30*cdf0e10cSrcweir #ifndef _SOLAR_HRC
31*cdf0e10cSrcweir #include <svl/solar.hrc>
32*cdf0e10cSrcweir #endif
33*cdf0e10cSrcweir #include <comphelper/sequence.hxx>
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #define ENTER_MOD_METHOD()  \
37*cdf0e10cSrcweir     ::osl::MutexGuard aGuard(s_aMutex); \
38*cdf0e10cSrcweir     ensureImpl()
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir //.........................................................................
41*cdf0e10cSrcweir namespace COMPMOD_NAMESPACE
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir //.........................................................................
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
46*cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
47*cdf0e10cSrcweir     using namespace ::com::sun::star::registry;
48*cdf0e10cSrcweir     using namespace ::comphelper;
49*cdf0e10cSrcweir     using namespace ::cppu;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir     //=========================================================================
52*cdf0e10cSrcweir     //= OModuleImpl
53*cdf0e10cSrcweir     //=========================================================================
54*cdf0e10cSrcweir     /** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
55*cdf0e10cSrcweir     */
56*cdf0e10cSrcweir     class OModuleImpl
57*cdf0e10cSrcweir     {
58*cdf0e10cSrcweir         ResMgr*     m_pRessources;
59*cdf0e10cSrcweir         sal_Bool    m_bInitialized;
60*cdf0e10cSrcweir         ByteString  m_sFilePrefix;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     public:
63*cdf0e10cSrcweir         /// ctor
64*cdf0e10cSrcweir         OModuleImpl();
65*cdf0e10cSrcweir         ~OModuleImpl();
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir         /// get the manager for the ressources of the module
68*cdf0e10cSrcweir         ResMgr* getResManager();
69*cdf0e10cSrcweir         void    setResourceFilePrefix(const ::rtl::OString& _rPrefix) { m_sFilePrefix = _rPrefix; }
70*cdf0e10cSrcweir     };
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     //-------------------------------------------------------------------------
73*cdf0e10cSrcweir     OModuleImpl::OModuleImpl()
74*cdf0e10cSrcweir         :m_pRessources(NULL)
75*cdf0e10cSrcweir         ,m_bInitialized(sal_False)
76*cdf0e10cSrcweir     {
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     //-------------------------------------------------------------------------
80*cdf0e10cSrcweir     OModuleImpl::~OModuleImpl()
81*cdf0e10cSrcweir     {
82*cdf0e10cSrcweir         if (m_pRessources)
83*cdf0e10cSrcweir             delete m_pRessources;
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     //-------------------------------------------------------------------------
87*cdf0e10cSrcweir     ResMgr* OModuleImpl::getResManager()
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         // note that this method is not threadsafe, which counts for the whole class !
90*cdf0e10cSrcweir         if (!m_pRessources && !m_bInitialized)
91*cdf0e10cSrcweir         {
92*cdf0e10cSrcweir             DBG_ASSERT(m_sFilePrefix.Len(), "OModuleImpl::getResManager: no resource file prefix!");
93*cdf0e10cSrcweir             // create a manager with a fixed prefix
94*cdf0e10cSrcweir             ByteString aMgrName = m_sFilePrefix;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir             m_pRessources = ResMgr::CreateResMgr(aMgrName.GetBuffer());
97*cdf0e10cSrcweir             DBG_ASSERT(m_pRessources,
98*cdf0e10cSrcweir                     (ByteString("OModuleImpl::getResManager: could not create the resource manager (file name: ")
99*cdf0e10cSrcweir                 +=  aMgrName
100*cdf0e10cSrcweir                 +=  ByteString(")!")).GetBuffer());
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir             m_bInitialized = sal_True;
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir         return m_pRessources;
105*cdf0e10cSrcweir     }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     //=========================================================================
108*cdf0e10cSrcweir     //= OModule
109*cdf0e10cSrcweir     //=========================================================================
110*cdf0e10cSrcweir     ::osl::Mutex    OModule::s_aMutex;
111*cdf0e10cSrcweir     sal_Int32       OModule::s_nClients = 0;
112*cdf0e10cSrcweir     OModuleImpl*    OModule::s_pImpl = NULL;
113*cdf0e10cSrcweir     ::rtl::OString  OModule::s_sResPrefix;
114*cdf0e10cSrcweir     //-------------------------------------------------------------------------
115*cdf0e10cSrcweir     ResMgr* OModule::getResManager()
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         ENTER_MOD_METHOD();
118*cdf0e10cSrcweir         return s_pImpl->getResManager();
119*cdf0e10cSrcweir     }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     //-------------------------------------------------------------------------
122*cdf0e10cSrcweir     void OModule::setResourceFilePrefix(const ::rtl::OString& _rPrefix)
123*cdf0e10cSrcweir     {
124*cdf0e10cSrcweir         ::osl::MutexGuard aGuard(s_aMutex);
125*cdf0e10cSrcweir         s_sResPrefix = _rPrefix;
126*cdf0e10cSrcweir         if (s_pImpl)
127*cdf0e10cSrcweir             s_pImpl->setResourceFilePrefix(_rPrefix);
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     //-------------------------------------------------------------------------
131*cdf0e10cSrcweir     void OModule::registerClient()
132*cdf0e10cSrcweir     {
133*cdf0e10cSrcweir         ::osl::MutexGuard aGuard(s_aMutex);
134*cdf0e10cSrcweir         ++s_nClients;
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     //-------------------------------------------------------------------------
138*cdf0e10cSrcweir     void OModule::revokeClient()
139*cdf0e10cSrcweir     {
140*cdf0e10cSrcweir         ::osl::MutexGuard aGuard(s_aMutex);
141*cdf0e10cSrcweir         if (!--s_nClients && s_pImpl)
142*cdf0e10cSrcweir         {
143*cdf0e10cSrcweir             delete s_pImpl;
144*cdf0e10cSrcweir             s_pImpl = NULL;
145*cdf0e10cSrcweir         }
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     //-------------------------------------------------------------------------
149*cdf0e10cSrcweir     void OModule::ensureImpl()
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         if (s_pImpl)
152*cdf0e10cSrcweir             return;
153*cdf0e10cSrcweir         s_pImpl = new OModuleImpl();
154*cdf0e10cSrcweir         s_pImpl->setResourceFilePrefix(s_sResPrefix);
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     //--------------------------------------------------------------------------
158*cdf0e10cSrcweir     //- registration helper
159*cdf0e10cSrcweir     //--------------------------------------------------------------------------
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir     Sequence< ::rtl::OUString >*                OModule::s_pImplementationNames = NULL;
162*cdf0e10cSrcweir     Sequence< Sequence< ::rtl::OUString > >*    OModule::s_pSupportedServices = NULL;
163*cdf0e10cSrcweir     Sequence< sal_Int64 >*                      OModule::s_pCreationFunctionPointers = NULL;
164*cdf0e10cSrcweir     Sequence< sal_Int64 >*                      OModule::s_pFactoryFunctionPointers = NULL;
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     //--------------------------------------------------------------------------
167*cdf0e10cSrcweir     void OModule::registerComponent(
168*cdf0e10cSrcweir         const ::rtl::OUString& _rImplementationName,
169*cdf0e10cSrcweir         const Sequence< ::rtl::OUString >& _rServiceNames,
170*cdf0e10cSrcweir         ComponentInstantiation _pCreateFunction,
171*cdf0e10cSrcweir         FactoryInstantiation _pFactoryFunction)
172*cdf0e10cSrcweir     {
173*cdf0e10cSrcweir         if (!s_pImplementationNames)
174*cdf0e10cSrcweir         {
175*cdf0e10cSrcweir             OSL_ENSURE(!s_pSupportedServices && !s_pCreationFunctionPointers && !s_pFactoryFunctionPointers,
176*cdf0e10cSrcweir                 "OModule::registerComponent : inconsistent state (the pointers (1)) !");
177*cdf0e10cSrcweir             s_pImplementationNames = new Sequence< ::rtl::OUString >;
178*cdf0e10cSrcweir             s_pSupportedServices = new Sequence< Sequence< ::rtl::OUString > >;
179*cdf0e10cSrcweir             s_pCreationFunctionPointers = new Sequence< sal_Int64 >;
180*cdf0e10cSrcweir             s_pFactoryFunctionPointers = new Sequence< sal_Int64 >;
181*cdf0e10cSrcweir         }
182*cdf0e10cSrcweir         OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
183*cdf0e10cSrcweir             "OModule::registerComponent : inconsistent state (the pointers (2)) !");
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir         OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
186*cdf0e10cSrcweir                     &&  (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
187*cdf0e10cSrcweir                     &&  (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
188*cdf0e10cSrcweir             "OModule::registerComponent : inconsistent state !");
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         sal_Int32 nOldLen = s_pImplementationNames->getLength();
191*cdf0e10cSrcweir         s_pImplementationNames->realloc(nOldLen + 1);
192*cdf0e10cSrcweir         s_pSupportedServices->realloc(nOldLen + 1);
193*cdf0e10cSrcweir         s_pCreationFunctionPointers->realloc(nOldLen + 1);
194*cdf0e10cSrcweir         s_pFactoryFunctionPointers->realloc(nOldLen + 1);
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir         s_pImplementationNames->getArray()[nOldLen] = _rImplementationName;
197*cdf0e10cSrcweir         s_pSupportedServices->getArray()[nOldLen] = _rServiceNames;
198*cdf0e10cSrcweir         s_pCreationFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pCreateFunction);
199*cdf0e10cSrcweir         s_pFactoryFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pFactoryFunction);
200*cdf0e10cSrcweir     }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     //--------------------------------------------------------------------------
203*cdf0e10cSrcweir     void OModule::revokeComponent(const ::rtl::OUString& _rImplementationName)
204*cdf0e10cSrcweir     {
205*cdf0e10cSrcweir         if (!s_pImplementationNames)
206*cdf0e10cSrcweir         {
207*cdf0e10cSrcweir             OSL_ASSERT("OModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
208*cdf0e10cSrcweir             return;
209*cdf0e10cSrcweir         }
210*cdf0e10cSrcweir         OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
211*cdf0e10cSrcweir             "OModule::revokeComponent : inconsistent state (the pointers) !");
212*cdf0e10cSrcweir         OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
213*cdf0e10cSrcweir                     &&  (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
214*cdf0e10cSrcweir                     &&  (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
215*cdf0e10cSrcweir             "OModule::revokeComponent : inconsistent state !");
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         sal_Int32 nLen = s_pImplementationNames->getLength();
218*cdf0e10cSrcweir         const ::rtl::OUString* pImplNames = s_pImplementationNames->getConstArray();
219*cdf0e10cSrcweir         for (sal_Int32 i=0; i<nLen; ++i, ++pImplNames)
220*cdf0e10cSrcweir         {
221*cdf0e10cSrcweir             if (pImplNames->equals(_rImplementationName))
222*cdf0e10cSrcweir             {
223*cdf0e10cSrcweir                 removeElementAt(*s_pImplementationNames, i);
224*cdf0e10cSrcweir                 removeElementAt(*s_pSupportedServices, i);
225*cdf0e10cSrcweir                 removeElementAt(*s_pCreationFunctionPointers, i);
226*cdf0e10cSrcweir                 removeElementAt(*s_pFactoryFunctionPointers, i);
227*cdf0e10cSrcweir                 break;
228*cdf0e10cSrcweir             }
229*cdf0e10cSrcweir         }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir         if (s_pImplementationNames->getLength() == 0)
232*cdf0e10cSrcweir         {
233*cdf0e10cSrcweir             delete s_pImplementationNames; s_pImplementationNames = NULL;
234*cdf0e10cSrcweir             delete s_pSupportedServices; s_pSupportedServices = NULL;
235*cdf0e10cSrcweir             delete s_pCreationFunctionPointers; s_pCreationFunctionPointers = NULL;
236*cdf0e10cSrcweir             delete s_pFactoryFunctionPointers; s_pFactoryFunctionPointers = NULL;
237*cdf0e10cSrcweir         }
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     //--------------------------------------------------------------------------
241*cdf0e10cSrcweir     Reference< XInterface > OModule::getComponentFactory(
242*cdf0e10cSrcweir         const ::rtl::OUString& _rImplementationName,
243*cdf0e10cSrcweir         const Reference< XMultiServiceFactory >& _rxServiceManager)
244*cdf0e10cSrcweir     {
245*cdf0e10cSrcweir         OSL_ENSURE(_rxServiceManager.is(), "OModule::getComponentFactory : invalid argument (service manager) !");
246*cdf0e10cSrcweir         OSL_ENSURE(_rImplementationName.getLength(), "OModule::getComponentFactory : invalid argument (implementation name) !");
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir         if (!s_pImplementationNames)
249*cdf0e10cSrcweir         {
250*cdf0e10cSrcweir             OSL_ASSERT("OModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
251*cdf0e10cSrcweir             return NULL;
252*cdf0e10cSrcweir         }
253*cdf0e10cSrcweir         OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
254*cdf0e10cSrcweir             "OModule::getComponentFactory : inconsistent state (the pointers) !");
255*cdf0e10cSrcweir         OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
256*cdf0e10cSrcweir                     &&  (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
257*cdf0e10cSrcweir                     &&  (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
258*cdf0e10cSrcweir             "OModule::getComponentFactory : inconsistent state !");
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir         Reference< XInterface > xReturn;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir         sal_Int32 nLen = s_pImplementationNames->getLength();
265*cdf0e10cSrcweir         const ::rtl::OUString* pImplName = s_pImplementationNames->getConstArray();
266*cdf0e10cSrcweir         const Sequence< ::rtl::OUString >* pServices = s_pSupportedServices->getConstArray();
267*cdf0e10cSrcweir         const sal_Int64* pComponentFunction = s_pCreationFunctionPointers->getConstArray();
268*cdf0e10cSrcweir         const sal_Int64* pFactoryFunction = s_pFactoryFunctionPointers->getConstArray();
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir         for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices, ++pComponentFunction, ++pFactoryFunction)
271*cdf0e10cSrcweir         {
272*cdf0e10cSrcweir             if (pImplName->equals(_rImplementationName))
273*cdf0e10cSrcweir             {
274*cdf0e10cSrcweir                 const FactoryInstantiation FactoryInstantiationFunction = reinterpret_cast<const FactoryInstantiation>(*pFactoryFunction);
275*cdf0e10cSrcweir                 const ComponentInstantiation ComponentInstantiationFunction = reinterpret_cast<const ComponentInstantiation>(*pComponentFunction);
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir                 xReturn = FactoryInstantiationFunction( _rxServiceManager, *pImplName, ComponentInstantiationFunction, *pServices, NULL);
278*cdf0e10cSrcweir                 if (xReturn.is())
279*cdf0e10cSrcweir                 {
280*cdf0e10cSrcweir                     xReturn->acquire();
281*cdf0e10cSrcweir                     return xReturn.get();
282*cdf0e10cSrcweir                 }
283*cdf0e10cSrcweir             }
284*cdf0e10cSrcweir         }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir         return NULL;
287*cdf0e10cSrcweir     }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir //.........................................................................
291*cdf0e10cSrcweir }   // namespace COMPMOD_NAMESPACE
292*cdf0e10cSrcweir //.........................................................................
293*cdf0e10cSrcweir 
294