update dev300-m58
[ooovba.git] / unotools / source / processfactory / componentfactory.cxx
blob6a972a86d76aacbc7b0694dc023dab9c150f3b7c
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: componentfactory.cxx,v $
10 * $Revision: 1.5 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_unotools.hxx"
34 #include <unotools/componentfactory.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #ifndef _COM_SUN_STAR_REGISTRY_XREGISTRYKEY_HDL_
40 #include <com/sun/star/registry/XRegistryKey.hpp>
41 #endif
43 #include <cppuhelper/factory.hxx>
45 #include <uno/environment.h>
46 #include <uno/mapping.hxx>
48 #include <rtl/ustring.hxx>
49 #include <osl/module.h>
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::lang;
57 using namespace ::com::sun::star::registry;
58 using namespace ::rtl;
61 namespace utl
64 Reference< XInterface > getComponentInstance(
65 const OUString & rLibraryName,
66 const OUString & rImplementationName
69 Reference< XInterface > xI;
70 Reference< XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
71 if ( xMSF.is() )
72 xI = xMSF->createInstance( rImplementationName );
73 if( !xI.is() )
75 Reference< XSingleServiceFactory > xSSF =
76 loadLibComponentFactory( rLibraryName, rImplementationName,
77 Reference< XMultiServiceFactory >(), Reference< XRegistryKey >() );
78 xI = xSSF->createInstance();
80 return xI;
84 Reference< XSingleServiceFactory > loadLibComponentFactory(
85 const OUString & rLibName,
86 const OUString & rImplName,
87 const Reference< XMultiServiceFactory > & xSF,
88 const Reference< XRegistryKey > & xKey
91 Reference< XSingleServiceFactory > xRet;
93 oslModule lib = osl_loadModule( rLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
94 if (lib)
96 void * pSym;
98 // ========================= LATEST VERSION =========================
99 OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
100 if (pSym = osl_getSymbol( lib, aGetEnvName.pData ))
102 uno_Environment * pCurrentEnv = 0;
103 uno_Environment * pEnv = 0;
104 const sal_Char * pEnvTypeName = 0;
105 (*((component_getImplementationEnvironmentFunc)pSym))( &pEnvTypeName, &pEnv );
107 sal_Bool bNeedsMapping =
108 (pEnv || 0 != rtl_str_compare( pEnvTypeName, CPPU_CURRENT_LANGUAGE_BINDING_NAME ));
110 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
112 if (bNeedsMapping)
114 if (! pEnv)
115 uno_getEnvironment( &pEnv, aEnvTypeName.pData, 0 );
116 if (pEnv)
118 OUString aCppEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
119 uno_getEnvironment( &pCurrentEnv, aCppEnvTypeName.pData, 0 );
120 if (pCurrentEnv)
121 bNeedsMapping = (pEnv != pCurrentEnv);
125 OUString aGetFactoryName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETFACTORY) );
126 if (pSym = osl_getSymbol( lib, aGetFactoryName.pData ))
128 OString aImplName( OUStringToOString( rImplName, RTL_TEXTENCODING_ASCII_US ) );
130 if (bNeedsMapping)
132 if (pEnv && pCurrentEnv)
134 Mapping aCurrent2Env( pCurrentEnv, pEnv );
135 Mapping aEnv2Current( pEnv, pCurrentEnv );
137 if (aCurrent2Env.is() && aEnv2Current.is())
139 void * pSMgr = aCurrent2Env.mapInterface(
140 xSF.get(), ::getCppuType( (const Reference< XMultiServiceFactory > *)0 ) );
141 void * pKey = aCurrent2Env.mapInterface(
142 xKey.get(), ::getCppuType( (const Reference< XRegistryKey > *)0 ) );
144 void * pSSF = (*((component_getFactoryFunc)pSym))(
145 aImplName.getStr(), pSMgr, pKey );
147 if (pKey)
148 (*pEnv->pExtEnv->releaseInterface)( pEnv->pExtEnv, pKey );
149 if (pSMgr)
150 (*pEnv->pExtEnv->releaseInterface)( pEnv->pExtEnv, pSMgr );
152 if (pSSF)
154 aEnv2Current.mapInterface(
155 reinterpret_cast< void ** >( &xRet ),
156 pSSF, ::getCppuType( (const Reference< XSingleServiceFactory > *)0 ) );
157 (*pEnv->pExtEnv->releaseInterface)( pEnv->pExtEnv, pSSF );
162 else
164 XSingleServiceFactory * pRet = (XSingleServiceFactory *)
165 (*((component_getFactoryFunc)pSym))(
166 aImplName.getStr(), xSF.get(), xKey.get() );
167 if (pRet)
169 xRet = pRet;
170 pRet->release();
175 if (pEnv)
176 (*pEnv->release)( pEnv );
177 if (pCurrentEnv)
178 (*pCurrentEnv->release)( pCurrentEnv );
182 if (! xRet.is())
183 osl_unloadModule( lib );
186 return xRet;
189 } // namespace utl