[java-atk-wrapper] cache JVM onLoad so env attaches current thread



commit fbce3ddc685e2964a7f82839105e9b4a40659b7e
Author: Magdalen Berns <m berns thismagpie com>
Date:   Sun Jan 18 13:25:08 2015 +0000

    cache JVM onLoad so env attaches current thread
    
    Bug: https://bugzilla.gnome.org/show_bug.cgi?id=743122

 jni/src/AtkWrapper.c |   12 ---------
 jni/src/jawutil.c    |   61 ++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 24 deletions(-)
---
diff --git a/jni/src/AtkWrapper.c b/jni/src/AtkWrapper.c
index 1b4aebe..5b18f9c 100644
--- a/jni/src/AtkWrapper.c
+++ b/jni/src/AtkWrapper.c
@@ -69,16 +69,6 @@ static GMainLoop* jni_main_loop;
 static int jaw_initialized = 0;
 
 gpointer current_bridge_data = NULL;
-JavaVM* cachedJVM;
-JNIEnv *cachedEnv;
-
-JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserve)
-{
-  return JNI_VERSION_1_6;
-}
-
-JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserve) {
-}
 
 gboolean jaw_accessibility_init (void)
 {
@@ -1087,7 +1077,6 @@ key_dispatch_handler (gpointer p)
   JNIEnv *jniEnv = jaw_util_get_jni_env();
 
   key_dispatch_result = 0;
-  cachedEnv = jniEnv;
 
   AtkKeyEventStruct *event = g_new0(AtkKeyEventStruct, 1);
   jclass classAtkKeyEvent = (*jniEnv)->FindClass(jniEnv, "org/GNOME/Accessibility/AtkKeyEvent");
@@ -1181,7 +1170,6 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_dispatchKeyEvent(JNIEnv *jniEnv,
                                                                  jobject jAtkKeyEvent)
 {
   jboolean key_consumed;
-  cachedEnv = jniEnv;
   jobject global_key_event = (*jniEnv)->NewGlobalRef(jniEnv, jAtkKeyEvent);
 
   g_mutex_lock(key_dispatch_mutex);
diff --git a/jni/src/jawutil.c b/jni/src/jawutil.c
index 1d6c7f4..eaa7409 100644
--- a/jni/src/jawutil.c
+++ b/jni/src/jawutil.c
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <glib.h>
+#include <glib/gprintf.h>
 #include "jawutil.h"
 #include "jawtoplevel.h"
 #include "jawobject.h"
@@ -393,24 +394,60 @@ jaw_util_is_same_jobject(gconstpointer a,
        }
 }
 
+JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserve)
+{
+  if (jvm == NULL) {
+    g_error("JavaVM pointer was NULL when initializing library");
+  }
+  cachedJVM = jvm;
+  return JNI_VERSION_1_6;
+}
+
+JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserve) {
+}
+
 JNIEnv*
 jaw_util_get_jni_env(void)
 {
   JNIEnv *env;
-  JavaVM *jvm;
-  jvm = cachedJVM;
-  env = cachedEnv;
-  if (jvm == NULL) (*env)->GetJavaVM(env,&jvm);
-  int res;
+  env  = NULL;
+  static int i;
+
+  i = 0;
+  void* ptr;
+  ptr = NULL;
+  JavaVMAttachArgs args = { 0, };
+  jint res;
+
   #ifdef JNI_VERSION_1_6
-    res = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
-  #else
-    (res*) = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
+  res = (*cachedJVM)->GetEnv(cachedJVM, &ptr, JNI_VERSION_1_6);
   #endif
-  if (&res < 0) {
-    fprintf(stderr, "Attach failed\n");
-  }
-  return env;
+  env = (JNIEnv*) ptr;
+
+  if (env != NULL)
+    return env;
+
+    switch (res)
+    {
+      case JNI_EDETACHED:
+        args.version = JNI_VERSION_1_6;
+        args.name = g_strdup_printf("NativeThread %d", i++);
+        res = (*cachedJVM)->AttachCurrentThread(cachedJVM, &ptr, NULL);
+        env = (JNIEnv*) ptr;
+        if ((res == JNI_OK) && (env != NULL))
+        {
+          g_free(args.name);
+          return env;
+        }
+        g_printerr("\n *** Attach failed. *** JNIEnv thread is detached.\n");
+        break;
+      case JNI_EVERSION:
+        g_printerr(" *** Version error *** \n");
+        break;
+    }
+    fflush(stderr);
+    exit(2);
+  return NULL;
 }
 
 static jobject


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]