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 // no include "precompiled_dbaccess.hxx" because this file is meant to
29*cdf0e10cSrcweir // be included in other cxx files
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef _REGISTRATIONHELPER_CXX_INCLUDED_INDIRECTLY_
32*cdf0e10cSrcweir #error "don't build this file directly! use dbu_reghelper.cxx instead!"
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir using namespace ::com::sun::star;
36*cdf0e10cSrcweir using namespace ::comphelper;
37*cdf0e10cSrcweir using namespace ::cppu;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString >*				    OModuleRegistration::s_pImplementationNames = NULL;
40*cdf0e10cSrcweir uno::Sequence< uno::Sequence< ::rtl::OUString > >*	OModuleRegistration::s_pSupportedServices = NULL;
41*cdf0e10cSrcweir uno::Sequence< sal_Int64 >*		            OModuleRegistration::s_pCreationFunctionPointers = NULL;
42*cdf0e10cSrcweir uno::Sequence< sal_Int64 >*				    OModuleRegistration::s_pFactoryFunctionPointers = NULL;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //--------------------------------------------------------------------------
45*cdf0e10cSrcweir void OModuleRegistration::registerComponent(
46*cdf0e10cSrcweir 	const ::rtl::OUString& _rImplementationName,
47*cdf0e10cSrcweir 	const uno::Sequence< ::rtl::OUString >& _rServiceNames,
48*cdf0e10cSrcweir 	ComponentInstantiation _pCreateFunction,
49*cdf0e10cSrcweir 	FactoryInstantiation _pFactoryFunction)
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 	if (!s_pImplementationNames)
52*cdf0e10cSrcweir 	{
53*cdf0e10cSrcweir 		OSL_ENSURE(!s_pSupportedServices && !s_pCreationFunctionPointers && !s_pFactoryFunctionPointers,
54*cdf0e10cSrcweir 			"OModuleRegistration::registerComponent : inconsistent state (the pointers (1)) !");
55*cdf0e10cSrcweir 		s_pImplementationNames = new uno::Sequence< ::rtl::OUString >;
56*cdf0e10cSrcweir 		s_pSupportedServices = new uno::Sequence< uno::Sequence< ::rtl::OUString > >;
57*cdf0e10cSrcweir 		s_pCreationFunctionPointers = new uno::Sequence< sal_Int64 >;
58*cdf0e10cSrcweir 		s_pFactoryFunctionPointers = new uno::Sequence< sal_Int64 >;
59*cdf0e10cSrcweir 	}
60*cdf0e10cSrcweir 	OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
61*cdf0e10cSrcweir 		"OModuleRegistration::registerComponent : inconsistent state (the pointers (2)) !");
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
64*cdf0e10cSrcweir 				&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
65*cdf0e10cSrcweir 				&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
66*cdf0e10cSrcweir 		"OModuleRegistration::registerComponent : inconsistent state !");
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	sal_Int32 nOldLen = s_pImplementationNames->getLength();
69*cdf0e10cSrcweir 	s_pImplementationNames->realloc(nOldLen + 1);
70*cdf0e10cSrcweir 	s_pSupportedServices->realloc(nOldLen + 1);
71*cdf0e10cSrcweir 	s_pCreationFunctionPointers->realloc(nOldLen + 1);
72*cdf0e10cSrcweir 	s_pFactoryFunctionPointers->realloc(nOldLen + 1);
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	s_pImplementationNames->getArray()[nOldLen] = _rImplementationName;
75*cdf0e10cSrcweir 	s_pSupportedServices->getArray()[nOldLen] = _rServiceNames;
76*cdf0e10cSrcweir 	s_pCreationFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pCreateFunction);
77*cdf0e10cSrcweir 	s_pFactoryFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pFactoryFunction);
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir //--------------------------------------------------------------------------
81*cdf0e10cSrcweir void OModuleRegistration::revokeComponent(const ::rtl::OUString& _rImplementationName)
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir 	if (!s_pImplementationNames)
84*cdf0e10cSrcweir 	{
85*cdf0e10cSrcweir 		OSL_ENSURE(sal_False, "OModuleRegistration::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
86*cdf0e10cSrcweir 		return;
87*cdf0e10cSrcweir 	}
88*cdf0e10cSrcweir 	OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
89*cdf0e10cSrcweir 		"OModuleRegistration::revokeComponent : inconsistent state (the pointers) !");
90*cdf0e10cSrcweir 	OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
91*cdf0e10cSrcweir 				&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
92*cdf0e10cSrcweir 				&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
93*cdf0e10cSrcweir 		"OModuleRegistration::revokeComponent : inconsistent state !");
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 	sal_Int32 nLen = s_pImplementationNames->getLength();
96*cdf0e10cSrcweir 	const ::rtl::OUString* pImplNames = s_pImplementationNames->getConstArray();
97*cdf0e10cSrcweir 	for (sal_Int32 i=0; i<nLen; ++i, ++pImplNames)
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		if (pImplNames->equals(_rImplementationName))
100*cdf0e10cSrcweir 		{
101*cdf0e10cSrcweir 			removeElementAt(*s_pImplementationNames, i);
102*cdf0e10cSrcweir 			removeElementAt(*s_pSupportedServices, i);
103*cdf0e10cSrcweir 			removeElementAt(*s_pCreationFunctionPointers, i);
104*cdf0e10cSrcweir 			removeElementAt(*s_pFactoryFunctionPointers, i);
105*cdf0e10cSrcweir 			break;
106*cdf0e10cSrcweir 		}
107*cdf0e10cSrcweir 	}
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	if (s_pImplementationNames->getLength() == 0)
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 		delete s_pImplementationNames; s_pImplementationNames = NULL;
112*cdf0e10cSrcweir 		delete s_pSupportedServices; s_pSupportedServices = NULL;
113*cdf0e10cSrcweir 		delete s_pCreationFunctionPointers; s_pCreationFunctionPointers = NULL;
114*cdf0e10cSrcweir 		delete s_pFactoryFunctionPointers; s_pFactoryFunctionPointers = NULL;
115*cdf0e10cSrcweir 	}
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir //--------------------------------------------------------------------------
119*cdf0e10cSrcweir uno::Reference< uno::XInterface > OModuleRegistration::getComponentFactory(
120*cdf0e10cSrcweir 	const ::rtl::OUString& _rImplementationName,
121*cdf0e10cSrcweir 	const uno::Reference< lang::XMultiServiceFactory >& _rxServiceManager)
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir 	OSL_ENSURE(_rxServiceManager.is(), "OModuleRegistration::getComponentFactory : invalid argument (service manager) !");
124*cdf0e10cSrcweir 	OSL_ENSURE(_rImplementationName.getLength(), "OModuleRegistration::getComponentFactory : invalid argument (implementation name) !");
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	if (!s_pImplementationNames)
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		OSL_ENSURE(sal_False, "OModuleRegistration::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
129*cdf0e10cSrcweir 		return NULL;
130*cdf0e10cSrcweir 	}
131*cdf0e10cSrcweir 	OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
132*cdf0e10cSrcweir 		"OModuleRegistration::getComponentFactory : inconsistent state (the pointers) !");
133*cdf0e10cSrcweir 	OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
134*cdf0e10cSrcweir 				&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
135*cdf0e10cSrcweir 				&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
136*cdf0e10cSrcweir 		"OModuleRegistration::getComponentFactory : inconsistent state !");
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	uno::Reference< uno::XInterface > xReturn;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	sal_Int32 nLen = s_pImplementationNames->getLength();
143*cdf0e10cSrcweir 	const ::rtl::OUString* pImplName = s_pImplementationNames->getConstArray();
144*cdf0e10cSrcweir 	const uno::Sequence< ::rtl::OUString >* pServices = s_pSupportedServices->getConstArray();
145*cdf0e10cSrcweir 	const sal_Int64* pComponentFunction = s_pCreationFunctionPointers->getConstArray();
146*cdf0e10cSrcweir 	const sal_Int64* pFactoryFunction = s_pFactoryFunctionPointers->getConstArray();
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 	for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices, ++pComponentFunction, ++pFactoryFunction)
149*cdf0e10cSrcweir 	{
150*cdf0e10cSrcweir 		if (pImplName->equals(_rImplementationName))
151*cdf0e10cSrcweir 		{
152*cdf0e10cSrcweir 			const FactoryInstantiation FactoryInstantiationFunction = reinterpret_cast<const FactoryInstantiation>(*pFactoryFunction);
153*cdf0e10cSrcweir 			const ComponentInstantiation ComponentInstantiationFunction = reinterpret_cast<const ComponentInstantiation>(*pComponentFunction);
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 			xReturn = FactoryInstantiationFunction( _rxServiceManager, *pImplName, ComponentInstantiationFunction, *pServices, NULL);
156*cdf0e10cSrcweir 			if (xReturn.is())
157*cdf0e10cSrcweir 			{
158*cdf0e10cSrcweir 				xReturn->acquire();
159*cdf0e10cSrcweir 				return xReturn.get();
160*cdf0e10cSrcweir 			}
161*cdf0e10cSrcweir 		}
162*cdf0e10cSrcweir 	}
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	return NULL;
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 
168