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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_stoc.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdlib.h>
32*cdf0e10cSrcweir #include <osl/file.h>
33*cdf0e10cSrcweir #include <vector>
34*cdf0e10cSrcweir #include <osl/mutex.hxx>
35*cdf0e10cSrcweir #include <osl/diagnose.h>
36*cdf0e10cSrcweir #include <osl/module.h>
37*cdf0e10cSrcweir #include <rtl/ustring.hxx>
38*cdf0e10cSrcweir #include <uno/environment.h>
39*cdf0e10cSrcweir #include <uno/mapping.hxx>
40*cdf0e10cSrcweir #include <cppuhelper/queryinterface.hxx>
41*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
42*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
43*cdf0e10cSrcweir #include <cppuhelper/shlib.hxx>
44*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
45*cdf0e10cSrcweir #ifndef _CPPUHELPER_IMPLEMENTATIONENTRY_HXX__
46*cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
47*cdf0e10cSrcweir #endif
48*cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <com/sun/star/loader/XImplementationLoader.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #define SERVICENAME "com.sun.star.loader.SharedLibrary"
58*cdf0e10cSrcweir #define IMPLNAME	"com.sun.star.comp.stoc.DLLComponentLoader"
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir using namespace com::sun::star;
64*cdf0e10cSrcweir using namespace com::sun::star::uno;
65*cdf0e10cSrcweir using namespace com::sun::star::loader;
66*cdf0e10cSrcweir using namespace com::sun::star::lang;
67*cdf0e10cSrcweir using namespace com::sun::star::registry;
68*cdf0e10cSrcweir using namespace cppu;
69*cdf0e10cSrcweir using namespace rtl;
70*cdf0e10cSrcweir using namespace osl;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir extern rtl_StandardModuleCount g_moduleCount;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir namespace stoc_bootstrap
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir Sequence< OUString > loader_getSupportedServiceNames()
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir 	static Sequence < OUString > *pNames = 0;
79*cdf0e10cSrcweir 	if( ! pNames )
80*cdf0e10cSrcweir 	{
81*cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
82*cdf0e10cSrcweir 		if( !pNames )
83*cdf0e10cSrcweir 		{
84*cdf0e10cSrcweir 			static Sequence< OUString > seqNames(1);
85*cdf0e10cSrcweir 			seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICENAME));
86*cdf0e10cSrcweir 			pNames = &seqNames;
87*cdf0e10cSrcweir 		}
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 	return *pNames;
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir OUString loader_getImplementationName()
93*cdf0e10cSrcweir {
94*cdf0e10cSrcweir 	static OUString *pImplName = 0;
95*cdf0e10cSrcweir 	if( ! pImplName )
96*cdf0e10cSrcweir 	{
97*cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
98*cdf0e10cSrcweir 		if( ! pImplName )
99*cdf0e10cSrcweir 		{
100*cdf0e10cSrcweir 			static OUString implName( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) );
101*cdf0e10cSrcweir 			pImplName = &implName;
102*cdf0e10cSrcweir 		}
103*cdf0e10cSrcweir 	}
104*cdf0e10cSrcweir 	return *pImplName;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir namespace stoc_loader
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir //*************************************************************************
111*cdf0e10cSrcweir // DllComponentLoader
112*cdf0e10cSrcweir //*************************************************************************
113*cdf0e10cSrcweir class DllComponentLoader
114*cdf0e10cSrcweir 	: public WeakImplHelper3< XImplementationLoader,
115*cdf0e10cSrcweir 							  XInitialization,
116*cdf0e10cSrcweir 							  XServiceInfo >
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir public:
119*cdf0e10cSrcweir 	DllComponentLoader( const Reference<XComponentContext> & xCtx );
120*cdf0e10cSrcweir 	~DllComponentLoader();
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     // XServiceInfo
123*cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
124*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
125*cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	// XInitialization
128*cdf0e10cSrcweir     virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	// XImplementationLoader
131*cdf0e10cSrcweir     virtual Reference<XInterface> SAL_CALL activate( const OUString& implementationName, const OUString& implementationLoaderUrl, const OUString& locationUrl, const Reference<XRegistryKey>& xKey ) throw(CannotActivateFactoryException, RuntimeException);
132*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL writeRegistryInfo( const Reference<XRegistryKey>& xKey, const OUString& implementationLoaderUrl, const OUString& locationUrl ) throw(CannotRegisterImplementationException, RuntimeException);
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir private:
135*cdf0e10cSrcweir     OUString expand_url( OUString const & url )
136*cdf0e10cSrcweir         SAL_THROW( (RuntimeException) );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	Reference<XMultiServiceFactory> m_xSMgr;
139*cdf0e10cSrcweir };
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir //*************************************************************************
142*cdf0e10cSrcweir DllComponentLoader::DllComponentLoader( const Reference<XComponentContext> & xCtx )
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir 	g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
145*cdf0e10cSrcweir 	m_xSMgr.set( xCtx->getServiceManager(), UNO_QUERY );
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir //*************************************************************************
149*cdf0e10cSrcweir DllComponentLoader::~DllComponentLoader()
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir //*************************************************************************
155*cdf0e10cSrcweir OUString SAL_CALL DllComponentLoader::getImplementationName(  )
156*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	return stoc_bootstrap::loader_getImplementationName();
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir //*************************************************************************
162*cdf0e10cSrcweir sal_Bool SAL_CALL DllComponentLoader::supportsService( const OUString& ServiceName )
163*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir 	Sequence< OUString > aSNL = getSupportedServiceNames();
166*cdf0e10cSrcweir 	const OUString * pArray = aSNL.getArray();
167*cdf0e10cSrcweir 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
168*cdf0e10cSrcweir 		if( pArray[i] == ServiceName )
169*cdf0e10cSrcweir 			return sal_True;
170*cdf0e10cSrcweir 	return sal_False;
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir //*************************************************************************
174*cdf0e10cSrcweir Sequence<OUString> SAL_CALL DllComponentLoader::getSupportedServiceNames(  )
175*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
176*cdf0e10cSrcweir {
177*cdf0e10cSrcweir 	return stoc_bootstrap::loader_getSupportedServiceNames();
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir //*************************************************************************
181*cdf0e10cSrcweir void DllComponentLoader::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& )
182*cdf0e10cSrcweir 	throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	OSL_ENSURE( 0, "dllcomponentloader::initialize should not be called !" );
185*cdf0e10cSrcweir //  	if( aArgs.getLength() != 1 )
186*cdf0e10cSrcweir //  	{
187*cdf0e10cSrcweir //  		throw IllegalArgumentException();
188*cdf0e10cSrcweir //  	}
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir //  	Reference< XMultiServiceFactory > rServiceManager;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir //  	if( aArgs.getConstArray()[0].getValueType().getTypeClass() == TypeClass_INTERFACE )
193*cdf0e10cSrcweir //  	{
194*cdf0e10cSrcweir //  		aArgs.getConstArray()[0] >>= rServiceManager;
195*cdf0e10cSrcweir //  	}
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir //  	if( !rServiceManager.is() )
198*cdf0e10cSrcweir //  	{
199*cdf0e10cSrcweir //  		throw IllegalArgumentException();
200*cdf0e10cSrcweir //  	}
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir //  	m_xSMgr = rServiceManager;
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir //==================================================================================================
206*cdf0e10cSrcweir OUString DllComponentLoader::expand_url( OUString const & url )
207*cdf0e10cSrcweir     SAL_THROW( (RuntimeException) )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir     try
210*cdf0e10cSrcweir     {
211*cdf0e10cSrcweir         return cppu::bootstrap_expandUri( url );
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir     catch ( IllegalArgumentException & e )
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         throw RuntimeException( e.Message, e.Context );
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir //*************************************************************************
220*cdf0e10cSrcweir Reference<XInterface> SAL_CALL DllComponentLoader::activate(
221*cdf0e10cSrcweir 	const OUString & rImplName, const OUString &, const OUString & rLibName,
222*cdf0e10cSrcweir 	const Reference< XRegistryKey > & xKey )
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 	throw(CannotActivateFactoryException, RuntimeException)
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir 	return loadSharedLibComponentFactory(
227*cdf0e10cSrcweir         expand_url( rLibName ), OUString(), rImplName, m_xSMgr, xKey );
228*cdf0e10cSrcweir }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir //*************************************************************************
232*cdf0e10cSrcweir sal_Bool SAL_CALL DllComponentLoader::writeRegistryInfo(
233*cdf0e10cSrcweir 	const Reference< XRegistryKey > & xKey, const OUString &, const OUString & rLibName )
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 	throw(CannotRegisterImplementationException, RuntimeException)
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir 	writeSharedLibComponentInfo(
238*cdf0e10cSrcweir         expand_url( rLibName ), OUString(), m_xSMgr, xKey );
239*cdf0e10cSrcweir 	return sal_True;
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir namespace stoc_bootstrap
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir //*************************************************************************
246*cdf0e10cSrcweir Reference<XInterface> SAL_CALL DllComponentLoader_CreateInstance( const Reference<XComponentContext> & xCtx ) throw(Exception)
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir 	Reference<XInterface> xRet;
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 	XImplementationLoader *pXLoader = (XImplementationLoader *)new stoc_loader::DllComponentLoader(xCtx);
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 	if (pXLoader)
253*cdf0e10cSrcweir 	{
254*cdf0e10cSrcweir 		xRet = Reference<XInterface>::query(pXLoader);
255*cdf0e10cSrcweir 	}
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir 	return xRet;
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir }
261*cdf0e10cSrcweir 
262