1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_javaunohelper.hxx"
26 
27 #include "jni.h"
28 
29 #include "rtl/ustring.hxx"
30 #include "osl/module.h"
31 
32 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
33 
34 #if ! defined SAL_DLLPREFIX
35 #define SAL_DLLPREFIX ""
36 #endif
37 
38 using ::rtl::OUString;
39 
40 extern "C"
41 {
42 typedef jboolean (JNICALL * fptr_writeInfo)(
43     JNIEnv *, jclass, jstring, jobject, jobject, jobject );
44 typedef jobject (JNICALL * fptr_getFactory)(
45     JNIEnv *, jclass, jstring, jstring, jobject, jobject, jobject );
46 typedef jobject (JNICALL * fptr_createRegistryServiceFactory)(
47     JNIEnv *, jclass, jstring, jstring, jboolean, jobject );
48 typedef jobject (JNICALL * fptr_bootstrap)(
49     JNIEnv *_env, jclass, jstring, jobjectArray, jobject );
50 
51 static fptr_writeInfo s_writeInfo;
52 static fptr_getFactory s_getFactory;
53 static fptr_createRegistryServiceFactory s_createRegistryServiceFactory;
54 static fptr_bootstrap s_bootstrap;
55 static bool s_inited = false;
56 
thisModule()57 extern "C" { static void SAL_CALL thisModule() {} }
58 
59 //--------------------------------------------------------------------------------------------------
inited_juhx(JNIEnv * jni_env)60 static bool inited_juhx( JNIEnv * jni_env )
61 {
62     if (s_inited)
63         return true;
64     OUString lib_name = OUSTR(SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION);
65     oslModule hModule =
66         osl_loadModuleRelative( &thisModule, lib_name.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
67     if (0 == hModule)
68     {
69         jclass c = jni_env->FindClass( "java/lang/RuntimeException" );
70         jni_env->ThrowNew(
71             c, "error loading " SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION "!" );
72         return false;
73     }
74     else
75     {
76         OUString symbol =
77               OUSTR("Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo");
78         s_writeInfo = (fptr_writeInfo)osl_getFunctionSymbol(
79             hModule, symbol.pData );
80         symbol =
81             OUSTR("Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory");
82         s_getFactory = (fptr_getFactory)osl_getFunctionSymbol(
83             hModule, symbol.pData );
84         symbol =
85             OUSTR("Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory");
86         s_createRegistryServiceFactory =
87             (fptr_createRegistryServiceFactory)osl_getFunctionSymbol(
88                 hModule, symbol.pData );
89         symbol =
90             OUSTR("Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap");
91         s_bootstrap =
92             (fptr_bootstrap)osl_getFunctionSymbol( hModule, symbol.pData );
93 
94         if (0 == s_writeInfo ||
95             0 == s_getFactory ||
96             0 == s_createRegistryServiceFactory ||
97             0 == s_bootstrap)
98         {
99             jclass c = jni_env->FindClass( "java/lang/RuntimeException" );
100             jni_env->ThrowNew(
101                 c, "error resolving symbols of " SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION "!" );
102             return false;
103         }
104     }
105     s_inited = true;
106     return true;
107 }
108 
109 //==================================================================================================
110 JNIEXPORT jboolean JNICALL
Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(JNIEnv * pJEnv,jclass jClass,jstring jLibName,jobject jSMgr,jobject jRegKey,jobject loader)111 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
112     JNIEnv * pJEnv, jclass jClass, jstring jLibName, jobject jSMgr,
113     jobject jRegKey, jobject loader )
114 {
115     if (inited_juhx( pJEnv ))
116         return (*s_writeInfo)(
117             pJEnv, jClass, jLibName, jSMgr, jRegKey, loader );
118     return JNI_FALSE;
119 }
120 //==================================================================================================
121 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)122 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
123     JNIEnv * pJEnv, jclass jClass, jstring jLibName, jstring jImplName,
124     jobject jSMgr, jobject jRegKey, jobject loader )
125 {
126     if (inited_juhx( pJEnv ))
127         return (*s_getFactory)(
128             pJEnv, jClass, jLibName, jImplName, jSMgr, jRegKey, loader );
129     return 0;
130 }
131 //==================================================================================================
132 JNIEXPORT jobject JNICALL
Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(JNIEnv * pJEnv,jclass jClass,jstring jWriteRegFile,jstring jReadRegFile,jboolean jbReadOnly,jobject loader)133 Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(
134     JNIEnv * pJEnv, jclass jClass, jstring jWriteRegFile,
135     jstring jReadRegFile, jboolean jbReadOnly, jobject loader )
136 {
137     if (inited_juhx( pJEnv ))
138     {
139         return (*s_createRegistryServiceFactory)(
140             pJEnv, jClass, jWriteRegFile, jReadRegFile, jbReadOnly, loader );
141     }
142     return 0;
143 }
144 //==================================================================================================
145 JNIEXPORT jobject JNICALL
Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(JNIEnv * jni_env,jclass jClass,jstring juno_rc,jobjectArray jpairs,jobject loader)146 Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
147     JNIEnv * jni_env, jclass jClass, jstring juno_rc, jobjectArray jpairs,
148     jobject loader )
149 {
150     if (inited_juhx( jni_env ))
151         return (*s_bootstrap)( jni_env, jClass, juno_rc, jpairs, loader );
152     return 0;
153 }
154 }
155