Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / javaunohelper / source / javaunohelper.cxx
blobd18979b7381d5a79c2675b872b6dfd247a0c9b7b
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>
23 #include <osl/module.hxx>
24 #include <osl/thread.h>
26 #include <uno/environment.hxx>
27 #include <uno/lbnames.h>
28 #include <uno/mapping.hxx>
30 #include <cppuhelper/factory.hxx>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/registry/XRegistryKey.hpp>
37 #include "jni.h"
38 #include "jvmaccess/virtualmachine.hxx"
39 #include "jvmaccess/unovirtualmachine.hxx"
41 #include "juhx-export-functions.hxx"
42 #include "vm.hxx"
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
48 * Class: com_sun_star_comp_helper_SharedLibraryLoader
49 * Method: component_writeInfo
50 * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z
52 jboolean Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
53 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jobject jSMgr,
54 jobject jRegKey, jobject loader )
56 bool bRet = false;
58 const jchar* pJLibName = pJEnv->GetStringChars(jLibName, nullptr);
59 OUString aLibName(reinterpret_cast<sal_Unicode const *>(pJLibName));
60 pJEnv->ReleaseStringChars(jLibName, pJLibName);
62 #ifdef DISABLE_DYNLOADING
63 (void) jSMgr;
64 (void) jRegKey;
65 (void) loader;
67 fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(aLibName, osl_getThreadTextEncoding()).getStr());
68 #else
69 osl::Module lib(aLibName, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
70 if (lib.is())
72 // ========================= LATEST VERSION =========================
73 oslGenericFunction pSym = lib.getFunctionSymbol(COMPONENT_GETENV);
74 if (pSym)
76 Environment java_env, loader_env;
78 const sal_Char * pEnvTypeName = nullptr;
79 (*reinterpret_cast<component_getImplementationEnvironmentFunc>(pSym))(
80 &pEnvTypeName, reinterpret_cast<uno_Environment **>(&loader_env) );
81 if (! loader_env.is())
83 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
84 uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, nullptr );
87 // create vm access
88 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
89 ::javaunohelper::create_vm_access( pJEnv, loader ) );
90 OUString java_env_name = UNO_LB_JAVA;
91 uno_getEnvironment(
92 reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() );
94 pSym = lib.getFunctionSymbol(COMPONENT_WRITEINFO);
95 if (pSym)
97 if (loader_env.is() && java_env.is())
99 Mapping java2dest(java_env.get(), loader_env.get());
101 if ( java2dest.is() )
103 void * pSMgr =
104 java2dest.mapInterface(
105 jSMgr, cppu::UnoType<lang::XMultiServiceFactory>::get());
106 void * pKey =
107 java2dest.mapInterface(
108 jRegKey, cppu::UnoType<registry::XRegistryKey>::get());
110 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
111 if (pKey)
113 bRet = (*reinterpret_cast<component_writeInfoFunc>(pSym))( pSMgr, pKey );
115 if (env)
116 (*env->releaseInterface)( env, pKey );
119 if (pSMgr && env)
120 (*env->releaseInterface)( env, pSMgr );
125 lib.release();
127 #endif
128 return bRet ? JNI_TRUE : JNI_FALSE;
132 * Class: com_sun_star_comp_helper_SharedLibraryLoader
133 * Method: component_getFactory
134 * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object;
136 jobject Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
137 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jstring jImplName,
138 jobject jSMgr, jobject jRegKey, jobject loader )
140 const jchar* pJLibName = pJEnv->GetStringChars(jLibName, nullptr);
141 OUString aLibName(reinterpret_cast<sal_Unicode const *>(pJLibName));
142 pJEnv->ReleaseStringChars(jLibName, pJLibName);
144 #ifdef DISABLE_DYNLOADING
145 (void) jImplName;
146 (void) jSMgr;
147 (void) jRegKey;
148 (void) loader;
150 fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(aLibName, osl_getThreadTextEncoding()).getStr());
151 #endif
153 aLibName += SAL_DLLEXTENSION;
155 jobject joSLL_cpp = nullptr;
157 #ifndef DISABLE_DYNLOADING
158 osl::Module lib(aLibName, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
159 if (lib.is())
161 // ========================= LATEST VERSION =========================
162 oslGenericFunction pSym = lib.getFunctionSymbol(COMPONENT_GETENV);
163 if (pSym)
165 Environment java_env, loader_env;
167 const sal_Char * pEnvTypeName = nullptr;
168 (*reinterpret_cast<component_getImplementationEnvironmentFunc>(pSym))(
169 &pEnvTypeName, reinterpret_cast<uno_Environment **>(&loader_env) );
171 if (! loader_env.is())
173 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
174 uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, nullptr );
177 // create vm access
178 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
179 ::javaunohelper::create_vm_access( pJEnv, loader ) );
180 OUString java_env_name = UNO_LB_JAVA;
181 uno_getEnvironment(
182 reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() );
184 pSym = lib.getFunctionSymbol(COMPONENT_GETFACTORY);
185 if (pSym)
187 if (loader_env.is() && java_env.is())
189 Mapping java2dest( java_env.get(), loader_env.get() );
190 Mapping dest2java( loader_env.get(), java_env.get() );
192 if (dest2java.is() && java2dest.is())
194 void * pSMgr =
195 java2dest.mapInterface(
196 jSMgr, cppu::UnoType<lang::XMultiServiceFactory>::get());
197 void * pKey =
198 java2dest.mapInterface(
199 jRegKey, cppu::UnoType<registry::XRegistryKey>::get());
201 const char* pImplName = pJEnv->GetStringUTFChars( jImplName, nullptr );
203 void * pSSF = (*reinterpret_cast<component_getFactoryFunc>(pSym))(
204 pImplName, pSMgr, pKey );
206 pJEnv->ReleaseStringUTFChars( jImplName, pImplName );
208 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
210 if (pKey && env)
211 (*env->releaseInterface)( env, pKey );
212 if (pSMgr && env)
213 (*env->releaseInterface)( env, pSMgr );
215 if (pSSF)
217 jobject jglobal = static_cast<jobject>(dest2java.mapInterface(
218 pSSF, cppu::UnoType<XInterface>::get()));
219 joSLL_cpp = pJEnv->NewLocalRef( jglobal );
220 pJEnv->DeleteGlobalRef( jglobal );
221 if (env)
222 (*env->releaseInterface)( env, pSSF );
228 lib.release();
230 #endif
231 return joSLL_cpp;
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */