1 /*
2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25 /*
26 * jtie_lib.hpp
27 */
28
29 #ifndef jtie_lib_hpp
30 #define jtie_lib_hpp
31
32 #include <jni.h>
33
34 #include "jtie.hpp"
35 #include "helpers.hpp"
36
37 // ---------------------------------------------------------------------------
38 // JTie Template Library: Include these global symbol definitions in
39 // exactly one compilation unit
40 // ---------------------------------------------------------------------------
41
42 // ---------------------------------------------------------------------------
43 // JTie Library: Global Variable Definitions & Template Instantiations
44 // ---------------------------------------------------------------------------
45
46 JTIE_INSTANTIATE_CLASS_MEMBER_INFO(_ByteBuffer_isReadOnly,
47 "java/nio/ByteBuffer",
48 "isReadOnly",
49 "()Z")
50
51 JTIE_INSTANTIATE_CLASS_MEMBER_INFO(_ByteBuffer_asReadOnlyBuffer,
52 "java/nio/ByteBuffer",
53 "asReadOnlyBuffer",
54 "()Ljava/nio/ByteBuffer;")
55
56 JTIE_INSTANTIATE_CLASS_MEMBER_INFO(_ByteBuffer_remaining,
57 "java/nio/ByteBuffer",
58 "remaining",
59 "()I")
60
61 JTIE_INSTANTIATE_CLASS_MEMBER_INFO(_ByteBuffer_position,
62 "java/nio/ByteBuffer",
63 "position",
64 "()I")
65
66 JTIE_INSTANTIATE_CLASS_MEMBER_INFO(_Wrapper_cdelegate,
67 "com/mysql/jtie/Wrapper",
68 "cdelegate",
69 "J")
70
71 // ---------------------------------------------------------------------------
72 // JTie Library: Load and Unload Handlers
73 // ---------------------------------------------------------------------------
74
75 // root object, which allows Threads to obtain their local JNIEnv
76 static JavaVM * jtie_cached_jvm;
77
78 /**
79 * Handler function to be called from a user-defined function JNI_OnLoad
80 * with the same signature.
81 *
82 * Initializes JTie's resources (e.g., cached JNI method and field ids)
83 * when the native, JTie-based wrapper library is loaded into a Java VM.
84 * As of JDK 1.2, the same JNI native library cannot be loaded into more
85 * than one class loader at a time (UnsatisfiedLinkError).
86 *
87 * Returns the JNI version needed by JTie or JNI_ERR with a pending error.
88 * If the VM does not recognize the version number returned by JNI_OnLoad,
89 * the native library cannot be loaded.
90 */
91 jint
JTie_OnLoad(JavaVM * jvm,void * reserved)92 JTie_OnLoad(JavaVM * jvm, void * reserved)
93 {
94 TRACE("jint JTie_OnLoad(JavaVM *, void *)");
95 (void)reserved;
96 VERBOSE("initializing the JTie resources ...");
97
98 // beware of circular loading dependencies: do not load classes here
99 // whose static initializers have a dependency upon this native library...
100
101 // cache the JavaVM pointer
102 jtie_cached_jvm = jvm;
103
104 // get the JNI environment
105 JNIEnv * env;
106 if (jvm->GetEnv((void**)&env, JNI_VERSION_1_2) != JNI_OK) {
107 return JNI_ERR; // unsupported version or thread not attached to VM
108 }
109
110 // JTie requires JDK 1.4 JNI functions (e.g., direct ByteBuffer access)
111 VERBOSE("... initialized the JTie resources");
112 return JNI_VERSION_1_4;
113 }
114
115 /**
116 * Handler function to be called from a user-defined function JNI_OnUnload
117 * with the same signature.
118 *
119 * Frees JTie's resources (e.g., cached JNI ids) when the class loader
120 * containing the native, JTie-based wrapper library is garbage-collected.
121 *
122 * This function is called in an unknown context (such as from a finalizer),
123 * which requires to be conservative and refrain from arbitrary Java
124 * call-backs (classes have been unloaded when JNI_OnUnload is invoked).
125 */
126 void
JTie_OnUnload(JavaVM * jvm,void * reserved)127 JTie_OnUnload(JavaVM * jvm, void * reserved)
128 {
129 TRACE("void JTie_OnUnload(JavaVM *, void *)");
130 (void)reserved;
131 VERBOSE("releasing the JTie resources ...");
132
133 // get the JNI environment
134 JNIEnv * env;
135 if (jvm->GetEnv((void**)&env, JNI_VERSION_1_2) != JNI_OK) {
136 return; // unsupported version or thread not attached to VM
137 }
138
139 jtie_cached_jvm = NULL;
140 VERBOSE("... released the JTie resources");
141 }
142
143 // ---------------------------------------------------------------------------
144 // JTie Library: Wrapper Class Load Handler
145 // ---------------------------------------------------------------------------
146
147 #if 0 // XXX not implemented, see comments in Wrapper.java
148
149 // XXX review & ceanup...
150
151 // XXX document: cannot cache/store:
152 // JNIEnv -- thread-specific
153 // local references -- call-frame- (and thread-) specific
154 // Local references are valid only inside a single invocation of a native method
155 // Local references are valid only within the thread in which they are created; do not pass between threads
156 // Create a global reference when it is necessary to pass a reference across threads.
157
158 #include "jtie_Wrapper.h"
159 // XXX how to avoid?
160 //#include "jtie_tconv_idcache_impl.hpp"
161
162 extern "C" {
163 JNIEXPORT void JNICALL
164 Java_com_mysql_jtie_Wrapper_initIds(JNIEnv *, jclass);
165
166 /**
167 * Handler function called when the class Wrapper is loaded.
168 */
169 JNIEXPORT void JNICALL
170 Java_com_mysql_jtie_Wrapper_initIds(JNIEnv * env, jclass cls)
171 {
172 TRACE("void Java_com_mysql_jtie_Wrapper_initIds(JNIEnv *, jclass)");
173 // store class in a weak global ref to allow for class to be unloaded
174 jtie_cls_com_mysql_jtie_Wrapper = env->NewWeakGlobalRef(cls);
175 if (jtie_cls_com_mysql_jtie_Wrapper == NULL) {
176 return; // OutOfMemoryError pending
177 }
178 }
179
180 }
181
182 #endif
183
184 // ---------------------------------------------------------------------------
185
186 #endif // jtie_lib_hpp
187