1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: javaunohelper.cxx,v $
*
* $Revision: 1.9 $
*
* last change: $Author: rt $ $Date: 2005-09-07 18:42:30 $
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
************************************************************************/
#include <osl/diagnose.h>
#include <osl/module.h>
#include <uno/environment.hxx>
#include <uno/mapping.hxx>
#include <cppuhelper/factory.hxx>
#include <cppuhelper/servicefactory.hxx>
#include <cppuhelper/component_context.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/registry/XRegistryKey.hpp>
#include "jni.h"
#include "jvmaccess/virtualmachine.hxx"
#include "jvmaccess/unovirtualmachine.hxx"
#include "vm.hxx"
#define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using ::rtl::OString;
using ::rtl::OUString;
/*
* Class: com_sun_star_comp_helper_SharedLibraryLoader
* Method: component_writeInfo
* Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z
*/
extern "C" JNIEXPORT jboolean JNICALL
Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
JNIEnv * pJEnv, jclass jClass, jstring jLibName, jobject jSMgr,
jobject jRegKey, jobject loader )
{
sal_Bool bRet = sal_False;
const jchar* pJLibName = pJEnv->GetStringChars( jLibName, NULL );
OUString aLibName( pJLibName );
pJEnv->ReleaseStringChars( jLibName, pJLibName);
oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
if (lib)
{
void * pSym;
// ========================= LATEST VERSION =========================
OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
if (pSym = osl_getSymbol( lib, aGetEnvName.pData ))
{
Environment java_env, loader_env;
const sal_Char * pEnvTypeName = 0;
(*((component_getImplementationEnvironmentFunc)pSym))(
&pEnvTypeName, (uno_Environment **)&loader_env );
if (! loader_env.is())
{
OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
}
// create vm access
::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
::javaunohelper::create_vm_access( pJEnv, loader ) );
OUString java_env_name = OUSTR(UNO_LB_JAVA);
uno_getEnvironment(
(uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
OUString aWriteInfoName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_WRITEINFO) );
if (pSym = osl_getSymbol( lib, aWriteInfoName.pData ))
{
if (loader_env.is() && java_env.is())
{
Mapping java2dest(java_env.get(), loader_env.get());
if ( java2dest.is() )
{
void * pSMgr =
java2dest.mapInterface(
jSMgr, getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
void * pKey =
java2dest.mapInterface(
jRegKey, getCppuType((Reference< registry::XRegistryKey > *) 0) );
uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
if (pKey)
{
bRet = (*((component_writeInfoFunc)pSym))( pSMgr, pKey );
if (env)
(*env->releaseInterface)( env, pKey );
}
if (pSMgr && env)
(*env->releaseInterface)( env, pSMgr );
}
}
}
}
}
return bRet == sal_False? JNI_FALSE : JNI_TRUE;
}
/*
* Class: com_sun_star_comp_helper_SharedLibraryLoader
* Method: component_getFactory
* Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object;
*/
extern "C" JNIEXPORT jobject JNICALL
Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
JNIEnv * pJEnv, jclass jClass, jstring jLibName, jstring jImplName,
jobject jSMgr, jobject jRegKey, jobject loader )
{
const jchar* pJLibName = pJEnv->GetStringChars(jLibName, NULL);
OUString aLibName( pJLibName );
pJEnv->ReleaseStringChars( jLibName, pJLibName);
aLibName += OUString( RTL_CONSTASCII_USTRINGPARAM(SAL_DLLEXTENSION) );
jobject joSLL_cpp = 0;
oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
if (lib)
{
void * pSym;
// ========================= LATEST VERSION =========================
OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
if (pSym = osl_getSymbol( lib, aGetEnvName.pData ))
{
Environment java_env, loader_env;
const sal_Char * pEnvTypeName = 0;
(*((component_getImplementationEnvironmentFunc)pSym))(
&pEnvTypeName, (uno_Environment **)&loader_env );
if (! loader_env.is())
{
OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
}
// create vm access
::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
::javaunohelper::create_vm_access( pJEnv, loader ) );
OUString java_env_name = OUSTR(UNO_LB_JAVA);
uno_getEnvironment(
(uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
OUString aGetFactoryName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETFACTORY) );
if (pSym = osl_getSymbol( lib, aGetFactoryName.pData ))
{
if (loader_env.is() && java_env.is())
{
Mapping java2dest( java_env.get(), loader_env.get() );
Mapping dest2java( loader_env.get(), java_env.get() );
if (dest2java.is() && java2dest.is())
{
void * pSMgr =
java2dest.mapInterface(
jSMgr, ::getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
void * pKey =
java2dest.mapInterface(
jRegKey, ::getCppuType((Reference< registry::XRegistryKey > *) 0) );
const char* pImplName = pJEnv->GetStringUTFChars( jImplName, NULL );
void * pSSF = (*((component_getFactoryFunc)pSym))(
pImplName, pSMgr, pKey );
pJEnv->ReleaseStringUTFChars( jImplName, pImplName );
uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
if (pKey && env)
(*env->releaseInterface)( env, pKey );
if (pSMgr && env)
(*env->releaseInterface)( env, pSMgr );
if (pSSF)
{
jobject jglobal = (jobject) dest2java.mapInterface(
pSSF, getCppuType((Reference< XInterface > *) 0) );
joSLL_cpp = pJEnv->NewLocalRef( jglobal );
pJEnv->DeleteGlobalRef( jglobal );
if (env)
(*env->releaseInterface)( env, pSSF );
}
}
}
}
}
}
return joSLL_cpp;
}
/*
* Class: com_sun_star_comp_helper_RegistryServiceFactory
* Method: createRegistryServiceFactory
* Signature: (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/Object;
*/
extern "C" JNIEXPORT jobject JNICALL
Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(
JNIEnv * pJEnv, jclass jClass, jstring jWriteRegFile,
jstring jReadRegFile, jboolean jbReadOnly, jobject loader )
{
jobject joRegServiceFac = 0;
try
{
OUString aWriteRegFile;
OUString aReadRegFile;
sal_Bool bReadOnly = jbReadOnly == JNI_FALSE ? sal_False : sal_True;
if (jReadRegFile) {
const jchar* pjReadRegFile = pJEnv->GetStringChars(jReadRegFile, NULL);
aReadRegFile = OUString(pjReadRegFile);
pJEnv->ReleaseStringChars(jReadRegFile, pjReadRegFile);
}
if (jWriteRegFile) {
const jchar * pjWriteRegFile = pJEnv->GetStringChars(jWriteRegFile, NULL);
aWriteRegFile = OUString(pjWriteRegFile);
pJEnv->ReleaseStringChars(jWriteRegFile, pjWriteRegFile);
}
// bootstrap
Reference< lang::XMultiServiceFactory > rMSFac;
if (aReadRegFile.getLength() == 0)
rMSFac = ::cppu::createRegistryServiceFactory( aWriteRegFile, bReadOnly);
else
rMSFac = ::cppu::createRegistryServiceFactory(aWriteRegFile, aReadRegFile, bReadOnly);
Reference< beans::XPropertySet > xProps(
rMSFac, UNO_QUERY_THROW );
Reference< XComponentContext > xContext(
xProps->getPropertyValue( OUSTR("DefaultContext") ), UNO_QUERY_THROW );
// create vm access
::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
::javaunohelper::create_vm_access( pJEnv, loader ) );
// wrap vm singleton entry
xContext = ::javaunohelper::install_vm_singleton(
xContext, vm_access->getVirtualMachine() );
rMSFac.set( xContext->getServiceManager(), UNO_QUERY_THROW );
// get uno envs
OUString aCurrentEnv(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
OUString java_env_name = OUSTR(UNO_LB_JAVA);
Environment java_env, curr_env;
uno_getEnvironment((uno_Environment **)&curr_env, aCurrentEnv.pData, NULL);
uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
Mapping curr_java(curr_env.get(), java_env.get());
if (! curr_java.is())
{
throw RuntimeException(
OUSTR("no C++ <-> Java mapping available!"), Reference< XInterface >() );
}
jobject joGlobalRegServiceFac =
(jobject)curr_java.mapInterface(
rMSFac.get(),
getCppuType((Reference< lang::XMultiServiceFactory > *)0) );
joRegServiceFac = pJEnv->NewLocalRef( joGlobalRegServiceFac );
pJEnv->DeleteGlobalRef(joGlobalRegServiceFac);
}
catch (Exception & exc)
{
jclass c = pJEnv->FindClass( "com/sun/star/uno/RuntimeException" );
if (0 != c)
{
OString cstr( ::rtl::OUStringToOString(
exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
pJEnv->ThrowNew( c, cstr.getStr() );
}
return 0;
}
OSL_TRACE("javaunohelper.cxx: object %i", joRegServiceFac);
return joRegServiceFac;
}
|