Project

Profile

Help

How to connect?
Download (13.1 KB) Statistics
| Branch: | Tag: | Revision:

he / latest9.6 / hec / Saxon.C.API / EEC / SaxonCGlue.c @ 0e42f323

1
#include "SaxonCGlue.h"
2

    
3
jobject cpp;
4
/*
5
* Set Dll name. Also set the saxon resources directory. 
6
* If the SAXONC_HOME sxnc_environmental variable is set then use that as base.
7
*/
8
void setDllname(){
9
        if(getenv("SAXONC_HOME")!= NULL) {
10

    
11
                char * env = getenv("SAXONC_HOME");
12
                size_t env_len = strlen(env);
13
                size_t name_len =  15;
14
                size_t rDir_len = 11;
15
                //dllname =malloc(sizeof(char)*name_len);
16
                //resources_dir =malloc(sizeof(char)*rDir_len);
17
                memset(&dllname[0], 0, sizeof(dllname));
18
                memset(&resources_dir[0], 0, sizeof(resources_dir));
19
                strncat(resources_dir, env,  env_len);
20
                strncat(resources_dir, "/saxon-data", rDir_len);
21
                strncat(dllname, env, env_len);
22
                strncat(dllname, "/libsaxoneec.so", name_len); //rename according to product edition (-hec or -pec)
23
#ifdef DEBUG        
24
                printf("resources_dir: %s\n", resources_dir);        
25
                printf("envDir: %s\n", env);
26
                printf("size of env %i\n", strlen(env));
27
                printf("size of dllname %i\n", strlen(dllname));
28
                printf("dllName: %s\n", dllname);
29
                printf("resources_dir: %s\n", resources_dir);
30
#endif
31
        }
32
#ifdef DEBUG        
33
                printf("resources_dir: %s\n", resources_dir);        
34
                printf("size of dllname %i\n", strlen(dllname));
35
                printf("dllName: %s\n", dllname);
36
                printf("resources_dir: %s\n", resources_dir);
37
                printf("size of resources dir %i\n", strlen(resources_dir));
38
#endif         
39

    
40
}
41

    
42
char * getDllname(){
43
        return dllname;
44
}
45

    
46
/*
47
 * Load dll using the default setting in Saxon/C
48
 * Recommended method to use to load library
49
 */
50
HANDLE loadDefaultDll(){
51
        return loadDll(NULL);
52
}
53

    
54

    
55
/*
56
 * Load dll.
57
 */
58
HANDLE loadDll(char* name)
59
{
60
        if(name == NULL) {
61
                setDllname();
62
                name = getDllname();
63
                //perror("Error1: ");
64
        }
65

    
66
#if !(defined (__linux__) || (defined (__APPLE__) && defined(__MACH__)))
67
    HANDLE hDll = LoadLibrary (_T(name)); // Used for windows only
68
#else
69
    HANDLE hDll = LoadLibrary(name);
70
#endif
71

    
72
    if (!hDll) {
73
        fprintf (stderr, "Unable to load %s\n", name);
74
        perror("Error: ");
75
        exit(1);
76
    }
77
#ifdef DEBUG
78
    fprintf (stderr, "%s loaded\n", name);
79
#endif
80

    
81
    return hDll;
82
}
83

    
84

    
85

    
86
jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
87
jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
88

    
89

    
90

    
91
void initDefaultJavaRT(sxnc_environment ** env){
92
        sxnc_environment *environ = *env;
93
        initJavaRT((environ->myDllHandle), &(environ->jvm), &(environ->env));
94
}
95

    
96
/*
97
 * Initialize JET run-time.
98
 */
99
void initJavaRT(HANDLE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
100
{
101
if(jvmCreated==0) {
102
     jvmCreated=1;
103
#ifdef DEBUG
104
    perror ("initJavaRT - load Saxon/C library\n");
105
#endif
106
    int result;
107
    JavaVMInitArgs args;
108
    JNI_GetDefaultJavaVMInitArgs_func =
109
    (jint (JNICALL *) (void *args))
110
#if !(defined (__linux__) || (defined (__APPLE__) && defined(__MACH__)))
111
    GetProcAddress ((HMODULE)myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
112
#else
113
    GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
114
#endif
115
 
116
    
117
    JNI_CreateJavaVM_func =
118
    (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
119
    
120
#if !(defined (__linux__) || (defined (__APPLE__) && defined(__MACH__)))
121
    GetProcAddress ((HMODULE)myDllHandle, "JNI_CreateJavaVM");
122
#else
123
    GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
124
    
125
#endif
126

    
127
    
128
    if(!JNI_GetDefaultJavaVMInitArgs_func) {
129
        fprintf (stderr,"%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", getDllname());
130
        exit (1);
131
    }
132
    
133
    if(!JNI_CreateJavaVM_func) {
134
        fprintf (stderr,"%s doesn't contain public JNI_CreateJavaVM\n", getDllname());
135
        exit (1);
136
    }
137
    
138
    memset (&args, 0, sizeof(args));
139
    
140
    args.version = JNI_VERSION_1_2;
141
    result = JNI_GetDefaultJavaVMInitArgs_func(&args);
142
    if (result != JNI_OK) {
143
        fprintf(stderr,"JNI_GetDefaultJavaVMInitArgs() failed with result\n");
144
        exit(1);
145
    }
146
  
147
    /*
148
     * NOTE: no JVM is actually created
149
     * this call to JNI_CreateJavaVM is intended for JET RT initialization
150
     */
151
    result = JNI_CreateJavaVM_func (pjvm, (void **)penv, &args);
152
    if (result != JNI_OK) {
153
        fprintf(stderr,"JNI_CreateJavaVM() failed with result: %i\n",result);
154
        exit(1);
155
    }
156

    
157

    
158
    fflush (stdout);
159
  } else {
160
#ifdef DEBUG
161
    perror ("initJavaRT - Saxon/C library loaded already\n");
162
#endif
163
        }
164
}
165

    
166

    
167
/*
168
 * Look for class.
169
 */
170
jclass lookForClass (JNIEnv* penv, const char* name)
171
{
172
    jclass clazz = (*penv)->FindClass (penv, name);
173

    
174
    if (!clazz) {
175
        printf("Unable to find class %s\n", name);
176
        return NULL;
177
    }
178
#ifdef DEBUG
179
    printf ("Class %s found\n", name);
180
    fflush (stdout);
181
#endif
182

    
183
    return clazz;
184
}
185

    
186

    
187
/*
188
 * Create an object and invoke the instance method
189
 */
190
void invokeInstanceMethod (JNIEnv* penv, jclass myClassInDll, char * name, char * arguments)
191
{
192
    jmethodID MID_init, MID_name;
193
    jobject obj;
194

    
195
    MID_init = (*penv)->GetMethodID (penv, myClassInDll, "<init>", "()V");
196
    if (!MID_init) {
197
        printf("Error: MyClassInDll.<init>() not found\n");
198
        return;
199
    }
200

    
201
    obj = (*penv)->NewObject(penv, myClassInDll, MID_init);
202
    if (!obj) {
203
        printf("Error: failed to allocate an object\n");
204
        return;
205
    }
206

    
207
    MID_name = (*penv)->GetMethodID (penv, myClassInDll, name, arguments);
208

    
209
    if (!MID_name) {
210
        printf("Error: %s not found\n", name);
211
        return;
212
    }
213
    
214
    (*(penv))->CallVoidMethod (penv, obj, MID_name);
215
}
216

    
217

    
218

    
219

    
220
/*
221
 * Invoke the static method
222
 */
223
void invokeStaticMethod(JNIEnv* penv, jclass myClassInDll, char* name, char* arguments)
224
{
225
    jmethodID MID_name;
226

    
227
    MID_name = (*penv)->GetStaticMethodID(penv, myClassInDll, name, arguments);
228
    if (!MID_name) {
229
        printf("\nError: method %s not found\n", name);
230
        return;
231
    }
232
    
233
    (*penv)->CallStaticVoidMethod(penv, myClassInDll, MID_name);
234
}
235

    
236

    
237
/*
238
 * Find a constructor with a set arguments
239
 */
240
jmethodID findConstructor (JNIEnv* penv, jclass myClassInDll, char* arguments)
241
{
242
    jmethodID MID_initj;
243
    jobject obj;
244

    
245
    MID_initj = (jmethodID)(*penv)->GetMethodID (penv, myClassInDll, "<init>", arguments);
246
    if (!MID_initj) {
247
        printf("Error: MyClassInDll.<init>() not found\n");
248
        fflush (stdout);
249
        return 0;
250
    }
251

    
252
  return MID_initj;
253
}
254

    
255
/*
256
 * Create the Java SaxonProcessor
257
 */
258
jobject createSaxonProcessor (JNIEnv* penv, jclass myClassInDll, const char * arguments, jobject argument1, jboolean license)
259
{
260
    jmethodID MID_initi;
261

    
262
    jobject obj;
263

    
264
    MID_initi = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
265
        if (!MID_initi) {
266
                printf("Error: MyClassInDll.<init>() not found\n");
267
                return NULL;
268
        }
269
     
270
    if(argument1) {
271

    
272
             obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_initi, argument1, license);
273
    } else {
274
        
275
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_initi, license);
276
     }
277
      if (!obj) {
278
        printf("Error: failed to allocate an object\n");
279
        return NULL;
280
      }
281
    return obj;
282
}
283

    
284

    
285
/*
286
 * Create the Java SaxonProcessor without boolean license argument
287
 */
288
jobject createSaxonProcessor2 (JNIEnv* penv, jclass myClassInDll, const char * arguments, jobject argument1)
289
{
290
    jmethodID MID_initi;
291

    
292
    jobject obj;
293

    
294
    MID_initi = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
295
        if (!MID_initi) {
296
                printf("Error: MyClassInDll.<init>() not found\n");
297
                return NULL;
298
        }
299
     
300
    if(argument1) {
301

    
302
             obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_initi, argument1);
303
    } else {
304
        
305
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_initi);
306
     }
307
      if (!obj) {
308
        printf("Error: failed to allocate an object\n");
309
        return NULL;
310
      }
311
    return obj;
312
}
313

    
314

    
315
/*
316
 * Callback to check for exceptions. When called it returns the exception as a string 
317
 */
318
const char * checkForException(sxnc_environment environ, jclass callingClass,  jobject callingObject){
319

    
320
    if ((*(environ.env))->ExceptionCheck(environ.env)) {
321

    
322
        jthrowable exc = (*(environ.env))->ExceptionOccurred(environ.env);
323
#ifdef DEBUG
324
        (*(environ.env))->ExceptionDescribe(environ.env);
325
#endif
326
         if(exc) {
327
                printf("Exception Occurred!");
328
        }
329
        jclass exccls = (jclass) (*(environ.env))->GetObjectClass(environ.env, exc);
330

    
331
        jclass clscls = (jclass) (*(environ.env))->FindClass(environ.env, "java/lang/Class");
332
        
333
        jmethodID getName = (jmethodID)(*(environ.env))->GetMethodID(environ.env, clscls, "getName", "()Ljava/lang/String;");
334
        jstring name = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exccls, getName));
335
        char const* utfName = (*(environ.env))->GetStringUTFChars(environ.env, name, NULL);
336
        
337
        if(callingObject != NULL && strcmp(utfName, "net.sf.saxon.s9api.SaxonApiException") == 0){
338
                  
339
                jclass saxonExcClass = (*(environ.env))->FindClass(environ.env, "java/lang/Throwable");
340
                
341
                jmethodID  getMessage =  (jmethodID)(*(environ.env))->GetMethodID(environ.env, exccls, "getMessage", "()Ljava/lang/String;");
342
                
343
        if(getMessage) {
344
            
345
            jstring message = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exc, getMessage));
346
       
347
        if(!message){
348
        
349
        (*(environ.env))->ExceptionClear(environ.env);
350
        return 0;
351
        }
352
            char const* utfMessage= (*(environ.env))->GetStringUTFChars(environ.env, message, NULL);
353
      
354
            if(utfMessage != NULL) {
355
                (*(environ.env))->ReleaseStringUTFChars(environ.env, name, utfName);
356
            }
357
       
358
            (*(environ.env))->ExceptionClear(environ.env);
359
            return utfMessage;
360
        }
361
    }
362
     (*(environ.env))->ReleaseStringUTFChars(environ.env, name, utfName);      
363
    }
364
(*(environ.env))->ExceptionClear(environ.env);
365
    return NULL;
366
}
367

    
368

    
369
/*
370
 * Clean up and destroy Java VM to release memory used. 
371
 */
372
void finalizeJavaRT (JavaVM* jvm)
373
{
374

    
375
  if(jvmCreated!= 0){
376
    (*jvm)->DestroyJavaVM (jvm);
377
    jvmCreated=0;        
378
  }
379
}
380

    
381

    
382
/*
383
 * Get a parameter from list 
384
 */
385
jobject getParameter(sxnc_parameter *parameters,  int parLen, const char* namespacei, const char * name){
386
        int i =0;
387
        for(i =0; i< parLen;i++) {
388
                if(strcmp(parameters[i].name, name) == 0)
389
                        return (jobject)parameters[i].value;                        
390
        }
391
        return NULL;
392
}
393

    
394

    
395
/*
396
 * Get a property from list 
397
 */
398
char* getProperty(sxnc_property * properties, int propLen, const char* namespacei, const char * name){
399
        int i =0;
400
        for(i =0; i< propLen;i++) {
401
                if(strcmp(properties[i].name, name) == 0)
402
                        return properties[i].value;                        
403
        }
404
        return 0;
405
}
406

    
407

    
408
/*
409
 * set a parameter 
410
 */
411
void setParameter(sxnc_parameter **parameters, int * parLen, int * parCap, const char * namespacei, const char * name, jobject value){
412

    
413
        if(getParameter(*parameters, (*parLen), "", name) != 0){
414
                return;                        
415
        }
416
        (*parLen)++;        
417
        if((*parLen) >= (*parCap)) {
418
                (*parCap) *=2; 
419
                sxnc_parameter* temp = (sxnc_parameter*)malloc(sizeof(sxnc_parameter)*(*parCap));        
420
                int i =0;
421
                for(i =0; i< (*parLen)-1;i++) {
422
                        temp[i] = (*parameters)[i];                        
423
                }
424
                free(parameters);
425
                parameters = &temp;
426
        }
427
        int nameLen = strlen(name)+7;        
428
        char *newName = malloc(sizeof(char)*nameLen);
429
          snprintf(newName, nameLen, "%s%s", "param:", name );
430
        (*parameters)[(*parLen)-1].name = (char*)newName;        
431
        (*parameters)[(*parLen)-1].value = (jobject)value;
432
}
433

    
434

    
435
/*
436
 * set a property 
437
 */
438
void setProperty(sxnc_property ** properties, int *propLen, int *propCap, const char* name, const char* value){        
439
        if(getProperty(*properties, (*propLen), "", name) != 0){
440
                return;                        
441
        }
442
                
443
        if(((*propLen)+1) >= (*propCap)) {
444
                (*propCap) *=2; 
445
                sxnc_property* temp = (sxnc_property*)malloc(sizeof(sxnc_property)*  (*propCap));
446
                int i =0;        
447
                for(i =0; i< (*propLen)-1;i++) {
448
                        temp[i] = (*properties)[i];                        
449
                }
450
                free(properties);
451
                properties = &temp;
452

    
453
        }
454
        int nameLen = strlen(name)+1;        
455
        char *newName = (char*)malloc(sizeof(char)*nameLen);
456
          snprintf(newName, nameLen, "%s", name );
457
        char *newValue = (char*)malloc(sizeof(char)*strlen(value)+1);
458
          snprintf(newValue, strlen(value)+1, "%s", value );
459
        (*properties)[(*propLen)].name = (char*)newName;        
460
        (*properties)[(*propLen)].value = (char*)newValue;
461
        (*propLen)++;
462
}
463

    
464
/*
465
 * clear parameter 
466
 */
467
void clearSettings(sxnc_parameter **parameters, int *parLen, sxnc_property ** properties, int *propLen){
468
        int i =0;
469
        free(*parameters);
470
        free(*parameters);
471
        /*for(i =0; i< parLen; i++){
472
                free((*parameters[i].name);
473
                free(parameters[i].value);
474
        }
475
        for(i =0; i< propLen; i++){
476
                free(properties[i].name);
477
                free(properties[i].value);
478
        }*/
479

    
480
            *parameters = (sxnc_parameter *)calloc(10, sizeof(sxnc_parameter));
481
            *properties = (sxnc_property *)calloc(10, sizeof(sxnc_property));
482
        (*parLen) = 0;
483
        (*propLen) = 0;
484
}
485

    
486

    
487
const char * stringValue(sxnc_environment environ, jobject value){
488
        jclass  objClass = lookForClass(environ.env, "java/lang/Object");
489
        static jmethodID strMID = NULL;
490
        if(!strMID) {
491
                strMID = (jmethodID)(*(environ.env))->GetMethodID(environ.env, objClass, "toString", "()Ljava/lang/String;");
492
                if(!strMID) {
493
                          printf("\nError: Object %s() not found\n","toString");
494
                            fflush (stdout);
495
                          return NULL;
496
                }
497
        }
498
        jstring result = (jstring)((*(environ.env))->CallObjectMethod(environ.env, value, strMID));
499
         if(result) {
500
                       const char * str = (*(environ.env))->GetStringUTFChars(environ.env, result, NULL);    
501
                return str;
502
        }
503

    
504
    //checkForException(environ, cppClass, cpp);
505
    return 0;
506
        
507
}
508

    
509

    
510

    
511

    
(1-1/3)