Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / javaunohelper / source / javaunohelper.cxx
blob823d069d769eb2d30c53648408b0405a36f02ce8
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/module.hxx>
24 #include <uno/environment.hxx>
25 #include <uno/lbnames.h>
26 #include <uno/mapping.hxx>
28 #include <cppuhelper/factory.hxx>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/registry/XRegistryKey.hpp>
33 #include <jni.h>
34 #include <jvmaccess/unovirtualmachine.hxx>
36 #include "juhx-export-functions.hxx"
37 #include "vm.hxx"
39 #ifdef DISABLE_DYNLOADING
40 #include <osl/thread.h>
41 #endif
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
47 * Class: com_sun_star_comp_helper_SharedLibraryLoader
48 * Method: component_writeInfo
49 * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z
51 jboolean Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
52 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jobject jSMgr,
53 jobject jRegKey, jobject loader )
55 bool bRet = false;
57 const jchar* pJLibName = pJEnv->GetStringChars(jLibName, nullptr);
58 OUString aLibName(reinterpret_cast<sal_Unicode const *>(pJLibName));
59 pJEnv->ReleaseStringChars(jLibName, pJLibName);
61 #ifdef DISABLE_DYNLOADING
62 (void) jSMgr;
63 (void) jRegKey;
64 (void) loader;
66 fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(aLibName, osl_getThreadTextEncoding()).getStr());
67 #else
68 osl::Module lib(aLibName, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
69 if (lib.is())
71 // ========================= LATEST VERSION =========================
72 oslGenericFunction pSym = lib.getFunctionSymbol(COMPONENT_GETENV);
73 if (pSym)
75 Environment java_env, loader_env;
77 const sal_Char * pEnvTypeName = nullptr;
78 (*reinterpret_cast<component_getImplementationEnvironmentFunc>(pSym))(
79 &pEnvTypeName, reinterpret_cast<uno_Environment **>(&loader_env) );
80 if (! loader_env.is())
82 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
83 uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, nullptr );
86 // create vm access
87 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
88 ::javaunohelper::create_vm_access( pJEnv, loader ) );
89 OUString java_env_name = UNO_LB_JAVA;
90 uno_getEnvironment(
91 reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() );
93 pSym = lib.getFunctionSymbol(COMPONENT_WRITEINFO);
94 if (pSym)
96 if (loader_env.is() && java_env.is())
98 Mapping java2dest(java_env.get(), loader_env.get());
100 if ( java2dest.is() )
102 void * pSMgr =
103 java2dest.mapInterface(
104 jSMgr, cppu::UnoType<lang::XMultiServiceFactory>::get());
105 void * pKey =
106 java2dest.mapInterface(
107 jRegKey, cppu::UnoType<registry::XRegistryKey>::get());
109 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
110 if (pKey)
112 bRet = (*reinterpret_cast<component_writeInfoFunc>(pSym))( pSMgr, pKey );
114 if (env)
115 (*env->releaseInterface)( env, pKey );
118 if (pSMgr && env)
119 (*env->releaseInterface)( env, pSMgr );
124 lib.release();
126 #endif
127 return bRet;
131 * Class: com_sun_star_comp_helper_SharedLibraryLoader
132 * Method: component_getFactory
133 * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object;
135 jobject Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
136 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jstring jImplName,
137 jobject jSMgr, jobject jRegKey, jobject loader )
139 const jchar* pJLibName = pJEnv->GetStringChars(jLibName, nullptr);
140 OUString aLibName(reinterpret_cast<sal_Unicode const *>(pJLibName));
141 pJEnv->ReleaseStringChars(jLibName, pJLibName);
143 #ifdef DISABLE_DYNLOADING
144 (void) jImplName;
145 (void) jSMgr;
146 (void) jRegKey;
147 (void) loader;
149 fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(aLibName, osl_getThreadTextEncoding()).getStr());
150 #endif
152 aLibName += SAL_DLLEXTENSION;
154 jobject joSLL_cpp = nullptr;
156 #ifndef DISABLE_DYNLOADING
157 osl::Module lib(aLibName, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
158 if (lib.is())
160 // ========================= LATEST VERSION =========================
161 oslGenericFunction pSym = lib.getFunctionSymbol(COMPONENT_GETENV);
162 if (pSym)
164 Environment java_env, loader_env;
166 const sal_Char * pEnvTypeName = nullptr;
167 (*reinterpret_cast<component_getImplementationEnvironmentFunc>(pSym))(
168 &pEnvTypeName, reinterpret_cast<uno_Environment **>(&loader_env) );
170 if (! loader_env.is())
172 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
173 uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, nullptr );
176 // create vm access
177 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
178 ::javaunohelper::create_vm_access( pJEnv, loader ) );
179 OUString java_env_name = UNO_LB_JAVA;
180 uno_getEnvironment(
181 reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() );
183 pSym = lib.getFunctionSymbol(COMPONENT_GETFACTORY);
184 if (pSym)
186 if (loader_env.is() && java_env.is())
188 Mapping java2dest( java_env.get(), loader_env.get() );
189 Mapping dest2java( loader_env.get(), java_env.get() );
191 if (dest2java.is() && java2dest.is())
193 void * pSMgr =
194 java2dest.mapInterface(
195 jSMgr, cppu::UnoType<lang::XMultiServiceFactory>::get());
196 void * pKey =
197 java2dest.mapInterface(
198 jRegKey, cppu::UnoType<registry::XRegistryKey>::get());
200 const char* pImplName = pJEnv->GetStringUTFChars( jImplName, nullptr );
202 void * pSSF = (*reinterpret_cast<component_getFactoryFunc>(pSym))(
203 pImplName, pSMgr, pKey );
205 pJEnv->ReleaseStringUTFChars( jImplName, pImplName );
207 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
209 if (pKey && env)
210 (*env->releaseInterface)( env, pKey );
211 if (pSMgr && env)
212 (*env->releaseInterface)( env, pSMgr );
214 if (pSSF)
216 jobject jglobal = static_cast<jobject>(dest2java.mapInterface(
217 pSSF, cppu::UnoType<XInterface>::get()));
218 joSLL_cpp = pJEnv->NewLocalRef( jglobal );
219 pJEnv->DeleteGlobalRef( jglobal );
220 if (env)
221 (*env->releaseInterface)( env, pSSF );
227 lib.release();
229 #endif
230 return joSLL_cpp;
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */