1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <osl/module.hxx>
23 
24 #include <uno/environment.hxx>
25 #include <uno/lbnames.h>
26 #include <uno/mapping.hxx>
27 
28 #include <cppuhelper/factory.hxx>
29 
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/registry/XRegistryKey.hpp>
32 
33 #include <jni.h>
34 #include <jvmaccess/unovirtualmachine.hxx>
35 
36 #include "juhx-export-functions.hxx"
37 #include "vm.hxx"
38 
39 #ifdef DISABLE_DYNLOADING
40 #include <osl/thread.h>
41 #endif
42 
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
45 
46 /*
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
50  */
Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(JNIEnv * pJEnv,SAL_UNUSED_PARAMETER jclass,jstring jLibName,jobject jSMgr,jobject jRegKey,jobject loader)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 )
54 {
55     bool bRet = false;
56 
57     const jchar* pJLibName = pJEnv->GetStringChars(jLibName, nullptr);
58     OUString aLibName(reinterpret_cast<sal_Unicode const *>(pJLibName));
59     pJEnv->ReleaseStringChars(jLibName, pJLibName);
60 
61 #ifdef DISABLE_DYNLOADING
62     (void) jSMgr;
63     (void) jRegKey;
64     (void) loader;
65 
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())
70     {
71         // ========================= LATEST VERSION =========================
72         oslGenericFunction pSym = lib.getFunctionSymbol(COMPONENT_GETENV);
73         if (pSym)
74         {
75             Environment java_env, loader_env;
76 
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())
81             {
82                 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
83                 uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, nullptr );
84             }
85 
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() );
92 
93             pSym = lib.getFunctionSymbol(COMPONENT_WRITEINFO);
94             if (pSym)
95             {
96                 if (loader_env.is() && java_env.is())
97                 {
98                     Mapping java2dest(java_env.get(), loader_env.get());
99 
100                     if ( java2dest.is() )
101                     {
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());
108 
109                         uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
110                         if (pKey)
111                         {
112                             bRet = (*reinterpret_cast<component_writeInfoFunc>(pSym))( pSMgr, pKey );
113 
114                             if (env)
115                                 (*env->releaseInterface)( env, pKey );
116                         }
117 
118                         if (pSMgr && env)
119                             (*env->releaseInterface)( env, pSMgr );
120                     }
121                 }
122             }
123         }
124         lib.release();
125     }
126 #endif
127     return bRet;
128 }
129 
130 /*
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;
134  */
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)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 )
138 {
139     const jchar* pJLibName = pJEnv->GetStringChars(jLibName, nullptr);
140     OUString aLibName(reinterpret_cast<sal_Unicode const *>(pJLibName));
141     pJEnv->ReleaseStringChars(jLibName, pJLibName);
142 
143 #ifdef DISABLE_DYNLOADING
144     (void) jImplName;
145     (void) jSMgr;
146     (void) jRegKey;
147     (void) loader;
148 
149     fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(aLibName, osl_getThreadTextEncoding()).getStr());
150 #endif
151 
152     aLibName += SAL_DLLEXTENSION;
153 
154     jobject joSLL_cpp = nullptr;
155 
156 #ifndef DISABLE_DYNLOADING
157     osl::Module lib(aLibName, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
158     if (lib.is())
159     {
160         // ========================= LATEST VERSION =========================
161         oslGenericFunction pSym = lib.getFunctionSymbol(COMPONENT_GETENV);
162         if (pSym)
163         {
164             Environment java_env, loader_env;
165 
166             const sal_Char * pEnvTypeName = nullptr;
167             (*reinterpret_cast<component_getImplementationEnvironmentFunc>(pSym))(
168                 &pEnvTypeName, reinterpret_cast<uno_Environment **>(&loader_env) );
169 
170             if (! loader_env.is())
171             {
172                 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
173                 uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, nullptr );
174             }
175 
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() );
182 
183             pSym = lib.getFunctionSymbol(COMPONENT_GETFACTORY);
184             if (pSym)
185             {
186                 if (loader_env.is() && java_env.is())
187                 {
188                     Mapping java2dest( java_env.get(), loader_env.get() );
189                     Mapping dest2java( loader_env.get(), java_env.get() );
190 
191                     if (dest2java.is() && java2dest.is())
192                     {
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());
199 
200                         const char* pImplName = pJEnv->GetStringUTFChars( jImplName, nullptr );
201 
202                         void * pSSF = (*reinterpret_cast<component_getFactoryFunc>(pSym))(
203                             pImplName, pSMgr, pKey );
204 
205                         pJEnv->ReleaseStringUTFChars( jImplName, pImplName );
206 
207                         uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
208 
209                         if (pKey && env)
210                             (*env->releaseInterface)( env, pKey );
211                         if (pSMgr && env)
212                             (*env->releaseInterface)( env, pSMgr );
213 
214                         if (pSSF)
215                         {
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 );
222                         }
223                     }
224                 }
225             }
226         }
227         lib.release();
228     }
229 #endif
230     return joSLL_cpp;
231 }
232 
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
234