1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2009-2012 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 | } |
---|
55 | #ifdef DEBUG |
---|
56 | fprintf(stderr,"CLASSPATH=%s\n",classpath); |
---|
57 | fprintf(stderr,"(%s)\n",oclasspath); |
---|
58 | #endif |
---|
59 | |
---|
60 | #ifdef WIN32 |
---|
61 | JavaVMOption options[2]; |
---|
62 | #else |
---|
63 | JavaVMOption options[1]; |
---|
64 | #endif |
---|
65 | JavaVMInitArgs vm_args; |
---|
66 | JavaVM *jvm; |
---|
67 | JNIEnv *env; |
---|
68 | long result; |
---|
69 | jmethodID pmid; |
---|
70 | jfieldID fid; |
---|
71 | jobject jobj; |
---|
72 | jclass cls; |
---|
73 | #ifdef JAVA7 |
---|
74 | jobject cls_gr; |
---|
75 | #else |
---|
76 | jclass cls_gr; |
---|
77 | #endif |
---|
78 | int i; |
---|
79 | |
---|
80 | options[0].optionString = strdup(oclasspath); |
---|
81 | #ifdef WIN32 |
---|
82 | options[1].optionString = "-Xmx512M"; |
---|
83 | #endif |
---|
84 | |
---|
85 | JNI_GetDefaultJavaVMInitArgs(&vm_args); |
---|
86 | vm_args.version = JNI_VERSION_1_6; |
---|
87 | vm_args.options = options; |
---|
88 | #ifdef WIN32 |
---|
89 | vm_args.nOptions = 2; |
---|
90 | #else |
---|
91 | vm_args.nOptions = 1; |
---|
92 | #endif |
---|
93 | vm_args.ignoreUnrecognized = JNI_TRUE; |
---|
94 | |
---|
95 | result = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args); |
---|
96 | if(result == JNI_ERR ) { |
---|
97 | fprintf(stderr,"Error invoking the JVM"); |
---|
98 | return -1; |
---|
99 | } |
---|
100 | #ifdef DEBUG |
---|
101 | else |
---|
102 | fprintf(stderr,"JAVA VM Started\n"); |
---|
103 | #endif |
---|
104 | |
---|
105 | tmp=getMap(s->content,"serviceProvider"); |
---|
106 | #ifdef JAVA7 |
---|
107 | cls = env->FindClass(tmp->value); |
---|
108 | cls_gr = env->NewGlobalRef(cls); |
---|
109 | #else |
---|
110 | cls = (*env)->FindClass(env,tmp->value); |
---|
111 | cls_gr = (*env)->NewGlobalRef(env, cls); |
---|
112 | #endif |
---|
113 | if( cls == NULL ) { |
---|
114 | char pbt[10240]; |
---|
115 | sprintf(pbt,"can't find class %s\n",tmp->value); |
---|
116 | fprintf(stderr,pbt); |
---|
117 | fflush(stderr); |
---|
118 | map* err=createMap("text",pbt); |
---|
119 | addToMap(err,"code","NoApplicableCode"); |
---|
120 | printExceptionReportResponse(m,err); |
---|
121 | freeMap(&err); |
---|
122 | free(err); |
---|
123 | #ifdef JAVA7 |
---|
124 | (*jvm).DestroyJavaVM(); |
---|
125 | #else |
---|
126 | (*jvm)->DestroyJavaVM(jvm); |
---|
127 | #endif |
---|
128 | return -1; |
---|
129 | } |
---|
130 | #ifdef DEBUG |
---|
131 | else{ |
---|
132 | fprintf(stderr,"%s loaded\n",tmp->value); |
---|
133 | } |
---|
134 | #endif |
---|
135 | |
---|
136 | if (cls != NULL) { |
---|
137 | #ifdef JAVA7 |
---|
138 | (*env).ExceptionClear(); |
---|
139 | pmid=(*env).GetStaticMethodID(cls, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I"); |
---|
140 | #else |
---|
141 | (*env)->ExceptionClear(env); |
---|
142 | pmid=(*env)->GetStaticMethodID(env,cls_gr, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I"); |
---|
143 | #endif |
---|
144 | if (pmid!=0){ |
---|
145 | #ifdef DEBUG |
---|
146 | fprintf(stderr,"Function successfully loaded\n"); |
---|
147 | #endif |
---|
148 | jclass scHashMapClass,scHashMap_class; |
---|
149 | jmethodID scHashMap_constructor; |
---|
150 | #ifdef JAVA7 |
---|
151 | scHashMapClass = (*env).FindClass("java/util/HashMap"); |
---|
152 | scHashMap_class = (jclass)(*env).NewGlobalRef(scHashMapClass); |
---|
153 | scHashMap_constructor = (*env).GetMethodID(scHashMap_class, "<init>", "()V"); |
---|
154 | #else |
---|
155 | scHashMapClass = (*env)->FindClass(env, "java/util/HashMap"); |
---|
156 | scHashMap_class = (*env)->NewGlobalRef(env, scHashMapClass); |
---|
157 | scHashMap_constructor = (*env)->GetMethodID(env, scHashMap_class, "<init>", "()V"); |
---|
158 | #endif |
---|
159 | /** |
---|
160 | * The 3 standard parameter for each services |
---|
161 | */ |
---|
162 | jobject arg1=HashMap_FromMaps(env,m,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
163 | jobject arg2=HashMap_FromMaps(env,inputs,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
164 | jobject arg3=HashMap_FromMaps(env,outputs,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
165 | jint pValue=0; |
---|
166 | |
---|
167 | #ifdef JAVA7 |
---|
168 | pValue=(*env).CallStaticIntMethod(cls,pmid,arg1,arg2,arg3); |
---|
169 | #else |
---|
170 | pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3); |
---|
171 | #endif |
---|
172 | if (pValue != (jint)NULL){ |
---|
173 | res=pValue; |
---|
174 | m=mapsFromHashMap(env,arg1,scHashMapClass); |
---|
175 | *main_conf=m; |
---|
176 | outputs=mapsFromHashMap(env,arg3,scHashMapClass); |
---|
177 | *real_outputs=outputs; |
---|
178 | |
---|
179 | #ifdef DEBUG |
---|
180 | fprintf(stderr,"Result of call: %i\n", pValue); |
---|
181 | dumpMaps(inputs); |
---|
182 | dumpMaps(outputs); |
---|
183 | #endif |
---|
184 | }else{ |
---|
185 | /** |
---|
186 | * Error handling displayig stack trace in ExceptionReport |
---|
187 | */ |
---|
188 | map *tmpm=getMapFromMaps(*main_conf,"main","tmpPath"); |
---|
189 | char tmps[1024]; |
---|
190 | sprintf(tmps,"%s/%d.ztmp",tmpm->value,getpid()); |
---|
191 | int old_stdout=dup(fileno(stdout)); |
---|
192 | FILE* new_stdout=fopen(tmps,"w+"); |
---|
193 | dup2(fileno(new_stdout),fileno(stdout)); |
---|
194 | #ifdef JAVA7 |
---|
195 | (*env).ExceptionDescribe(); |
---|
196 | #else |
---|
197 | (*env)->ExceptionDescribe(env); |
---|
198 | #endif |
---|
199 | fflush(stdout); |
---|
200 | dup2(old_stdout,fileno(stdout)); |
---|
201 | fseek(new_stdout, 0, SEEK_END); |
---|
202 | long flen=ftell(new_stdout); |
---|
203 | rewind(new_stdout); |
---|
204 | char *tmps1=(char*)malloc((flen+1)*sizeof(char)); |
---|
205 | fread(tmps1,flen,1,new_stdout); |
---|
206 | fclose(new_stdout); |
---|
207 | char *pbt=(char*)malloc((100+flen+1)*sizeof(char)); |
---|
208 | sprintf(pbt,"Unable to run your java process properly. Server returns : %s",tmps1); |
---|
209 | map* err=createMap("text",pbt); |
---|
210 | addToMap(err,"code","NoApplicableCode"); |
---|
211 | printExceptionReportResponse(m,err); |
---|
212 | freeMap(&err); |
---|
213 | free(err); |
---|
214 | #ifdef JAVA7 |
---|
215 | (*jvm).DestroyJavaVM(); |
---|
216 | #else |
---|
217 | (*jvm)->DestroyJavaVM(jvm); |
---|
218 | #endif |
---|
219 | return -1; |
---|
220 | } |
---|
221 | } |
---|
222 | else{ |
---|
223 | char tmpS[1024]; |
---|
224 | sprintf(tmpS, "Cannot find function %s \n", s->name); |
---|
225 | map* err=createMap("text",tmpS); |
---|
226 | printExceptionReportResponse(m,err); |
---|
227 | freeMap(&err); |
---|
228 | free(err); |
---|
229 | #ifdef JAVA7 |
---|
230 | (*jvm).DestroyJavaVM(); |
---|
231 | #else |
---|
232 | (*jvm)->DestroyJavaVM(jvm); |
---|
233 | #endif |
---|
234 | return -1; |
---|
235 | } |
---|
236 | }else{ |
---|
237 | char tmpS[1024]; |
---|
238 | sprintf(tmpS, "Cannot find function %s \n", tmp->value); |
---|
239 | map* err=createMap("text",tmpS); |
---|
240 | printExceptionReportResponse(m,err); |
---|
241 | freeMap(&err); |
---|
242 | free(err); |
---|
243 | #ifdef JAVA7 |
---|
244 | (*jvm).DestroyJavaVM(); |
---|
245 | #else |
---|
246 | (*jvm)->DestroyJavaVM(jvm); |
---|
247 | #endif |
---|
248 | return -1; |
---|
249 | } |
---|
250 | #ifdef JAVA7 |
---|
251 | (*jvm).DestroyJavaVM(); |
---|
252 | #else |
---|
253 | (*jvm)->DestroyJavaVM(jvm); |
---|
254 | #endif |
---|
255 | return res; |
---|
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,scObject3,scObject4; |
---|
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 | scObject3 = (*env).NewObject(scArrayList_class, scArrayList_constructor); |
---|
323 | scObject4 = (*env).NewObject(scArrayList_class, scArrayList_constructor); |
---|
324 | |
---|
325 | add_mid = (*env).GetMethodID(scArrayListClass, |
---|
326 | "add","(Ljava/lang/Object;)Z"); |
---|
327 | #else |
---|
328 | scArrayListClass = (*env)->FindClass(env, "java/util/ArrayList"); |
---|
329 | scArrayList_class = (*env)->NewGlobalRef(env, scArrayListClass); |
---|
330 | scArrayList_constructor = (*env)->GetMethodID(env, scArrayList_class, "<init>", "()V"); |
---|
331 | jmethodID add_mid = 0; |
---|
332 | scObject2 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor); |
---|
333 | scObject3 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor); |
---|
334 | scObject4 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor); |
---|
335 | |
---|
336 | add_mid = (*env)->GetMethodID(env,scArrayListClass, |
---|
337 | "add","(Ljava/lang/Object;)Z"); |
---|
338 | #endif |
---|
339 | int i; |
---|
340 | |
---|
341 | for(i=0;i<alen1;i++){ |
---|
342 | map* vMap=getMapArray(tmp->content,"value",i); |
---|
343 | map* sMap=getMapArray(tmp->content,"size",i); |
---|
344 | map* mMap=getMapArray(tmp->content,tmap->value,i); |
---|
345 | |
---|
346 | if(sMap!=NULL && vMap!=NULL && strncmp(vMap->name,"value",5)==0){ |
---|
347 | #ifdef JAVA7 |
---|
348 | jbyteArray tmpData=(*env).NewByteArray(atoi(sMap->value)); |
---|
349 | (*env).SetByteArrayRegion(tmpData,0,atoi(sMap->value),(const jbyte *)vMap->value); |
---|
350 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
351 | #else |
---|
352 | jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sMap->value)); |
---|
353 | (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sMap->value),vMap->value); |
---|
354 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
355 | #endif |
---|
356 | }else{ |
---|
357 | #ifdef JAVA7 |
---|
358 | jobject tmpData=(*env).NewStringUTF(vMap->value); |
---|
359 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
360 | #else |
---|
361 | jobject tmpData=(*env)->NewStringUTF(env,vMap->value); |
---|
362 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
363 | #endif |
---|
364 | } |
---|
365 | |
---|
366 | } |
---|
367 | |
---|
368 | #ifdef JAVA7 |
---|
369 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), scObject2); |
---|
370 | #else |
---|
371 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), scObject2); |
---|
372 | #endif |
---|
373 | |
---|
374 | } |
---|
375 | } |
---|
376 | else |
---|
377 | #ifdef JAVA7 |
---|
378 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value)); |
---|
379 | #else |
---|
380 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value)); |
---|
381 | #endif |
---|
382 | tmp1=tmp1->next; |
---|
383 | } |
---|
384 | #ifdef JAVA7 |
---|
385 | (*env).CallObjectMethod(scObject, put_mid, (*env).NewStringUTF(tmp->name), scObject1); |
---|
386 | #else |
---|
387 | (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1); |
---|
388 | #endif |
---|
389 | tmp=tmp->next; |
---|
390 | } |
---|
391 | return scObject; |
---|
392 | } |
---|
393 | else |
---|
394 | return NULL; |
---|
395 | } |
---|
396 | |
---|
397 | maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){ |
---|
398 | #ifdef DEBUG |
---|
399 | fprintf(stderr,"mapsFromHashMap start\n"); |
---|
400 | #endif |
---|
401 | /** |
---|
402 | * What need to be done (in java). |
---|
403 | * Set set = hm.entrySet(); |
---|
404 | * Iterator i = set.iterator(); |
---|
405 | * while(i.hasNext()){ |
---|
406 | * Map.Entry me = (Map.Entry)i.next(); |
---|
407 | * System.out.println(me.getKey() + " : " + me.getValue() ); |
---|
408 | * } |
---|
409 | */ |
---|
410 | jclass scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass; |
---|
411 | jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid; |
---|
412 | jobject scObject,scObject1; |
---|
413 | if(scHashMapClass==NULL){ |
---|
414 | #ifdef DEBUG |
---|
415 | fprintf(stderr,"Unable to load java.util.HashMap\n"); |
---|
416 | #endif |
---|
417 | return NULL; |
---|
418 | } |
---|
419 | #ifdef JAVA7 |
---|
420 | entrySet_mid = (*env).GetMethodID(scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
421 | containsKey_mid = (*env).GetMethodID(scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
422 | get_mid = (*env).GetMethodID(scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
423 | #else |
---|
424 | entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
425 | containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
426 | get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
427 | #endif |
---|
428 | |
---|
429 | if(containsKey_mid==0){ |
---|
430 | #ifdef DEBUG |
---|
431 | fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid); |
---|
432 | #endif |
---|
433 | return NULL; |
---|
434 | } |
---|
435 | if(get_mid==0){ |
---|
436 | #ifdef DEBUG |
---|
437 | fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid); |
---|
438 | #endif |
---|
439 | return NULL; |
---|
440 | } |
---|
441 | if(entrySet_mid==0){ |
---|
442 | #ifdef DEBUG |
---|
443 | fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid); |
---|
444 | #endif |
---|
445 | return NULL; |
---|
446 | } |
---|
447 | #ifdef DEBUG |
---|
448 | else |
---|
449 | fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid); |
---|
450 | #endif |
---|
451 | |
---|
452 | #ifdef JAVA7 |
---|
453 | scSetClass = (*env).FindClass("java/util/Set"); |
---|
454 | iterator_mid = (*env).GetMethodID(scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
455 | #else |
---|
456 | scSetClass = (*env)->FindClass(env, "java/util/Set"); |
---|
457 | iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
458 | #endif |
---|
459 | #ifdef DEBUG |
---|
460 | fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid); |
---|
461 | #endif |
---|
462 | |
---|
463 | #ifdef JAVA7 |
---|
464 | scIteratorClass = (*env).FindClass("java/util/Iterator"); |
---|
465 | hasNext_mid = (*env).GetMethodID(scIteratorClass, "hasNext", "()Z"); |
---|
466 | #else |
---|
467 | scIteratorClass = (*env)->FindClass(env, "java/util/Iterator"); |
---|
468 | hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z"); |
---|
469 | #endif |
---|
470 | |
---|
471 | #ifdef DEBUG |
---|
472 | fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid); |
---|
473 | #endif |
---|
474 | |
---|
475 | #ifdef JAVA7 |
---|
476 | next_mid = (*env).GetMethodID(scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
477 | #else |
---|
478 | next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
479 | #endif |
---|
480 | #ifdef DEBUG |
---|
481 | fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid); |
---|
482 | #endif |
---|
483 | |
---|
484 | #ifdef JAVA7 |
---|
485 | scMapEntryClass = (*env).FindClass("java/util/Map$Entry"); |
---|
486 | getKey_mid = (*env).GetMethodID(scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
487 | #else |
---|
488 | scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry"); |
---|
489 | getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
490 | #endif |
---|
491 | #ifdef DEBUG |
---|
492 | fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid); |
---|
493 | #endif |
---|
494 | #ifdef JAVA7 |
---|
495 | getValue_mid = (*env).GetMethodID(scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
496 | #else |
---|
497 | getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
498 | #endif |
---|
499 | #ifdef DEBUG |
---|
500 | fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid); |
---|
501 | #endif |
---|
502 | |
---|
503 | #ifdef JAVA7 |
---|
504 | jobject final_set=(*env).CallObjectMethod(t,entrySet_mid); |
---|
505 | jobject final_iterator=(*env).CallObjectMethod(final_set,iterator_mid); |
---|
506 | #else |
---|
507 | jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid); |
---|
508 | jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid); |
---|
509 | #endif |
---|
510 | |
---|
511 | maps* final_res=NULL; |
---|
512 | map* res=NULL; |
---|
513 | #ifdef JAVA7 |
---|
514 | while((*env).CallBooleanMethod(final_iterator,hasNext_mid)){ |
---|
515 | jobject tmp=(*env).CallObjectMethod(final_iterator,next_mid); |
---|
516 | |
---|
517 | jobject imap=(*env).CallObjectMethod(tmp,getValue_mid); |
---|
518 | jobject set=(*env).CallObjectMethod(imap,entrySet_mid); |
---|
519 | jobject iterator=(*env).CallObjectMethod(set,iterator_mid); |
---|
520 | |
---|
521 | #else |
---|
522 | while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){ |
---|
523 | jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid); |
---|
524 | |
---|
525 | jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid); |
---|
526 | jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid); |
---|
527 | jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid); |
---|
528 | |
---|
529 | #endif |
---|
530 | int size=-1; |
---|
531 | #ifdef JAVA7 |
---|
532 | if((*env).CallBooleanMethod(imap,containsKey_mid,(*env).NewStringUTF("size"))){ |
---|
533 | jobject sizeV=(*env).CallObjectMethod(imap, get_mid,(*env).NewStringUTF("size")); |
---|
534 | const char* sizeVS=(*env).GetStringUTFChars((jstring)sizeV, NULL); |
---|
535 | #else |
---|
536 | if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){ |
---|
537 | jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size")); |
---|
538 | const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL); |
---|
539 | #endif |
---|
540 | size=atoi(sizeVS); |
---|
541 | fprintf(stderr,"SIZE : %s\n",sizeVS); |
---|
542 | #ifdef JAVA7 |
---|
543 | (*env).ReleaseStringUTFChars((jstring)sizeV, sizeVS); |
---|
544 | #else |
---|
545 | (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS); |
---|
546 | #endif |
---|
547 | } |
---|
548 | |
---|
549 | #ifdef JAVA7 |
---|
550 | while((*env).CallBooleanMethod(iterator,hasNext_mid)){ |
---|
551 | jobject tmp1=(*env).CallObjectMethod(iterator,next_mid); |
---|
552 | jobject jk=(*env).CallObjectMethod(tmp1,getKey_mid); |
---|
553 | jobject jv=(*env).CallObjectMethod(tmp1,getValue_mid); |
---|
554 | |
---|
555 | const char* jkd=(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
556 | #else |
---|
557 | while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){ |
---|
558 | jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid); |
---|
559 | jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid); |
---|
560 | jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid); |
---|
561 | |
---|
562 | const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
563 | #endif |
---|
564 | if(size>=0 && strcmp(jkd,"value")==0){ |
---|
565 | #ifdef JAVA7 |
---|
566 | jobject value=(jobject)(*env).GetByteArrayElements((jbyteArray)jv, NULL); |
---|
567 | #else |
---|
568 | jobject value=(*env)->GetByteArrayElements(env, jv, NULL); |
---|
569 | #endif |
---|
570 | if(res==NULL){ |
---|
571 | res=createMap(jkd,""); |
---|
572 | }else{ |
---|
573 | addToMap(res,jkd,""); |
---|
574 | } |
---|
575 | map* tmpR=getMap(res,"value"); |
---|
576 | free(tmpR->value); |
---|
577 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
578 | memmove(tmpR->value,value,size*sizeof(char)); |
---|
579 | tmpR->value[size]=0; |
---|
580 | char tmp[128]; |
---|
581 | sprintf(tmp,"%d",size); |
---|
582 | addToMap(res,"size",tmp); |
---|
583 | } |
---|
584 | else{ |
---|
585 | #ifdef JAVA7 |
---|
586 | const char* jvd=(*env).GetStringUTFChars((jstring)jv, NULL); |
---|
587 | #else |
---|
588 | const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL); |
---|
589 | #endif |
---|
590 | if(res==NULL){ |
---|
591 | res=createMap(jkd,jvd); |
---|
592 | }else{ |
---|
593 | addToMap(res,jkd,jvd); |
---|
594 | } |
---|
595 | #ifdef JAVA7 |
---|
596 | (*env).ReleaseStringUTFChars((jstring)jv, jvd); |
---|
597 | #else |
---|
598 | (*env)->ReleaseStringUTFChars(env, jv, jvd); |
---|
599 | #endif |
---|
600 | } |
---|
601 | |
---|
602 | #ifdef JAVA7 |
---|
603 | (*env).ReleaseStringUTFChars((jstring)jk, jkd); |
---|
604 | #else |
---|
605 | (*env)->ReleaseStringUTFChars(env, jk, jkd); |
---|
606 | #endif |
---|
607 | |
---|
608 | } |
---|
609 | #ifdef JAVA7 |
---|
610 | jobject jk=(*env).CallObjectMethod(tmp,getKey_mid); |
---|
611 | #else |
---|
612 | jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid); |
---|
613 | #endif |
---|
614 | maps* cmap=(maps*)malloc(sizeof(maps)); |
---|
615 | #ifdef JAVA7 |
---|
616 | cmap->name=(char*)(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
617 | #else |
---|
618 | cmap->name=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
619 | #endif |
---|
620 | #ifdef DEBUG |
---|
621 | fprintf(stderr," / %s \n",cmap->name); |
---|
622 | #endif |
---|
623 | cmap->content=res; |
---|
624 | cmap->next=NULL; |
---|
625 | if(final_res==NULL) |
---|
626 | final_res=dupMaps(&cmap); |
---|
627 | else |
---|
628 | addMapsToMaps(&final_res,cmap); |
---|
629 | freeMaps(&cmap); |
---|
630 | free(cmap); |
---|
631 | cmap=NULL; |
---|
632 | res=NULL; |
---|
633 | } |
---|
634 | #ifdef DEBUG |
---|
635 | fprintf(stderr,"mapsFromHashMap end\n"); |
---|
636 | #endif |
---|
637 | |
---|
638 | return final_res; |
---|
639 | } |
---|