Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / javaunohelper / source / bootstrap.cxx
blobd053d5793d2f8d368a2a3957dae4a20b1a9f4aa1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "osl/diagnose.h"
32 #include "rtl/alloc.h"
33 #include "rtl/bootstrap.hxx"
34 #include "rtl/string.hxx"
36 #include "uno/mapping.hxx"
37 #include "uno/environment.hxx"
39 #include "cppuhelper/bootstrap.hxx"
41 #include "com/sun/star/lang/XComponent.hpp"
42 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
44 #include "jni.h"
45 #include "jvmaccess/virtualmachine.hxx"
46 #include "jvmaccess/unovirtualmachine.hxx"
48 #include "vm.hxx"
50 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::uno;
55 using ::rtl::OString;
56 using ::rtl::OUString;
58 namespace javaunohelper
61 inline ::rtl::OUString jstring_to_oustring( jstring jstr, JNIEnv * jni_env )
63 OSL_ASSERT( sizeof (sal_Unicode) == sizeof (jchar) );
64 jsize len = jni_env->GetStringLength( jstr );
65 rtl_uString * ustr =
66 (rtl_uString *)rtl_allocateMemory( sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) );
67 jni_env->GetStringRegion( jstr, 0, len, ustr->buffer );
68 OSL_ASSERT( JNI_FALSE == jni_env->ExceptionCheck() );
69 ustr->refCount = 1;
70 ustr->length = len;
71 ustr->buffer[ len ] = '\0';
72 return ::rtl::OUString( ustr, SAL_NO_ACQUIRE );
77 //==================================================================================================
78 extern "C" JNIEXPORT jobject JNICALL Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
79 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jclass, jstring juno_rc, jobjectArray jpairs,
80 jobject loader )
82 try
84 if (0 != jpairs)
86 jsize nPos = 0, len = jni_env->GetArrayLength( jpairs );
87 while (nPos < len)
89 // name
90 jstring jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos );
91 if (JNI_FALSE != jni_env->ExceptionCheck())
93 jni_env->ExceptionClear();
94 throw RuntimeException(
95 OUSTR("index out of bounds?!"), Reference< XInterface >() );
97 if (0 != jstr)
99 OUString name( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
100 // value
101 jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos +1 );
102 if (JNI_FALSE != jni_env->ExceptionCheck())
104 jni_env->ExceptionClear();
105 throw RuntimeException(
106 OUSTR("index out of bounds?!"), Reference< XInterface >() );
108 if (0 != jstr)
110 OUString value( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
112 // set bootstrap parameter
113 ::rtl::Bootstrap::set( name, value );
116 nPos += 2;
120 // bootstrap uno
121 Reference< XComponentContext > xContext;
122 if (0 == juno_rc)
124 xContext = ::cppu::defaultBootstrap_InitialComponentContext();
126 else
128 OUString uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc, jni_env ) );
129 xContext = ::cppu::defaultBootstrap_InitialComponentContext( uno_rc );
132 // create vm access
133 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
134 ::javaunohelper::create_vm_access( jni_env, loader ) );
135 // wrap vm singleton entry
136 xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
138 // get uno envs
139 OUString cpp_env_name = OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
140 OUString java_env_name = OUSTR(UNO_LB_JAVA);
141 Environment java_env, cpp_env;
142 uno_getEnvironment((uno_Environment **)&cpp_env, cpp_env_name.pData, NULL);
143 uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
145 // map to java
146 Mapping mapping( cpp_env.get(), java_env.get() );
147 if (! mapping.is())
149 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
150 if (xComp.is())
151 xComp->dispose();
152 throw RuntimeException(
153 OUSTR("cannot get mapping C++ <-> Java!"),
154 Reference< XInterface >() );
157 jobject jret = (jobject)mapping.mapInterface( xContext.get(), ::getCppuType( &xContext ) );
158 jobject jlocal = jni_env->NewLocalRef( jret );
159 jni_env->DeleteGlobalRef( jret );
161 return jlocal;
163 catch (const RuntimeException & exc)
165 jclass c = jni_env->FindClass( "com/sun/star/uno/RuntimeException" );
166 if (0 != c)
168 OString cstr( ::rtl::OUStringToOString(
169 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
170 OSL_TRACE( __FILE__": forwarding RuntimeException: %s", cstr.getStr() );
171 jni_env->ThrowNew( c, cstr.getStr() );
174 catch (const Exception & exc)
176 jclass c = jni_env->FindClass( "com/sun/star/uno/Exception" );
177 if (0 != c)
179 OString cstr( ::rtl::OUStringToOString(
180 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
181 OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
182 jni_env->ThrowNew( c, cstr.getStr() );
186 return 0;
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */