xref: /trunk/main/dbaccess/source/shared/registrationhelper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // no include "precompiled_dbaccess.hxx" because this file is meant to
29 // be included in other cxx files
30 
31 #ifndef _REGISTRATIONHELPER_CXX_INCLUDED_INDIRECTLY_
32 #error "don't build this file directly! use dbu_reghelper.cxx instead!"
33 #endif
34 
35 using namespace ::com::sun::star;
36 using namespace ::comphelper;
37 using namespace ::cppu;
38 
39 uno::Sequence< ::rtl::OUString >*                   OModuleRegistration::s_pImplementationNames = NULL;
40 uno::Sequence< uno::Sequence< ::rtl::OUString > >*  OModuleRegistration::s_pSupportedServices = NULL;
41 uno::Sequence< sal_Int64 >*                 OModuleRegistration::s_pCreationFunctionPointers = NULL;
42 uno::Sequence< sal_Int64 >*                 OModuleRegistration::s_pFactoryFunctionPointers = NULL;
43 
44 //--------------------------------------------------------------------------
45 void OModuleRegistration::registerComponent(
46     const ::rtl::OUString& _rImplementationName,
47     const uno::Sequence< ::rtl::OUString >& _rServiceNames,
48     ComponentInstantiation _pCreateFunction,
49     FactoryInstantiation _pFactoryFunction)
50 {
51     if (!s_pImplementationNames)
52     {
53         OSL_ENSURE(!s_pSupportedServices && !s_pCreationFunctionPointers && !s_pFactoryFunctionPointers,
54             "OModuleRegistration::registerComponent : inconsistent state (the pointers (1)) !");
55         s_pImplementationNames = new uno::Sequence< ::rtl::OUString >;
56         s_pSupportedServices = new uno::Sequence< uno::Sequence< ::rtl::OUString > >;
57         s_pCreationFunctionPointers = new uno::Sequence< sal_Int64 >;
58         s_pFactoryFunctionPointers = new uno::Sequence< sal_Int64 >;
59     }
60     OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
61         "OModuleRegistration::registerComponent : inconsistent state (the pointers (2)) !");
62 
63     OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
64                 &&  (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
65                 &&  (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
66         "OModuleRegistration::registerComponent : inconsistent state !");
67 
68     sal_Int32 nOldLen = s_pImplementationNames->getLength();
69     s_pImplementationNames->realloc(nOldLen + 1);
70     s_pSupportedServices->realloc(nOldLen + 1);
71     s_pCreationFunctionPointers->realloc(nOldLen + 1);
72     s_pFactoryFunctionPointers->realloc(nOldLen + 1);
73 
74     s_pImplementationNames->getArray()[nOldLen] = _rImplementationName;
75     s_pSupportedServices->getArray()[nOldLen] = _rServiceNames;
76     s_pCreationFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pCreateFunction);
77     s_pFactoryFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pFactoryFunction);
78 }
79 
80 //--------------------------------------------------------------------------
81 void OModuleRegistration::revokeComponent(const ::rtl::OUString& _rImplementationName)
82 {
83     if (!s_pImplementationNames)
84     {
85         OSL_ENSURE(sal_False, "OModuleRegistration::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
86         return;
87     }
88     OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
89         "OModuleRegistration::revokeComponent : inconsistent state (the pointers) !");
90     OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
91                 &&  (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
92                 &&  (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
93         "OModuleRegistration::revokeComponent : inconsistent state !");
94 
95     sal_Int32 nLen = s_pImplementationNames->getLength();
96     const ::rtl::OUString* pImplNames = s_pImplementationNames->getConstArray();
97     for (sal_Int32 i=0; i<nLen; ++i, ++pImplNames)
98     {
99         if (pImplNames->equals(_rImplementationName))
100         {
101             removeElementAt(*s_pImplementationNames, i);
102             removeElementAt(*s_pSupportedServices, i);
103             removeElementAt(*s_pCreationFunctionPointers, i);
104             removeElementAt(*s_pFactoryFunctionPointers, i);
105             break;
106         }
107     }
108 
109     if (s_pImplementationNames->getLength() == 0)
110     {
111         delete s_pImplementationNames; s_pImplementationNames = NULL;
112         delete s_pSupportedServices; s_pSupportedServices = NULL;
113         delete s_pCreationFunctionPointers; s_pCreationFunctionPointers = NULL;
114         delete s_pFactoryFunctionPointers; s_pFactoryFunctionPointers = NULL;
115     }
116 }
117 
118 //--------------------------------------------------------------------------
119 uno::Reference< uno::XInterface > OModuleRegistration::getComponentFactory(
120     const ::rtl::OUString& _rImplementationName,
121     const uno::Reference< lang::XMultiServiceFactory >& _rxServiceManager)
122 {
123     OSL_ENSURE(_rxServiceManager.is(), "OModuleRegistration::getComponentFactory : invalid argument (service manager) !");
124     OSL_ENSURE(_rImplementationName.getLength(), "OModuleRegistration::getComponentFactory : invalid argument (implementation name) !");
125 
126     if (!s_pImplementationNames)
127     {
128         OSL_ENSURE(sal_False, "OModuleRegistration::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
129         return NULL;
130     }
131     OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
132         "OModuleRegistration::getComponentFactory : inconsistent state (the pointers) !");
133     OSL_ENSURE( (s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
134                 &&  (s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
135                 &&  (s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
136         "OModuleRegistration::getComponentFactory : inconsistent state !");
137 
138 
139     uno::Reference< uno::XInterface > xReturn;
140 
141 
142     sal_Int32 nLen = s_pImplementationNames->getLength();
143     const ::rtl::OUString* pImplName = s_pImplementationNames->getConstArray();
144     const uno::Sequence< ::rtl::OUString >* pServices = s_pSupportedServices->getConstArray();
145     const sal_Int64* pComponentFunction = s_pCreationFunctionPointers->getConstArray();
146     const sal_Int64* pFactoryFunction = s_pFactoryFunctionPointers->getConstArray();
147 
148     for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices, ++pComponentFunction, ++pFactoryFunction)
149     {
150         if (pImplName->equals(_rImplementationName))
151         {
152             const FactoryInstantiation FactoryInstantiationFunction = reinterpret_cast<const FactoryInstantiation>(*pFactoryFunction);
153             const ComponentInstantiation ComponentInstantiationFunction = reinterpret_cast<const ComponentInstantiation>(*pComponentFunction);
154 
155             xReturn = FactoryInstantiationFunction( _rxServiceManager, *pImplName, ComponentInstantiationFunction, *pServices, NULL);
156             if (xReturn.is())
157             {
158                 xReturn->acquire();
159                 return xReturn.get();
160             }
161         }
162     }
163 
164     return NULL;
165 }
166 
167 
168