Branch libreoffice-5-0-4
[LibreOffice.git] / javaunohelper / source / javaunohelper.cxx
blob145a753a9dddbba6a2200c58521d9c938d6207ee
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
22 #include <osl/diagnose.h>
23 #include <osl/module.hxx>
24 #include <osl/thread.h>
26 #include <uno/environment.hxx>
27 #include <uno/lbnames.h>
28 #include <uno/mapping.hxx>
30 #include <cppuhelper/factory.hxx>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/registry/XRegistryKey.hpp>
37 #include "jni.h"
38 #include "jvmaccess/virtualmachine.hxx"
39 #include "jvmaccess/unovirtualmachine.hxx"
41 #include "juhx-export-functions.hxx"
42 #include "vm.hxx"
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
48 * Class: com_sun_star_comp_helper_SharedLibraryLoader
49 * Method: component_writeInfo
50 * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z
52 jboolean Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
53 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jobject jSMgr,
54 jobject jRegKey, jobject loader )
56 bool bRet = false;
58 const jchar* pJLibName = pJEnv->GetStringChars(jLibName, NULL);
59 OUString aLibName(pJLibName);
60 pJEnv->ReleaseStringChars(jLibName, pJLibName);
62 #ifdef DISABLE_DYNLOADING
63 (void) jSMgr;
64 (void) jRegKey;
65 (void) loader;
67 fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(aLibName, osl_getThreadTextEncoding()).getStr());
68 #else
69 osl::Module lib(aLibName, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
70 if (lib.is())
72 // ========================= LATEST VERSION =========================
73 OUString aGetEnvName( COMPONENT_GETENV );
74 oslGenericFunction pSym = lib.getFunctionSymbol(aGetEnvName);
75 if (pSym)
77 Environment java_env, loader_env;
79 const sal_Char * pEnvTypeName = 0;
80 (*reinterpret_cast<component_getImplementationEnvironmentFunc>(pSym))(
81 &pEnvTypeName, reinterpret_cast<uno_Environment **>(&loader_env) );
82 if (! loader_env.is())
84 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
85 uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, 0 );
88 // create vm access
89 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
90 ::javaunohelper::create_vm_access( pJEnv, loader ) );
91 OUString java_env_name = UNO_LB_JAVA;
92 uno_getEnvironment(
93 reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() );
95 OUString aWriteInfoName( COMPONENT_WRITEINFO );
96 pSym = lib.getFunctionSymbol(aWriteInfoName);
97 if (pSym)
99 if (loader_env.is() && java_env.is())
101 Mapping java2dest(java_env.get(), loader_env.get());
103 if ( java2dest.is() )
105 void * pSMgr =
106 java2dest.mapInterface(
107 jSMgr, cppu::UnoType<lang::XMultiServiceFactory>::get());
108 void * pKey =
109 java2dest.mapInterface(
110 jRegKey, cppu::UnoType<registry::XRegistryKey>::get());
112 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
113 if (pKey)
115 bRet = (*reinterpret_cast<component_writeInfoFunc>(pSym))( pSMgr, pKey );
117 if (env)
118 (*env->releaseInterface)( env, pKey );
121 if (pSMgr && env)
122 (*env->releaseInterface)( env, pSMgr );
127 lib.release();
129 #endif
130 return bRet ? JNI_TRUE : JNI_FALSE;
134 * Class: com_sun_star_comp_helper_SharedLibraryLoader
135 * Method: component_getFactory
136 * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object;
138 jobject Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
139 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jstring jImplName,
140 jobject jSMgr, jobject jRegKey, jobject loader )
142 const jchar* pJLibName = pJEnv->GetStringChars(jLibName, NULL);
143 OUString aLibName(pJLibName);
144 pJEnv->ReleaseStringChars(jLibName, pJLibName);
146 #ifdef DISABLE_DYNLOADING
147 (void) jImplName;
148 (void) jSMgr;
149 (void) jRegKey;
150 (void) loader;
152 fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(aLibName, osl_getThreadTextEncoding()).getStr());
153 #endif
155 aLibName += OUString( SAL_DLLEXTENSION );
157 jobject joSLL_cpp = 0;
159 #ifndef DISABLE_DYNLOADING
160 osl::Module lib(aLibName, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
161 if (lib.is())
163 // ========================= LATEST VERSION =========================
164 OUString aGetEnvName( COMPONENT_GETENV );
165 oslGenericFunction pSym = lib.getFunctionSymbol(aGetEnvName);
166 if (pSym)
168 Environment java_env, loader_env;
170 const sal_Char * pEnvTypeName = 0;
171 (*reinterpret_cast<component_getImplementationEnvironmentFunc>(pSym))(
172 &pEnvTypeName, reinterpret_cast<uno_Environment **>(&loader_env) );
174 if (! loader_env.is())
176 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
177 uno_getEnvironment( reinterpret_cast<uno_Environment **>(&loader_env), aEnvTypeName.pData, 0 );
180 // create vm access
181 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
182 ::javaunohelper::create_vm_access( pJEnv, loader ) );
183 OUString java_env_name = UNO_LB_JAVA;
184 uno_getEnvironment(
185 reinterpret_cast<uno_Environment **>(&java_env), java_env_name.pData, vm_access.get() );
187 OUString aGetFactoryName( COMPONENT_GETFACTORY );
188 pSym = lib.getFunctionSymbol(aGetFactoryName);
189 if (pSym)
191 if (loader_env.is() && java_env.is())
193 Mapping java2dest( java_env.get(), loader_env.get() );
194 Mapping dest2java( loader_env.get(), java_env.get() );
196 if (dest2java.is() && java2dest.is())
198 void * pSMgr =
199 java2dest.mapInterface(
200 jSMgr, cppu::UnoType<lang::XMultiServiceFactory>::get());
201 void * pKey =
202 java2dest.mapInterface(
203 jRegKey, cppu::UnoType<registry::XRegistryKey>::get());
205 const char* pImplName = pJEnv->GetStringUTFChars( jImplName, NULL );
207 void * pSSF = (*reinterpret_cast<component_getFactoryFunc>(pSym))(
208 pImplName, pSMgr, pKey );
210 pJEnv->ReleaseStringUTFChars( jImplName, pImplName );
212 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
214 if (pKey && env)
215 (*env->releaseInterface)( env, pKey );
216 if (pSMgr && env)
217 (*env->releaseInterface)( env, pSMgr );
219 if (pSSF)
221 jobject jglobal = static_cast<jobject>(dest2java.mapInterface(
222 pSSF, cppu::UnoType<XInterface>::get()));
223 joSLL_cpp = pJEnv->NewLocalRef( jglobal );
224 pJEnv->DeleteGlobalRef( jglobal );
225 if (env)
226 (*env->releaseInterface)( env, pSSF );
232 lib.release();
234 #endif
235 return joSLL_cpp;
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */