Update ooo320-m1
[ooovba.git] / javaunohelper / source / bootstrap.cxx
blob7d1a32f8d61d05a69a34e6cc0d5eccce52b72f71
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bootstrap.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_javaunohelper.hxx"
34 #include "osl/diagnose.h"
36 #include "rtl/alloc.h"
37 #include "rtl/bootstrap.hxx"
38 #include "rtl/string.hxx"
40 #include "uno/mapping.hxx"
41 #include "uno/environment.hxx"
43 #include "cppuhelper/bootstrap.hxx"
45 #include "com/sun/star/lang/XComponent.hpp"
46 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
48 #include "jni.h"
49 #include "jvmaccess/virtualmachine.hxx"
50 #include "jvmaccess/unovirtualmachine.hxx"
52 #include "vm.hxx"
54 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
57 using namespace ::com::sun::star;
58 using namespace ::com::sun::star::uno;
59 using ::rtl::OString;
60 using ::rtl::OUString;
62 namespace javaunohelper
65 inline ::rtl::OUString jstring_to_oustring( jstring jstr, JNIEnv * jni_env )
67 OSL_ASSERT( sizeof (sal_Unicode) == sizeof (jchar) );
68 jsize len = jni_env->GetStringLength( jstr );
69 rtl_uString * ustr =
70 (rtl_uString *)rtl_allocateMemory( sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) );
71 jni_env->GetStringRegion( jstr, 0, len, ustr->buffer );
72 OSL_ASSERT( JNI_FALSE == jni_env->ExceptionCheck() );
73 ustr->refCount = 1;
74 ustr->length = len;
75 ustr->buffer[ len ] = '\0';
76 return ::rtl::OUString( ustr, SAL_NO_ACQUIRE );
81 //==================================================================================================
82 extern "C" JNIEXPORT jobject JNICALL Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
83 JNIEnv * jni_env, jclass, jstring juno_rc, jobjectArray jpairs,
84 jobject loader )
86 try
88 if (0 != jpairs)
90 jsize nPos = 0, len = jni_env->GetArrayLength( jpairs );
91 while (nPos < len)
93 // name
94 jstring jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos );
95 if (JNI_FALSE != jni_env->ExceptionCheck())
97 jni_env->ExceptionClear();
98 throw RuntimeException(
99 OUSTR("index out of bounds?!"), Reference< XInterface >() );
101 if (0 != jstr)
103 OUString name( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
104 // value
105 jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos +1 );
106 if (JNI_FALSE != jni_env->ExceptionCheck())
108 jni_env->ExceptionClear();
109 throw RuntimeException(
110 OUSTR("index out of bounds?!"), Reference< XInterface >() );
112 if (0 != jstr)
114 OUString value( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
116 // set bootstrap parameter
117 ::rtl::Bootstrap::set( name, value );
120 nPos += 2;
124 // bootstrap uno
125 Reference< XComponentContext > xContext;
126 if (0 == juno_rc)
128 xContext = ::cppu::defaultBootstrap_InitialComponentContext();
130 else
132 OUString uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc, jni_env ) );
133 xContext = ::cppu::defaultBootstrap_InitialComponentContext( uno_rc );
136 // create vm access
137 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
138 ::javaunohelper::create_vm_access( jni_env, loader ) );
139 // wrap vm singleton entry
140 xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
142 // get uno envs
143 OUString cpp_env_name = OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
144 OUString java_env_name = OUSTR(UNO_LB_JAVA);
145 Environment java_env, cpp_env;
146 uno_getEnvironment((uno_Environment **)&cpp_env, cpp_env_name.pData, NULL);
147 uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
149 // map to java
150 Mapping mapping( cpp_env.get(), java_env.get() );
151 if (! mapping.is())
153 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
154 if (xComp.is())
155 xComp->dispose();
156 throw RuntimeException(
157 OUSTR("cannot get mapping C++ <-> Java!"),
158 Reference< XInterface >() );
161 jobject jret = (jobject)mapping.mapInterface( xContext.get(), ::getCppuType( &xContext ) );
162 jobject jlocal = jni_env->NewLocalRef( jret );
163 jni_env->DeleteGlobalRef( jret );
165 return jlocal;
167 catch (RuntimeException & exc)
169 jclass c = jni_env->FindClass( "com/sun/star/uno/RuntimeException" );
170 if (0 != c)
172 OString cstr( ::rtl::OUStringToOString(
173 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
174 OSL_TRACE( __FILE__": forwarding RuntimeException: %s", cstr.getStr() );
175 jni_env->ThrowNew( c, cstr.getStr() );
178 catch (Exception & exc)
180 jclass c = jni_env->FindClass( "com/sun/star/uno/Exception" );
181 if (0 != c)
183 OString cstr( ::rtl::OUStringToOString(
184 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
185 OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
186 jni_env->ThrowNew( c, cstr.getStr() );
190 return 0;