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