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