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