1*61f28c9eSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*61f28c9eSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*61f28c9eSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*61f28c9eSAndrew Rist * distributed with this work for additional information 6*61f28c9eSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*61f28c9eSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*61f28c9eSAndrew Rist * "License"); you may not use this file except in compliance 9*61f28c9eSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*61f28c9eSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*61f28c9eSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*61f28c9eSAndrew Rist * software distributed under the License is distributed on an 15*61f28c9eSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*61f28c9eSAndrew Rist * KIND, either express or implied. See the License for the 17*61f28c9eSAndrew Rist * specific language governing permissions and limitations 18*61f28c9eSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*61f28c9eSAndrew Rist *************************************************************/ 21*61f28c9eSAndrew Rist 22*61f28c9eSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_javaunohelper.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "osl/diagnose.h" 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include "rtl/alloc.h" 30cdf0e10cSrcweir #include "rtl/bootstrap.hxx" 31cdf0e10cSrcweir #include "rtl/string.hxx" 32cdf0e10cSrcweir 33cdf0e10cSrcweir #include "uno/mapping.hxx" 34cdf0e10cSrcweir #include "uno/environment.hxx" 35cdf0e10cSrcweir 36cdf0e10cSrcweir #include "cppuhelper/bootstrap.hxx" 37cdf0e10cSrcweir 38cdf0e10cSrcweir #include "com/sun/star/lang/XComponent.hpp" 39cdf0e10cSrcweir #include "com/sun/star/lang/XSingleComponentFactory.hpp" 40cdf0e10cSrcweir 41cdf0e10cSrcweir #include "jni.h" 42cdf0e10cSrcweir #include "jvmaccess/virtualmachine.hxx" 43cdf0e10cSrcweir #include "jvmaccess/unovirtualmachine.hxx" 44cdf0e10cSrcweir 45cdf0e10cSrcweir #include "vm.hxx" 46cdf0e10cSrcweir 47cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) ) 48cdf0e10cSrcweir 49cdf0e10cSrcweir 50cdf0e10cSrcweir using namespace ::com::sun::star; 51cdf0e10cSrcweir using namespace ::com::sun::star::uno; 52cdf0e10cSrcweir using ::rtl::OString; 53cdf0e10cSrcweir using ::rtl::OUString; 54cdf0e10cSrcweir 55cdf0e10cSrcweir namespace javaunohelper 56cdf0e10cSrcweir { 57cdf0e10cSrcweir 58cdf0e10cSrcweir inline ::rtl::OUString jstring_to_oustring( jstring jstr, JNIEnv * jni_env ) 59cdf0e10cSrcweir { 60cdf0e10cSrcweir OSL_ASSERT( sizeof (sal_Unicode) == sizeof (jchar) ); 61cdf0e10cSrcweir jsize len = jni_env->GetStringLength( jstr ); 62cdf0e10cSrcweir rtl_uString * ustr = 63cdf0e10cSrcweir (rtl_uString *)rtl_allocateMemory( sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ); 64cdf0e10cSrcweir jni_env->GetStringRegion( jstr, 0, len, ustr->buffer ); 65cdf0e10cSrcweir OSL_ASSERT( JNI_FALSE == jni_env->ExceptionCheck() ); 66cdf0e10cSrcweir ustr->refCount = 1; 67cdf0e10cSrcweir ustr->length = len; 68cdf0e10cSrcweir ustr->buffer[ len ] = '\0'; 69cdf0e10cSrcweir return ::rtl::OUString( ustr, SAL_NO_ACQUIRE ); 70cdf0e10cSrcweir } 71cdf0e10cSrcweir 72cdf0e10cSrcweir } 73cdf0e10cSrcweir 74cdf0e10cSrcweir //================================================================================================== 75cdf0e10cSrcweir extern "C" JNIEXPORT jobject JNICALL Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap( 76cdf0e10cSrcweir JNIEnv * jni_env, jclass, jstring juno_rc, jobjectArray jpairs, 77cdf0e10cSrcweir jobject loader ) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir try 80cdf0e10cSrcweir { 81cdf0e10cSrcweir if (0 != jpairs) 82cdf0e10cSrcweir { 83cdf0e10cSrcweir jsize nPos = 0, len = jni_env->GetArrayLength( jpairs ); 84cdf0e10cSrcweir while (nPos < len) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir // name 87cdf0e10cSrcweir jstring jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos ); 88cdf0e10cSrcweir if (JNI_FALSE != jni_env->ExceptionCheck()) 89cdf0e10cSrcweir { 90cdf0e10cSrcweir jni_env->ExceptionClear(); 91cdf0e10cSrcweir throw RuntimeException( 92cdf0e10cSrcweir OUSTR("index out of bounds?!"), Reference< XInterface >() ); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir if (0 != jstr) 95cdf0e10cSrcweir { 96cdf0e10cSrcweir OUString name( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) ); 97cdf0e10cSrcweir // value 98cdf0e10cSrcweir jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos +1 ); 99cdf0e10cSrcweir if (JNI_FALSE != jni_env->ExceptionCheck()) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir jni_env->ExceptionClear(); 102cdf0e10cSrcweir throw RuntimeException( 103cdf0e10cSrcweir OUSTR("index out of bounds?!"), Reference< XInterface >() ); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir if (0 != jstr) 106cdf0e10cSrcweir { 107cdf0e10cSrcweir OUString value( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) ); 108cdf0e10cSrcweir 109cdf0e10cSrcweir // set bootstrap parameter 110cdf0e10cSrcweir ::rtl::Bootstrap::set( name, value ); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir } 113cdf0e10cSrcweir nPos += 2; 114cdf0e10cSrcweir } 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir // bootstrap uno 118cdf0e10cSrcweir Reference< XComponentContext > xContext; 119cdf0e10cSrcweir if (0 == juno_rc) 120cdf0e10cSrcweir { 121cdf0e10cSrcweir xContext = ::cppu::defaultBootstrap_InitialComponentContext(); 122cdf0e10cSrcweir } 123cdf0e10cSrcweir else 124cdf0e10cSrcweir { 125cdf0e10cSrcweir OUString uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc, jni_env ) ); 126cdf0e10cSrcweir xContext = ::cppu::defaultBootstrap_InitialComponentContext( uno_rc ); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir // create vm access 130cdf0e10cSrcweir ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access( 131cdf0e10cSrcweir ::javaunohelper::create_vm_access( jni_env, loader ) ); 132cdf0e10cSrcweir // wrap vm singleton entry 133cdf0e10cSrcweir xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access ); 134cdf0e10cSrcweir 135cdf0e10cSrcweir // get uno envs 136cdf0e10cSrcweir OUString cpp_env_name = OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME); 137cdf0e10cSrcweir OUString java_env_name = OUSTR(UNO_LB_JAVA); 138cdf0e10cSrcweir Environment java_env, cpp_env; 139cdf0e10cSrcweir uno_getEnvironment((uno_Environment **)&cpp_env, cpp_env_name.pData, NULL); 140cdf0e10cSrcweir uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() ); 141cdf0e10cSrcweir 142cdf0e10cSrcweir // map to java 143cdf0e10cSrcweir Mapping mapping( cpp_env.get(), java_env.get() ); 144cdf0e10cSrcweir if (! mapping.is()) 145cdf0e10cSrcweir { 146cdf0e10cSrcweir Reference< lang::XComponent > xComp( xContext, UNO_QUERY ); 147cdf0e10cSrcweir if (xComp.is()) 148cdf0e10cSrcweir xComp->dispose(); 149cdf0e10cSrcweir throw RuntimeException( 150cdf0e10cSrcweir OUSTR("cannot get mapping C++ <-> Java!"), 151cdf0e10cSrcweir Reference< XInterface >() ); 152cdf0e10cSrcweir } 153cdf0e10cSrcweir 154cdf0e10cSrcweir jobject jret = (jobject)mapping.mapInterface( xContext.get(), ::getCppuType( &xContext ) ); 155cdf0e10cSrcweir jobject jlocal = jni_env->NewLocalRef( jret ); 156cdf0e10cSrcweir jni_env->DeleteGlobalRef( jret ); 157cdf0e10cSrcweir 158cdf0e10cSrcweir return jlocal; 159cdf0e10cSrcweir } 160cdf0e10cSrcweir catch (RuntimeException & exc) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir jclass c = jni_env->FindClass( "com/sun/star/uno/RuntimeException" ); 163cdf0e10cSrcweir if (0 != c) 164cdf0e10cSrcweir { 165cdf0e10cSrcweir OString cstr( ::rtl::OUStringToOString( 166cdf0e10cSrcweir exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) ); 167cdf0e10cSrcweir OSL_TRACE( __FILE__": forwarding RuntimeException: %s", cstr.getStr() ); 168cdf0e10cSrcweir jni_env->ThrowNew( c, cstr.getStr() ); 169cdf0e10cSrcweir } 170cdf0e10cSrcweir } 171cdf0e10cSrcweir catch (Exception & exc) 172cdf0e10cSrcweir { 173cdf0e10cSrcweir jclass c = jni_env->FindClass( "com/sun/star/uno/Exception" ); 174cdf0e10cSrcweir if (0 != c) 175cdf0e10cSrcweir { 176cdf0e10cSrcweir OString cstr( ::rtl::OUStringToOString( 177cdf0e10cSrcweir exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) ); 178cdf0e10cSrcweir OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() ); 179cdf0e10cSrcweir jni_env->ThrowNew( c, cstr.getStr() ); 180cdf0e10cSrcweir } 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir return 0; 184cdf0e10cSrcweir } 185cdf0e10cSrcweir 186