1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2009-2013 GeoLabs SARL |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "service_internal_java.h" |
---|
26 | |
---|
27 | int zoo_java_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ |
---|
28 | maps* m=*main_conf; |
---|
29 | maps* inputs=*real_inputs; |
---|
30 | maps* outputs=*real_outputs; |
---|
31 | char ntmp[1024]; |
---|
32 | getcwd(ntmp,1024); |
---|
33 | map* tmp=getMap(request,"metapath"); |
---|
34 | char *classpath; |
---|
35 | char *oclasspath; |
---|
36 | int res=SERVICE_FAILED; |
---|
37 | char *cclasspath=getenv("CLASSPATH"); |
---|
38 | if(tmp!=NULL){ |
---|
39 | if(cclasspath!=NULL){ |
---|
40 | classpath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+strlen(cclasspath)+4)*sizeof(char)); |
---|
41 | oclasspath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+strlen(cclasspath)+22)*sizeof(char)); |
---|
42 | #ifndef WIN32 |
---|
43 | sprintf(classpath,"%s/%s/:%s",ntmp,tmp->value,cclasspath); |
---|
44 | #else |
---|
45 | sprintf(classpath,"%s/%s/;%s",ntmp,tmp->value,cclasspath); |
---|
46 | #endif |
---|
47 | } |
---|
48 | else{ |
---|
49 | classpath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+3)*sizeof(char)); |
---|
50 | oclasspath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+21)*sizeof(char)); |
---|
51 | sprintf(classpath,"%s/%s/",ntmp,tmp->value); |
---|
52 | } |
---|
53 | sprintf(oclasspath,"-Djava.class.path=%s",classpath); |
---|
54 | }else{ |
---|
55 | if(cclasspath!=NULL){ |
---|
56 | classpath=(char*) malloc((strlen(ntmp)+strlen(cclasspath)+3)*sizeof(char)); |
---|
57 | oclasspath=(char*) malloc((strlen(ntmp)+strlen(cclasspath)+21)*sizeof(char)); |
---|
58 | #ifndef WIN32 |
---|
59 | sprintf(classpath,"%s/:%s",ntmp,cclasspath); |
---|
60 | #else |
---|
61 | sprintf(classpath,"%s/;%s",ntmp,cclasspath); |
---|
62 | #endif |
---|
63 | } |
---|
64 | else{ |
---|
65 | classpath=(char*) malloc((strlen(ntmp)+2)*sizeof(char)); |
---|
66 | oclasspath=(char*) malloc((strlen(ntmp)+20)*sizeof(char)); |
---|
67 | sprintf(classpath,"%s/",ntmp); |
---|
68 | } |
---|
69 | sprintf(oclasspath,"-Djava.class.path=%s",classpath); |
---|
70 | |
---|
71 | } |
---|
72 | sprintf(oclasspath,"-Djava.class.path=%s",classpath); |
---|
73 | |
---|
74 | #ifdef DEBUG |
---|
75 | fprintf(stderr,"CLASSPATH=%s\n",classpath); |
---|
76 | fprintf(stderr,"(%s)\n",oclasspath); |
---|
77 | #endif |
---|
78 | |
---|
79 | #ifdef WIN32 |
---|
80 | JavaVMOption options[2]; |
---|
81 | #else |
---|
82 | JavaVMOption options[1]; |
---|
83 | #endif |
---|
84 | JavaVMInitArgs vm_args; |
---|
85 | JavaVM *jvm; |
---|
86 | JNIEnv *env; |
---|
87 | long result; |
---|
88 | jmethodID pmid; |
---|
89 | jfieldID fid; |
---|
90 | jobject jobj; |
---|
91 | jclass cls; |
---|
92 | #ifdef JAVA7 |
---|
93 | jobject cls_gr; |
---|
94 | #else |
---|
95 | jclass cls_gr; |
---|
96 | #endif |
---|
97 | int i; |
---|
98 | |
---|
99 | options[0].optionString = oclasspath; |
---|
100 | #ifdef WIN32 |
---|
101 | options[1].optionString = "-Xmx512m"; |
---|
102 | /*options[2].optionString = "-Xms128m"; |
---|
103 | options[3].optionString = "-XX:MaxPermSize=256m"; |
---|
104 | options[4].optionString = "-XX:MaxHeapFreeRatio=70";*/ |
---|
105 | #endif |
---|
106 | |
---|
107 | JNI_GetDefaultJavaVMInitArgs(&vm_args); |
---|
108 | vm_args.version = JNI_VERSION_1_6; |
---|
109 | vm_args.options = options; |
---|
110 | #ifdef WIN32 |
---|
111 | vm_args.nOptions = 2; |
---|
112 | #else |
---|
113 | vm_args.nOptions = 1; |
---|
114 | #endif |
---|
115 | vm_args.ignoreUnrecognized = JNI_TRUE; |
---|
116 | |
---|
117 | result = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args); |
---|
118 | if(result == JNI_ERR ) { |
---|
119 | fprintf(stderr,"Error invoking the JVM"); |
---|
120 | return -1; |
---|
121 | } |
---|
122 | #ifdef DEBUG |
---|
123 | else |
---|
124 | fprintf(stderr,"JAVA VM Started\n"); |
---|
125 | #endif |
---|
126 | |
---|
127 | tmp=getMap(s->content,"serviceProvider"); |
---|
128 | #ifdef JAVA7 |
---|
129 | cls = env->FindClass(tmp->value); |
---|
130 | cls_gr = env->NewGlobalRef(cls); |
---|
131 | #else |
---|
132 | cls = (*env)->FindClass(env,tmp->value); |
---|
133 | cls_gr = (*env)->NewGlobalRef(env, cls); |
---|
134 | #endif |
---|
135 | if( cls == NULL ) { |
---|
136 | displayStack(env,*main_conf); |
---|
137 | #ifdef JAVA7 |
---|
138 | (*jvm).DestroyJavaVM(); |
---|
139 | #else |
---|
140 | (*jvm)->DestroyJavaVM(jvm); |
---|
141 | #endif |
---|
142 | return -1; |
---|
143 | } |
---|
144 | #ifdef DEBUG |
---|
145 | else{ |
---|
146 | fprintf(stderr,"%s loaded\n",tmp->value); |
---|
147 | } |
---|
148 | #endif |
---|
149 | |
---|
150 | if (cls != NULL) { |
---|
151 | #ifdef JAVA7 |
---|
152 | (*env).ExceptionClear(); |
---|
153 | pmid=(*env).GetStaticMethodID(cls, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I"); |
---|
154 | #else |
---|
155 | (*env)->ExceptionClear(env); |
---|
156 | pmid=(*env)->GetStaticMethodID(env,cls_gr, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I"); |
---|
157 | #endif |
---|
158 | if (pmid!=0){ |
---|
159 | #ifdef DEBUG |
---|
160 | fprintf(stderr,"Function successfully loaded\n"); |
---|
161 | #endif |
---|
162 | jclass scHashMapClass,scHashMap_class; |
---|
163 | jmethodID scHashMap_constructor; |
---|
164 | #ifdef JAVA7 |
---|
165 | scHashMapClass = (*env).FindClass("java/util/HashMap"); |
---|
166 | scHashMap_class = (jclass)(*env).NewGlobalRef(scHashMapClass); |
---|
167 | scHashMap_constructor = (*env).GetMethodID(scHashMap_class, "<init>", "()V"); |
---|
168 | #else |
---|
169 | scHashMapClass = (*env)->FindClass(env, "java/util/HashMap"); |
---|
170 | scHashMap_class = (*env)->NewGlobalRef(env, scHashMapClass); |
---|
171 | scHashMap_constructor = (*env)->GetMethodID(env, scHashMap_class, "<init>", "()V"); |
---|
172 | #endif |
---|
173 | /** |
---|
174 | * The 3 standard parameter for each services |
---|
175 | */ |
---|
176 | jobject arg1=HashMap_FromMaps(env,m,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
177 | jobject arg2=HashMap_FromMaps(env,inputs,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
178 | jobject arg3=HashMap_FromMaps(env,outputs,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
179 | jint pValue=0; |
---|
180 | |
---|
181 | #ifdef JAVA7 |
---|
182 | pValue=(*env).CallStaticIntMethod(cls,pmid,arg1,arg2,arg3); |
---|
183 | #else |
---|
184 | pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3); |
---|
185 | #endif |
---|
186 | if (pValue != (jint)NULL){ |
---|
187 | res=pValue; |
---|
188 | m=mapsFromHashMap(env,arg1,scHashMapClass); |
---|
189 | *main_conf=m; |
---|
190 | outputs=mapsFromHashMap(env,arg3,scHashMapClass); |
---|
191 | *real_outputs=outputs; |
---|
192 | |
---|
193 | #ifdef DEBUG |
---|
194 | fprintf(stderr,"Result of call: %i\n", pValue); |
---|
195 | dumpMaps(inputs); |
---|
196 | dumpMaps(outputs); |
---|
197 | #endif |
---|
198 | }else{ |
---|
199 | displayStack(env,*main_conf); |
---|
200 | #ifdef JAVA7 |
---|
201 | (*jvm).DestroyJavaVM(); |
---|
202 | #else |
---|
203 | (*jvm)->DestroyJavaVM(jvm); |
---|
204 | #endif |
---|
205 | return -1; |
---|
206 | } |
---|
207 | } |
---|
208 | else{ |
---|
209 | displayStack(env,*main_conf); |
---|
210 | #ifdef JAVA7 |
---|
211 | (*jvm).DestroyJavaVM(); |
---|
212 | #else |
---|
213 | (*jvm)->DestroyJavaVM(jvm); |
---|
214 | #endif |
---|
215 | return -1; |
---|
216 | } |
---|
217 | } |
---|
218 | #ifdef JAVA7 |
---|
219 | (*jvm).DestroyJavaVM(); |
---|
220 | #else |
---|
221 | (*jvm)->DestroyJavaVM(jvm); |
---|
222 | #endif |
---|
223 | return res; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * Error handling: display stack trace in an ExceptionReport Document |
---|
228 | */ |
---|
229 | void displayStack(JNIEnv *env,maps* main_conf){ |
---|
230 | map *tmpm=getMapFromMaps(main_conf,"main","tmpPath"); |
---|
231 | char tmps[1024]; |
---|
232 | sprintf(tmps,"%s/%d.ztmp",tmpm->value,getpid()); |
---|
233 | FILE* new_stdout=fopen(tmps,"wb+"); |
---|
234 | fflush(stderr); |
---|
235 | dup2(fileno(new_stdout),fileno(stderr)); |
---|
236 | fprintf(stderr,"Unable to run your java process properly: "); |
---|
237 | fflush(stderr); |
---|
238 | #ifdef JAVA7 |
---|
239 | (*env).ExceptionDescribe(); |
---|
240 | #else |
---|
241 | (*env)->ExceptionDescribe(env); |
---|
242 | #endif |
---|
243 | fflush(new_stdout); |
---|
244 | fseek(new_stdout, 0, SEEK_END); |
---|
245 | long flen=ftell(new_stdout); |
---|
246 | fseek(new_stdout, 0, SEEK_SET); |
---|
247 | char *tmps1=(char*)malloc((flen+1)*sizeof(char)); |
---|
248 | fread(tmps1,flen,1,new_stdout); |
---|
249 | fclose(new_stdout); |
---|
250 | tmps1[flen]=0; |
---|
251 | map* err=createMap("text",tmps1); |
---|
252 | addToMap(err,"code","InternalError"); |
---|
253 | printExceptionReportResponse(main_conf,err); |
---|
254 | freeMap(&err); |
---|
255 | free(err); |
---|
256 | } |
---|
257 | |
---|
258 | jobject HashMap_FromMaps(JNIEnv *env,maps* t,jclass scHashMapClass,jclass scHashMap_class,jmethodID scHashMap_constructor){ |
---|
259 | jobject scObject,scObject1; |
---|
260 | if(scHashMap_constructor!=NULL){ |
---|
261 | #ifdef JAVA7 |
---|
262 | scObject = (*env).NewObject(scHashMap_class, scHashMap_constructor); |
---|
263 | #else |
---|
264 | scObject = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor); |
---|
265 | #endif |
---|
266 | jmethodID put_mid = 0; |
---|
267 | |
---|
268 | #ifdef JAVA7 |
---|
269 | put_mid = (*env).GetMethodID(scHashMapClass, "put", |
---|
270 | "(Ljava/lang/Object;Ljava/lang/Object;)" |
---|
271 | "Ljava/lang/Object;"); |
---|
272 | #else |
---|
273 | put_mid = (*env)->GetMethodID(env,scHashMapClass, "put", |
---|
274 | "(Ljava/lang/Object;Ljava/lang/Object;)" |
---|
275 | "Ljava/lang/Object;"); |
---|
276 | #endif |
---|
277 | maps* tmp=t; |
---|
278 | while(tmp!=NULL){ |
---|
279 | map* tmap=getMapType(tmp->content); |
---|
280 | map* tmp1=tmp->content; |
---|
281 | #ifdef JAVA7 |
---|
282 | scObject1 = (*env).NewObject(scHashMap_class, scHashMap_constructor); |
---|
283 | #else |
---|
284 | scObject1 = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor); |
---|
285 | #endif |
---|
286 | map* sizeV=getMap(tmp1,"size"); |
---|
287 | map* isArray=getMap(tmp1,"isArray"); |
---|
288 | map* alen=getMap(tmp1,"length"); |
---|
289 | while(tmp1!=NULL){ |
---|
290 | if(strcmp(tmp1->name,"value")==0){ |
---|
291 | if(isArray==NULL){ |
---|
292 | if(sizeV!=NULL && strcmp(tmp1->name,"value")==0){ |
---|
293 | #ifdef JAVA7 |
---|
294 | jbyteArray tmpData=(*env).NewByteArray(atoi(sizeV->value)); |
---|
295 | (*env).SetByteArrayRegion(tmpData,0,atoi(sizeV->value),(const jbyte *)tmp1->value); |
---|
296 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), tmpData); |
---|
297 | #else |
---|
298 | jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sizeV->value)); |
---|
299 | (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sizeV->value),tmp1->value); |
---|
300 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), tmpData); |
---|
301 | #endif |
---|
302 | }else |
---|
303 | #ifdef JAVA7 |
---|
304 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value)); |
---|
305 | #else |
---|
306 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value)); |
---|
307 | #endif |
---|
308 | } |
---|
309 | else{ |
---|
310 | int alen1=atoi(alen->value); |
---|
311 | fprintf(stderr,"LENGTH %d \n",alen1); |
---|
312 | |
---|
313 | jclass scArrayListClass,scArrayList_class; |
---|
314 | jmethodID scArrayList_constructor; |
---|
315 | jobject scObject2; |
---|
316 | #ifdef JAVA7 |
---|
317 | scArrayListClass = (*env).FindClass("java/util/ArrayList"); |
---|
318 | scArrayList_class = (jclass)(*env).NewGlobalRef(scArrayListClass); |
---|
319 | scArrayList_constructor = (*env).GetMethodID(scArrayList_class, "<init>", "()V"); |
---|
320 | jmethodID add_mid = 0; |
---|
321 | scObject2 = (*env).NewObject(scArrayList_class, scArrayList_constructor); |
---|
322 | |
---|
323 | add_mid = (*env).GetMethodID(scArrayListClass, |
---|
324 | "add","(Ljava/lang/Object;)Z"); |
---|
325 | #else |
---|
326 | scArrayListClass = (*env)->FindClass(env, "java/util/ArrayList"); |
---|
327 | scArrayList_class = (*env)->NewGlobalRef(env, scArrayListClass); |
---|
328 | scArrayList_constructor = (*env)->GetMethodID(env, scArrayList_class, "<init>", "()V"); |
---|
329 | jmethodID add_mid = 0; |
---|
330 | scObject2 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor); |
---|
331 | |
---|
332 | add_mid = (*env)->GetMethodID(env,scArrayListClass, |
---|
333 | "add","(Ljava/lang/Object;)Z"); |
---|
334 | #endif |
---|
335 | int i; |
---|
336 | |
---|
337 | for(i=0;i<alen1;i++){ |
---|
338 | map* vMap=getMapArray(tmp->content,"value",i); |
---|
339 | map* sMap=getMapArray(tmp->content,"size",i); |
---|
340 | map* mMap=getMapArray(tmp->content,tmap->value,i); |
---|
341 | |
---|
342 | if(sMap!=NULL && vMap!=NULL && strncmp(vMap->name,"value",5)==0){ |
---|
343 | #ifdef JAVA7 |
---|
344 | jbyteArray tmpData=(*env).NewByteArray(atoi(sMap->value)); |
---|
345 | (*env).SetByteArrayRegion(tmpData,0,atoi(sMap->value),(const jbyte *)vMap->value); |
---|
346 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
347 | #else |
---|
348 | jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sMap->value)); |
---|
349 | (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sMap->value),vMap->value); |
---|
350 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
351 | #endif |
---|
352 | }else{ |
---|
353 | #ifdef JAVA7 |
---|
354 | jobject tmpData=(*env).NewStringUTF(vMap->value); |
---|
355 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
356 | #else |
---|
357 | jobject tmpData=(*env)->NewStringUTF(env,vMap->value); |
---|
358 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
359 | #endif |
---|
360 | } |
---|
361 | |
---|
362 | } |
---|
363 | |
---|
364 | #ifdef JAVA7 |
---|
365 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), scObject2); |
---|
366 | #else |
---|
367 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), scObject2); |
---|
368 | #endif |
---|
369 | |
---|
370 | } |
---|
371 | } |
---|
372 | else |
---|
373 | #ifdef JAVA7 |
---|
374 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value)); |
---|
375 | #else |
---|
376 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value)); |
---|
377 | #endif |
---|
378 | tmp1=tmp1->next; |
---|
379 | } |
---|
380 | #ifdef JAVA7 |
---|
381 | (*env).CallObjectMethod(scObject, put_mid, (*env).NewStringUTF(tmp->name), scObject1); |
---|
382 | #else |
---|
383 | (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1); |
---|
384 | #endif |
---|
385 | tmp=tmp->next; |
---|
386 | } |
---|
387 | return scObject; |
---|
388 | } |
---|
389 | else |
---|
390 | return NULL; |
---|
391 | } |
---|
392 | |
---|
393 | maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){ |
---|
394 | #ifdef DEBUG |
---|
395 | fprintf(stderr,"mapsFromHashMap start\n"); |
---|
396 | #endif |
---|
397 | /** |
---|
398 | * What need to be done (in java). |
---|
399 | * Set set = hm.entrySet(); |
---|
400 | * Iterator i = set.iterator(); |
---|
401 | * while(i.hasNext()){ |
---|
402 | * Map.Entry me = (Map.Entry)i.next(); |
---|
403 | * System.out.println(me.getKey() + " : " + me.getValue() ); |
---|
404 | * } |
---|
405 | */ |
---|
406 | jclass scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass; |
---|
407 | jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid; |
---|
408 | jobject scObject,scObject1; |
---|
409 | if(scHashMapClass==NULL){ |
---|
410 | #ifdef DEBUG |
---|
411 | fprintf(stderr,"Unable to load java.util.HashMap\n"); |
---|
412 | #endif |
---|
413 | return NULL; |
---|
414 | } |
---|
415 | #ifdef JAVA7 |
---|
416 | entrySet_mid = (*env).GetMethodID(scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
417 | containsKey_mid = (*env).GetMethodID(scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
418 | get_mid = (*env).GetMethodID(scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
419 | #else |
---|
420 | entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
421 | containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
422 | get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
423 | #endif |
---|
424 | |
---|
425 | if(containsKey_mid==0){ |
---|
426 | #ifdef DEBUG |
---|
427 | fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid); |
---|
428 | #endif |
---|
429 | return NULL; |
---|
430 | } |
---|
431 | if(get_mid==0){ |
---|
432 | #ifdef DEBUG |
---|
433 | fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid); |
---|
434 | #endif |
---|
435 | return NULL; |
---|
436 | } |
---|
437 | if(entrySet_mid==0){ |
---|
438 | #ifdef DEBUG |
---|
439 | fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid); |
---|
440 | #endif |
---|
441 | return NULL; |
---|
442 | } |
---|
443 | #ifdef DEBUG |
---|
444 | else |
---|
445 | fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid); |
---|
446 | #endif |
---|
447 | |
---|
448 | #ifdef JAVA7 |
---|
449 | scSetClass = (*env).FindClass("java/util/Set"); |
---|
450 | iterator_mid = (*env).GetMethodID(scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
451 | #else |
---|
452 | scSetClass = (*env)->FindClass(env, "java/util/Set"); |
---|
453 | iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
454 | #endif |
---|
455 | #ifdef DEBUG |
---|
456 | fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid); |
---|
457 | #endif |
---|
458 | |
---|
459 | #ifdef JAVA7 |
---|
460 | scIteratorClass = (*env).FindClass("java/util/Iterator"); |
---|
461 | hasNext_mid = (*env).GetMethodID(scIteratorClass, "hasNext", "()Z"); |
---|
462 | #else |
---|
463 | scIteratorClass = (*env)->FindClass(env, "java/util/Iterator"); |
---|
464 | hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z"); |
---|
465 | #endif |
---|
466 | |
---|
467 | #ifdef DEBUG |
---|
468 | fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid); |
---|
469 | #endif |
---|
470 | |
---|
471 | #ifdef JAVA7 |
---|
472 | next_mid = (*env).GetMethodID(scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
473 | #else |
---|
474 | next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
475 | #endif |
---|
476 | #ifdef DEBUG |
---|
477 | fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid); |
---|
478 | #endif |
---|
479 | |
---|
480 | #ifdef JAVA7 |
---|
481 | scMapEntryClass = (*env).FindClass("java/util/Map$Entry"); |
---|
482 | getKey_mid = (*env).GetMethodID(scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
483 | #else |
---|
484 | scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry"); |
---|
485 | getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
486 | #endif |
---|
487 | #ifdef DEBUG |
---|
488 | fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid); |
---|
489 | #endif |
---|
490 | #ifdef JAVA7 |
---|
491 | getValue_mid = (*env).GetMethodID(scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
492 | #else |
---|
493 | getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
494 | #endif |
---|
495 | #ifdef DEBUG |
---|
496 | fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid); |
---|
497 | #endif |
---|
498 | |
---|
499 | #ifdef JAVA7 |
---|
500 | jobject final_set=(*env).CallObjectMethod(t,entrySet_mid); |
---|
501 | jobject final_iterator=(*env).CallObjectMethod(final_set,iterator_mid); |
---|
502 | #else |
---|
503 | jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid); |
---|
504 | jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid); |
---|
505 | #endif |
---|
506 | |
---|
507 | maps* final_res=NULL; |
---|
508 | map* res=NULL; |
---|
509 | #ifdef JAVA7 |
---|
510 | while((*env).CallBooleanMethod(final_iterator,hasNext_mid)){ |
---|
511 | jobject tmp=(*env).CallObjectMethod(final_iterator,next_mid); |
---|
512 | |
---|
513 | jobject imap=(*env).CallObjectMethod(tmp,getValue_mid); |
---|
514 | jobject set=(*env).CallObjectMethod(imap,entrySet_mid); |
---|
515 | jobject iterator=(*env).CallObjectMethod(set,iterator_mid); |
---|
516 | |
---|
517 | #else |
---|
518 | while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){ |
---|
519 | jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid); |
---|
520 | |
---|
521 | jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid); |
---|
522 | jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid); |
---|
523 | jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid); |
---|
524 | |
---|
525 | #endif |
---|
526 | int size=-1; |
---|
527 | #ifdef JAVA7 |
---|
528 | if((*env).CallBooleanMethod(imap,containsKey_mid,(*env).NewStringUTF("size"))){ |
---|
529 | jobject sizeV=(*env).CallObjectMethod(imap, get_mid,(*env).NewStringUTF("size")); |
---|
530 | const char* sizeVS=(*env).GetStringUTFChars((jstring)sizeV, NULL); |
---|
531 | #else |
---|
532 | if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){ |
---|
533 | jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size")); |
---|
534 | const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL); |
---|
535 | #endif |
---|
536 | size=atoi(sizeVS); |
---|
537 | fprintf(stderr,"SIZE : %s\n",sizeVS); |
---|
538 | #ifdef JAVA7 |
---|
539 | (*env).ReleaseStringUTFChars((jstring)sizeV, sizeVS); |
---|
540 | #else |
---|
541 | (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS); |
---|
542 | #endif |
---|
543 | } |
---|
544 | |
---|
545 | #ifdef JAVA7 |
---|
546 | while((*env).CallBooleanMethod(iterator,hasNext_mid)){ |
---|
547 | jobject tmp1=(*env).CallObjectMethod(iterator,next_mid); |
---|
548 | jobject jk=(*env).CallObjectMethod(tmp1,getKey_mid); |
---|
549 | jobject jv=(*env).CallObjectMethod(tmp1,getValue_mid); |
---|
550 | |
---|
551 | const char* jkd=(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
552 | #else |
---|
553 | while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){ |
---|
554 | jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid); |
---|
555 | jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid); |
---|
556 | jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid); |
---|
557 | |
---|
558 | const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
559 | #endif |
---|
560 | if(size>=0 && strcmp(jkd,"value")==0){ |
---|
561 | #ifdef JAVA7 |
---|
562 | jobject value=(jobject)(*env).GetByteArrayElements((jbyteArray)jv, NULL); |
---|
563 | #else |
---|
564 | jobject value=(*env)->GetByteArrayElements(env, jv, NULL); |
---|
565 | #endif |
---|
566 | if(res==NULL){ |
---|
567 | res=createMap(jkd,""); |
---|
568 | }else{ |
---|
569 | addToMap(res,jkd,""); |
---|
570 | } |
---|
571 | map* tmpR=getMap(res,"value"); |
---|
572 | free(tmpR->value); |
---|
573 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
574 | memmove(tmpR->value,value,size*sizeof(char)); |
---|
575 | tmpR->value[size]=0; |
---|
576 | char tmp[128]; |
---|
577 | sprintf(tmp,"%d",size); |
---|
578 | addToMap(res,"size",tmp); |
---|
579 | } |
---|
580 | else{ |
---|
581 | #ifdef JAVA7 |
---|
582 | const char* jvd=(*env).GetStringUTFChars((jstring)jv, NULL); |
---|
583 | #else |
---|
584 | const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL); |
---|
585 | #endif |
---|
586 | if(res==NULL){ |
---|
587 | res=createMap(jkd,jvd); |
---|
588 | }else{ |
---|
589 | addToMap(res,jkd,jvd); |
---|
590 | } |
---|
591 | #ifdef JAVA7 |
---|
592 | (*env).ReleaseStringUTFChars((jstring)jv, jvd); |
---|
593 | #else |
---|
594 | (*env)->ReleaseStringUTFChars(env, jv, jvd); |
---|
595 | #endif |
---|
596 | } |
---|
597 | |
---|
598 | #ifdef JAVA7 |
---|
599 | (*env).ReleaseStringUTFChars((jstring)jk, jkd); |
---|
600 | #else |
---|
601 | (*env)->ReleaseStringUTFChars(env, jk, jkd); |
---|
602 | #endif |
---|
603 | |
---|
604 | } |
---|
605 | #ifdef JAVA7 |
---|
606 | jobject jk=(*env).CallObjectMethod(tmp,getKey_mid); |
---|
607 | #else |
---|
608 | jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid); |
---|
609 | #endif |
---|
610 | maps* cmap=(maps*)malloc(sizeof(maps)); |
---|
611 | #ifdef JAVA7 |
---|
612 | cmap->name=(char*)(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
613 | #else |
---|
614 | cmap->name=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
615 | #endif |
---|
616 | #ifdef DEBUG |
---|
617 | fprintf(stderr," / %s \n",cmap->name); |
---|
618 | #endif |
---|
619 | cmap->content=res; |
---|
620 | cmap->next=NULL; |
---|
621 | if(final_res==NULL) |
---|
622 | final_res=dupMaps(&cmap); |
---|
623 | else |
---|
624 | addMapsToMaps(&final_res,cmap); |
---|
625 | freeMaps(&cmap); |
---|
626 | free(cmap); |
---|
627 | cmap=NULL; |
---|
628 | res=NULL; |
---|
629 | } |
---|
630 | #ifdef DEBUG |
---|
631 | fprintf(stderr,"mapsFromHashMap end\n"); |
---|
632 | #endif |
---|
633 | |
---|
634 | return final_res; |
---|
635 | } |
---|