bump product version to 4.1.6.2
[LibreOffice.git] / javaunohelper / source / javaunohelper.cxx
blob0ba0b5fdea6c35db7de3ab8b6d36afc3f5603320
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.h>
25 #include <uno/environment.hxx>
26 #include <uno/mapping.hxx>
28 #include <cppuhelper/factory.hxx>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/registry/XRegistryKey.hpp>
35 #include "jni.h"
36 #include "jvmaccess/virtualmachine.hxx"
37 #include "jvmaccess/unovirtualmachine.hxx"
39 #include "juhx-export-functions.hxx"
40 #include "vm.hxx"
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
46 * Class: com_sun_star_comp_helper_SharedLibraryLoader
47 * Method: component_writeInfo
48 * Signature: (Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Z
50 jboolean Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
51 JNIEnv * pJEnv, SAL_UNUSED_PARAMETER jclass, jstring jLibName, jobject jSMgr,
52 jobject jRegKey, jobject loader )
54 sal_Bool bRet = sal_False;
56 const jchar* pJLibName = pJEnv->GetStringChars( jLibName, NULL );
57 OUString aLibName( pJLibName );
58 pJEnv->ReleaseStringChars( jLibName, pJLibName);
60 #ifdef DISABLE_DYNLOADING
61 (void) jSMgr;
62 (void) jRegKey;
63 (void) loader;
65 fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(pJLibName, RTL_TEXTENCODING_JAVA_UTF8).getStr());
66 #else
67 oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
68 if (lib)
70 // ========================= LATEST VERSION =========================
71 OUString aGetEnvName( COMPONENT_GETENV );
72 oslGenericFunction pSym =
73 osl_getFunctionSymbol( lib, aGetEnvName.pData );
74 if (pSym)
76 Environment java_env, loader_env;
78 const sal_Char * pEnvTypeName = 0;
79 (*((component_getImplementationEnvironmentFunc)pSym))(
80 &pEnvTypeName, (uno_Environment **)&loader_env );
81 if (! loader_env.is())
83 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
84 uno_getEnvironment( (uno_Environment **)&loader_env, aEnvTypeName.pData, 0 );
87 // create vm access
88 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
89 ::javaunohelper::create_vm_access( pJEnv, loader ) );
90 OUString java_env_name = UNO_LB_JAVA;
91 uno_getEnvironment(
92 (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
94 OUString aWriteInfoName( COMPONENT_WRITEINFO );
95 pSym = osl_getFunctionSymbol( lib, aWriteInfoName.pData );
96 if (pSym)
98 if (loader_env.is() && java_env.is())
100 Mapping java2dest(java_env.get(), loader_env.get());
102 if ( java2dest.is() )
104 void * pSMgr =
105 java2dest.mapInterface(
106 jSMgr, getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
107 void * pKey =
108 java2dest.mapInterface(
109 jRegKey, getCppuType((Reference< registry::XRegistryKey > *) 0) );
111 uno_ExtEnvironment * env = loader_env.get()->pExtEnv;
112 if (pKey)
114 bRet = (*((component_writeInfoFunc)pSym))( pSMgr, pKey );
116 if (env)
117 (*env->releaseInterface)( env, pKey );
120 if (pSMgr && env)
121 (*env->releaseInterface)( env, pSMgr );
127 #endif
128 return bRet == sal_False? JNI_FALSE : JNI_TRUE;
132 * Class: com_sun_star_comp_helper_SharedLibraryLoader
133 * Method: component_getFactory
134 * Signature: (Ljava/lang/String;Ljava/lang/String;Lcom/sun/star/lang/XMultiServiceFactory;Lcom/sun/star/registry/XRegistryKey;)Ljava/lang/Object;
136 jobject 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 )
140 const jchar* pJLibName = pJEnv->GetStringChars(jLibName, NULL);
142 #ifdef DISABLE_DYNLOADING
143 (void) jImplName;
144 (void) jSMgr;
145 (void) jRegKey;
146 (void) loader;
148 fprintf(stderr, "Hmm, %s called for %s\n", __PRETTY_FUNCTION__, OUStringToOString(pJLibName, RTL_TEXTENCODING_JAVA_UTF8).getStr());
149 #endif
151 OUString aLibName( pJLibName );
152 pJEnv->ReleaseStringChars( jLibName, pJLibName);
154 aLibName += OUString( SAL_DLLEXTENSION );
156 jobject joSLL_cpp = 0;
158 #ifndef DISABLE_DYNLOADING
159 oslModule lib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
160 if (lib)
162 // ========================= LATEST VERSION =========================
163 OUString aGetEnvName( COMPONENT_GETENV );
164 oslGenericFunction pSym =
165 osl_getFunctionSymbol( lib, aGetEnvName.pData );
166 if (pSym)
168 Environment java_env, loader_env;
170 const sal_Char * pEnvTypeName = 0;
171 (*((component_getImplementationEnvironmentFunc)pSym))(
172 &pEnvTypeName, (uno_Environment **)&loader_env );
174 if (! loader_env.is())
176 OUString aEnvTypeName( OUString::createFromAscii( pEnvTypeName ) );
177 uno_getEnvironment( (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 (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
187 OUString aGetFactoryName( COMPONENT_GETFACTORY );
188 pSym = osl_getFunctionSymbol( lib, aGetFactoryName.pData );
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, ::getCppuType((Reference< lang::XMultiServiceFactory > *) 0) );
201 void * pKey =
202 java2dest.mapInterface(
203 jRegKey, ::getCppuType((Reference< registry::XRegistryKey > *) 0) );
205 const char* pImplName = pJEnv->GetStringUTFChars( jImplName, NULL );
207 void * pSSF = (*((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 = (jobject) dest2java.mapInterface(
222 pSSF, getCppuType((Reference< XInterface > *) 0) );
223 joSLL_cpp = pJEnv->NewLocalRef( jglobal );
224 pJEnv->DeleteGlobalRef( jglobal );
225 if (env)
226 (*env->releaseInterface)( env, pSSF );
233 #endif
234 return joSLL_cpp;
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */