1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b5088357SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b5088357SAndrew Rist * distributed with this work for additional information
6*b5088357SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b5088357SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*b5088357SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*b5088357SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist * software distributed under the License is distributed on an
15*b5088357SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist * KIND, either express or implied. See the License for the
17*b5088357SAndrew Rist * specific language governing permissions and limitations
18*b5088357SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*b5088357SAndrew Rist *************************************************************/
21*b5088357SAndrew Rist
22*b5088357SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <unotools/componentfactory.hxx>
28cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
29cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31cdf0e10cSrcweir
32cdf0e10cSrcweir #ifndef _COM_SUN_STAR_REGISTRY_XREGISTRYKEY_HDL_
33cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir
36cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
37cdf0e10cSrcweir
38cdf0e10cSrcweir #include <uno/environment.h>
39cdf0e10cSrcweir #include <uno/mapping.hxx>
40cdf0e10cSrcweir
41cdf0e10cSrcweir #include <rtl/ustring.hxx>
42cdf0e10cSrcweir #include <osl/module.h>
43cdf0e10cSrcweir
44cdf0e10cSrcweir
45cdf0e10cSrcweir
46cdf0e10cSrcweir
47cdf0e10cSrcweir using namespace ::com::sun::star;
48cdf0e10cSrcweir using namespace ::com::sun::star::uno;
49cdf0e10cSrcweir using namespace ::com::sun::star::lang;
50cdf0e10cSrcweir using namespace ::com::sun::star::registry;
51cdf0e10cSrcweir using namespace ::rtl;
52cdf0e10cSrcweir
53cdf0e10cSrcweir
54cdf0e10cSrcweir namespace utl
55cdf0e10cSrcweir {
56cdf0e10cSrcweir
getComponentInstance(const OUString & rLibraryName,const OUString & rImplementationName)57cdf0e10cSrcweir Reference< XInterface > getComponentInstance(
58cdf0e10cSrcweir const OUString & rLibraryName,
59cdf0e10cSrcweir const OUString & rImplementationName
60cdf0e10cSrcweir )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir Reference< XInterface > xI;
63cdf0e10cSrcweir Reference< XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
64cdf0e10cSrcweir if ( xMSF.is() )
65cdf0e10cSrcweir xI = xMSF->createInstance( rImplementationName );
66cdf0e10cSrcweir if( !xI.is() )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir Reference< XSingleServiceFactory > xSSF =
69cdf0e10cSrcweir loadLibComponentFactory( rLibraryName, rImplementationName,
70cdf0e10cSrcweir Reference< XMultiServiceFactory >(), Reference< XRegistryKey >() );
71cdf0e10cSrcweir xI = xSSF->createInstance();
72cdf0e10cSrcweir }
73cdf0e10cSrcweir return xI;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir
76cdf0e10cSrcweir
loadLibComponentFactory(const OUString & rLibName,const OUString & rImplName,const Reference<XMultiServiceFactory> & xSF,const Reference<XRegistryKey> & xKey)77cdf0e10cSrcweir Reference< XSingleServiceFactory > loadLibComponentFactory(
78cdf0e10cSrcweir const OUString & rLibName,
79cdf0e10cSrcweir const OUString & rImplName,
80cdf0e10cSrcweir const Reference< XMultiServiceFactory > & xSF,
81cdf0e10cSrcweir const Reference< XRegistryKey > & xKey
82cdf0e10cSrcweir )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir Reference< XSingleServiceFactory > xRet;
85cdf0e10cSrcweir
86cdf0e10cSrcweir oslModule lib = osl_loadModule( rLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
87cdf0e10cSrcweir if (lib)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir void * pSym;
90cdf0e10cSrcweir
91cdf0e10cSrcweir // ========================= LATEST VERSION =========================
92cdf0e10cSrcweir OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
93cdf0e10cSrcweir if (pSym = osl_getSymbol( lib, aGetEnvName.pData ))
94cdf0e10cSrcweir {
95cdf0e10cSrcweir uno_Environment * pCurrentEnv = 0;
96cdf0e10cSrcweir uno_Environment * pEnv = 0;
97cdf0e10cSrcweir const sal_Char * pEnvTypeName = 0;
98cdf0e10cSrcweir (*((component_getImplementationEnvironmentFunc)pSym))( &pEnvTypeName, &pEnv );
99cdf0e10cSrcweir
100cdf0e10cSrcweir sal_Bool bNeedsMapping =
101cdf0e10cSrcweir (pEnv || 0 != rtl_str_compare( pEnvTypeName, CPPU_CURRENT_LANGUAGE_BINDING_NAME ));
102cdf0e10cSrcweir
103cdf0e10cSrcweir OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
104cdf0e10cSrcweir
105cdf0e10cSrcweir if (bNeedsMapping)
106cdf0e10cSrcweir {
107cdf0e10cSrcweir if (! pEnv)
108cdf0e10cSrcweir uno_getEnvironment( &pEnv, aEnvTypeName.pData, 0 );
109cdf0e10cSrcweir if (pEnv)
110cdf0e10cSrcweir {
111cdf0e10cSrcweir OUString aCppEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
112cdf0e10cSrcweir uno_getEnvironment( &pCurrentEnv, aCppEnvTypeName.pData, 0 );
113cdf0e10cSrcweir if (pCurrentEnv)
114cdf0e10cSrcweir bNeedsMapping = (pEnv != pCurrentEnv);
115cdf0e10cSrcweir }
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
118cdf0e10cSrcweir OUString aGetFactoryName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETFACTORY) );
119cdf0e10cSrcweir if (pSym = osl_getSymbol( lib, aGetFactoryName.pData ))
120cdf0e10cSrcweir {
121cdf0e10cSrcweir OString aImplName( OUStringToOString( rImplName, RTL_TEXTENCODING_ASCII_US ) );
122cdf0e10cSrcweir
123cdf0e10cSrcweir if (bNeedsMapping)
124cdf0e10cSrcweir {
125cdf0e10cSrcweir if (pEnv && pCurrentEnv)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir Mapping aCurrent2Env( pCurrentEnv, pEnv );
128cdf0e10cSrcweir Mapping aEnv2Current( pEnv, pCurrentEnv );
129cdf0e10cSrcweir
130cdf0e10cSrcweir if (aCurrent2Env.is() && aEnv2Current.is())
131cdf0e10cSrcweir {
132cdf0e10cSrcweir void * pSMgr = aCurrent2Env.mapInterface(
133cdf0e10cSrcweir xSF.get(), ::getCppuType( (const Reference< XMultiServiceFactory > *)0 ) );
134cdf0e10cSrcweir void * pKey = aCurrent2Env.mapInterface(
135cdf0e10cSrcweir xKey.get(), ::getCppuType( (const Reference< XRegistryKey > *)0 ) );
136cdf0e10cSrcweir
137cdf0e10cSrcweir void * pSSF = (*((component_getFactoryFunc)pSym))(
138cdf0e10cSrcweir aImplName.getStr(), pSMgr, pKey );
139cdf0e10cSrcweir
140cdf0e10cSrcweir if (pKey)
141cdf0e10cSrcweir (*pEnv->pExtEnv->releaseInterface)( pEnv->pExtEnv, pKey );
142cdf0e10cSrcweir if (pSMgr)
143cdf0e10cSrcweir (*pEnv->pExtEnv->releaseInterface)( pEnv->pExtEnv, pSMgr );
144cdf0e10cSrcweir
145cdf0e10cSrcweir if (pSSF)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir aEnv2Current.mapInterface(
148cdf0e10cSrcweir reinterpret_cast< void ** >( &xRet ),
149cdf0e10cSrcweir pSSF, ::getCppuType( (const Reference< XSingleServiceFactory > *)0 ) );
150cdf0e10cSrcweir (*pEnv->pExtEnv->releaseInterface)( pEnv->pExtEnv, pSSF );
151cdf0e10cSrcweir }
152cdf0e10cSrcweir }
153cdf0e10cSrcweir }
154cdf0e10cSrcweir }
155cdf0e10cSrcweir else
156cdf0e10cSrcweir {
157cdf0e10cSrcweir XSingleServiceFactory * pRet = (XSingleServiceFactory *)
158cdf0e10cSrcweir (*((component_getFactoryFunc)pSym))(
159cdf0e10cSrcweir aImplName.getStr(), xSF.get(), xKey.get() );
160cdf0e10cSrcweir if (pRet)
161cdf0e10cSrcweir {
162cdf0e10cSrcweir xRet = pRet;
163cdf0e10cSrcweir pRet->release();
164cdf0e10cSrcweir }
165cdf0e10cSrcweir }
166cdf0e10cSrcweir }
167cdf0e10cSrcweir
168cdf0e10cSrcweir if (pEnv)
169cdf0e10cSrcweir (*pEnv->release)( pEnv );
170cdf0e10cSrcweir if (pCurrentEnv)
171cdf0e10cSrcweir (*pCurrentEnv->release)( pCurrentEnv );
172cdf0e10cSrcweir }
173cdf0e10cSrcweir
174cdf0e10cSrcweir
175cdf0e10cSrcweir if (! xRet.is())
176cdf0e10cSrcweir osl_unloadModule( lib );
177cdf0e10cSrcweir }
178cdf0e10cSrcweir
179cdf0e10cSrcweir return xRet;
180cdf0e10cSrcweir }
181cdf0e10cSrcweir
182cdf0e10cSrcweir } // namespace utl
183