Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / javaunohelper / source / javaunohelper.cxx
blob9016b486ee3e6848999e6ddb1016f65a5b0a22b2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <osl/diagnose.h>
31 #include <osl/module.h>
33 #include <uno/environment.hxx>
34 #include <uno/mapping.hxx>
36 #include <cppuhelper/factory.hxx>
37 #include <cppuhelper/servicefactory.hxx>
38 #include <cppuhelper/component_context.hxx>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/registry/XRegistryKey.hpp>
45 #include "jni.h"
46 #include "jvmaccess/virtualmachine.hxx"
47 #include "jvmaccess/unovirtualmachine.hxx"
49 #include "vm.hxx"
51 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::uno;
56 using ::rtl::OString;
57 using ::rtl::OUString;
60 * Class: com_sun_star_comp_helper_SharedLibraryLoader
61 * Method: component_writeInfo
62 * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z
64 extern "C" JNIEXPORT jboolean JNICALL
65 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
66 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jobject jSMgr,
67 jobject jRegKey, jobject loader )
69 sal_Bool bRet = sal_False;
71 const jchar* pJLibName = pJEnv->GetStringChars( jLibName, NULL );
72 OUString aLibName( pJLibName );
73 pJEnv->ReleaseStringChars( jLibName, pJLibName);
75 oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
76 if (lib)
78 // ========================= LATEST VERSION =========================
79 OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
80 oslGenericFunction pSym =
81 osl_getFunctionSymbol( lib, aGetEnvName.pData );
82 if (pSym)
84 Environment java_env, loader_env;
86 const sal_Char * pEnvTypeName = 0;
87 (*((component_getImplementationEnvironmentFunc)pSym))(
88 &pEnvTypeName, (uno_Environment **)&loader_env );
89 if (! loader_env.is())
91 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
92 uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
95 // create vm access
96 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
97 ::javaunohelper::create_vm_access( pJEnv, loader ) );
98 OUString java_env_name = OUSTR(UNO_LB_JAVA);
99 uno_getEnvironment(
100 (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
102 OUString aWriteInfoName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_WRITEINFO) );
103 pSym = osl_getFunctionSymbol( lib, aWriteInfoName.pData );
104 if (pSym)
106 if (loader_env.is() && java_env.is())
108 Mapping java2dest(java_env.get(), loader_env.get());
110 if ( java2dest.is() )
112 void * pSMgr =
113 java2dest.mapInterface(
114 jSMgr, getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
115 void * pKey =
116 java2dest.mapInterface(
117 jRegKey, getCppuType((Reference< registry::XRegistryKey > *) 0) );
119 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
120 if (pKey)
122 bRet = (*((component_writeInfoFunc)pSym))( pSMgr, pKey );
124 if (env)
125 (*env->releaseInterface)( env, pKey );
128 if (pSMgr && env)
129 (*env->releaseInterface)( env, pSMgr );
136 return bRet == sal_False? JNI_FALSE : JNI_TRUE;
140 * Class: com_sun_star_comp_helper_SharedLibraryLoader
141 * Method: component_getFactory
142 * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object;
144 extern "C" JNIEXPORT jobject JNICALL
145 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
146 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jstring jImplName,
147 jobject jSMgr, jobject jRegKey, jobject loader )
149 const jchar* pJLibName = pJEnv->GetStringChars(jLibName, NULL);
150 OUString aLibName( pJLibName );
151 pJEnv->ReleaseStringChars( jLibName, pJLibName);
153 aLibName += OUString( RTL_CONSTASCII_USTRINGPARAM(SAL_DLLEXTENSION) );
155 jobject joSLL_cpp = 0;
157 oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
158 if (lib)
160 // ========================= LATEST VERSION =========================
161 OUString aGetEnvName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETENV) );
162 oslGenericFunction pSym =
163 osl_getFunctionSymbol( lib, aGetEnvName.pData );
164 if (pSym)
166 Environment java_env, loader_env;
168 const sal_Char * pEnvTypeName = 0;
169 (*((component_getImplementationEnvironmentFunc)pSym))(
170 &pEnvTypeName, (uno_Environment **)&loader_env );
172 if (! loader_env.is())
174 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
175 uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
178 // create vm access
179 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
180 ::javaunohelper::create_vm_access( pJEnv, loader ) );
181 OUString java_env_name = OUSTR(UNO_LB_JAVA);
182 uno_getEnvironment(
183 (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
185 OUString aGetFactoryName( RTL_CONSTASCII_USTRINGPARAM(COMPONENT_GETFACTORY) );
186 pSym = osl_getFunctionSymbol( lib, aGetFactoryName.pData );
187 if (pSym)
189 if (loader_env.is() && java_env.is())
191 Mapping java2dest( java_env.get(), loader_env.get() );
192 Mapping dest2java( loader_env.get(), java_env.get() );
194 if (dest2java.is() && java2dest.is())
196 void * pSMgr =
197 java2dest.mapInterface(
198 jSMgr, ::getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
199 void * pKey =
200 java2dest.mapInterface(
201 jRegKey, ::getCppuType((Reference< registry::XRegistryKey > *) 0) );
203 const char* pImplName = pJEnv->GetStringUTFChars( jImplName, NULL );
205 void * pSSF = (*((component_getFactoryFunc)pSym))(
206 pImplName, pSMgr, pKey );
208 pJEnv->ReleaseStringUTFChars( jImplName, pImplName );
210 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
212 if (pKey && env)
213 (*env->releaseInterface)( env, pKey );
214 if (pSMgr && env)
215 (*env->releaseInterface)( env, pSMgr );
217 if (pSSF)
219 jobject jglobal = (jobject) dest2java.mapInterface(
220 pSSF, getCppuType((Reference< XInterface > *) 0) );
221 joSLL_cpp = pJEnv->NewLocalRef( jglobal );
222 pJEnv->DeleteGlobalRef( jglobal );
223 if (env)
224 (*env->releaseInterface)( env, pSSF );
232 return joSLL_cpp;
236 * Class: com_sun_star_comp_helper_RegistryServiceFactory
237 * Method: createRegistryServiceFactory
238 * Signature: (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/Object;
240 extern "C" JNIEXPORT jobject JNICALL
241 Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(
242 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jWriteRegFile,
243 jstring jReadRegFile, jboolean jbReadOnly, jobject loader )
245 jobject joRegServiceFac = 0;
249 OUString aWriteRegFile;
250 OUString aReadRegFile;
252 sal_Bool bReadOnly = jbReadOnly == JNI_FALSE ? sal_False : sal_True;
254 if (jReadRegFile) {
255 const jchar* pjReadRegFile = pJEnv->GetStringChars(jReadRegFile, NULL);
256 aReadRegFile = OUString(pjReadRegFile);
257 pJEnv->ReleaseStringChars(jReadRegFile, pjReadRegFile);
260 if (jWriteRegFile) {
261 const jchar * pjWriteRegFile = pJEnv->GetStringChars(jWriteRegFile, NULL);
262 aWriteRegFile = OUString(pjWriteRegFile);
263 pJEnv->ReleaseStringChars(jWriteRegFile, pjWriteRegFile);
266 // bootstrap
267 Reference< lang::XMultiServiceFactory > rMSFac;
268 if (aReadRegFile.isEmpty())
269 rMSFac = ::cppu::createRegistryServiceFactory( aWriteRegFile, bReadOnly);
270 else
271 rMSFac = ::cppu::createRegistryServiceFactory(aWriteRegFile, aReadRegFile, bReadOnly);
273 Reference< beans::XPropertySet > xProps(
274 rMSFac, UNO_QUERY_THROW );
275 Reference< XComponentContext > xContext(
276 xProps->getPropertyValue( OUSTR("DefaultContext") ), UNO_QUERY_THROW );
278 // create vm access
279 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
280 ::javaunohelper::create_vm_access( pJEnv, loader ) );
281 // wrap vm singleton entry
282 xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
283 rMSFac.set( xContext->getServiceManager(), UNO_QUERY_THROW );
285 // get uno envs
286 OUString aCurrentEnv(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
287 OUString java_env_name = OUSTR(UNO_LB_JAVA);
288 Environment java_env, curr_env;
289 uno_getEnvironment((uno_Environment **)&curr_env, aCurrentEnv.pData, NULL);
290 uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
292 Mapping curr_java(curr_env.get(), java_env.get());
293 if (! curr_java.is())
295 throw RuntimeException(
296 OUSTR("no C++ <-> Java mapping available!"), Reference< XInterface >() );
299 jobject joGlobalRegServiceFac =
300 (jobject)curr_java.mapInterface(
301 rMSFac.get(),
302 getCppuType((Reference< lang::XMultiServiceFactory > *)0) );
303 joRegServiceFac = pJEnv->NewLocalRef( joGlobalRegServiceFac );
304 pJEnv->DeleteGlobalRef(joGlobalRegServiceFac);
306 catch (Exception & exc)
308 jclass c = pJEnv->FindClass( "com/sun/star/uno/RuntimeException" );
309 if (0 != c)
311 OString cstr( ::rtl::OUStringToOString(
312 exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
313 OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
314 pJEnv->ThrowNew( c, cstr.getStr() );
316 return 0;
319 OSL_TRACE("javaunohelper.cxx: object %i", joRegServiceFac);
321 return joRegServiceFac;
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */