1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_javaunohelper.hxx"
26
27 #include <osl/diagnose.h>
28 #include <osl/module.h>
29
30 #include <uno/environment.hxx>
31 #include <uno/mapping.hxx>
32
33 #include <cppuhelper/factory.hxx>
34 #include <cppuhelper/servicefactory.hxx>
35 #include <cppuhelper/component_context.hxx>
36
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/registry/XRegistryKey.hpp>
41
42 #include "jni.h"
43 #include "jvmaccess/virtualmachine.hxx"
44 #include "jvmaccess/unovirtualmachine.hxx"
45
46 #include "vm.hxx"
47
48 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
49
50
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using ::rtl::OString;
54 using ::rtl::OUString;
55
56 /*
57 * Class: com_sun_star_comp_helper_SharedLibraryLoader
58 * Method: component_writeInfo
59 * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z
60 */
61 extern "C" JNIEXPORT jboolean JNICALL
Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(JNIEnv * pJEnv,jclass,jstring jLibName,jobject jSMgr,jobject jRegKey,jobject loader)62 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
63 JNIEnv * pJEnv, jclass, jstring jLibName, jobject jSMgr,
64 jobject jRegKey, jobject loader )
65 {
66 sal_Bool bRet = sal_False;
67
68 const jchar* pJLibName = pJEnv->GetStringChars( jLibName, NULL );
69 OUString aLibName( pJLibName );
70 pJEnv->ReleaseStringChars( jLibName, pJLibName);
71
72 oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
73 if (lib)
74 {
75 // ========================= LATEST VERSION =========================
76 OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
77 oslGenericFunction pSym =
78 osl_getFunctionSymbol( lib, aGetEnvName.pData );
79 if (pSym)
80 {
81 Environment java_env, loader_env;
82
83 const sal_Char * pEnvTypeName = 0;
84 (*((component_getImplementationEnvironmentFunc)pSym))(
85 &pEnvTypeName, (uno_Environment **)&loader_env );
86 if (! loader_env.is())
87 {
88 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
89 uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
90 }
91
92 // create vm access
93 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
94 ::javaunohelper::create_vm_access( pJEnv, loader ) );
95 OUString java_env_name = OUSTR(UNO_LB_JAVA);
96 uno_getEnvironment(
97 (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
98
99 OUString aWriteInfoName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_WRITEINFO) );
100 pSym = osl_getFunctionSymbol( lib, aWriteInfoName.pData );
101 if (pSym)
102 {
103 if (loader_env.is() && java_env.is())
104 {
105 Mapping java2dest(java_env.get(), loader_env.get());
106
107 if ( java2dest.is() )
108 {
109 void * pSMgr =
110 java2dest.mapInterface(
111 jSMgr, getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
112 void * pKey =
113 java2dest.mapInterface(
114 jRegKey, getCppuType((Reference< registry::XRegistryKey > *) 0) );
115
116 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
117 if (pKey)
118 {
119 bRet = (*((component_writeInfoFunc)pSym))( pSMgr, pKey );
120
121 if (env)
122 (*env->releaseInterface)( env, pKey );
123 }
124
125 if (pSMgr && env)
126 (*env->releaseInterface)( env, pSMgr );
127 }
128 }
129 }
130 }
131 }
132
133 return bRet == sal_False? JNI_FALSE : JNI_TRUE;
134 }
135
136 /*
137 * Class: com_sun_star_comp_helper_SharedLibraryLoader
138 * Method: component_getFactory
139 * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object;
140 */
141 extern "C" JNIEXPORT jobject JNICALL
Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(JNIEnv * pJEnv,jclass,jstring jLibName,jstring jImplName,jobject jSMgr,jobject jRegKey,jobject loader)142 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
143 JNIEnv * pJEnv, jclass, jstring jLibName, jstring jImplName,
144 jobject jSMgr, jobject jRegKey, jobject loader )
145 {
146 const jchar* pJLibName = pJEnv->GetStringChars(jLibName, NULL);
147 OUString aLibName( pJLibName );
148 pJEnv->ReleaseStringChars( jLibName, pJLibName);
149
150 aLibName += OUString( RTL_CONSTASCII_USTRINGPARAM(SAL_DLLEXTENSION) );
151
152 jobject joSLL_cpp = 0;
153
154 oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
155 if (lib)
156 {
157 // ========================= LATEST VERSION =========================
158 OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
159 oslGenericFunction pSym =
160 osl_getFunctionSymbol( lib, aGetEnvName.pData );
161 if (pSym)
162 {
163 Environment java_env, loader_env;
164
165 const sal_Char * pEnvTypeName = 0;
166 (*((component_getImplementationEnvironmentFunc)pSym))(
167 &pEnvTypeName, (uno_Environment **)&loader_env );
168
169 if (! loader_env.is())
170 {
171 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
172 uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
173 }
174
175 // create vm access
176 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
177 ::javaunohelper::create_vm_access( pJEnv, loader ) );
178 OUString java_env_name = OUSTR(UNO_LB_JAVA);
179 uno_getEnvironment(
180 (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
181
182 OUString aGetFactoryName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETFACTORY) );
183 pSym = osl_getFunctionSymbol( lib, aGetFactoryName.pData );
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, ::getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
196 void * pKey =
197 java2dest.mapInterface(
198 jRegKey, ::getCppuType((Reference< registry::XRegistryKey > *) 0) );
199
200 const char* pImplName = pJEnv->GetStringUTFChars( jImplName, NULL );
201
202 void * pSSF = (*((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 = (jobject) dest2java.mapInterface(
217 pSSF, getCppuType((Reference< XInterface > *) 0) );
218 joSLL_cpp = pJEnv->NewLocalRef( jglobal );
219 pJEnv->DeleteGlobalRef( jglobal );
220 if (env)
221 (*env->releaseInterface)( env, pSSF );
222 }
223 }
224 }
225 }
226 }
227 }
228
229 return joSLL_cpp;
230 }
231
232 /*
233 * Class: com_sun_star_comp_helper_RegistryServiceFactory
234 * Method: createRegistryServiceFactory
235 * Signature: (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/Object;
236 */
237 extern "C" JNIEXPORT jobject JNICALL
Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(JNIEnv * pJEnv,jclass,jstring jWriteRegFile,jstring jReadRegFile,jboolean jbReadOnly,jobject loader)238 Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(
239 JNIEnv * pJEnv, jclass, jstring jWriteRegFile,
240 jstring jReadRegFile, jboolean jbReadOnly, jobject loader )
241 {
242 jobject joRegServiceFac = 0;
243
244 try
245 {
246 OUString aWriteRegFile;
247 OUString aReadRegFile;
248
249 sal_Bool bReadOnly = jbReadOnly == JNI_FALSE ? sal_False : sal_True;
250
251 if (jReadRegFile) {
252 const jchar* pjReadRegFile = pJEnv->GetStringChars(jReadRegFile, NULL);
253 aReadRegFile = OUString(pjReadRegFile);
254 pJEnv->ReleaseStringChars(jReadRegFile, pjReadRegFile);
255 }
256
257 if (jWriteRegFile) {
258 const jchar * pjWriteRegFile = pJEnv->GetStringChars(jWriteRegFile, NULL);
259 aWriteRegFile = OUString(pjWriteRegFile);
260 pJEnv->ReleaseStringChars(jWriteRegFile, pjWriteRegFile);
261 }
262
263 // bootstrap
264 Reference< lang::XMultiServiceFactory > rMSFac;
265 if (aReadRegFile.getLength() == 0)
266 rMSFac = ::cppu::createRegistryServiceFactory( aWriteRegFile, bReadOnly);
267 else
268 rMSFac = ::cppu::createRegistryServiceFactory(aWriteRegFile, aReadRegFile, bReadOnly);
269
270 Reference< beans::XPropertySet > xProps(
271 rMSFac, UNO_QUERY_THROW );
272 Reference< XComponentContext > xContext(
273 xProps->getPropertyValue( OUSTR("DefaultContext") ), UNO_QUERY_THROW );
274
275 // create vm access
276 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
277 ::javaunohelper::create_vm_access( pJEnv, loader ) );
278 // wrap vm singleton entry
279 xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
280 rMSFac.set( xContext->getServiceManager(), UNO_QUERY_THROW );
281
282 // get uno envs
283 OUString aCurrentEnv(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
284 OUString java_env_name = OUSTR(UNO_LB_JAVA);
285 Environment java_env, curr_env;
286 uno_getEnvironment((uno_Environment **)&curr_env, aCurrentEnv.pData, NULL);
287 uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
288
289 Mapping curr_java(curr_env.get(), java_env.get());
290 if (! curr_java.is())
291 {
292 throw RuntimeException(
293 OUSTR("no C++ <-> Java mapping available!"), Reference< XInterface >() );
294 }
295
296 jobject joGlobalRegServiceFac =
297 (jobject)curr_java.mapInterface(
298 rMSFac.get(),
299 getCppuType((Reference< lang::XMultiServiceFactory > *)0) );
300 joRegServiceFac = pJEnv->NewLocalRef( joGlobalRegServiceFac );
301 pJEnv->DeleteGlobalRef(joGlobalRegServiceFac);
302 }
303 catch (Exception & exc)
304 {
305 jclass c = pJEnv->FindClass( "com/sun/star/uno/RuntimeException" );
306 if (0 != c)
307 {
308 OString cstr( ::rtl::OUStringToOString(
309 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
310 OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
311 pJEnv->ThrowNew( c, cstr.getStr() );
312 }
313 return 0;
314 }
315
316 OSL_TRACE("javaunohelper.cxx: object %i", joRegServiceFac);
317
318 return joRegServiceFac;
319 }
320