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

Last change on this file since 766 was 765, checked in by djay, 9 years ago

Fix issue when running service in asynch mode with missing parameters.

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