Project

Profile

Help

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

he / latest9.8 / hec / command / Query.c @ 52505fd1

1
#include <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
/*! <code>Environment</code>. This struct captures the jni, JVM and handler to the cross compiled Saxon/C library.
58
 * <p/>
59
 */
60
typedef struct {
61
                JNIEnv *env;
62
                HANDLE myDllHandle;
63
                JavaVM *jvm;
64
        } Environment;
65

    
66

    
67
//===============================================================================================//
68

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

    
77
//===============================================================================================//
78

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

    
87
jobject cpp;
88

    
89

    
90

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

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

    
109

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

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

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

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

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

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

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

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

    
163

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

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

    
180
    return clazz;
181
}
182

    
183

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

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

    
196
  return MID_init;
197
}
198

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

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

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

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

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

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

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

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

    
243
     }
244
        //return NULL;
245

    
246
}
247

    
248

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

    
254

    
255

    
256

    
257

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

    
260

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

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

    
290

    
291

    
292

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

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

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

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

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