Project

Profile

Help

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

he / latest9.9 / hec / command / Query.c @ 4364429b

1
#include "jni/jni.h"
2

    
3
#if defined __linux__ || defined __APPLE__
4
    #include <stdlib.h>
5
    #include <string.h>
6
    #include <dlfcn.h>
7
    #include <stdio.h>  
8
    #define HANDLE void*
9
    #define LoadLibrary(x) dlopen(x, RTLD_LAZY)
10
//    #define FreeLibrary(x) dlclose(x, RTLD_LAZY)
11
    #define GetProcAddress(x,y) dlsym(x,y)
12
#else
13
    #include <windows.h>
14
#endif
15

    
16
#ifndef __cplusplus
17
    #ifndef _BOOL
18
        #include <stdbool.h>
19
        //typedef int bool;
20
        #define true 1
21
        #define false 0
22
    #else
23
        #define true 1
24
        #define false 0
25
    #endif
26
#endif
27

    
28

    
29
char dllname[] =
30
#ifdef __linux__
31
    #ifdef EEC
32
            "/usr/lib/libsaxoneec.so";
33
    #elif defined PEC
34
            "/usr/lib/libsaxonpec.so";
35
    #else
36
            "/usr/lib/libsaxonhec.so";
37
    #endif
38
#elif defined __APPLE__
39
    #ifdef EEC
40
        "/usr/local/lib/libsaxoneec.dylib";
41
    #elif defined PEC
42
            "/usr/local/lib/libsaxonpec.dylib";
43
    #else
44
            "/usr/local/lib/libsaxonhec.dylib";
45
    #endif
46
#else
47
    #ifdef EEC
48
        "libsaxoneec.dll";
49
    #elif defined PEC
50
            "libsaxonpec.dll";
51
    #else
52
            "libsaxonhec.dll";
53
    #endif
54
#endif
55

    
56

    
57
//===============================================================================================//
58
/*! <code>Environment</code>. This struct captures the jni, JVM and handler to the cross compiled Saxon/C library.
59
 * <p/>
60
 */
61
typedef struct {
62
                JNIEnv *env;
63
                HANDLE myDllHandle;
64
                JavaVM *jvm;
65
        } Environment;
66

    
67

    
68
//===============================================================================================//
69

    
70
/*! <code>MyParameter</code>. This struct captures details of paramaters used for the transformation as (string, value) pairs.
71
 * <p/>
72
 */
73
typedef struct {
74
                char* name;
75
                jobject value;
76
        } MyParameter;
77

    
78
//===============================================================================================//
79

    
80
/*! <code>MyProperty</code>. This struct captures details of properties used for the transformation as (string, string) pairs.
81
 * <p/>
82
 */
83
typedef struct {
84
                char * name;
85
                char * value;
86
        } MyProperty;
87

    
88
jobject cpp;
89

    
90

    
91

    
92
const char * failure;
93
/*
94
 * Load dll.
95
 */
96
HANDLE loadDll(char* name)
97
{
98
    HANDLE hDll = LoadLibrary (name);
99

    
100
    if (!hDll) {
101
        printf ("Unable to load %s\n", name);
102
        exit(1);
103
    }
104
#ifdef DEBUG
105
    printf ("%s loaded\n", name);
106
#endif
107
    return hDll;
108
}
109

    
110

    
111
jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
112
jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
113

    
114
/*
115
 * Initialize JET run-time.
116
 */
117
void initJavaRT(HANDLE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
118
{
119
    int            result;
120
    JavaVMInitArgs args;
121

    
122
    JNI_GetDefaultJavaVMInitArgs_func = 
123
             (jint (JNICALL *) (void *args))
124
             GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
125

    
126
    JNI_CreateJavaVM_func =
127
             (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
128
             GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
129

    
130
    if(!JNI_GetDefaultJavaVMInitArgs_func) {
131
        printf ("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", dllname);
132
        exit (1);
133
    }
134

    
135
    if(!JNI_CreateJavaVM_func) {
136
        printf ("%s doesn't contain public JNI_CreateJavaVM\n", dllname);
137
        exit (1);
138
    }
139

    
140
    memset (&args, 0, sizeof(args));
141

    
142
    args.version = JNI_VERSION_1_2;
143
    result = JNI_GetDefaultJavaVMInitArgs_func(&args);
144
    if (result != JNI_OK) {
145
        printf ("JNI_GetDefaultJavaVMInitArgs() failed with result %d\n", result);
146
        exit(1);
147
    }
148
  
149
    /*
150
     * NOTE: no JVM is actually created
151
     * this call to JNI_CreateJavaVM is intended for JET RT initialization
152
     */
153
    result = JNI_CreateJavaVM_func (pjvm, (void **)penv, &args);
154
    if (result != JNI_OK) {
155
        printf ("JNI_CreateJavaVM() failed with result %d\n", result);
156
        exit(1);
157
    }
158
#ifdef DEBUG
159
    printf ("JET RT initialized\n");
160
    fflush (stdout);
161
#endif
162
}
163

    
164

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

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

    
181
    return clazz;
182
}
183

    
184

    
185
jmethodID findConstructor (JNIEnv* penv, jclass myClassInDll, char* arguments)
186
{
187
    jmethodID MID_init, mID;
188
    jobject obj;
189

    
190
    MID_init = (jmethodID)(*penv)->GetMethodID (penv, myClassInDll, "<init>", arguments);
191
    if (!MID_init) {
192
        printf("Error: MyClassInDll.<init>() not found\n");
193
        fflush (stdout);
194
        return 0;
195
    }
196

    
197
  return MID_init;
198
}
199

    
200
jobject createObject (JNIEnv* penv, jclass myClassInDll, const char * arguments)
201
{
202
    jmethodID MID_init, mID;
203
    jobject obj;
204

    
205
    MID_init = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
206
    if (!MID_init) {
207
        printf("Error: MyClassInDll.<init>() not found\n");
208
        return NULL;
209
    }
210

    
211
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_init, (jboolean)true);
212
      if (!obj) {
213
        printf("Error: failed to allocate an object\n");
214
        return NULL;
215
      }
216
    return obj;
217
}
218

    
219
void checkForException(Environment environ, jclass callingClass,  jobject callingObject){
220

    
221
    if ((*(environ.env))->ExceptionCheck(environ.env)) {
222
        char *  result1;
223
        const char * errorCode = NULL;
224
        jthrowable exc = (*(environ.env))->ExceptionOccurred(environ.env);
225
        (*(environ.env))->ExceptionDescribe(environ.env); //comment code
226
         jclass exccls = (jclass)(*(environ.env))->GetObjectClass(environ.env, exc);
227
        jclass clscls = (jclass)(*(environ.env))->FindClass(environ.env, "java/lang/Class");
228

    
229
        jmethodID getName = (jmethodID)(*(environ.env))->GetMethodID(environ.env, clscls, "getName", "()Ljava/lang/String;");
230
        jstring name =(jstring)((*(environ.env))->CallObjectMethod(environ.env, exccls, getName));
231
        char const* utfName = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, name, 0);
232
        printf(utfName);
233

    
234
         jmethodID  getMessage = (jmethodID)(*(environ.env))->GetMethodID(environ.env, exccls, "getMessage", "()Ljava/lang/String;");
235
        if(getMessage) {
236

    
237
                jstring message = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exc, getMessage));
238
                if(message) {                
239
                        char const* utfMessage = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, message, 0);
240
                }
241
        
242
        }
243

    
244
     }
245
        //return NULL;
246

    
247
}
248

    
249

    
250
void finalizeJavaRT (JavaVM* jvm)
251
{
252
    (*jvm)->DestroyJavaVM (jvm);
253
}
254

    
255

    
256

    
257

    
258

    
259
int query(Environment environ, int argc, const char* argv[]) {
260

    
261

    
262
    jmethodID MID_foo;
263
    jclass transClass = lookForClass(environ.env, "net/sf/saxon/Query");
264
    char methodName[] = "main";
265
    char args[] = "([Ljava/lang/String;)V";
266
    jobjectArray stringArray = NULL;
267
    MID_foo = (jmethodID)(*(environ.env))->GetStaticMethodID(environ.env, transClass, methodName, args);
268
    if (!MID_foo) {
269
        printf("\nError: MyClassInDll %s() not found\n",methodName);
270
        fflush (stdout);
271
        return -1;
272
    }
273
     if(argc < 2) {
274
        printf("\nError: Not enough arguments in Query");
275
        return 0;
276
    }
277
           jclass stringClass = lookForClass(environ.env, "java/lang/String");
278
           stringArray = (*(environ.env))->NewObjectArray(environ.env, (jint)argc-1, stringClass, 0 );
279
           if(!stringArray) { return 0;}
280
  int i, j;
281
  for(i=1, j=0; i< argc; i++, j++) {
282
             (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, j, (*(environ.env))->NewStringUTF(environ.env, argv[i]));
283
           }
284

    
285
   (*(environ.env))->CallStaticVoidMethod(environ.env, transClass, MID_foo, stringArray);
286
   
287
  (*(environ.env))->DeleteLocalRef(environ.env, stringArray);
288
        return 0;
289
}
290

    
291

    
292

    
293

    
294
int main( int argc, const char* argv[] )
295
{
296
    HANDLE myDllHandle;
297
    //JNIEnv *(environ.env);
298
    //JavaVM *jvm;
299
    jclass  myClassInDll;
300

    
301
    Environment environ;
302
    /*
303
     * First of all, load required component.
304
     * By the time of JET initialization, all components should be loaded.
305
     */
306
    environ.myDllHandle = loadDll (dllname);
307
   
308

    
309
    /*
310
     * Initialize JET run-time.
311
     * The handle of loaded component is used to retrieve Invocation API.
312
     */
313
    initJavaRT (environ.myDllHandle, &environ.jvm, &environ.env);
314
    query(environ, argc, argv);        
315

    
316
  
317
fflush(stdout);
318
    /*
319
     * Finalize JET run-time.
320
     */
321
    finalizeJavaRT (environ.jvm);
322

    
323
    return 0;
324
}
(1-1/9)