Line data Source code
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 :
21 : #include <osl/diagnose.h>
22 : #include <osl/module.h>
23 :
24 : #include <uno/environment.hxx>
25 : #include <uno/mapping.hxx>
26 :
27 : #include <cppuhelper/factory.hxx>
28 : #include <cppuhelper/servicefactory.hxx>
29 : #include <cppuhelper/component_context.hxx>
30 :
31 : #include <com/sun/star/beans/XPropertySet.hpp>
32 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 : #include <com/sun/star/lang/XServiceInfo.hpp>
34 : #include <com/sun/star/registry/XRegistryKey.hpp>
35 :
36 : #include "jni.h"
37 : #include "jvmaccess/virtualmachine.hxx"
38 : #include "jvmaccess/unovirtualmachine.hxx"
39 :
40 : #include "vm.hxx"
41 :
42 : #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
43 :
44 :
45 : using namespace ::com::sun::star;
46 : using namespace ::com::sun::star::uno;
47 : using ::rtl::OString;
48 : using ::rtl::OUString;
49 :
50 : /*
51 : * Class: com_sun_star_comp_helper_SharedLibraryLoader
52 : * Method: component_writeInfo
53 : * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z
54 : */
55 : extern "C" SAL_JNI_EXPORT jboolean JNICALL
56 0 : Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
57 : JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jobject jSMgr,
58 : jobject jRegKey, jobject loader )
59 : {
60 0 : sal_Bool bRet = sal_False;
61 :
62 0 : const jchar* pJLibName = pJEnv->GetStringChars( jLibName, NULL );
63 0 : OUString aLibName( pJLibName );
64 0 : pJEnv->ReleaseStringChars( jLibName, pJLibName);
65 :
66 : #ifdef DISABLE_DYNLOADING
67 : (void) jSMgr;
68 : (void) jRegKey;
69 : (void) loader;
70 :
71 : fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, ::rtl::OUStringToOString(pJLibName, RTL_TEXTENCODING_JAVA_UTF8).getStr());
72 : #else
73 0 : oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
74 0 : if (lib)
75 : {
76 : // ========================= LATEST VERSION =========================
77 0 : OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
78 : oslGenericFunction pSym =
79 0 : osl_getFunctionSymbol( lib, aGetEnvName.pData );
80 0 : if (pSym)
81 : {
82 0 : Environment java_env, loader_env;
83 :
84 0 : const sal_Char * pEnvTypeName = 0;
85 : (*((component_getImplementationEnvironmentFunc)pSym))(
86 0 : &pEnvTypeName, (uno_Environment **)&loader_env );
87 0 : if (! loader_env.is())
88 : {
89 0 : OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
90 0 : uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
91 : }
92 :
93 : // create vm access
94 : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
95 0 : ::javaunohelper::create_vm_access( pJEnv, loader ) );
96 0 : OUString java_env_name = OUSTR(UNO_LB_JAVA);
97 : uno_getEnvironment(
98 0 : (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
99 :
100 0 : OUString aWriteInfoName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_WRITEINFO) );
101 0 : pSym = osl_getFunctionSymbol( lib, aWriteInfoName.pData );
102 0 : if (pSym)
103 : {
104 0 : if (loader_env.is() && java_env.is())
105 : {
106 0 : Mapping java2dest(java_env.get(), loader_env.get());
107 :
108 0 : if ( java2dest.is() )
109 : {
110 : void * pSMgr =
111 : java2dest.mapInterface(
112 0 : jSMgr, getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
113 : void * pKey =
114 : java2dest.mapInterface(
115 0 : jRegKey, getCppuType((Reference< registry::XRegistryKey > *) 0) );
116 :
117 0 : uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
118 0 : if (pKey)
119 : {
120 0 : bRet = (*((component_writeInfoFunc)pSym))( pSMgr, pKey );
121 :
122 0 : if (env)
123 0 : (*env->releaseInterface)( env, pKey );
124 : }
125 :
126 0 : if (pSMgr && env)
127 0 : (*env->releaseInterface)( env, pSMgr );
128 0 : }
129 : }
130 0 : }
131 0 : }
132 : }
133 : #endif
134 0 : return bRet == sal_False? JNI_FALSE : JNI_TRUE;
135 : }
136 :
137 : /*
138 : * Class: com_sun_star_comp_helper_SharedLibraryLoader
139 : * Method: component_getFactory
140 : * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object;
141 : */
142 : extern "C" SAL_JNI_EXPORT jobject JNICALL
143 0 : Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
144 : JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jstring jImplName,
145 : jobject jSMgr, jobject jRegKey, jobject loader )
146 : {
147 0 : const jchar* pJLibName = pJEnv->GetStringChars(jLibName, NULL);
148 :
149 : #ifdef DISABLE_DYNLOADING
150 : (void) jImplName;
151 : (void) jSMgr;
152 : (void) jRegKey;
153 : (void) loader;
154 :
155 : fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, ::rtl::OUStringToOString(pJLibName, RTL_TEXTENCODING_JAVA_UTF8).getStr());
156 : #endif
157 :
158 0 : OUString aLibName( pJLibName );
159 0 : pJEnv->ReleaseStringChars( jLibName, pJLibName);
160 :
161 0 : aLibName += OUString( RTL_CONSTASCII_USTRINGPARAM(SAL_DLLEXTENSION) );
162 :
163 0 : jobject joSLL_cpp = 0;
164 :
165 : #ifndef DISABLE_DYNLOADING
166 0 : oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
167 0 : if (lib)
168 : {
169 : // ========================= LATEST VERSION =========================
170 0 : OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
171 : oslGenericFunction pSym =
172 0 : osl_getFunctionSymbol( lib, aGetEnvName.pData );
173 0 : if (pSym)
174 : {
175 0 : Environment java_env, loader_env;
176 :
177 0 : const sal_Char * pEnvTypeName = 0;
178 : (*((component_getImplementationEnvironmentFunc)pSym))(
179 0 : &pEnvTypeName, (uno_Environment **)&loader_env );
180 :
181 0 : if (! loader_env.is())
182 : {
183 0 : OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
184 0 : uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
185 : }
186 :
187 : // create vm access
188 : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
189 0 : ::javaunohelper::create_vm_access( pJEnv, loader ) );
190 0 : OUString java_env_name = OUSTR(UNO_LB_JAVA);
191 : uno_getEnvironment(
192 0 : (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
193 :
194 0 : OUString aGetFactoryName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETFACTORY) );
195 0 : pSym = osl_getFunctionSymbol( lib, aGetFactoryName.pData );
196 0 : if (pSym)
197 : {
198 0 : if (loader_env.is() && java_env.is())
199 : {
200 0 : Mapping java2dest( java_env.get(), loader_env.get() );
201 0 : Mapping dest2java( loader_env.get(), java_env.get() );
202 :
203 0 : if (dest2java.is() && java2dest.is())
204 : {
205 : void * pSMgr =
206 : java2dest.mapInterface(
207 0 : jSMgr, ::getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
208 : void * pKey =
209 : java2dest.mapInterface(
210 0 : jRegKey, ::getCppuType((Reference< registry::XRegistryKey > *) 0) );
211 :
212 0 : const char* pImplName = pJEnv->GetStringUTFChars( jImplName, NULL );
213 :
214 : void * pSSF = (*((component_getFactoryFunc)pSym))(
215 0 : pImplName, pSMgr, pKey );
216 :
217 0 : pJEnv->ReleaseStringUTFChars( jImplName, pImplName );
218 :
219 0 : uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
220 :
221 0 : if (pKey && env)
222 0 : (*env->releaseInterface)( env, pKey );
223 0 : if (pSMgr && env)
224 0 : (*env->releaseInterface)( env, pSMgr );
225 :
226 0 : if (pSSF)
227 : {
228 : jobject jglobal = (jobject) dest2java.mapInterface(
229 0 : pSSF, getCppuType((Reference< XInterface > *) 0) );
230 0 : joSLL_cpp = pJEnv->NewLocalRef( jglobal );
231 0 : pJEnv->DeleteGlobalRef( jglobal );
232 0 : if (env)
233 0 : (*env->releaseInterface)( env, pSSF );
234 : }
235 0 : }
236 : }
237 0 : }
238 0 : }
239 : }
240 : #endif
241 0 : return joSLL_cpp;
242 : }
243 :
244 : /*
245 : * Class: com_sun_star_comp_helper_RegistryServiceFactory
246 : * Method: createRegistryServiceFactory
247 : * Signature: (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/Object;
248 : */
249 : extern "C" SAL_JNI_EXPORT jobject JNICALL
250 0 : Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(
251 : JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jWriteRegFile,
252 : jstring jReadRegFile, jboolean jbReadOnly, jobject loader )
253 : {
254 0 : jobject joRegServiceFac = 0;
255 :
256 : try
257 : {
258 0 : OUString aWriteRegFile;
259 0 : OUString aReadRegFile;
260 :
261 0 : sal_Bool bReadOnly = jbReadOnly == JNI_FALSE ? sal_False : sal_True;
262 :
263 0 : if (jReadRegFile) {
264 0 : const jchar* pjReadRegFile = pJEnv->GetStringChars(jReadRegFile, NULL);
265 0 : aReadRegFile = OUString(pjReadRegFile);
266 0 : pJEnv->ReleaseStringChars(jReadRegFile, pjReadRegFile);
267 : }
268 :
269 0 : if (jWriteRegFile) {
270 0 : const jchar * pjWriteRegFile = pJEnv->GetStringChars(jWriteRegFile, NULL);
271 0 : aWriteRegFile = OUString(pjWriteRegFile);
272 0 : pJEnv->ReleaseStringChars(jWriteRegFile, pjWriteRegFile);
273 : }
274 :
275 : // bootstrap
276 0 : Reference< lang::XMultiServiceFactory > rMSFac;
277 0 : if (aReadRegFile.isEmpty())
278 0 : rMSFac = ::cppu::createRegistryServiceFactory( aWriteRegFile, bReadOnly);
279 : else
280 0 : rMSFac = ::cppu::createRegistryServiceFactory(aWriteRegFile, aReadRegFile, bReadOnly);
281 :
282 : Reference< beans::XPropertySet > xProps(
283 0 : rMSFac, UNO_QUERY_THROW );
284 : Reference< XComponentContext > xContext(
285 0 : xProps->getPropertyValue( OUSTR("DefaultContext") ), UNO_QUERY_THROW );
286 :
287 : // create vm access
288 : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
289 0 : ::javaunohelper::create_vm_access( pJEnv, loader ) );
290 : // wrap vm singleton entry
291 0 : xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
292 0 : rMSFac.set( xContext->getServiceManager(), UNO_QUERY_THROW );
293 :
294 : // get uno envs
295 0 : OUString aCurrentEnv(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
296 0 : OUString java_env_name = OUSTR(UNO_LB_JAVA);
297 0 : Environment java_env, curr_env;
298 0 : uno_getEnvironment((uno_Environment **)&curr_env, aCurrentEnv.pData, NULL);
299 0 : uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
300 :
301 0 : Mapping curr_java(curr_env.get(), java_env.get());
302 0 : if (! curr_java.is())
303 : {
304 : throw RuntimeException(
305 0 : OUSTR("no C++ <-> Java mapping available!"), Reference< XInterface >() );
306 : }
307 :
308 : jobject joGlobalRegServiceFac =
309 : (jobject)curr_java.mapInterface(
310 0 : rMSFac.get(),
311 0 : getCppuType((Reference< lang::XMultiServiceFactory > *)0) );
312 0 : joRegServiceFac = pJEnv->NewLocalRef( joGlobalRegServiceFac );
313 0 : pJEnv->DeleteGlobalRef(joGlobalRegServiceFac);
314 : }
315 0 : catch (Exception & exc)
316 : {
317 0 : jclass c = pJEnv->FindClass( "com/sun/star/uno/RuntimeException" );
318 0 : if (0 != c)
319 : {
320 : OString cstr( ::rtl::OUStringToOString(
321 0 : exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
322 : OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
323 0 : pJEnv->ThrowNew( c, cstr.getStr() );
324 : }
325 0 : return 0;
326 : }
327 :
328 : OSL_TRACE("javaunohelper.cxx: object %i", joRegServiceFac);
329 :
330 0 : return joRegServiceFac;
331 : }
332 :
333 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|