source: trunk/zoo-project/zoo-kernel/service_internal_java.c @ 748

Last change on this file since 748 was 748, checked in by djay, 4 years ago

Fix in java support.

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

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png