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 { 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 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( ); 120 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ); 121 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ); 122 123 // XInitialization 124 virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ); 125 126 // XImplementationLoader 127 virtual Reference<XInterface> SAL_CALL activate( const OUString& implementationName, const OUString& implementationLoaderUrl, const OUString& locationUrl, const Reference<XRegistryKey>& xKey ); 128 virtual sal_Bool SAL_CALL writeRegistryInfo( const Reference<XRegistryKey>& xKey, const OUString& implementationLoaderUrl, const OUString& locationUrl ); 129 130 private: 131 OUString expand_url( OUString const & url ); 132 133 Reference<XMultiServiceFactory> m_xSMgr; 134 }; 135 136 //************************************************************************* 137 DllComponentLoader::DllComponentLoader( const Reference<XComponentContext> & xCtx ) 138 { 139 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt ); 140 m_xSMgr.set( xCtx->getServiceManager(), UNO_QUERY ); 141 } 142 143 //************************************************************************* 144 DllComponentLoader::~DllComponentLoader() 145 { 146 g_moduleCount.modCnt.release( &g_moduleCount.modCnt ); 147 } 148 149 //************************************************************************* 150 OUString SAL_CALL DllComponentLoader::getImplementationName( ) 151 { 152 return stoc_bootstrap::loader_getImplementationName(); 153 } 154 155 //************************************************************************* 156 sal_Bool SAL_CALL DllComponentLoader::supportsService( const OUString& ServiceName ) 157 { 158 Sequence< OUString > aSNL = getSupportedServiceNames(); 159 const OUString * pArray = aSNL.getArray(); 160 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ ) 161 if( pArray[i] == ServiceName ) 162 return sal_True; 163 return sal_False; 164 } 165 166 //************************************************************************* 167 Sequence<OUString> SAL_CALL DllComponentLoader::getSupportedServiceNames( ) 168 { 169 return stoc_bootstrap::loader_getSupportedServiceNames(); 170 } 171 172 //************************************************************************* 173 void DllComponentLoader::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& ) 174 { 175 OSL_ENSURE( 0, "dllcomponentloader::initialize should not be called !" ); 176 // if( aArgs.getLength() != 1 ) 177 // { 178 // throw IllegalArgumentException(); 179 // } 180 181 // Reference< XMultiServiceFactory > rServiceManager; 182 183 // if( aArgs.getConstArray()[0].getValueType().getTypeClass() == TypeClass_INTERFACE ) 184 // { 185 // aArgs.getConstArray()[0] >>= rServiceManager; 186 // } 187 188 // if( !rServiceManager.is() ) 189 // { 190 // throw IllegalArgumentException(); 191 // } 192 193 // m_xSMgr = rServiceManager; 194 } 195 196 //================================================================================================== 197 OUString DllComponentLoader::expand_url( OUString const & url ) 198 { 199 try 200 { 201 return cppu::bootstrap_expandUri( url ); 202 } 203 catch ( IllegalArgumentException & e ) 204 { 205 throw RuntimeException( e.Message, e.Context ); 206 } 207 } 208 209 //************************************************************************* 210 Reference<XInterface> SAL_CALL DllComponentLoader::activate( 211 const OUString & rImplName, const OUString &, const OUString & rLibName, 212 const Reference< XRegistryKey > & xKey ) 213 { 214 return loadSharedLibComponentFactory( 215 expand_url( rLibName ), OUString(), rImplName, m_xSMgr, xKey ); 216 } 217 218 219 //************************************************************************* 220 sal_Bool SAL_CALL DllComponentLoader::writeRegistryInfo( 221 const Reference< XRegistryKey > & xKey, const OUString &, const OUString & rLibName ) 222 { 223 writeSharedLibComponentInfo( 224 expand_url( rLibName ), OUString(), m_xSMgr, xKey ); 225 return sal_True; 226 } 227 } 228 229 namespace stoc_bootstrap 230 { 231 //************************************************************************* 232 Reference<XInterface> SAL_CALL DllComponentLoader_CreateInstance( const Reference<XComponentContext> & xCtx ) 233 { 234 Reference<XInterface> xRet; 235 236 XImplementationLoader *pXLoader = (XImplementationLoader *)new stoc_loader::DllComponentLoader(xCtx); 237 238 if (pXLoader) 239 { 240 xRet = Reference<XInterface>::query(pXLoader); 241 } 242 243 return xRet; 244 } 245 246 } 247