Changeset 1423 for trunk/bindings


Ignore:
Timestamp:
12/02/10 14:22:56 (2 years ago)
Author:
Tobias Richter
Message:

refs #210 #219

work around threading issue in c api by getting environment dynamically
simplify error reporting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bindings/java/native/NexusFile.c

    r1421 r1423  
    5959 
    6060static jclass nexusException;  // Global variable 
    61 static jmethodID nexusExceptionConstructor;  // Global variable 
     61static JavaVM *jvm;  // Global variable 
    6262 
    6363JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { 
     
    6868    assert(ret == JNI_OK); 
    6969 
    70     // Find the class and store the method ID 
    71     // Will use the class loader that loaded the JNI library 
     70    // Find and store the NexusException class for use in JapiError 
    7271    nexusException = (*env)->FindClass(env,"org/nexusformat/NexusException"); 
    7372    assert(nexusException); 
    7473 
    75     nexusExceptionConstructor = (*env)->GetMethodID(env, nexusException, "<init>","(Ljava/lang/String;)V"); 
    76     assert(nexusExceptionConstructor); 
     74    jvm = vm; 
    7775 
    7876    return JNI_VERSION_1_1; 
     
    8280                              ERROR TREATMENT 
    8381 
    84   The NAPI writes any errors to stdout through a special function.  
    85   This is not very feasible in a Java environment where an exception should 
    86   be thrown. Fortunately it is possible to define an own error processing 
    87   function to be used for error processing. This error handling function 
    88   is defined here. A NexusException is constructed and thrown. 
     82  The NAPI posts any errors to a customisable function.  
     83  We construct and throw a NexusException with the message received. 
    8984  --------------------------------------------------------------------------*/ 
    90 static void JapiError(void *pData, char *text) 
    91 { 
    92     JNIEnv *env = (JNIEnv *)pData; 
    93     jobject exception; 
    94     jstring jtext; 
    95     char *args[2]; 
     85static void JapiError(void *pData, char *text) { 
     86    // better ignore the env passed in via pData, it is unsafe due to #219 
     87    JNIEnv *env; 
    9688 
    9789#ifdef DEBUG 
     
    9991#endif 
    10092 
     93    (*jvm)->AttachCurrentThread (jvm, (void **) &env, NULL); 
     94 
    10195    if (env == NULL) { 
    102         // if there is not thread environment we do not need to throw an exception 
     96        // if there is no thread environment we do not need to throw an exception 
    10397        return; 
    10498    } 
    10599 
    106     jtext = (*env)->NewStringUTF(env,text); 
    107     args[0] = (char *)jtext; 
    108     args[1] = 0; 
    109     exception = (*env)->NewObjectA(env, nexusException, nexusExceptionConstructor, (jvalue *) args); 
    110     (*env)->Throw(env, exception); 
    111  
     100    (*env)->ThrowNew(env, nexusException, text); 
    112101}  
    113102 
Note: See TracChangeset for help on using the changeset viewer.