update dev300-m58
[ooovba.git] / dbaccess / source / shared / registrationhelper.cxx
blob4abd2713d148b2c505c4ecb46e5f68f4fc2b04dd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: registrationhelper.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // no include "precompiled_dbaccess.hxx" because this file is meant to
32 // be included in other cxx files
34 #ifndef _REGISTRATIONHELPER_CXX_INCLUDED_INDIRECTLY_
35 #error "don't build this file directly! use dbu_reghelper.cxx instead!"
36 #endif
38 using namespace ::com::sun::star;
39 using namespace ::comphelper;
40 using namespace ::cppu;
42 uno::Sequence< ::rtl::OUString >* OModuleRegistration::s_pImplementationNames = NULL;
43 uno::Sequence< uno::Sequence< ::rtl::OUString > >* OModuleRegistration::s_pSupportedServices = NULL;
44 uno::Sequence< sal_Int64 >* OModuleRegistration::s_pCreationFunctionPointers = NULL;
45 uno::Sequence< sal_Int64 >* OModuleRegistration::s_pFactoryFunctionPointers = NULL;
47 //--------------------------------------------------------------------------
48 void OModuleRegistration::registerComponent(
49 const ::rtl::OUString& _rImplementationName,
50 const uno::Sequence< ::rtl::OUString >& _rServiceNames,
51 ComponentInstantiation _pCreateFunction,
52 FactoryInstantiation _pFactoryFunction)
54 if (!s_pImplementationNames)
56 OSL_ENSURE(!s_pSupportedServices && !s_pCreationFunctionPointers && !s_pFactoryFunctionPointers,
57 "OModuleRegistration::registerComponent : inconsistent state (the pointers (1)) !");
58 s_pImplementationNames = new uno::Sequence< ::rtl::OUString >;
59 s_pSupportedServices = new uno::Sequence< uno::Sequence< ::rtl::OUString > >;
60 s_pCreationFunctionPointers = new uno::Sequence< sal_Int64 >;
61 s_pFactoryFunctionPointers = new uno::Sequence< sal_Int64 >;
63 OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
64 "OModuleRegistration::registerComponent : inconsistent state (the pointers (2)) !");
66 OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
67 && (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
68 && (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
69 "OModuleRegistration::registerComponent : inconsistent state !");
71 sal_Int32 nOldLen = s_pImplementationNames->getLength();
72 s_pImplementationNames->realloc(nOldLen + 1);
73 s_pSupportedServices->realloc(nOldLen + 1);
74 s_pCreationFunctionPointers->realloc(nOldLen + 1);
75 s_pFactoryFunctionPointers->realloc(nOldLen + 1);
77 s_pImplementationNames->getArray()[nOldLen] = _rImplementationName;
78 s_pSupportedServices->getArray()[nOldLen] = _rServiceNames;
79 s_pCreationFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pCreateFunction);
80 s_pFactoryFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pFactoryFunction);
83 //--------------------------------------------------------------------------
84 void OModuleRegistration::revokeComponent(const ::rtl::OUString& _rImplementationName)
86 if (!s_pImplementationNames)
88 OSL_ENSURE(sal_False, "OModuleRegistration::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
89 return;
91 OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
92 "OModuleRegistration::revokeComponent : inconsistent state (the pointers) !");
93 OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
94 && (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
95 && (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
96 "OModuleRegistration::revokeComponent : inconsistent state !");
98 sal_Int32 nLen = s_pImplementationNames->getLength();
99 const ::rtl::OUString* pImplNames = s_pImplementationNames->getConstArray();
100 for (sal_Int32 i=0; i<nLen; ++i, ++pImplNames)
102 if (pImplNames->equals(_rImplementationName))
104 removeElementAt(*s_pImplementationNames, i);
105 removeElementAt(*s_pSupportedServices, i);
106 removeElementAt(*s_pCreationFunctionPointers, i);
107 removeElementAt(*s_pFactoryFunctionPointers, i);
108 break;
112 if (s_pImplementationNames->getLength() == 0)
114 delete s_pImplementationNames; s_pImplementationNames = NULL;
115 delete s_pSupportedServices; s_pSupportedServices = NULL;
116 delete s_pCreationFunctionPointers; s_pCreationFunctionPointers = NULL;
117 delete s_pFactoryFunctionPointers; s_pFactoryFunctionPointers = NULL;
121 //--------------------------------------------------------------------------
122 sal_Bool OModuleRegistration::writeComponentInfos(
123 const uno::Reference< lang::XMultiServiceFactory >& /*_rxServiceManager*/,
124 const uno::Reference< registry::XRegistryKey >& _rxRootKey)
126 OSL_ENSURE(_rxRootKey.is(), "OModuleRegistration::writeComponentInfos : invalid argument !");
128 if (!s_pImplementationNames)
130 OSL_ENSURE(sal_False, "OModuleRegistration::writeComponentInfos : have no class infos ! Are you sure called this method at the right time ?");
131 return sal_True;
133 OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
134 "OModuleRegistration::writeComponentInfos : inconsistent state (the pointers) !");
135 OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
136 && (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
137 && (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
138 "OModuleRegistration::writeComponentInfos : inconsistent state !");
140 sal_Int32 nLen = s_pImplementationNames->getLength();
141 const ::rtl::OUString* pImplName = s_pImplementationNames->getConstArray();
142 const uno::Sequence< ::rtl::OUString >* pServices = s_pSupportedServices->getConstArray();
144 ::rtl::OUString sRootKey("/", 1, RTL_TEXTENCODING_ASCII_US);
145 for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices)
147 ::rtl::OUString aMainKeyName(sRootKey);
148 aMainKeyName += *pImplName;
149 aMainKeyName += ::rtl::OUString::createFromAscii("/UNO/SERVICES");
153 uno::Reference< registry::XRegistryKey > xNewKey( _rxRootKey->createKey(aMainKeyName) );
155 const ::rtl::OUString* pService = pServices->getConstArray();
156 for (sal_Int32 j=0; j<pServices->getLength(); ++j, ++pService)
157 xNewKey->createKey(*pService);
159 catch(uno::Exception const&)
161 OSL_ENSURE(sal_False, "OModuleRegistration::writeComponentInfos : something went wrong while creating the keys !");
162 return sal_False;
166 return sal_True;
169 //--------------------------------------------------------------------------
170 uno::Reference< uno::XInterface > OModuleRegistration::getComponentFactory(
171 const ::rtl::OUString& _rImplementationName,
172 const uno::Reference< lang::XMultiServiceFactory >& _rxServiceManager)
174 OSL_ENSURE(_rxServiceManager.is(), "OModuleRegistration::getComponentFactory : invalid argument (service manager) !");
175 OSL_ENSURE(_rImplementationName.getLength(), "OModuleRegistration::getComponentFactory : invalid argument (implementation name) !");
177 if (!s_pImplementationNames)
179 OSL_ENSURE(sal_False, "OModuleRegistration::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
180 return NULL;
182 OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
183 "OModuleRegistration::getComponentFactory : inconsistent state (the pointers) !");
184 OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
185 && (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
186 && (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
187 "OModuleRegistration::getComponentFactory : inconsistent state !");
190 uno::Reference< uno::XInterface > xReturn;
193 sal_Int32 nLen = s_pImplementationNames->getLength();
194 const ::rtl::OUString* pImplName = s_pImplementationNames->getConstArray();
195 const uno::Sequence< ::rtl::OUString >* pServices = s_pSupportedServices->getConstArray();
196 const sal_Int64* pComponentFunction = s_pCreationFunctionPointers->getConstArray();
197 const sal_Int64* pFactoryFunction = s_pFactoryFunctionPointers->getConstArray();
199 for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices, ++pComponentFunction, ++pFactoryFunction)
201 if (pImplName->equals(_rImplementationName))
203 const FactoryInstantiation FactoryInstantiationFunction = reinterpret_cast<const FactoryInstantiation>(*pFactoryFunction);
204 const ComponentInstantiation ComponentInstantiationFunction = reinterpret_cast<const ComponentInstantiation>(*pComponentFunction);
206 xReturn = FactoryInstantiationFunction( _rxServiceManager, *pImplName, ComponentInstantiationFunction, *pServices, NULL);
207 if (xReturn.is())
209 xReturn->acquire();
210 return xReturn.get();
215 return NULL;