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 33*cdf0e10cSrcweir #include "rtl/alloc.h" 34*cdf0e10cSrcweir #include "rtl/bootstrap.hxx" 35*cdf0e10cSrcweir #include "rtl/string.hxx" 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #include "uno/mapping.hxx" 38*cdf0e10cSrcweir #include "uno/environment.hxx" 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include "cppuhelper/bootstrap.hxx" 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir #include "com/sun/star/lang/XComponent.hpp" 43*cdf0e10cSrcweir #include "com/sun/star/lang/XSingleComponentFactory.hpp" 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir #include "jni.h" 46*cdf0e10cSrcweir #include "jvmaccess/virtualmachine.hxx" 47*cdf0e10cSrcweir #include "jvmaccess/unovirtualmachine.hxx" 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir #include "vm.hxx" 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) ) 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir using namespace ::com::sun::star; 55*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 56*cdf0e10cSrcweir using ::rtl::OString; 57*cdf0e10cSrcweir using ::rtl::OUString; 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir namespace javaunohelper 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir inline ::rtl::OUString jstring_to_oustring( jstring jstr, JNIEnv * jni_env ) 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir OSL_ASSERT( sizeof (sal_Unicode) == sizeof (jchar) ); 65*cdf0e10cSrcweir jsize len = jni_env->GetStringLength( jstr ); 66*cdf0e10cSrcweir rtl_uString * ustr = 67*cdf0e10cSrcweir (rtl_uString *)rtl_allocateMemory( sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ); 68*cdf0e10cSrcweir jni_env->GetStringRegion( jstr, 0, len, ustr->buffer ); 69*cdf0e10cSrcweir OSL_ASSERT( JNI_FALSE == jni_env->ExceptionCheck() ); 70*cdf0e10cSrcweir ustr->refCount = 1; 71*cdf0e10cSrcweir ustr->length = len; 72*cdf0e10cSrcweir ustr->buffer[ len ] = '\0'; 73*cdf0e10cSrcweir return ::rtl::OUString( ustr, SAL_NO_ACQUIRE ); 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir //================================================================================================== 79*cdf0e10cSrcweir extern "C" JNIEXPORT jobject JNICALL Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap( 80*cdf0e10cSrcweir JNIEnv * jni_env, jclass, jstring juno_rc, jobjectArray jpairs, 81*cdf0e10cSrcweir jobject loader ) 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir try 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir if (0 != jpairs) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir jsize nPos = 0, len = jni_env->GetArrayLength( jpairs ); 88*cdf0e10cSrcweir while (nPos < len) 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir // name 91*cdf0e10cSrcweir jstring jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos ); 92*cdf0e10cSrcweir if (JNI_FALSE != jni_env->ExceptionCheck()) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir jni_env->ExceptionClear(); 95*cdf0e10cSrcweir throw RuntimeException( 96*cdf0e10cSrcweir OUSTR("index out of bounds?!"), Reference< XInterface >() ); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir if (0 != jstr) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir OUString name( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) ); 101*cdf0e10cSrcweir // value 102*cdf0e10cSrcweir jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos +1 ); 103*cdf0e10cSrcweir if (JNI_FALSE != jni_env->ExceptionCheck()) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir jni_env->ExceptionClear(); 106*cdf0e10cSrcweir throw RuntimeException( 107*cdf0e10cSrcweir OUSTR("index out of bounds?!"), Reference< XInterface >() ); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir if (0 != jstr) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir OUString value( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) ); 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir // set bootstrap parameter 114*cdf0e10cSrcweir ::rtl::Bootstrap::set( name, value ); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir nPos += 2; 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir // bootstrap uno 122*cdf0e10cSrcweir Reference< XComponentContext > xContext; 123*cdf0e10cSrcweir if (0 == juno_rc) 124*cdf0e10cSrcweir { 125*cdf0e10cSrcweir xContext = ::cppu::defaultBootstrap_InitialComponentContext(); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir else 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir OUString uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc, jni_env ) ); 130*cdf0e10cSrcweir xContext = ::cppu::defaultBootstrap_InitialComponentContext( uno_rc ); 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir // create vm access 134*cdf0e10cSrcweir ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access( 135*cdf0e10cSrcweir ::javaunohelper::create_vm_access( jni_env, loader ) ); 136*cdf0e10cSrcweir // wrap vm singleton entry 137*cdf0e10cSrcweir xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access ); 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir // get uno envs 140*cdf0e10cSrcweir OUString cpp_env_name = OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME); 141*cdf0e10cSrcweir OUString java_env_name = OUSTR(UNO_LB_JAVA); 142*cdf0e10cSrcweir Environment java_env, cpp_env; 143*cdf0e10cSrcweir uno_getEnvironment((uno_Environment **)&cpp_env, cpp_env_name.pData, NULL); 144*cdf0e10cSrcweir uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() ); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir // map to java 147*cdf0e10cSrcweir Mapping mapping( cpp_env.get(), java_env.get() ); 148*cdf0e10cSrcweir if (! mapping.is()) 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir Reference< lang::XComponent > xComp( xContext, UNO_QUERY ); 151*cdf0e10cSrcweir if (xComp.is()) 152*cdf0e10cSrcweir xComp->dispose(); 153*cdf0e10cSrcweir throw RuntimeException( 154*cdf0e10cSrcweir OUSTR("cannot get mapping C++ <-> Java!"), 155*cdf0e10cSrcweir Reference< XInterface >() ); 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir jobject jret = (jobject)mapping.mapInterface( xContext.get(), ::getCppuType( &xContext ) ); 159*cdf0e10cSrcweir jobject jlocal = jni_env->NewLocalRef( jret ); 160*cdf0e10cSrcweir jni_env->DeleteGlobalRef( jret ); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir return jlocal; 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir catch (RuntimeException & exc) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir jclass c = jni_env->FindClass( "com/sun/star/uno/RuntimeException" ); 167*cdf0e10cSrcweir if (0 != c) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir OString cstr( ::rtl::OUStringToOString( 170*cdf0e10cSrcweir exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) ); 171*cdf0e10cSrcweir OSL_TRACE( __FILE__": forwarding RuntimeException: %s", cstr.getStr() ); 172*cdf0e10cSrcweir jni_env->ThrowNew( c, cstr.getStr() ); 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir catch (Exception & exc) 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir jclass c = jni_env->FindClass( "com/sun/star/uno/Exception" ); 178*cdf0e10cSrcweir if (0 != c) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir OString cstr( ::rtl::OUStringToOString( 181*cdf0e10cSrcweir exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) ); 182*cdf0e10cSrcweir OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() ); 183*cdf0e10cSrcweir jni_env->ThrowNew( c, cstr.getStr() ); 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir return 0; 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir 190