xref: /trunk/main/javaunohelper/source/preload.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_javaunohelper.hxx"
30 
31 #include "jni.h"
32 
33 #include "rtl/ustring.hxx"
34 #include "osl/module.h"
35 
36 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
37 
38 #if ! defined SAL_DLLPREFIX
39 #define SAL_DLLPREFIX ""
40 #endif
41 
42 using ::rtl::OUString;
43 
44 extern "C"
45 {
46 typedef jboolean (JNICALL * fptr_writeInfo)(
47     JNIEnv *, jclass, jstring, jobject, jobject, jobject );
48 typedef jobject (JNICALL * fptr_getFactory)(
49     JNIEnv *, jclass, jstring, jstring, jobject, jobject, jobject );
50 typedef jobject (JNICALL * fptr_createRegistryServiceFactory)(
51     JNIEnv *, jclass, jstring, jstring, jboolean, jobject );
52 typedef jobject (JNICALL * fptr_bootstrap)(
53     JNIEnv *_env, jclass, jstring, jobjectArray, jobject );
54 
55 static fptr_writeInfo s_writeInfo;
56 static fptr_getFactory s_getFactory;
57 static fptr_createRegistryServiceFactory s_createRegistryServiceFactory;
58 static fptr_bootstrap s_bootstrap;
59 static bool s_inited = false;
60 
61 extern "C" { static void SAL_CALL thisModule() {} }
62 
63 //--------------------------------------------------------------------------------------------------
64 static bool inited_juhx( JNIEnv * jni_env )
65 {
66     if (s_inited)
67         return true;
68     OUString lib_name = OUSTR(SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION);
69     oslModule hModule =
70         osl_loadModuleRelative( &thisModule, lib_name.pData, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL );
71     if (0 == hModule)
72     {
73         jclass c = jni_env->FindClass( "java/lang/RuntimeException" );
74         jni_env->ThrowNew(
75             c, "error loading " SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION "!" );
76         return false;
77     }
78     else
79     {
80         OUString symbol =
81               OUSTR("Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo");
82         s_writeInfo = (fptr_writeInfo)osl_getFunctionSymbol(
83             hModule, symbol.pData );
84         symbol =
85             OUSTR("Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory");
86         s_getFactory = (fptr_getFactory)osl_getFunctionSymbol(
87             hModule, symbol.pData );
88         symbol =
89             OUSTR("Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory");
90         s_createRegistryServiceFactory =
91             (fptr_createRegistryServiceFactory)osl_getFunctionSymbol(
92                 hModule, symbol.pData );
93         symbol =
94             OUSTR("Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap");
95         s_bootstrap =
96             (fptr_bootstrap)osl_getFunctionSymbol( hModule, symbol.pData );
97 
98         if (0 == s_writeInfo ||
99             0 == s_getFactory ||
100             0 == s_createRegistryServiceFactory ||
101             0 == s_bootstrap)
102         {
103             jclass c = jni_env->FindClass( "java/lang/RuntimeException" );
104             jni_env->ThrowNew(
105                 c, "error resolving symbols of " SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION "!" );
106             return false;
107         }
108     }
109     s_inited = true;
110     return true;
111 }
112 
113 //==================================================================================================
114 JNIEXPORT jboolean JNICALL
115 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
116     JNIEnv * pJEnv, jclass jClass, jstring jLibName, jobject jSMgr,
117     jobject jRegKey, jobject loader )
118 {
119     if (inited_juhx( pJEnv ))
120         return (*s_writeInfo)(
121             pJEnv, jClass, jLibName, jSMgr, jRegKey, loader );
122     return JNI_FALSE;
123 }
124 //==================================================================================================
125 JNIEXPORT jobject JNICALL
126 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
127     JNIEnv * pJEnv, jclass jClass, jstring jLibName, jstring jImplName,
128     jobject jSMgr, jobject jRegKey, jobject loader )
129 {
130     if (inited_juhx( pJEnv ))
131         return (*s_getFactory)(
132             pJEnv, jClass, jLibName, jImplName, jSMgr, jRegKey, loader );
133     return 0;
134 }
135 //==================================================================================================
136 JNIEXPORT jobject JNICALL
137 Java_com_sun_star_comp_helper_RegistryServiceFactory_createRegistryServiceFactory(
138     JNIEnv * pJEnv, jclass jClass, jstring jWriteRegFile,
139     jstring jReadRegFile, jboolean jbReadOnly, jobject loader )
140 {
141     if (inited_juhx( pJEnv ))
142     {
143         return (*s_createRegistryServiceFactory)(
144             pJEnv, jClass, jWriteRegFile, jReadRegFile, jbReadOnly, loader );
145     }
146     return 0;
147 }
148 //==================================================================================================
149 JNIEXPORT jobject JNICALL
150 Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
151     JNIEnv * jni_env, jclass jClass, jstring juno_rc, jobjectArray jpairs,
152     jobject loader )
153 {
154     if (inited_juhx( jni_env ))
155         return (*s_bootstrap)( jni_env, jClass, juno_rc, jpairs, loader );
156     return 0;
157 }
158 }
159