1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: javaunohelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_javaunohelper.hxx"
34 #include <osl/diagnose.h>
35 #include <osl/module.h>
37 #include <uno/environment.hxx>
38 #include <uno/mapping.hxx>
40 #include <cppuhelper/factory.hxx>
41 #include <cppuhelper/servicefactory.hxx>
42 #include <cppuhelper/component_context.hxx>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <com/sun/star/lang/XServiceInfo.hpp>
47 #include <com/sun/star/registry/XRegistryKey.hpp>
50 #include "jvmaccess/virtualmachine.hxx"
51 #include "jvmaccess/unovirtualmachine.hxx"
55 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
58 using namespace ::com::sun::star
;
59 using namespace ::com::sun::star::uno
;
61 using ::rtl::OUString
;
64 * Class: com_sun_star_comp_helper_SharedLibraryLoader
65 * Method: component_writeInfo
66 * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z
68 extern "C" JNIEXPORT jboolean JNICALL
69 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
70 JNIEnv
* pJEnv
, jclass
, jstring jLibName
, jobject jSMgr
,
71 jobject jRegKey
, jobject loader
)
73 sal_Bool bRet
= sal_False
;
75 const jchar
* pJLibName
= pJEnv
->GetStringChars( jLibName
, NULL
);
76 OUString
aLibName( pJLibName
);
77 pJEnv
->ReleaseStringChars( jLibName
, pJLibName
);
79 oslModule lib
= osl_loadModule( aLibName
.pData
, SAL_LOADMODULE_LAZY
| SAL_LOADMODULE_GLOBAL
);
82 // ========================= LATEST VERSION =========================
83 OUString
aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV
) );
84 oslGenericFunction pSym
=
85 osl_getFunctionSymbol( lib
, aGetEnvName
.pData
);
88 Environment java_env
, loader_env
;
90 const sal_Char
* pEnvTypeName
= 0;
91 (*((component_getImplementationEnvironmentFunc
)pSym
))(
92 &pEnvTypeName
, (uno_Environment
**)&loader_env
);
93 if (! loader_env
.is())
95 OUString
aEnvTypeName( OUString::createFromAscii( pEnvTypeName
) );
96 uno_getEnvironment( (uno_Environment
**)&loader_env
, aEnvTypeName
.pData
, 0 );
100 ::rtl::Reference
< ::jvmaccess::UnoVirtualMachine
> vm_access(
101 ::javaunohelper::create_vm_access( pJEnv
, loader
) );
102 OUString java_env_name
= OUSTR(UNO_LB_JAVA
);
104 (uno_Environment
**)&java_env
, java_env_name
.pData
, vm_access
.get() );
106 OUString
aWriteInfoName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_WRITEINFO
) );
107 pSym
= osl_getFunctionSymbol( lib
, aWriteInfoName
.pData
);
110 if (loader_env
.is() && java_env
.is())
112 Mapping
java2dest(java_env
.get(), loader_env
.get());
114 if ( java2dest
.is() )
117 java2dest
.mapInterface(
118 jSMgr
, getCppuType((Reference
< lang::XMultiServiceFactory
> *) 0) );
120 java2dest
.mapInterface(
121 jRegKey
, getCppuType((Reference
< registry::XRegistryKey
> *) 0) );
123 uno_ExtEnvironment
* env
= loader_env
.get()->pExtEnv
;
126 bRet
= (*((component_writeInfoFunc
)pSym
))( pSMgr
, pKey
);
129 (*env
->releaseInterface
)( env
, pKey
);
133 (*env
->releaseInterface
)( env
, pSMgr
);
140 return bRet
== sal_False
? JNI_FALSE
: JNI_TRUE
;
144 * Class: com_sun_star_comp_helper_SharedLibraryLoader
145 * Method: component_getFactory
146 * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object;
148 extern "C" JNIEXPORT jobject JNICALL
149 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
150 JNIEnv
* pJEnv
, jclass
, jstring jLibName
, jstring jImplName
,
151 jobject jSMgr
, jobject jRegKey
, jobject loader
)
153 const jchar
* pJLibName
= pJEnv
->GetStringChars(jLibName
, NULL
);
154 OUString
aLibName( pJLibName
);
155 pJEnv
->ReleaseStringChars( jLibName
, pJLibName
);
157 aLibName
+= OUString( RTL_CONSTASCII_USTRINGPARAM(SAL_DLLEXTENSION
) );
159 jobject joSLL_cpp
= 0;
161 oslModule lib
= osl_loadModule( aLibName
.pData
, SAL_LOADMODULE_LAZY
| SAL_LOADMODULE_GLOBAL
);
164 // ========================= LATEST VERSION =========================
165 OUString
aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV
) );
166 oslGenericFunction pSym
=
167 osl_getFunctionSymbol( lib
, aGetEnvName
.pData
);
170 Environment java_env
, loader_env
;
172 const sal_Char
* pEnvTypeName
= 0;
173 (*((component_getImplementationEnvironmentFunc
)pSym
))(
174 &pEnvTypeName
, (uno_Environment
**)&loader_env
);
176 if (! loader_env
.is())
178 OUString
aEnvTypeName( OUString::createFromAscii( pEnvTypeName
) );
179 uno_getEnvironment( (uno_Environment
**)&loader_env
, aEnvTypeName
.pData
, 0 );
183 ::rtl::Reference
< ::jvmaccess::UnoVirtualMachine
> vm_access(
184 ::javaunohelper::create_vm_access( pJEnv
, loader
) );
185 OUString java_env_name
= OUSTR(UNO_LB_JAVA
);
187 (uno_Environment
**)&java_env
, java_env_name
.pData
, vm_access
.get() );
189 OUString
aGetFactoryName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETFACTORY
) );
190 pSym
= osl_getFunctionSymbol( lib
, aGetFactoryName
.pData
);
193 if (loader_env
.is() && java_env
.is())
195 Mapping
java2dest( java_env
.get(), loader_env
.get() );
196 Mapping
dest2java( loader_env
.get(), java_env
.get() );
198 if (dest2java
.is() && java2dest
.is())
201 java2dest
.mapInterface(
202 jSMgr
, ::getCppuType((Reference
< lang::XMultiServiceFactory
> *) 0) );
204 java2dest
.mapInterface(
205 jRegKey
, ::getCppuType((Reference
< registry::XRegistryKey
> *) 0) );
207 const char* pImplName
= pJEnv
->GetStringUTFChars( jImplName
, NULL
);
209 void * pSSF
= (*((component_getFactoryFunc
)pSym
))(
210 pImplName
, pSMgr
, pKey
);
212 pJEnv
->ReleaseStringUTFChars( jImplName
, pImplName
);
214 uno_ExtEnvironment
* env
= loader_env
.get()->pExtEnv
;
217 (*env
->releaseInterface
)( env
, pKey
);
219 (*env
->releaseInterface
)( env
, pSMgr
);
223 jobject jglobal
= (jobject
) dest2java
.mapInterface(
224 pSSF
, getCppuType((Reference
< XInterface
> *) 0) );
225 joSLL_cpp
= pJEnv
->NewLocalRef( jglobal
);
226 pJEnv
->DeleteGlobalRef( jglobal
);
228 (*env
->releaseInterface
)( env
, pSSF
);
240 * Class: com_sun_star_comp_helper_RegistryServiceFactory
241 * Method: createRegistryServiceFactory
242 * Signature: (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/Object;
244 extern "C" JNIEXPORT jobject JNICALL
245 Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(
246 JNIEnv
* pJEnv
, jclass
, jstring jWriteRegFile
,
247 jstring jReadRegFile
, jboolean jbReadOnly
, jobject loader
)
249 jobject joRegServiceFac
= 0;
253 OUString aWriteRegFile
;
254 OUString aReadRegFile
;
256 sal_Bool bReadOnly
= jbReadOnly
== JNI_FALSE
? sal_False
: sal_True
;
259 const jchar
* pjReadRegFile
= pJEnv
->GetStringChars(jReadRegFile
, NULL
);
260 aReadRegFile
= OUString(pjReadRegFile
);
261 pJEnv
->ReleaseStringChars(jReadRegFile
, pjReadRegFile
);
265 const jchar
* pjWriteRegFile
= pJEnv
->GetStringChars(jWriteRegFile
, NULL
);
266 aWriteRegFile
= OUString(pjWriteRegFile
);
267 pJEnv
->ReleaseStringChars(jWriteRegFile
, pjWriteRegFile
);
271 Reference
< lang::XMultiServiceFactory
> rMSFac
;
272 if (aReadRegFile
.getLength() == 0)
273 rMSFac
= ::cppu::createRegistryServiceFactory( aWriteRegFile
, bReadOnly
);
275 rMSFac
= ::cppu::createRegistryServiceFactory(aWriteRegFile
, aReadRegFile
, bReadOnly
);
277 Reference
< beans::XPropertySet
> xProps(
278 rMSFac
, UNO_QUERY_THROW
);
279 Reference
< XComponentContext
> xContext(
280 xProps
->getPropertyValue( OUSTR("DefaultContext") ), UNO_QUERY_THROW
);
283 ::rtl::Reference
< ::jvmaccess::UnoVirtualMachine
> vm_access(
284 ::javaunohelper::create_vm_access( pJEnv
, loader
) );
285 // wrap vm singleton entry
286 xContext
= ::javaunohelper::install_vm_singleton( xContext
, vm_access
);
287 rMSFac
.set( xContext
->getServiceManager(), UNO_QUERY_THROW
);
290 OUString
aCurrentEnv(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME
));
291 OUString java_env_name
= OUSTR(UNO_LB_JAVA
);
292 Environment java_env
, curr_env
;
293 uno_getEnvironment((uno_Environment
**)&curr_env
, aCurrentEnv
.pData
, NULL
);
294 uno_getEnvironment( (uno_Environment
**)&java_env
, java_env_name
.pData
, vm_access
.get() );
296 Mapping
curr_java(curr_env
.get(), java_env
.get());
297 if (! curr_java
.is())
299 throw RuntimeException(
300 OUSTR("no C++ <-> Java mapping available!"), Reference
< XInterface
>() );
303 jobject joGlobalRegServiceFac
=
304 (jobject
)curr_java
.mapInterface(
306 getCppuType((Reference
< lang::XMultiServiceFactory
> *)0) );
307 joRegServiceFac
= pJEnv
->NewLocalRef( joGlobalRegServiceFac
);
308 pJEnv
->DeleteGlobalRef(joGlobalRegServiceFac
);
310 catch (Exception
& exc
)
312 jclass c
= pJEnv
->FindClass( "com/sun/star/uno/RuntimeException" );
315 OString
cstr( ::rtl::OUStringToOString(
316 exc
.Message
, RTL_TEXTENCODING_JAVA_UTF8
) );
317 OSL_TRACE( __FILE__
": forwarding Exception: %s", cstr
.getStr() );
318 pJEnv
->ThrowNew( c
, cstr
.getStr() );
323 OSL_TRACE("javaunohelper.cxx: object %i", joRegServiceFac
);
325 return joRegServiceFac
;