Branch libreoffice-5-0-4
[LibreOffice.git] / javaunohelper / source / preload.cxx
blob3858d8f9ce0a1c53aa69a92478a82734b22f143b
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 "jni.h"
24 #include "rtl/ustring.hxx"
25 #include "osl/module.hxx"
27 #include "juhx-export-types.hxx"
29 // In retrospect, the reason to create a juh wrapper around the juhx library was
30 // probably because java.lang.System.loadLibrary uses RTLD_LOCAL, so uniqueness
31 // of GCC RTTI symbols needed for exception handling would not be guaranteed.
33 #if ! defined SAL_DLLPREFIX
34 #define SAL_DLLPREFIX ""
35 #endif
38 extern "C"
41 static javaunohelper::detail::Func_writeInfo * s_writeInfo;
42 static javaunohelper::detail::Func_getFactory * s_getFactory;
43 static javaunohelper::detail::Func_bootstrap * s_bootstrap;
44 static bool s_inited = false;
46 extern "C" { static void SAL_CALL thisModule() {} }
49 static bool inited_juhx( JNIEnv * jni_env )
51 if (s_inited)
52 return true;
53 OUString lib_name = SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION;
54 osl::Module aModule;
55 if (!aModule.loadRelative(&thisModule, lib_name, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL))
57 jclass c = jni_env->FindClass( "java/lang/RuntimeException" );
58 jni_env->ThrowNew(
59 c, "error loading " SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION "!" );
60 return false;
62 else
64 OUString symbol =
65 "Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo";
66 s_writeInfo = reinterpret_cast<javaunohelper::detail::Func_writeInfo *>(aModule.getFunctionSymbol(symbol));
67 symbol =
68 "Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory";
69 s_getFactory = reinterpret_cast<javaunohelper::detail::Func_getFactory *>(aModule.getFunctionSymbol(symbol));
70 symbol =
71 "Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap";
72 s_bootstrap =
73 reinterpret_cast<javaunohelper::detail::Func_bootstrap *>(aModule.getFunctionSymbol(symbol));
75 if (0 == s_writeInfo ||
76 0 == s_getFactory ||
77 0 == s_bootstrap)
79 jclass c = jni_env->FindClass( "java/lang/RuntimeException" );
80 jni_env->ThrowNew(
81 c, "error resolving symbols of " SAL_DLLPREFIX "juhx" SAL_DLLEXTENSION "!" );
82 return false;
84 aModule.release();
86 s_inited = true;
87 return true;
91 SAL_DLLPUBLIC_EXPORT jboolean JNICALL
92 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1writeInfo(
93 JNIEnv * pJEnv, jclass jClass, jstring jLibName, jobject jSMgr,
94 jobject jRegKey, jobject loader )
96 if (inited_juhx( pJEnv ))
97 return (*s_writeInfo)(
98 pJEnv, jClass, jLibName, jSMgr, jRegKey, loader );
99 return JNI_FALSE;
102 SAL_DLLPUBLIC_EXPORT jobject JNICALL
103 Java_com_sun_star_comp_helper_SharedLibraryLoader_component_1getFactory(
104 JNIEnv * pJEnv, jclass jClass, jstring jLibName, jstring jImplName,
105 jobject jSMgr, jobject jRegKey, jobject loader )
107 if (inited_juhx( pJEnv ))
108 return (*s_getFactory)(
109 pJEnv, jClass, jLibName, jImplName, jSMgr, jRegKey, loader );
110 return 0;
113 SAL_DLLPUBLIC_EXPORT jobject JNICALL
114 Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
115 JNIEnv * jni_env, jclass jClass, jstring juno_rc, jobjectArray jpairs,
116 jobject loader )
118 if (inited_juhx( jni_env ))
119 return (*s_bootstrap)( jni_env, jClass, juno_rc, jpairs, loader );
120 return 0;
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */