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_javaunohelper.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <osl/diagnose.h> 32*cdf0e10cSrcweir #include <osl/module.h> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #include <uno/environment.hxx> 35*cdf0e10cSrcweir #include <uno/mapping.hxx> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #include <cppuhelper/factory.hxx> 38*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx> 39*cdf0e10cSrcweir #include <cppuhelper/component_context.hxx> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 42*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 43*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 44*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp> 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir #include "jni.h" 47*cdf0e10cSrcweir #include "jvmaccess/virtualmachine.hxx" 48*cdf0e10cSrcweir #include "jvmaccess/unovirtualmachine.hxx" 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir #include "vm.hxx" 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) ) 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir using namespace ::com::sun::star; 56*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 57*cdf0e10cSrcweir using ::rtl::OString; 58*cdf0e10cSrcweir using ::rtl::OUString; 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir /* 61*cdf0e10cSrcweir * Class: com_sun_star_comp_helper_SharedLibraryLoader 62*cdf0e10cSrcweir * Method: component_writeInfo 63*cdf0e10cSrcweir * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z 64*cdf0e10cSrcweir */ 65*cdf0e10cSrcweir extern "C" JNIEXPORT jboolean JNICALL 66*cdf0e10cSrcweir Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo( 67*cdf0e10cSrcweir JNIEnv * pJEnv, jclass, jstring jLibName, jobject jSMgr, 68*cdf0e10cSrcweir jobject jRegKey, jobject loader ) 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir sal_Bool bRet = sal_False; 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir const jchar* pJLibName = pJEnv->GetStringChars( jLibName, NULL ); 73*cdf0e10cSrcweir OUString aLibName( pJLibName ); 74*cdf0e10cSrcweir pJEnv->ReleaseStringChars( jLibName, pJLibName); 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL ); 77*cdf0e10cSrcweir if (lib) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir // ========================= LATEST VERSION ========================= 80*cdf0e10cSrcweir OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) ); 81*cdf0e10cSrcweir oslGenericFunction pSym = 82*cdf0e10cSrcweir osl_getFunctionSymbol( lib, aGetEnvName.pData ); 83*cdf0e10cSrcweir if (pSym) 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir Environment java_env, loader_env; 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir const sal_Char * pEnvTypeName = 0; 88*cdf0e10cSrcweir (*((component_getImplementationEnvironmentFunc)pSym))( 89*cdf0e10cSrcweir &pEnvTypeName, (uno_Environment **)&loader_env ); 90*cdf0e10cSrcweir if (! loader_env.is()) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) ); 93*cdf0e10cSrcweir uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 ); 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir // create vm access 97*cdf0e10cSrcweir ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access( 98*cdf0e10cSrcweir ::javaunohelper::create_vm_access( pJEnv, loader ) ); 99*cdf0e10cSrcweir OUString java_env_name = OUSTR(UNO_LB_JAVA); 100*cdf0e10cSrcweir uno_getEnvironment( 101*cdf0e10cSrcweir (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() ); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir OUString aWriteInfoName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_WRITEINFO) ); 104*cdf0e10cSrcweir pSym = osl_getFunctionSymbol( lib, aWriteInfoName.pData ); 105*cdf0e10cSrcweir if (pSym) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir if (loader_env.is() && java_env.is()) 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir Mapping java2dest(java_env.get(), loader_env.get()); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir if ( java2dest.is() ) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir void * pSMgr = 114*cdf0e10cSrcweir java2dest.mapInterface( 115*cdf0e10cSrcweir jSMgr, getCppuType((Reference< lang::XMultiServiceFactory > *) 0) ); 116*cdf0e10cSrcweir void * pKey = 117*cdf0e10cSrcweir java2dest.mapInterface( 118*cdf0e10cSrcweir jRegKey, getCppuType((Reference< registry::XRegistryKey > *) 0) ); 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir uno_ExtEnvironment * env = loader_env.get()->pExtEnv; 121*cdf0e10cSrcweir if (pKey) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir bRet = (*((component_writeInfoFunc)pSym))( pSMgr, pKey ); 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir if (env) 126*cdf0e10cSrcweir (*env->releaseInterface)( env, pKey ); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir if (pSMgr && env) 130*cdf0e10cSrcweir (*env->releaseInterface)( env, pSMgr ); 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir return bRet == sal_False? JNI_FALSE : JNI_TRUE; 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir /* 141*cdf0e10cSrcweir * Class: com_sun_star_comp_helper_SharedLibraryLoader 142*cdf0e10cSrcweir * Method: component_getFactory 143*cdf0e10cSrcweir * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object; 144*cdf0e10cSrcweir */ 145*cdf0e10cSrcweir extern "C" JNIEXPORT jobject JNICALL 146*cdf0e10cSrcweir Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory( 147*cdf0e10cSrcweir JNIEnv * pJEnv, jclass, jstring jLibName, jstring jImplName, 148*cdf0e10cSrcweir jobject jSMgr, jobject jRegKey, jobject loader ) 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir const jchar* pJLibName = pJEnv->GetStringChars(jLibName, NULL); 151*cdf0e10cSrcweir OUString aLibName( pJLibName ); 152*cdf0e10cSrcweir pJEnv->ReleaseStringChars( jLibName, pJLibName); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir aLibName += OUString( RTL_CONSTASCII_USTRINGPARAM(SAL_DLLEXTENSION) ); 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir jobject joSLL_cpp = 0; 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL ); 159*cdf0e10cSrcweir if (lib) 160*cdf0e10cSrcweir { 161*cdf0e10cSrcweir // ========================= LATEST VERSION ========================= 162*cdf0e10cSrcweir OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) ); 163*cdf0e10cSrcweir oslGenericFunction pSym = 164*cdf0e10cSrcweir osl_getFunctionSymbol( lib, aGetEnvName.pData ); 165*cdf0e10cSrcweir if (pSym) 166*cdf0e10cSrcweir { 167*cdf0e10cSrcweir Environment java_env, loader_env; 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir const sal_Char * pEnvTypeName = 0; 170*cdf0e10cSrcweir (*((component_getImplementationEnvironmentFunc)pSym))( 171*cdf0e10cSrcweir &pEnvTypeName, (uno_Environment **)&loader_env ); 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir if (! loader_env.is()) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) ); 176*cdf0e10cSrcweir uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 ); 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir // create vm access 180*cdf0e10cSrcweir ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access( 181*cdf0e10cSrcweir ::javaunohelper::create_vm_access( pJEnv, loader ) ); 182*cdf0e10cSrcweir OUString java_env_name = OUSTR(UNO_LB_JAVA); 183*cdf0e10cSrcweir uno_getEnvironment( 184*cdf0e10cSrcweir (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() ); 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir OUString aGetFactoryName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETFACTORY) ); 187*cdf0e10cSrcweir pSym = osl_getFunctionSymbol( lib, aGetFactoryName.pData ); 188*cdf0e10cSrcweir if (pSym) 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir if (loader_env.is() && java_env.is()) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir Mapping java2dest( java_env.get(), loader_env.get() ); 193*cdf0e10cSrcweir Mapping dest2java( loader_env.get(), java_env.get() ); 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir if (dest2java.is() && java2dest.is()) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir void * pSMgr = 198*cdf0e10cSrcweir java2dest.mapInterface( 199*cdf0e10cSrcweir jSMgr, ::getCppuType((Reference< lang::XMultiServiceFactory > *) 0) ); 200*cdf0e10cSrcweir void * pKey = 201*cdf0e10cSrcweir java2dest.mapInterface( 202*cdf0e10cSrcweir jRegKey, ::getCppuType((Reference< registry::XRegistryKey > *) 0) ); 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir const char* pImplName = pJEnv->GetStringUTFChars( jImplName, NULL ); 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir void * pSSF = (*((component_getFactoryFunc)pSym))( 207*cdf0e10cSrcweir pImplName, pSMgr, pKey ); 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir pJEnv->ReleaseStringUTFChars( jImplName, pImplName ); 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir uno_ExtEnvironment * env = loader_env.get()->pExtEnv; 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir if (pKey && env) 214*cdf0e10cSrcweir (*env->releaseInterface)( env, pKey ); 215*cdf0e10cSrcweir if (pSMgr && env) 216*cdf0e10cSrcweir (*env->releaseInterface)( env, pSMgr ); 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir if (pSSF) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir jobject jglobal = (jobject) dest2java.mapInterface( 221*cdf0e10cSrcweir pSSF, getCppuType((Reference< XInterface > *) 0) ); 222*cdf0e10cSrcweir joSLL_cpp = pJEnv->NewLocalRef( jglobal ); 223*cdf0e10cSrcweir pJEnv->DeleteGlobalRef( jglobal ); 224*cdf0e10cSrcweir if (env) 225*cdf0e10cSrcweir (*env->releaseInterface)( env, pSSF ); 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir return joSLL_cpp; 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir /* 237*cdf0e10cSrcweir * Class: com_sun_star_comp_helper_RegistryServiceFactory 238*cdf0e10cSrcweir * Method: createRegistryServiceFactory 239*cdf0e10cSrcweir * Signature: (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/Object; 240*cdf0e10cSrcweir */ 241*cdf0e10cSrcweir extern "C" JNIEXPORT jobject JNICALL 242*cdf0e10cSrcweir Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory( 243*cdf0e10cSrcweir JNIEnv * pJEnv, jclass, jstring jWriteRegFile, 244*cdf0e10cSrcweir jstring jReadRegFile, jboolean jbReadOnly, jobject loader ) 245*cdf0e10cSrcweir { 246*cdf0e10cSrcweir jobject joRegServiceFac = 0; 247*cdf0e10cSrcweir 248*cdf0e10cSrcweir try 249*cdf0e10cSrcweir { 250*cdf0e10cSrcweir OUString aWriteRegFile; 251*cdf0e10cSrcweir OUString aReadRegFile; 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir sal_Bool bReadOnly = jbReadOnly == JNI_FALSE ? sal_False : sal_True; 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir if (jReadRegFile) { 256*cdf0e10cSrcweir const jchar* pjReadRegFile = pJEnv->GetStringChars(jReadRegFile, NULL); 257*cdf0e10cSrcweir aReadRegFile = OUString(pjReadRegFile); 258*cdf0e10cSrcweir pJEnv->ReleaseStringChars(jReadRegFile, pjReadRegFile); 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir if (jWriteRegFile) { 262*cdf0e10cSrcweir const jchar * pjWriteRegFile = pJEnv->GetStringChars(jWriteRegFile, NULL); 263*cdf0e10cSrcweir aWriteRegFile = OUString(pjWriteRegFile); 264*cdf0e10cSrcweir pJEnv->ReleaseStringChars(jWriteRegFile, pjWriteRegFile); 265*cdf0e10cSrcweir } 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir // bootstrap 268*cdf0e10cSrcweir Reference< lang::XMultiServiceFactory > rMSFac; 269*cdf0e10cSrcweir if (aReadRegFile.getLength() == 0) 270*cdf0e10cSrcweir rMSFac = ::cppu::createRegistryServiceFactory( aWriteRegFile, bReadOnly); 271*cdf0e10cSrcweir else 272*cdf0e10cSrcweir rMSFac = ::cppu::createRegistryServiceFactory(aWriteRegFile, aReadRegFile, bReadOnly); 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir Reference< beans::XPropertySet > xProps( 275*cdf0e10cSrcweir rMSFac, UNO_QUERY_THROW ); 276*cdf0e10cSrcweir Reference< XComponentContext > xContext( 277*cdf0e10cSrcweir xProps->getPropertyValue( OUSTR("DefaultContext") ), UNO_QUERY_THROW ); 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir // create vm access 280*cdf0e10cSrcweir ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access( 281*cdf0e10cSrcweir ::javaunohelper::create_vm_access( pJEnv, loader ) ); 282*cdf0e10cSrcweir // wrap vm singleton entry 283*cdf0e10cSrcweir xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access ); 284*cdf0e10cSrcweir rMSFac.set( xContext->getServiceManager(), UNO_QUERY_THROW ); 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir // get uno envs 287*cdf0e10cSrcweir OUString aCurrentEnv(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME)); 288*cdf0e10cSrcweir OUString java_env_name = OUSTR(UNO_LB_JAVA); 289*cdf0e10cSrcweir Environment java_env, curr_env; 290*cdf0e10cSrcweir uno_getEnvironment((uno_Environment **)&curr_env, aCurrentEnv.pData, NULL); 291*cdf0e10cSrcweir uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() ); 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir Mapping curr_java(curr_env.get(), java_env.get()); 294*cdf0e10cSrcweir if (! curr_java.is()) 295*cdf0e10cSrcweir { 296*cdf0e10cSrcweir throw RuntimeException( 297*cdf0e10cSrcweir OUSTR("no C++ <-> Java mapping available!"), Reference< XInterface >() ); 298*cdf0e10cSrcweir } 299*cdf0e10cSrcweir 300*cdf0e10cSrcweir jobject joGlobalRegServiceFac = 301*cdf0e10cSrcweir (jobject)curr_java.mapInterface( 302*cdf0e10cSrcweir rMSFac.get(), 303*cdf0e10cSrcweir getCppuType((Reference< lang::XMultiServiceFactory > *)0) ); 304*cdf0e10cSrcweir joRegServiceFac = pJEnv->NewLocalRef( joGlobalRegServiceFac ); 305*cdf0e10cSrcweir pJEnv->DeleteGlobalRef(joGlobalRegServiceFac); 306*cdf0e10cSrcweir } 307*cdf0e10cSrcweir catch (Exception & exc) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir jclass c = pJEnv->FindClass( "com/sun/star/uno/RuntimeException" ); 310*cdf0e10cSrcweir if (0 != c) 311*cdf0e10cSrcweir { 312*cdf0e10cSrcweir OString cstr( ::rtl::OUStringToOString( 313*cdf0e10cSrcweir exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) ); 314*cdf0e10cSrcweir OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() ); 315*cdf0e10cSrcweir pJEnv->ThrowNew( c, cstr.getStr() ); 316*cdf0e10cSrcweir } 317*cdf0e10cSrcweir return 0; 318*cdf0e10cSrcweir } 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir OSL_TRACE("javaunohelper.cxx: object %i", joRegServiceFac); 321*cdf0e10cSrcweir 322*cdf0e10cSrcweir return joRegServiceFac; 323*cdf0e10cSrcweir } 324