diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
commit | 267c6f2ac71f92999e969232431ba04678e7437e (patch) | |
tree | 358c9467650e1d0a1d7227a21dac2e3d08b622b2 /javaunohelper/source | |
parent | Initial commit. (diff) | |
download | libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip |
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'javaunohelper/source')
-rw-r--r-- | javaunohelper/source/bootstrap.cxx | 182 | ||||
-rw-r--r-- | javaunohelper/source/javaunohelper.cxx | 241 | ||||
-rw-r--r-- | javaunohelper/source/juhx-export-functions.hxx | 44 | ||||
-rw-r--r-- | javaunohelper/source/juhx-export-types.hxx | 52 | ||||
-rw-r--r-- | javaunohelper/source/preload.cxx | 130 | ||||
-rw-r--r-- | javaunohelper/source/vm.cxx | 114 | ||||
-rw-r--r-- | javaunohelper/source/vm.hxx | 55 |
7 files changed, 818 insertions, 0 deletions
diff --git a/javaunohelper/source/bootstrap.cxx b/javaunohelper/source/bootstrap.cxx new file mode 100644 index 0000000000..1cacea950c --- /dev/null +++ b/javaunohelper/source/bootstrap.cxx @@ -0,0 +1,182 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> +#include <sal/log.hxx> + +#include <osl/diagnose.h> + +#include <rtl/bootstrap.hxx> +#include <rtl/string.hxx> + +#include <uno/lbnames.h> +#include <uno/mapping.hxx> +#include <uno/environment.hxx> + +#include <cppuhelper/bootstrap.hxx> + +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> + +#if defined __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-attributes" +#endif +#include <jni.h> +#if defined __clang__ +#pragma clang diagnostic pop +#endif + +#include <jvmaccess/unovirtualmachine.hxx> + +#include "juhx-export-functions.hxx" +#include "vm.hxx" + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; + +namespace javaunohelper +{ + +static OUString jstring_to_oustring( jstring jstr, JNIEnv * jni_env ) +{ + OSL_ASSERT( sizeof (sal_Unicode) == sizeof (jchar) ); + jsize len = jni_env->GetStringLength( jstr ); + rtl_uString * ustr = + static_cast<rtl_uString *>(std::malloc( sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) )); + assert(ustr); + jni_env->GetStringRegion( jstr, 0, len, reinterpret_cast<jchar *>(ustr->buffer) ); + OSL_ASSERT( !jni_env->ExceptionCheck() ); + ustr->refCount = 1; + ustr->length = len; + ustr->buffer[ len ] = '\0'; + return OUString( ustr, SAL_NO_ACQUIRE ); +} + +} + + +jobject Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap( + JNIEnv * jni_env, SAL_UNUSED_PARAMETER jclass, jstring juno_rc, jobjectArray jpairs, + jobject loader ) +{ + try + { + if (nullptr != jpairs) + { + jsize nPos = 0, len = jni_env->GetArrayLength( jpairs ); + while (nPos < len) + { + // name + jstring jstr = static_cast<jstring>(jni_env->GetObjectArrayElement( jpairs, nPos )); + if (jni_env->ExceptionCheck()) + { + jni_env->ExceptionClear(); + throw RuntimeException( "index out of bounds?!" ); + } + if (nullptr != jstr) + { + OUString name( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) ); + // value + jstr = static_cast<jstring>(jni_env->GetObjectArrayElement( jpairs, nPos +1 )); + if (jni_env->ExceptionCheck()) + { + jni_env->ExceptionClear(); + throw RuntimeException( "index out of bounds?!" ); + } + if (nullptr != jstr) + { + OUString value( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) ); + + // set bootstrap parameter + ::rtl::Bootstrap::set( name, value ); + } + } + nPos += 2; + } + } + + // bootstrap uno + Reference< XComponentContext > xContext; + if (nullptr == juno_rc) + { + xContext = ::cppu::defaultBootstrap_InitialComponentContext(); + } + else + { + OUString uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc, jni_env ) ); + xContext = ::cppu::defaultBootstrap_InitialComponentContext( uno_rc ); + } + + // create vm access + ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access( + ::javaunohelper::create_vm_access( jni_env, loader ) ); + // wrap vm singleton entry + xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access ); + + // get uno envs + OUString cpp_env_name = CPPU_CURRENT_LANGUAGE_BINDING_NAME; + OUString java_env_name = UNO_LB_JAVA; + Environment java_env, cpp_env; + uno_getEnvironment(reinterpret_cast<uno_Environment **>(&cpp_env), cpp_env_name.pData, nullptr); + uno_getEnvironment(reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() ); + + // map to java + Mapping mapping( cpp_env.get(), java_env.get() ); + if (! mapping.is()) + { + Reference< lang::XComponent > xComp( xContext, UNO_QUERY ); + if (xComp.is()) + xComp->dispose(); + throw RuntimeException("cannot get mapping C++ <-> Java!" ); + } + + jobject jret = static_cast<jobject>(mapping.mapInterface( xContext.get(), cppu::UnoType<decltype(xContext)>::get() )); + jobject jlocal = jni_env->NewLocalRef( jret ); + jni_env->DeleteGlobalRef( jret ); + + return jlocal; + } + catch (const RuntimeException & exc) + { + jclass c = jni_env->FindClass( "com/sun/star/uno/RuntimeException" ); + if (nullptr != c) + { + SAL_WARN("javaunohelper", "forwarding RuntimeException: " << exc ); + OString cstr( OUStringToOString( + exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) ); + jni_env->ThrowNew( c, cstr.getStr() ); + } + } + catch (const Exception & exc) + { + jclass c = jni_env->FindClass( "com/sun/star/uno/Exception" ); + if (nullptr != c) + { + SAL_WARN("javaunohelper", "forwarding Exception: " << exc ); + OString cstr( OUStringToOString( + exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) ); + jni_env->ThrowNew( c, cstr.getStr() ); + } + } + + return nullptr; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/javaunohelper/source/javaunohelper.cxx b/javaunohelper/source/javaunohelper.cxx new file mode 100644 index 0000000000..8db57c5b78 --- /dev/null +++ b/javaunohelper/source/javaunohelper.cxx @@ -0,0 +1,241 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <osl/module.hxx> + +#include <uno/environment.hxx> +#include <uno/lbnames.h> +#include <uno/mapping.hxx> + +#include <cppuhelper/factory.hxx> + +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/registry/XRegistryKey.hpp> + +#if defined __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-attributes" +#endif +#include <jni.h> +#if defined __clang__ +#pragma clang diagnostic pop +#endif + +#include <jvmaccess/unovirtualmachine.hxx> + +#include "juhx-export-functions.hxx" +#include "vm.hxx" + +#ifdef DISABLE_DYNLOADING +#include <osl/thread.h> +#endif + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; + +/* + * Class: com_sun_star_comp_helper_SharedLibraryLoader + * Method: component_writeInfo + * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z + */ +jboolean Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo( + JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jobject jSMgr, + jobject jRegKey, jobject loader ) +{ + bool bRet = false; + + const jchar* pJLibName = pJEnv->GetStringChars(jLibName, nullptr); + OUString aLibName(reinterpret_cast<sal_Unicode const *>(pJLibName)); + pJEnv->ReleaseStringChars(jLibName, pJLibName); + +#ifdef DISABLE_DYNLOADING + (void) jSMgr; + (void) jRegKey; + (void) loader; + + fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(aLibName, osl_getThreadTextEncoding()).getStr()); +#else + osl::Module lib(aLibName, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL); + if (lib.is()) + { + // ========================= LATEST VERSION ========================= + oslGenericFunction pSym = lib.getFunctionSymbol(COMPONENT_GETENV); + if (pSym) + { + Environment java_env, loader_env; + + const char * pEnvTypeName = nullptr; + (*reinterpret_cast<component_getImplementationEnvironmentFunc>(pSym))( + &pEnvTypeName, reinterpret_cast<uno_Environment **>(&loader_env) ); + if (! loader_env.is()) + { + OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) ); + uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, nullptr ); + } + + // create vm access + ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access( + ::javaunohelper::create_vm_access( pJEnv, loader ) ); + OUString java_env_name = UNO_LB_JAVA; + uno_getEnvironment( + reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() ); + + pSym = lib.getFunctionSymbol(COMPONENT_WRITEINFO); + if (pSym) + { + if (loader_env.is() && java_env.is()) + { + Mapping java2dest(java_env.get(), loader_env.get()); + + if ( java2dest.is() ) + { + void * pSMgr = + java2dest.mapInterface( + jSMgr, cppu::UnoType<lang::XMultiServiceFactory>::get()); + void * pKey = + java2dest.mapInterface( + jRegKey, cppu::UnoType<registry::XRegistryKey>::get()); + + uno_ExtEnvironment * env = loader_env.get()->pExtEnv; + if (pKey) + { + bRet = (*reinterpret_cast<component_writeInfoFunc>(pSym))( pSMgr, pKey ); + + if (env) + (*env->releaseInterface)( env, pKey ); + } + + if (pSMgr && env) + (*env->releaseInterface)( env, pSMgr ); + } + } + } + } + lib.release(); + } +#endif + return bRet; +} + +/* + * Class: com_sun_star_comp_helper_SharedLibraryLoader + * Method: component_getFactory + * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object; + */ +jobject Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory( + JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jstring jImplName, + jobject jSMgr, jobject jRegKey, jobject loader ) +{ + const jchar* pJLibName = pJEnv->GetStringChars(jLibName, nullptr); + OUString aLibName(reinterpret_cast<sal_Unicode const *>(pJLibName)); + pJEnv->ReleaseStringChars(jLibName, pJLibName); + +#ifdef DISABLE_DYNLOADING + (void) jImplName; + (void) jSMgr; + (void) jRegKey; + (void) loader; + + fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(aLibName, osl_getThreadTextEncoding()).getStr()); +#endif + + aLibName += SAL_DLLEXTENSION; + + jobject joSLL_cpp = nullptr; + +#ifndef DISABLE_DYNLOADING + osl::Module lib(aLibName, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL); + if (lib.is()) + { + // ========================= LATEST VERSION ========================= + oslGenericFunction pSym = lib.getFunctionSymbol(COMPONENT_GETENV); + if (pSym) + { + Environment java_env, loader_env; + + const char * pEnvTypeName = nullptr; + (*reinterpret_cast<component_getImplementationEnvironmentFunc>(pSym))( + &pEnvTypeName, reinterpret_cast<uno_Environment **>(&loader_env) ); + + if (! loader_env.is()) + { + OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) ); + uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, nullptr ); + } + + // create vm access + ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access( + ::javaunohelper::create_vm_access( pJEnv, loader ) ); + OUString java_env_name = UNO_LB_JAVA; + uno_getEnvironment( + reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() ); + + pSym = lib.getFunctionSymbol(COMPONENT_GETFACTORY); + if (pSym) + { + if (loader_env.is() && java_env.is()) + { + Mapping java2dest( java_env.get(), loader_env.get() ); + Mapping dest2java( loader_env.get(), java_env.get() ); + + if (dest2java.is() && java2dest.is()) + { + void * pSMgr = + java2dest.mapInterface( + jSMgr, cppu::UnoType<lang::XMultiServiceFactory>::get()); + void * pKey = + java2dest.mapInterface( + jRegKey, cppu::UnoType<registry::XRegistryKey>::get()); + + const char* pImplName = pJEnv->GetStringUTFChars( jImplName, nullptr ); + + void * pSSF = (*reinterpret_cast<component_getFactoryFunc>(pSym))( + pImplName, pSMgr, pKey ); + + pJEnv->ReleaseStringUTFChars( jImplName, pImplName ); + + uno_ExtEnvironment * env = loader_env.get()->pExtEnv; + + if (pKey && env) + (*env->releaseInterface)( env, pKey ); + if (pSMgr && env) + (*env->releaseInterface)( env, pSMgr ); + + if (pSSF) + { + jobject jglobal = static_cast<jobject>(dest2java.mapInterface( + pSSF, cppu::UnoType<XInterface>::get())); + joSLL_cpp = pJEnv->NewLocalRef( jglobal ); + pJEnv->DeleteGlobalRef( jglobal ); + if (env) + (*env->releaseInterface)( env, pSSF ); + } + } + } + } + } + lib.release(); + } +#endif + return joSLL_cpp; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/javaunohelper/source/juhx-export-functions.hxx b/javaunohelper/source/juhx-export-functions.hxx new file mode 100644 index 0000000000..569822f2c5 --- /dev/null +++ b/javaunohelper/source/juhx-export-functions.hxx @@ -0,0 +1,44 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_JAVAUNOHELPER_SOURCE_JUHX_EXPORT_FUNCTIONS_HXX +#define INCLUDED_JAVAUNOHELPER_SOURCE_JUHX_EXPORT_FUNCTIONS_HXX + +#include <sal/config.h> + +#include <sal/types.h> + +#include "juhx-export-types.hxx" + +extern "C" { + +SAL_JNI_EXPORT javaunohelper::detail::Func_bootstrap +Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap; + +SAL_JNI_EXPORT javaunohelper::detail::Func_getFactory +Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory; + +SAL_JNI_EXPORT javaunohelper::detail::Func_writeInfo +Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo; + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/javaunohelper/source/juhx-export-types.hxx b/javaunohelper/source/juhx-export-types.hxx new file mode 100644 index 0000000000..8e27834522 --- /dev/null +++ b/javaunohelper/source/juhx-export-types.hxx @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_JAVAUNOHELPER_SOURCE_JUHX_EXPORT_TYPES_HXX +#define INCLUDED_JAVAUNOHELPER_SOURCE_JUHX_EXPORT_TYPES_HXX + +#include <sal/config.h> + +#include <jni.h> + +#if defined DISABLE_DYNLOADING +#define JAVAUNOHELPER_DETAIL_CALLCONV JNICALL +#else +#define JAVAUNOHELPER_DETAIL_CALLCONV +#endif + +extern "C" { + +namespace javaunohelper::detail { + +typedef jobject JAVAUNOHELPER_DETAIL_CALLCONV Func_bootstrap( + JNIEnv *_env, jclass, jstring, jobjectArray, jobject); + +typedef jobject JAVAUNOHELPER_DETAIL_CALLCONV Func_getFactory( + JNIEnv *, jclass, jstring, jstring, jobject, jobject, jobject); + +typedef jboolean JAVAUNOHELPER_DETAIL_CALLCONV Func_writeInfo( + JNIEnv *, jclass, jstring, jobject, jobject, jobject); + +} + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/javaunohelper/source/preload.cxx b/javaunohelper/source/preload.cxx new file mode 100644 index 0000000000..f73b44b346 --- /dev/null +++ b/javaunohelper/source/preload.cxx @@ -0,0 +1,130 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#if defined __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-attributes" +#endif +#include <jni.h> +#if defined __clang__ +#pragma clang diagnostic pop +#endif + +#include <rtl/ustring.hxx> +#include <osl/module.hxx> + +#include "juhx-export-types.hxx" + +// In retrospect, the reason to create a juh wrapper around the juhx library was +// probably because java.lang.System.loadLibrary uses RTLD_LOCAL, so uniqueness +// of GCC RTTI symbols needed for exception handling would not be guaranteed. + +#if ! defined SAL_DLLPREFIX +#define SAL_DLLPREFIX "" +#endif + + +extern "C" +{ + +static javaunohelper::detail::Func_writeInfo * s_writeInfo; +static javaunohelper::detail::Func_getFactory * s_getFactory; +static javaunohelper::detail::Func_bootstrap * s_bootstrap; +static bool s_inited = false; + +extern "C" { static void thisModule() {} } + + +static bool inited_juhx( JNIEnv * jni_env ) +{ + if (s_inited) + return true; + osl::Module aModule; + if (!aModule.loadRelative(&thisModule, SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL)) + { + jclass c = jni_env->FindClass( "java/lang/RuntimeException" ); + jni_env->ThrowNew( + c, "error loading " SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION "!" ); + return false; + } + else + { + OUString symbol = + "Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo"; + s_writeInfo = reinterpret_cast<javaunohelper::detail::Func_writeInfo *>(aModule.getFunctionSymbol(symbol)); + symbol = + "Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory"; + s_getFactory = reinterpret_cast<javaunohelper::detail::Func_getFactory *>(aModule.getFunctionSymbol(symbol)); + symbol = + "Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap"; + s_bootstrap = + reinterpret_cast<javaunohelper::detail::Func_bootstrap *>(aModule.getFunctionSymbol(symbol)); + + if (nullptr == s_writeInfo || + nullptr == s_getFactory || + nullptr == s_bootstrap) + { + jclass c = jni_env->FindClass( "java/lang/RuntimeException" ); + jni_env->ThrowNew( + c, "error resolving symbols of " SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION "!" ); + return false; + } + aModule.release(); + } + s_inited = true; + return true; +} + + +SAL_DLLPUBLIC_EXPORT jboolean JNICALL +Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo( + JNIEnv * pJEnv, jclass jClass, jstring jLibName, jobject jSMgr, + jobject jRegKey, jobject loader ) +{ + if (inited_juhx( pJEnv )) + return (*s_writeInfo)( + pJEnv, jClass, jLibName, jSMgr, jRegKey, loader ); + return JNI_FALSE; +} + +SAL_DLLPUBLIC_EXPORT jobject JNICALL +Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory( + JNIEnv * pJEnv, jclass jClass, jstring jLibName, jstring jImplName, + jobject jSMgr, jobject jRegKey, jobject loader ) +{ + if (inited_juhx( pJEnv )) + return (*s_getFactory)( + pJEnv, jClass, jLibName, jImplName, jSMgr, jRegKey, loader ); + return nullptr; +} + +SAL_DLLPUBLIC_EXPORT jobject JNICALL +Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap( + JNIEnv * jni_env, jclass jClass, jstring juno_rc, jobjectArray jpairs, + jobject loader ) +{ + if (inited_juhx( jni_env )) + return (*s_bootstrap)( jni_env, jClass, juno_rc, jpairs, loader ); + return nullptr; +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/javaunohelper/source/vm.cxx b/javaunohelper/source/vm.cxx new file mode 100644 index 0000000000..7191e58602 --- /dev/null +++ b/javaunohelper/source/vm.cxx @@ -0,0 +1,114 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <sal/config.h> + +#include "vm.hxx" + +#include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/lang/XSingleComponentFactory.hpp> +#include <cppuhelper/compbase.hxx> +#include <cppuhelper/component_context.hxx> +#include <cppuhelper/basemutex.hxx> +#include <jvmaccess/virtualmachine.hxx> +#include <jvmaccess/unovirtualmachine.hxx> +#include <utility> + +namespace { + +typedef ::cppu::WeakComponentImplHelper< + css::lang::XSingleComponentFactory > t_impl; + +class SingletonFactory : public cppu::BaseMutex, public t_impl +{ + ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > m_vm_access; + +protected: + virtual void SAL_CALL disposing() override; + +public: + explicit SingletonFactory( ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access ) + : t_impl( m_aMutex ), + m_vm_access(std::move( vm_access )) + {} + + // XSingleComponentFactory impl + virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext( + css::uno::Reference< css::uno::XComponentContext > const & xContext ) override; + virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext( + css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext ) override; +}; + +void SingletonFactory::disposing() +{ + m_vm_access.clear(); +} + +css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithContext( + css::uno::Reference< css::uno::XComponentContext > const & xContext ) +{ + sal_Int64 handle = reinterpret_cast< sal_Int64 >( m_vm_access.get() ); + css::uno::Any arg( css::beans::NamedValue( "UnoVirtualMachine", css::uno::Any( handle ) ) ); + return xContext->getServiceManager()->createInstanceWithArgumentsAndContext( + "com.sun.star.java.JavaVirtualMachine", + css::uno::Sequence< css::uno::Any >( &arg, 1 ), xContext ); +} + +css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithArgumentsAndContext( + css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext ) +{ + return xContext->getServiceManager()->createInstanceWithArgumentsAndContext( + "com.sun.star.java.JavaVirtualMachine", + args, xContext ); +} + +} + +namespace javaunohelper { + +::rtl::Reference< ::jvmaccess::UnoVirtualMachine > create_vm_access( + JNIEnv * jni_env, jobject loader ) +{ + JavaVM * vm; + jni_env->GetJavaVM( &vm ); + try { + return new ::jvmaccess::UnoVirtualMachine( + new ::jvmaccess::VirtualMachine( + vm, JNI_VERSION_1_2, false, jni_env ), + loader ); + } catch ( ::jvmaccess::UnoVirtualMachine::CreationException & ) { + throw css::uno::RuntimeException( "jvmaccess::UnoVirtualMachine::CreationException occurred" ); + } +} + +css::uno::Reference< css::uno::XComponentContext > install_vm_singleton( + css::uno::Reference< css::uno::XComponentContext > const & xContext, + ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access ) +{ + css::uno::Reference< css::lang::XSingleComponentFactory > xFac( new SingletonFactory( vm_access ) ); + ::cppu::ContextEntry_Init entry( + "/singletons/com.sun.star.java.theJavaVirtualMachine", + css::uno::Any( xFac ), true ); + return ::cppu::createComponentContext( &entry, 1, xContext ); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/javaunohelper/source/vm.hxx b/javaunohelper/source/vm.hxx new file mode 100644 index 0000000000..6681dfffac --- /dev/null +++ b/javaunohelper/source/vm.hxx @@ -0,0 +1,55 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_JAVAUNOHELPER_SOURCE_VM_HXX +#define INCLUDED_JAVAUNOHELPER_SOURCE_VM_HXX + +#include <sal/config.h> + +#if defined __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-attributes" +#endif +#include <jni.h> +#if defined __clang__ +#pragma clang diagnostic pop +#endif + +#include <com/sun/star/uno/Reference.hxx> +#include <rtl/ref.hxx> + +namespace com::sun::star::uno { class XComponentContext; } +namespace jvmaccess { class UnoVirtualMachine; } + +namespace javaunohelper { + +::rtl::Reference< ::jvmaccess::UnoVirtualMachine > create_vm_access( + JNIEnv * jni_env, jobject loader ); + +css::uno::Reference< css::uno::XComponentContext > +install_vm_singleton( + css::uno::Reference< css::uno::XComponentContext > + const & xContext, + ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access ); + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |