Update git submodules
[LibreOffice.git] / javaunohelper / source / bootstrap.cxx
blob1cacea950c0db2aaaf5403e67528979c03000a2c
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>
21 #include <sal/log.hxx>
23 #include <osl/diagnose.h>
25 #include <rtl/bootstrap.hxx>
26 #include <rtl/string.hxx>
28 #include <uno/lbnames.h>
29 #include <uno/mapping.hxx>
30 #include <uno/environment.hxx>
32 #include <cppuhelper/bootstrap.hxx>
34 #include <com/sun/star/lang/XComponent.hpp>
35 #include <com/sun/star/uno/XComponentContext.hpp>
37 #if defined __clang__
38 #pragma clang diagnostic push
39 #pragma clang diagnostic ignored "-Wunknown-attributes"
40 #endif
41 #include <jni.h>
42 #if defined __clang__
43 #pragma clang diagnostic pop
44 #endif
46 #include <jvmaccess/unovirtualmachine.hxx>
48 #include "juhx-export-functions.hxx"
49 #include "vm.hxx"
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
54 namespace javaunohelper
57 static OUString jstring_to_oustring( jstring jstr, JNIEnv * jni_env )
59 OSL_ASSERT( sizeof (sal_Unicode) == sizeof (jchar) );
60 jsize len = jni_env->GetStringLength( jstr );
61 rtl_uString * ustr =
62 static_cast<rtl_uString *>(std::malloc( sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ));
63 assert(ustr);
64 jni_env->GetStringRegion( jstr, 0, len, reinterpret_cast<jchar *>(ustr->buffer) );
65 OSL_ASSERT( !jni_env->ExceptionCheck() );
66 ustr->refCount = 1;
67 ustr->length = len;
68 ustr->buffer[ len ] = '\0';
69 return OUString( ustr, SAL_NO_ACQUIRE );
75 jobject Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
76 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jclass, jstring juno_rc, jobjectArray jpairs,
77 jobject loader )
79 try
81 if (nullptr != jpairs)
83 jsize nPos = 0, len = jni_env->GetArrayLength( jpairs );
84 while (nPos < len)
86 // name
87 jstring jstr = static_cast<jstring>(jni_env->GetObjectArrayElement( jpairs, nPos ));
88 if (jni_env->ExceptionCheck())
90 jni_env->ExceptionClear();
91 throw RuntimeException( "index out of bounds?!" );
93 if (nullptr != jstr)
95 OUString name( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
96 // value
97 jstr = static_cast<jstring>(jni_env->GetObjectArrayElement( jpairs, nPos +1 ));
98 if (jni_env->ExceptionCheck())
100 jni_env->ExceptionClear();
101 throw RuntimeException( "index out of bounds?!" );
103 if (nullptr != jstr)
105 OUString value( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
107 // set bootstrap parameter
108 ::rtl::Bootstrap::set( name, value );
111 nPos += 2;
115 // bootstrap uno
116 Reference< XComponentContext > xContext;
117 if (nullptr == juno_rc)
119 xContext = ::cppu::defaultBootstrap_InitialComponentContext();
121 else
123 OUString uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc, jni_env ) );
124 xContext = ::cppu::defaultBootstrap_InitialComponentContext( uno_rc );
127 // create vm access
128 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
129 ::javaunohelper::create_vm_access( jni_env, loader ) );
130 // wrap vm singleton entry
131 xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
133 // get uno envs
134 OUString cpp_env_name = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
135 OUString java_env_name = UNO_LB_JAVA;
136 Environment java_env, cpp_env;
137 uno_getEnvironment(reinterpret_cast<uno_Environment **>(&cpp_env), cpp_env_name.pData, nullptr);
138 uno_getEnvironment(reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() );
140 // map to java
141 Mapping mapping( cpp_env.get(), java_env.get() );
142 if (! mapping.is())
144 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
145 if (xComp.is())
146 xComp->dispose();
147 throw RuntimeException("cannot get mapping C++ <-> Java!" );
150 jobject jret = static_cast<jobject>(mapping.mapInterface( xContext.get(), cppu::UnoType<decltype(xContext)>::get() ));
151 jobject jlocal = jni_env->NewLocalRef( jret );
152 jni_env->DeleteGlobalRef( jret );
154 return jlocal;
156 catch (const RuntimeException & exc)
158 jclass c = jni_env->FindClass( "com/sun/star/uno/RuntimeException" );
159 if (nullptr != c)
161 SAL_WARN("javaunohelper", "forwarding RuntimeException: " << exc );
162 OString cstr( OUStringToOString(
163 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
164 jni_env->ThrowNew( c, cstr.getStr() );
167 catch (const Exception & exc)
169 jclass c = jni_env->FindClass( "com/sun/star/uno/Exception" );
170 if (nullptr != c)
172 SAL_WARN("javaunohelper", "forwarding Exception: " << exc );
173 OString cstr( OUStringToOString(
174 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
175 jni_env->ThrowNew( c, cstr.getStr() );
179 return nullptr;
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */