1 /* Copyright (C) 2005-2011 Fabio Riccardi */ 2 3 /* 4 * JavaAppLauncher: a simple Java application launcher for Mac OS X. 5 * LC_JNIUtils.cpp 6 * 7 * Paul J. Lucas [paul@lightcrafts.com] 8 */ 9 10 // standard 11 #include <cstdlib> 12 #include <iostream> 13 14 // local 15 #include "LC_JNIUtils.h" 16 17 using namespace std; 18 19 extern JavaVM *g_jvm; 20 21 /** 22 * Attach to the current JVM thread. 23 */ LC_attachCurrentThread()24JNIEnv* LC_attachCurrentThread() { 25 JNIEnv *env; 26 if ( g_jvm->AttachCurrentThread( (void**)&env, NULL ) != 0 ) { 27 cerr << "AttachCurrentThread() failed" << endl; 28 ::exit( 1 ); 29 } 30 return env; 31 } 32 33 /** 34 * Check to see if Java threw an exception: if so, report it, then clear it. 35 */ LC_exceptionOccurred(JNIEnv * env)36bool LC_exceptionOccurred( JNIEnv *env ) { 37 bool const exceptionOccurred = env->ExceptionCheck(); 38 if ( exceptionOccurred ) { 39 env->ExceptionDescribe(); 40 env->ExceptionClear(); 41 } 42 return exceptionOccurred; 43 } 44 45 /** 46 * Gets the JNI env for the current thread. 47 */ LC_getJNIEnv(int * mustDetach)48JNIEnv* LC_getJNIEnv( int *mustDetach ) { 49 JNIEnv *env; 50 switch ( g_jvm->GetEnv( (void**)&env, JNI_VERSION_1_4 ) ) { 51 case JNI_OK: 52 if ( mustDetach ) 53 *mustDetach = false; 54 return env; 55 case JNI_EDETACHED: 56 if ( mustDetach ) 57 *mustDetach = true; 58 return LC_attachCurrentThread(); 59 default: 60 cerr << "GetEnv() failed" << endl; 61 ::exit( 1 ); 62 } 63 } 64 65 /* vim:set et sw=4 ts=4: */ 66