Branch data 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_DLLPUBLIC_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 : 0 : oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
67 : 0 : if (lib)
68 : : {
69 : : // ========================= LATEST VERSION =========================
70 : 0 : OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
71 : : oslGenericFunction pSym =
72 : 0 : osl_getFunctionSymbol( lib, aGetEnvName.pData );
73 : 0 : if (pSym)
74 : : {
75 : 0 : Environment java_env, loader_env;
76 : :
77 : 0 : const sal_Char * pEnvTypeName = 0;
78 : : (*((component_getImplementationEnvironmentFunc)pSym))(
79 : 0 : &pEnvTypeName, (uno_Environment **)&loader_env );
80 : 0 : if (! loader_env.is())
81 : : {
82 : 0 : OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
83 : 0 : uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
84 : : }
85 : :
86 : : // create vm access
87 : : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
88 : 0 : ::javaunohelper::create_vm_access( pJEnv, loader ) );
89 : 0 : OUString java_env_name = OUSTR(UNO_LB_JAVA);
90 : : uno_getEnvironment(
91 : 0 : (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
92 : :
93 : 0 : OUString aWriteInfoName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_WRITEINFO) );
94 : 0 : pSym = osl_getFunctionSymbol( lib, aWriteInfoName.pData );
95 : 0 : if (pSym)
96 : : {
97 : 0 : if (loader_env.is() && java_env.is())
98 : : {
99 : 0 : Mapping java2dest(java_env.get(), loader_env.get());
100 : :
101 : 0 : if ( java2dest.is() )
102 : : {
103 : : void * pSMgr =
104 : : java2dest.mapInterface(
105 : 0 : jSMgr, getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
106 : : void * pKey =
107 : : java2dest.mapInterface(
108 : 0 : jRegKey, getCppuType((Reference< registry::XRegistryKey > *) 0) );
109 : :
110 : 0 : uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
111 : 0 : if (pKey)
112 : : {
113 : 0 : bRet = (*((component_writeInfoFunc)pSym))( pSMgr, pKey );
114 : :
115 : 0 : if (env)
116 : 0 : (*env->releaseInterface)( env, pKey );
117 : : }
118 : :
119 : 0 : if (pSMgr && env)
120 : 0 : (*env->releaseInterface)( env, pSMgr );
121 : 0 : }
122 : : }
123 : 0 : }
124 : 0 : }
125 : : }
126 : :
127 : 0 : return bRet == sal_False? JNI_FALSE : JNI_TRUE;
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 : : */
135 : : extern "C" SAL_DLLPUBLIC_EXPORT jobject JNICALL
136 : 0 : 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 )
139 : : {
140 : 0 : const jchar* pJLibName = pJEnv->GetStringChars(jLibName, NULL);
141 : 0 : OUString aLibName( pJLibName );
142 : 0 : pJEnv->ReleaseStringChars( jLibName, pJLibName);
143 : :
144 : 0 : aLibName += OUString( RTL_CONSTASCII_USTRINGPARAM(SAL_DLLEXTENSION) );
145 : :
146 : 0 : jobject joSLL_cpp = 0;
147 : :
148 : 0 : oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
149 : 0 : if (lib)
150 : : {
151 : : // ========================= LATEST VERSION =========================
152 : 0 : OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
153 : : oslGenericFunction pSym =
154 : 0 : osl_getFunctionSymbol( lib, aGetEnvName.pData );
155 : 0 : if (pSym)
156 : : {
157 : 0 : Environment java_env, loader_env;
158 : :
159 : 0 : const sal_Char * pEnvTypeName = 0;
160 : : (*((component_getImplementationEnvironmentFunc)pSym))(
161 : 0 : &pEnvTypeName, (uno_Environment **)&loader_env );
162 : :
163 : 0 : if (! loader_env.is())
164 : : {
165 : 0 : OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
166 : 0 : uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
167 : : }
168 : :
169 : : // create vm access
170 : : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
171 : 0 : ::javaunohelper::create_vm_access( pJEnv, loader ) );
172 : 0 : OUString java_env_name = OUSTR(UNO_LB_JAVA);
173 : : uno_getEnvironment(
174 : 0 : (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
175 : :
176 : 0 : OUString aGetFactoryName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETFACTORY) );
177 : 0 : pSym = osl_getFunctionSymbol( lib, aGetFactoryName.pData );
178 : 0 : if (pSym)
179 : : {
180 : 0 : if (loader_env.is() && java_env.is())
181 : : {
182 : 0 : Mapping java2dest( java_env.get(), loader_env.get() );
183 : 0 : Mapping dest2java( loader_env.get(), java_env.get() );
184 : :
185 : 0 : if (dest2java.is() && java2dest.is())
186 : : {
187 : : void * pSMgr =
188 : : java2dest.mapInterface(
189 : 0 : jSMgr, ::getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
190 : : void * pKey =
191 : : java2dest.mapInterface(
192 : 0 : jRegKey, ::getCppuType((Reference< registry::XRegistryKey > *) 0) );
193 : :
194 : 0 : const char* pImplName = pJEnv->GetStringUTFChars( jImplName, NULL );
195 : :
196 : : void * pSSF = (*((component_getFactoryFunc)pSym))(
197 : 0 : pImplName, pSMgr, pKey );
198 : :
199 : 0 : pJEnv->ReleaseStringUTFChars( jImplName, pImplName );
200 : :
201 : 0 : uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
202 : :
203 : 0 : if (pKey && env)
204 : 0 : (*env->releaseInterface)( env, pKey );
205 : 0 : if (pSMgr && env)
206 : 0 : (*env->releaseInterface)( env, pSMgr );
207 : :
208 : 0 : if (pSSF)
209 : : {
210 : : jobject jglobal = (jobject) dest2java.mapInterface(
211 : 0 : pSSF, getCppuType((Reference< XInterface > *) 0) );
212 : 0 : joSLL_cpp = pJEnv->NewLocalRef( jglobal );
213 : 0 : pJEnv->DeleteGlobalRef( jglobal );
214 : 0 : if (env)
215 : 0 : (*env->releaseInterface)( env, pSSF );
216 : : }
217 : 0 : }
218 : : }
219 : 0 : }
220 : 0 : }
221 : : }
222 : :
223 : 0 : return joSLL_cpp;
224 : : }
225 : :
226 : : /*
227 : : * Class: com_sun_star_comp_helper_RegistryServiceFactory
228 : : * Method: createRegistryServiceFactory
229 : : * Signature: (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/Object;
230 : : */
231 : : extern "C" SAL_DLLPUBLIC_EXPORT jobject JNICALL
232 : 0 : Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(
233 : : JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jWriteRegFile,
234 : : jstring jReadRegFile, jboolean jbReadOnly, jobject loader )
235 : : {
236 : 0 : jobject joRegServiceFac = 0;
237 : :
238 : : try
239 : : {
240 : 0 : OUString aWriteRegFile;
241 : 0 : OUString aReadRegFile;
242 : :
243 : 0 : sal_Bool bReadOnly = jbReadOnly == JNI_FALSE ? sal_False : sal_True;
244 : :
245 : 0 : if (jReadRegFile) {
246 : 0 : const jchar* pjReadRegFile = pJEnv->GetStringChars(jReadRegFile, NULL);
247 : 0 : aReadRegFile = OUString(pjReadRegFile);
248 : 0 : pJEnv->ReleaseStringChars(jReadRegFile, pjReadRegFile);
249 : : }
250 : :
251 : 0 : if (jWriteRegFile) {
252 : 0 : const jchar * pjWriteRegFile = pJEnv->GetStringChars(jWriteRegFile, NULL);
253 : 0 : aWriteRegFile = OUString(pjWriteRegFile);
254 : 0 : pJEnv->ReleaseStringChars(jWriteRegFile, pjWriteRegFile);
255 : : }
256 : :
257 : : // bootstrap
258 : 0 : Reference< lang::XMultiServiceFactory > rMSFac;
259 : 0 : if (aReadRegFile.isEmpty())
260 : 0 : rMSFac = ::cppu::createRegistryServiceFactory( aWriteRegFile, bReadOnly);
261 : : else
262 : 0 : rMSFac = ::cppu::createRegistryServiceFactory(aWriteRegFile, aReadRegFile, bReadOnly);
263 : :
264 : : Reference< beans::XPropertySet > xProps(
265 : 0 : rMSFac, UNO_QUERY_THROW );
266 : : Reference< XComponentContext > xContext(
267 : 0 : xProps->getPropertyValue( OUSTR("DefaultContext") ), UNO_QUERY_THROW );
268 : :
269 : : // create vm access
270 : : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
271 : 0 : ::javaunohelper::create_vm_access( pJEnv, loader ) );
272 : : // wrap vm singleton entry
273 : 0 : xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
274 : 0 : rMSFac.set( xContext->getServiceManager(), UNO_QUERY_THROW );
275 : :
276 : : // get uno envs
277 : 0 : OUString aCurrentEnv(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
278 : 0 : OUString java_env_name = OUSTR(UNO_LB_JAVA);
279 : 0 : Environment java_env, curr_env;
280 : 0 : uno_getEnvironment((uno_Environment **)&curr_env, aCurrentEnv.pData, NULL);
281 : 0 : uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
282 : :
283 : 0 : Mapping curr_java(curr_env.get(), java_env.get());
284 : 0 : if (! curr_java.is())
285 : : {
286 : : throw RuntimeException(
287 : 0 : OUSTR("no C++ <-> Java mapping available!"), Reference< XInterface >() );
288 : : }
289 : :
290 : : jobject joGlobalRegServiceFac =
291 : : (jobject)curr_java.mapInterface(
292 : 0 : rMSFac.get(),
293 : 0 : getCppuType((Reference< lang::XMultiServiceFactory > *)0) );
294 : 0 : joRegServiceFac = pJEnv->NewLocalRef( joGlobalRegServiceFac );
295 : 0 : pJEnv->DeleteGlobalRef(joGlobalRegServiceFac);
296 : : }
297 : 0 : catch (Exception & exc)
298 : : {
299 : 0 : jclass c = pJEnv->FindClass( "com/sun/star/uno/RuntimeException" );
300 : 0 : if (0 != c)
301 : : {
302 : : OString cstr( ::rtl::OUStringToOString(
303 : 0 : exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
304 : : OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
305 : 0 : pJEnv->ThrowNew( c, cstr.getStr() );
306 : : }
307 : 0 : return 0;
308 : : }
309 : :
310 : : OSL_TRACE("javaunohelper.cxx: object %i", joRegServiceFac);
311 : :
312 : 0 : return joRegServiceFac;
313 : : }
314 : :
315 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|