Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / javaunohelper / source / bootstrap.cxx
blob0fa2558c87f131f368f0433e7262b3a0d55b6275
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
22 #include "osl/diagnose.h"
24 #include "rtl/alloc.h"
25 #include "rtl/bootstrap.hxx"
26 #include "rtl/string.hxx"
28 #include "uno/mapping.hxx"
29 #include "uno/environment.hxx"
31 #include "cppuhelper/bootstrap.hxx"
33 #include "com/sun/star/lang/XComponent.hpp"
34 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
36 #include "jni.h"
37 #include "jvmaccess/virtualmachine.hxx"
38 #include "jvmaccess/unovirtualmachine.hxx"
40 #include "juhx-export-functions.hxx"
41 #include "vm.hxx"
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
46 namespace javaunohelper
49 inline OUString jstring_to_oustring( jstring jstr, JNIEnv * jni_env )
51 OSL_ASSERT( sizeof (sal_Unicode) == sizeof (jchar) );
52 jsize len = jni_env->GetStringLength( jstr );
53 rtl_uString * ustr =
54 (rtl_uString *)rtl_allocateMemory( sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) );
55 jni_env->GetStringRegion( jstr, 0, len, ustr->buffer );
56 OSL_ASSERT( JNI_FALSE == jni_env->ExceptionCheck() );
57 ustr->refCount = 1;
58 ustr->length = len;
59 ustr->buffer[ len ] = '\0';
60 return OUString( ustr, SAL_NO_ACQUIRE );
65 //==================================================================================================
66 jobject Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
67 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jclass, jstring juno_rc, jobjectArray jpairs,
68 jobject loader )
70 try
72 if (0 != jpairs)
74 jsize nPos = 0, len = jni_env->GetArrayLength( jpairs );
75 while (nPos < len)
77 // name
78 jstring jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos );
79 if (JNI_FALSE != jni_env->ExceptionCheck())
81 jni_env->ExceptionClear();
82 throw RuntimeException(
83 "index out of bounds?!", Reference< XInterface >() );
85 if (0 != jstr)
87 OUString name( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
88 // value
89 jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos +1 );
90 if (JNI_FALSE != jni_env->ExceptionCheck())
92 jni_env->ExceptionClear();
93 throw RuntimeException(
94 "index out of bounds?!", Reference< XInterface >() );
96 if (0 != jstr)
98 OUString value( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
100 // set bootstrap parameter
101 ::rtl::Bootstrap::set( name, value );
104 nPos += 2;
108 // bootstrap uno
109 Reference< XComponentContext > xContext;
110 if (0 == juno_rc)
112 xContext = ::cppu::defaultBootstrap_InitialComponentContext();
114 else
116 OUString uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc, jni_env ) );
117 xContext = ::cppu::defaultBootstrap_InitialComponentContext( uno_rc );
120 // create vm access
121 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
122 ::javaunohelper::create_vm_access( jni_env, loader ) );
123 // wrap vm singleton entry
124 xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
126 // get uno envs
127 OUString cpp_env_name = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
128 OUString java_env_name = UNO_LB_JAVA;
129 Environment java_env, cpp_env;
130 uno_getEnvironment((uno_Environment **)&cpp_env, cpp_env_name.pData, NULL);
131 uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
133 // map to java
134 Mapping mapping( cpp_env.get(), java_env.get() );
135 if (! mapping.is())
137 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
138 if (xComp.is())
139 xComp->dispose();
140 throw RuntimeException(
141 "cannot get mapping C++ <-> Java!",
142 Reference< XInterface >() );
145 jobject jret = (jobject)mapping.mapInterface( xContext.get(), ::getCppuType( &xContext ) );
146 jobject jlocal = jni_env->NewLocalRef( jret );
147 jni_env->DeleteGlobalRef( jret );
149 return jlocal;
151 catch (const RuntimeException & exc)
153 jclass c = jni_env->FindClass( "com/sun/star/uno/RuntimeException" );
154 if (0 != c)
156 OString cstr( OUStringToOString(
157 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
158 OSL_TRACE( __FILE__": forwarding RuntimeException: %s", cstr.getStr() );
159 jni_env->ThrowNew( c, cstr.getStr() );
162 catch (const Exception & exc)
164 jclass c = jni_env->FindClass( "com/sun/star/uno/Exception" );
165 if (0 != c)
167 OString cstr( OUStringToOString(
168 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
169 OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
170 jni_env->ThrowNew( c, cstr.getStr() );
174 return 0;
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */