1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_stoc.hxx"
30 
31 #include <stdlib.h>
32 #include <osl/file.h>
33 #include <vector>
34 #include <osl/mutex.hxx>
35 #include <osl/diagnose.h>
36 #include <osl/module.h>
37 #include <rtl/ustring.hxx>
38 #include <uno/environment.h>
39 #include <uno/mapping.hxx>
40 #include <cppuhelper/queryinterface.hxx>
41 #include <cppuhelper/weak.hxx>
42 #include <cppuhelper/factory.hxx>
43 #include <cppuhelper/shlib.hxx>
44 #include <cppuhelper/implbase3.hxx>
45 #ifndef _CPPUHELPER_IMPLEMENTATIONENTRY_HXX__
46 #include <cppuhelper/implementationentry.hxx>
47 #endif
48 #include <cppuhelper/bootstrap.hxx>
49 
50 #include <com/sun/star/loader/XImplementationLoader.hpp>
51 #include <com/sun/star/lang/IllegalArgumentException.hpp>
52 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 #include <com/sun/star/lang/XServiceInfo.hpp>
54 #include <com/sun/star/lang/XInitialization.hpp>
55 #include <com/sun/star/registry/XRegistryKey.hpp>
56 
57 #define SERVICENAME "com.sun.star.loader.SharedLibrary"
58 #define IMPLNAME	"com.sun.star.comp.stoc.DLLComponentLoader"
59 
60 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
61 
62 
63 using namespace com::sun::star;
64 using namespace com::sun::star::uno;
65 using namespace com::sun::star::loader;
66 using namespace com::sun::star::lang;
67 using namespace com::sun::star::registry;
68 using namespace cppu;
69 using namespace rtl;
70 using namespace osl;
71 
72 extern rtl_StandardModuleCount g_moduleCount;
73 
74 namespace stoc_bootstrap
75 {
76 Sequence< OUString > loader_getSupportedServiceNames()
77 {
78 	static Sequence < OUString > *pNames = 0;
79 	if( ! pNames )
80 	{
81 		MutexGuard guard( Mutex::getGlobalMutex() );
82 		if( !pNames )
83 		{
84 			static Sequence< OUString > seqNames(1);
85 			seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICENAME));
86 			pNames = &seqNames;
87 		}
88 	}
89 	return *pNames;
90 }
91 
92 OUString loader_getImplementationName()
93 {
94 	static OUString *pImplName = 0;
95 	if( ! pImplName )
96 	{
97 		MutexGuard guard( Mutex::getGlobalMutex() );
98 		if( ! pImplName )
99 		{
100 			static OUString implName( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) );
101 			pImplName = &implName;
102 		}
103 	}
104 	return *pImplName;
105 }
106 }
107 
108 namespace stoc_loader
109 {
110 //*************************************************************************
111 // DllComponentLoader
112 //*************************************************************************
113 class DllComponentLoader
114 	: public WeakImplHelper3< XImplementationLoader,
115 							  XInitialization,
116 							  XServiceInfo >
117 {
118 public:
119 	DllComponentLoader( const Reference<XComponentContext> & xCtx );
120 	~DllComponentLoader();
121 
122     // XServiceInfo
123     virtual OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
124     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
125     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
126 
127 	// XInitialization
128     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 
130 	// XImplementationLoader
131     virtual Reference<XInterface> SAL_CALL activate( const OUString& implementationName, const OUString& implementationLoaderUrl, const OUString& locationUrl, const Reference<XRegistryKey>& xKey ) throw(CannotActivateFactoryException, RuntimeException);
132     virtual sal_Bool SAL_CALL writeRegistryInfo( const Reference<XRegistryKey>& xKey, const OUString& implementationLoaderUrl, const OUString& locationUrl ) throw(CannotRegisterImplementationException, RuntimeException);
133 
134 private:
135     OUString expand_url( OUString const & url )
136         SAL_THROW( (RuntimeException) );
137 
138 	Reference<XMultiServiceFactory> m_xSMgr;
139 };
140 
141 //*************************************************************************
142 DllComponentLoader::DllComponentLoader( const Reference<XComponentContext> & xCtx )
143 {
144 	g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
145 	m_xSMgr.set( xCtx->getServiceManager(), UNO_QUERY );
146 }
147 
148 //*************************************************************************
149 DllComponentLoader::~DllComponentLoader()
150 {
151 	g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
152 }
153 
154 //*************************************************************************
155 OUString SAL_CALL DllComponentLoader::getImplementationName(  )
156 	throw(::com::sun::star::uno::RuntimeException)
157 {
158 	return stoc_bootstrap::loader_getImplementationName();
159 }
160 
161 //*************************************************************************
162 sal_Bool SAL_CALL DllComponentLoader::supportsService( const OUString& ServiceName )
163 	throw(::com::sun::star::uno::RuntimeException)
164 {
165 	Sequence< OUString > aSNL = getSupportedServiceNames();
166 	const OUString * pArray = aSNL.getArray();
167 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
168 		if( pArray[i] == ServiceName )
169 			return sal_True;
170 	return sal_False;
171 }
172 
173 //*************************************************************************
174 Sequence<OUString> SAL_CALL DllComponentLoader::getSupportedServiceNames(  )
175 	throw(::com::sun::star::uno::RuntimeException)
176 {
177 	return stoc_bootstrap::loader_getSupportedServiceNames();
178 }
179 
180 //*************************************************************************
181 void DllComponentLoader::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& )
182 	throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
183 {
184 	OSL_ENSURE( 0, "dllcomponentloader::initialize should not be called !" );
185 //  	if( aArgs.getLength() != 1 )
186 //  	{
187 //  		throw IllegalArgumentException();
188 //  	}
189 
190 //  	Reference< XMultiServiceFactory > rServiceManager;
191 
192 //  	if( aArgs.getConstArray()[0].getValueType().getTypeClass() == TypeClass_INTERFACE )
193 //  	{
194 //  		aArgs.getConstArray()[0] >>= rServiceManager;
195 //  	}
196 
197 //  	if( !rServiceManager.is() )
198 //  	{
199 //  		throw IllegalArgumentException();
200 //  	}
201 
202 //  	m_xSMgr = rServiceManager;
203 }
204 
205 //==================================================================================================
206 OUString DllComponentLoader::expand_url( OUString const & url )
207     SAL_THROW( (RuntimeException) )
208 {
209     try
210     {
211         return cppu::bootstrap_expandUri( url );
212     }
213     catch ( IllegalArgumentException & e )
214     {
215         throw RuntimeException( e.Message, e.Context );
216     }
217 }
218 
219 //*************************************************************************
220 Reference<XInterface> SAL_CALL DllComponentLoader::activate(
221 	const OUString & rImplName, const OUString &, const OUString & rLibName,
222 	const Reference< XRegistryKey > & xKey )
223 
224 	throw(CannotActivateFactoryException, RuntimeException)
225 {
226 	return loadSharedLibComponentFactory(
227         expand_url( rLibName ), OUString(), rImplName, m_xSMgr, xKey );
228 }
229 
230 
231 //*************************************************************************
232 sal_Bool SAL_CALL DllComponentLoader::writeRegistryInfo(
233 	const Reference< XRegistryKey > & xKey, const OUString &, const OUString & rLibName )
234 
235 	throw(CannotRegisterImplementationException, RuntimeException)
236 {
237 	writeSharedLibComponentInfo(
238         expand_url( rLibName ), OUString(), m_xSMgr, xKey );
239 	return sal_True;
240 }
241 }
242 
243 namespace stoc_bootstrap
244 {
245 //*************************************************************************
246 Reference<XInterface> SAL_CALL DllComponentLoader_CreateInstance( const Reference<XComponentContext> & xCtx ) throw(Exception)
247 {
248 	Reference<XInterface> xRet;
249 
250 	XImplementationLoader *pXLoader = (XImplementationLoader *)new stoc_loader::DllComponentLoader(xCtx);
251 
252 	if (pXLoader)
253 	{
254 		xRet = Reference<XInterface>::query(pXLoader);
255 	}
256 
257 	return xRet;
258 }
259 
260 }
261 
262