Basic compiler undetected typo
[LibreOffice.git] / javaunohelper / source / bootstrap.cxx
blobcec9d500ba9cba42b7babcd0ae6ee18694aa529c
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>
47 #include <tools/diagnose_ex.h>
49 #include "juhx-export-functions.hxx"
50 #include "vm.hxx"
52 using namespace ::com::sun::star;
53 using namespace ::com::sun::star::uno;
55 namespace javaunohelper
58 static OUString jstring_to_oustring( jstring jstr, JNIEnv * jni_env )
60 OSL_ASSERT( sizeof (sal_Unicode) == sizeof (jchar) );
61 jsize len = jni_env->GetStringLength( jstr );
62 rtl_uString * ustr =
63 static_cast<rtl_uString *>(std::malloc( sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ));
64 assert(ustr);
65 jni_env->GetStringRegion( jstr, 0, len, reinterpret_cast<jchar *>(ustr->buffer) );
66 OSL_ASSERT( !jni_env->ExceptionCheck() );
67 ustr->refCount = 1;
68 ustr->length = len;
69 ustr->buffer[ len ] = '\0';
70 return OUString( ustr, SAL_NO_ACQUIRE );
76 jobject Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
77 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jclass, jstring juno_rc, jobjectArray jpairs,
78 jobject loader )
80 try
82 if (nullptr != jpairs)
84 jsize nPos = 0, len = jni_env->GetArrayLength( jpairs );
85 while (nPos < len)
87 // name
88 jstring jstr = static_cast<jstring>(jni_env->GetObjectArrayElement( jpairs, nPos ));
89 if (jni_env->ExceptionCheck())
91 jni_env->ExceptionClear();
92 throw RuntimeException( "index out of bounds?!" );
94 if (nullptr != jstr)
96 OUString name( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
97 // value
98 jstr = static_cast<jstring>(jni_env->GetObjectArrayElement( jpairs, nPos +1 ));
99 if (jni_env->ExceptionCheck())
101 jni_env->ExceptionClear();
102 throw RuntimeException( "index out of bounds?!" );
104 if (nullptr != jstr)
106 OUString value( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
108 // set bootstrap parameter
109 ::rtl::Bootstrap::set( name, value );
112 nPos += 2;
116 // bootstrap uno
117 Reference< XComponentContext > xContext;
118 if (nullptr == juno_rc)
120 xContext = ::cppu::defaultBootstrap_InitialComponentContext();
122 else
124 OUString uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc, jni_env ) );
125 xContext = ::cppu::defaultBootstrap_InitialComponentContext( uno_rc );
128 // create vm access
129 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
130 ::javaunohelper::create_vm_access( jni_env, loader ) );
131 // wrap vm singleton entry
132 xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
134 // get uno envs
135 OUString cpp_env_name = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
136 OUString java_env_name = UNO_LB_JAVA;
137 Environment java_env, cpp_env;
138 uno_getEnvironment(reinterpret_cast<uno_Environment **>(&cpp_env), cpp_env_name.pData, nullptr);
139 uno_getEnvironment(reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() );
141 // map to java
142 Mapping mapping( cpp_env.get(), java_env.get() );
143 if (! mapping.is())
145 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
146 if (xComp.is())
147 xComp->dispose();
148 throw RuntimeException("cannot get mapping C++ <-> Java!" );
151 jobject jret = static_cast<jobject>(mapping.mapInterface( xContext.get(), cppu::UnoType<decltype(xContext)>::get() ));
152 jobject jlocal = jni_env->NewLocalRef( jret );
153 jni_env->DeleteGlobalRef( jret );
155 return jlocal;
157 catch (const RuntimeException & exc)
159 css::uno::Any exAny( cppu::getCaughtException() );
160 jclass c = jni_env->FindClass( "com/sun/star/uno/RuntimeException" );
161 if (nullptr != c)
163 SAL_WARN("javaunohelper", "forwarding RuntimeException: " << exceptionToString(exAny) );
164 OString cstr( OUStringToOString(
165 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
166 jni_env->ThrowNew( c, cstr.getStr() );
169 catch (const Exception & exc)
171 css::uno::Any ex( cppu::getCaughtException() );
172 jclass c = jni_env->FindClass( "com/sun/star/uno/Exception" );
173 if (nullptr != c)
175 SAL_WARN("javaunohelper", "forwarding Exception: " << exceptionToString(ex) );
176 OString cstr( OUStringToOString(
177 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
178 jni_env->ThrowNew( c, cstr.getStr() );
182 return nullptr;
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */