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

Last change on this file since 615 was 613, checked in by djay, 10 years ago

Fix typo and update the Java part of the documentation.

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