1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski 
24*b1cdbd2cSJim Jagielski // no include "precompiled_dbaccess.hxx" because this file is meant to
25*b1cdbd2cSJim Jagielski // be included in other cxx files
26*b1cdbd2cSJim Jagielski 
27*b1cdbd2cSJim Jagielski #ifndef _REGISTRATIONHELPER_CXX_INCLUDED_INDIRECTLY_
28*b1cdbd2cSJim Jagielski #error "don't build this file directly! use dbu_reghelper.cxx instead!"
29*b1cdbd2cSJim Jagielski #endif
30*b1cdbd2cSJim Jagielski 
31*b1cdbd2cSJim Jagielski using namespace ::com::sun::star;
32*b1cdbd2cSJim Jagielski using namespace ::comphelper;
33*b1cdbd2cSJim Jagielski using namespace ::cppu;
34*b1cdbd2cSJim Jagielski 
35*b1cdbd2cSJim Jagielski uno::Sequence< ::rtl::OUString >*				    OModuleRegistration::s_pImplementationNames = NULL;
36*b1cdbd2cSJim Jagielski uno::Sequence< uno::Sequence< ::rtl::OUString > >*	OModuleRegistration::s_pSupportedServices = NULL;
37*b1cdbd2cSJim Jagielski uno::Sequence< sal_Int64 >*		            OModuleRegistration::s_pCreationFunctionPointers = NULL;
38*b1cdbd2cSJim Jagielski uno::Sequence< sal_Int64 >*				    OModuleRegistration::s_pFactoryFunctionPointers = NULL;
39*b1cdbd2cSJim Jagielski 
40*b1cdbd2cSJim Jagielski //--------------------------------------------------------------------------
registerComponent(const::rtl::OUString & _rImplementationName,const uno::Sequence<::rtl::OUString> & _rServiceNames,ComponentInstantiation _pCreateFunction,FactoryInstantiation _pFactoryFunction)41*b1cdbd2cSJim Jagielski void OModuleRegistration::registerComponent(
42*b1cdbd2cSJim Jagielski 	const ::rtl::OUString& _rImplementationName,
43*b1cdbd2cSJim Jagielski 	const uno::Sequence< ::rtl::OUString >& _rServiceNames,
44*b1cdbd2cSJim Jagielski 	ComponentInstantiation _pCreateFunction,
45*b1cdbd2cSJim Jagielski 	FactoryInstantiation _pFactoryFunction)
46*b1cdbd2cSJim Jagielski {
47*b1cdbd2cSJim Jagielski 	if (!s_pImplementationNames)
48*b1cdbd2cSJim Jagielski 	{
49*b1cdbd2cSJim Jagielski 		OSL_ENSURE(!s_pSupportedServices && !s_pCreationFunctionPointers && !s_pFactoryFunctionPointers,
50*b1cdbd2cSJim Jagielski 			"OModuleRegistration::registerComponent : inconsistent state (the pointers (1)) !");
51*b1cdbd2cSJim Jagielski 		s_pImplementationNames = new uno::Sequence< ::rtl::OUString >;
52*b1cdbd2cSJim Jagielski 		s_pSupportedServices = new uno::Sequence< uno::Sequence< ::rtl::OUString > >;
53*b1cdbd2cSJim Jagielski 		s_pCreationFunctionPointers = new uno::Sequence< sal_Int64 >;
54*b1cdbd2cSJim Jagielski 		s_pFactoryFunctionPointers = new uno::Sequence< sal_Int64 >;
55*b1cdbd2cSJim Jagielski 	}
56*b1cdbd2cSJim Jagielski 	OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
57*b1cdbd2cSJim Jagielski 		"OModuleRegistration::registerComponent : inconsistent state (the pointers (2)) !");
58*b1cdbd2cSJim Jagielski 
59*b1cdbd2cSJim Jagielski 	OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
60*b1cdbd2cSJim Jagielski 				&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
61*b1cdbd2cSJim Jagielski 				&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
62*b1cdbd2cSJim Jagielski 		"OModuleRegistration::registerComponent : inconsistent state !");
63*b1cdbd2cSJim Jagielski 
64*b1cdbd2cSJim Jagielski 	sal_Int32 nOldLen = s_pImplementationNames->getLength();
65*b1cdbd2cSJim Jagielski 	s_pImplementationNames->realloc(nOldLen + 1);
66*b1cdbd2cSJim Jagielski 	s_pSupportedServices->realloc(nOldLen + 1);
67*b1cdbd2cSJim Jagielski 	s_pCreationFunctionPointers->realloc(nOldLen + 1);
68*b1cdbd2cSJim Jagielski 	s_pFactoryFunctionPointers->realloc(nOldLen + 1);
69*b1cdbd2cSJim Jagielski 
70*b1cdbd2cSJim Jagielski 	s_pImplementationNames->getArray()[nOldLen] = _rImplementationName;
71*b1cdbd2cSJim Jagielski 	s_pSupportedServices->getArray()[nOldLen] = _rServiceNames;
72*b1cdbd2cSJim Jagielski 	s_pCreationFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pCreateFunction);
73*b1cdbd2cSJim Jagielski 	s_pFactoryFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pFactoryFunction);
74*b1cdbd2cSJim Jagielski }
75*b1cdbd2cSJim Jagielski 
76*b1cdbd2cSJim Jagielski //--------------------------------------------------------------------------
revokeComponent(const::rtl::OUString & _rImplementationName)77*b1cdbd2cSJim Jagielski void OModuleRegistration::revokeComponent(const ::rtl::OUString& _rImplementationName)
78*b1cdbd2cSJim Jagielski {
79*b1cdbd2cSJim Jagielski 	if (!s_pImplementationNames)
80*b1cdbd2cSJim Jagielski 	{
81*b1cdbd2cSJim Jagielski 		OSL_ENSURE(sal_False, "OModuleRegistration::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
82*b1cdbd2cSJim Jagielski 		return;
83*b1cdbd2cSJim Jagielski 	}
84*b1cdbd2cSJim Jagielski 	OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
85*b1cdbd2cSJim Jagielski 		"OModuleRegistration::revokeComponent : inconsistent state (the pointers) !");
86*b1cdbd2cSJim Jagielski 	OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
87*b1cdbd2cSJim Jagielski 				&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
88*b1cdbd2cSJim Jagielski 				&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
89*b1cdbd2cSJim Jagielski 		"OModuleRegistration::revokeComponent : inconsistent state !");
90*b1cdbd2cSJim Jagielski 
91*b1cdbd2cSJim Jagielski 	sal_Int32 nLen = s_pImplementationNames->getLength();
92*b1cdbd2cSJim Jagielski 	const ::rtl::OUString* pImplNames = s_pImplementationNames->getConstArray();
93*b1cdbd2cSJim Jagielski 	for (sal_Int32 i=0; i<nLen; ++i, ++pImplNames)
94*b1cdbd2cSJim Jagielski 	{
95*b1cdbd2cSJim Jagielski 		if (pImplNames->equals(_rImplementationName))
96*b1cdbd2cSJim Jagielski 		{
97*b1cdbd2cSJim Jagielski 			removeElementAt(*s_pImplementationNames, i);
98*b1cdbd2cSJim Jagielski 			removeElementAt(*s_pSupportedServices, i);
99*b1cdbd2cSJim Jagielski 			removeElementAt(*s_pCreationFunctionPointers, i);
100*b1cdbd2cSJim Jagielski 			removeElementAt(*s_pFactoryFunctionPointers, i);
101*b1cdbd2cSJim Jagielski 			break;
102*b1cdbd2cSJim Jagielski 		}
103*b1cdbd2cSJim Jagielski 	}
104*b1cdbd2cSJim Jagielski 
105*b1cdbd2cSJim Jagielski 	if (s_pImplementationNames->getLength() == 0)
106*b1cdbd2cSJim Jagielski 	{
107*b1cdbd2cSJim Jagielski 		delete s_pImplementationNames; s_pImplementationNames = NULL;
108*b1cdbd2cSJim Jagielski 		delete s_pSupportedServices; s_pSupportedServices = NULL;
109*b1cdbd2cSJim Jagielski 		delete s_pCreationFunctionPointers; s_pCreationFunctionPointers = NULL;
110*b1cdbd2cSJim Jagielski 		delete s_pFactoryFunctionPointers; s_pFactoryFunctionPointers = NULL;
111*b1cdbd2cSJim Jagielski 	}
112*b1cdbd2cSJim Jagielski }
113*b1cdbd2cSJim Jagielski 
114*b1cdbd2cSJim Jagielski //--------------------------------------------------------------------------
getComponentFactory(const::rtl::OUString & _rImplementationName,const uno::Reference<lang::XMultiServiceFactory> & _rxServiceManager)115*b1cdbd2cSJim Jagielski uno::Reference< uno::XInterface > OModuleRegistration::getComponentFactory(
116*b1cdbd2cSJim Jagielski 	const ::rtl::OUString& _rImplementationName,
117*b1cdbd2cSJim Jagielski 	const uno::Reference< lang::XMultiServiceFactory >& _rxServiceManager)
118*b1cdbd2cSJim Jagielski {
119*b1cdbd2cSJim Jagielski 	OSL_ENSURE(_rxServiceManager.is(), "OModuleRegistration::getComponentFactory : invalid argument (service manager) !");
120*b1cdbd2cSJim Jagielski 	OSL_ENSURE(_rImplementationName.getLength(), "OModuleRegistration::getComponentFactory : invalid argument (implementation name) !");
121*b1cdbd2cSJim Jagielski 
122*b1cdbd2cSJim Jagielski 	if (!s_pImplementationNames)
123*b1cdbd2cSJim Jagielski 	{
124*b1cdbd2cSJim Jagielski 		OSL_ENSURE(sal_False, "OModuleRegistration::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
125*b1cdbd2cSJim Jagielski 		return NULL;
126*b1cdbd2cSJim Jagielski 	}
127*b1cdbd2cSJim Jagielski 	OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
128*b1cdbd2cSJim Jagielski 		"OModuleRegistration::getComponentFactory : inconsistent state (the pointers) !");
129*b1cdbd2cSJim Jagielski 	OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
130*b1cdbd2cSJim Jagielski 				&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
131*b1cdbd2cSJim Jagielski 				&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
132*b1cdbd2cSJim Jagielski 		"OModuleRegistration::getComponentFactory : inconsistent state !");
133*b1cdbd2cSJim Jagielski 
134*b1cdbd2cSJim Jagielski 
135*b1cdbd2cSJim Jagielski 	uno::Reference< uno::XInterface > xReturn;
136*b1cdbd2cSJim Jagielski 
137*b1cdbd2cSJim Jagielski 
138*b1cdbd2cSJim Jagielski 	sal_Int32 nLen = s_pImplementationNames->getLength();
139*b1cdbd2cSJim Jagielski 	const ::rtl::OUString* pImplName = s_pImplementationNames->getConstArray();
140*b1cdbd2cSJim Jagielski 	const uno::Sequence< ::rtl::OUString >* pServices = s_pSupportedServices->getConstArray();
141*b1cdbd2cSJim Jagielski 	const sal_Int64* pComponentFunction = s_pCreationFunctionPointers->getConstArray();
142*b1cdbd2cSJim Jagielski 	const sal_Int64* pFactoryFunction = s_pFactoryFunctionPointers->getConstArray();
143*b1cdbd2cSJim Jagielski 
144*b1cdbd2cSJim Jagielski 	for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices, ++pComponentFunction, ++pFactoryFunction)
145*b1cdbd2cSJim Jagielski 	{
146*b1cdbd2cSJim Jagielski 		if (pImplName->equals(_rImplementationName))
147*b1cdbd2cSJim Jagielski 		{
148*b1cdbd2cSJim Jagielski 			const FactoryInstantiation FactoryInstantiationFunction = reinterpret_cast<const FactoryInstantiation>(*pFactoryFunction);
149*b1cdbd2cSJim Jagielski 			const ComponentInstantiation ComponentInstantiationFunction = reinterpret_cast<const ComponentInstantiation>(*pComponentFunction);
150*b1cdbd2cSJim Jagielski 
151*b1cdbd2cSJim Jagielski 			xReturn = FactoryInstantiationFunction( _rxServiceManager, *pImplName, ComponentInstantiationFunction, *pServices, NULL);
152*b1cdbd2cSJim Jagielski 			if (xReturn.is())
153*b1cdbd2cSJim Jagielski 			{
154*b1cdbd2cSJim Jagielski 				xReturn->acquire();
155*b1cdbd2cSJim Jagielski 				return xReturn.get();
156*b1cdbd2cSJim Jagielski 			}
157*b1cdbd2cSJim Jagielski 		}
158*b1cdbd2cSJim Jagielski 	}
159*b1cdbd2cSJim Jagielski 
160*b1cdbd2cSJim Jagielski 	return NULL;
161*b1cdbd2cSJim Jagielski }
162*b1cdbd2cSJim Jagielski 
163*b1cdbd2cSJim Jagielski 
164