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

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

Small fix in java support.

  • 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#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 = zStrdup(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 = 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 = 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  return res;
259}
260
261/**
262 * Error handling: display stack trace in an ExceptionReport Document
263 *
264 * @param env the JNI environment pointer
265 * @param main_conf the conf maps containing the main.cfg settings
266 */
267void displayStack(JNIEnv *env,maps* main_conf){
268  map *tmpm=getMapFromMaps(main_conf,"main","tmpPath");
269  char tmps[1024];
270  sprintf(tmps,"%s/%d.ztmp",tmpm->value,getpid());
271  FILE* new_stdout=fopen(tmps,"wb+");
272  if(new_stdout==NULL){
273    map* err=createMap("text","Unable to run your service, no debug informations can be provided (please verify privileges on tmpPath)");
274    addToMap(err,"code","InternalError");
275    printExceptionReportResponse(main_conf,err);
276    freeMap(&err);
277    free(err);
278    return;
279  }
280  fflush(stderr);
281  dup2(fileno(new_stdout),fileno(stderr));
282  fprintf(stderr,"Unable to run your java process properly: ");
283  fflush(stderr);
284#ifdef JAVA7
285  (*env).ExceptionDescribe();
286#else
287  (*env)->ExceptionDescribe(env);
288#endif
289  fflush(new_stdout);
290  fseek(new_stdout, 0, SEEK_END);
291  long flen=ftell(new_stdout);
292  fseek(new_stdout, 0, SEEK_SET);
293  char *tmps1=(char*)malloc((flen+1)*sizeof(char));
294  fread(tmps1,flen,1,new_stdout);
295  fclose(new_stdout);
296  tmps1[flen]=0;
297  map* err=createMap("text",tmps1);
298  addToMap(err,"code","InternalError");
299  printExceptionReportResponse(main_conf,err);
300  freeMap(&err);
301  free(err);
302}
303
304/**
305 * Create a string containing the JVM -XX:* option for a given map
306 * Depending on the map' name:
307 *  - in case the value is minus then the result will be : -XX:-name
308 *  - in case the value is plus then the result will be : -XX:+name
309 *  - in other cases the result will be : -XX:name=value
310 *
311 * @param m the map containing the option
312 * @return a char* containing the valide JVM option (-XX:*)
313 */
314char *parseJVMXXOption(map* m){
315  char *res=(char*)malloc((strlen(m->name)+strlen(m->value)+6)*sizeof(char));
316  if(strncasecmp(m->value,"minus",5)==0)
317    sprintf(res,"-XX:-%s",m->name);
318  else if(strncasecmp(m->value,"plus",5)==0)
319    sprintf(res,"-XX:+%s",m->name);
320  else
321    sprintf(res,"-XX:%s=%s",m->name,m->value);
322  return res;
323}
324
325/**
326 * Create a string containing the JVM -X** option for a given map.
327 * The result will be in the following format: -Xnamevalue
328 *
329 * @param m the map containing the option
330 * @return a char* containing the valide JVM option (-X**)
331 */
332char *parseJVMXOption(map* m){
333  char *res=(char*)malloc((strlen(m->name)+strlen(m->value)+5)*sizeof(char));
334  sprintf(res,"-X%s%s",m->name,m->value);
335  return res;
336}
337
338/**
339 * Convert a maps to a JAVA HashMap<String,HashMap<String,String>>
340 *
341 * @param env the JNI environment pointer
342 * @param t the maps to convert
343 * @param scHashMapClass the HashMap class
344 * @param scHashMap_class the HashMap class
345 * @param scHashMap_constructor the pointer to the hashMap constructor method
346 * @return a created JAVA HashMap containing the converted maps
347 * @warning make sure to free ressources returned by this function
348 */
349jobject HashMap_FromMaps(JNIEnv *env,maps* t,jclass scHashMapClass,jclass scHashMap_class,jmethodID scHashMap_constructor){
350  jobject scObject,scObject1;
351  if(scHashMap_constructor!=NULL){
352#ifdef JAVA7
353    scObject = (*env).NewObject(scHashMap_class, scHashMap_constructor);
354#else
355    scObject = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
356#endif
357    jmethodID put_mid = 0;
358
359#ifdef JAVA7
360    put_mid = (*env).GetMethodID(scHashMapClass, "put",
361                                  "(Ljava/lang/Object;Ljava/lang/Object;)"
362                                  "Ljava/lang/Object;");
363#else
364    put_mid = (*env)->GetMethodID(env,scHashMapClass, "put",
365                                  "(Ljava/lang/Object;Ljava/lang/Object;)"
366                                  "Ljava/lang/Object;");
367#endif
368    maps* tmp=t;
369    while(tmp!=NULL){
370      map* tmap=getMapType(tmp->content);
371      map* tmp1=tmp->content;
372#ifdef JAVA7
373      scObject1 = (*env).NewObject(scHashMap_class, scHashMap_constructor);
374#else
375      scObject1 = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
376#endif
377      map* sizeV=getMap(tmp1,"size");
378      map* isArray=getMap(tmp1,"isArray");
379      map* alen=getMap(tmp1,"length");
380      while(tmp1!=NULL){
381        if(strcmp(tmp1->name,"value")==0){
382          if(isArray==NULL){
383            if(sizeV!=NULL && strcmp(tmp1->name,"value")==0){
384#ifdef JAVA7
385              jbyteArray tmpData=(*env).NewByteArray(atoi(sizeV->value));
386              (*env).SetByteArrayRegion(tmpData,0,atoi(sizeV->value),(const jbyte *)tmp1->value);
387              (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), tmpData);
388#else
389              jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sizeV->value));
390              (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sizeV->value),tmp1->value);
391              (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), tmpData);
392#endif
393            }else
394#ifdef JAVA7
395              (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value));
396#else
397              (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
398#endif
399          }
400          else{
401            int alen1=atoi(alen->value);
402            fprintf(stderr,"LENGTH %d \n",alen1);
403           
404            jclass scArrayListClass,scArrayList_class;
405            jmethodID scArrayList_constructor;
406            jobject scObject2;
407#ifdef JAVA7
408            scArrayListClass = (*env).FindClass("java/util/ArrayList");
409            scArrayList_class = (jclass)(*env).NewGlobalRef(scArrayListClass);
410            scArrayList_constructor = (*env).GetMethodID(scArrayList_class, "<init>", "()V");
411            jmethodID add_mid = 0;
412            scObject2 = (*env).NewObject(scArrayList_class, scArrayList_constructor);
413
414            add_mid = (*env).GetMethodID(scArrayListClass,
415                                          "add","(Ljava/lang/Object;)Z");
416#else
417            scArrayListClass = (*env)->FindClass(env, "java/util/ArrayList");
418            scArrayList_class = (*env)->NewGlobalRef(env, scArrayListClass);
419            scArrayList_constructor = (*env)->GetMethodID(env, scArrayList_class, "<init>", "()V");
420            jmethodID add_mid = 0;
421            scObject2 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor);
422
423            add_mid = (*env)->GetMethodID(env,scArrayListClass,
424                                          "add","(Ljava/lang/Object;)Z");
425#endif     
426            int i;
427           
428            for(i=0;i<alen1;i++){
429              map* vMap=getMapArray(tmp->content,"value",i);       
430              map* sMap=getMapArray(tmp->content,"size",i);
431              map* mMap=getMapArray(tmp->content,tmap->value,i);
432             
433              if(sMap!=NULL && vMap!=NULL && strncmp(vMap->name,"value",5)==0){
434#ifdef JAVA7
435                jbyteArray tmpData=(*env).NewByteArray(atoi(sMap->value));
436                (*env).SetByteArrayRegion(tmpData,0,atoi(sMap->value),(const jbyte *)vMap->value);
437                (*env).CallObjectMethod(scObject2, add_mid, tmpData);
438#else
439                jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sMap->value));
440                (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sMap->value),vMap->value);
441                (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData);
442#endif
443              }else{
444#ifdef JAVA7
445                jobject tmpData=(*env).NewStringUTF(vMap->value);
446                (*env).CallObjectMethod(scObject2, add_mid, tmpData);
447#else
448                jobject tmpData=(*env)->NewStringUTF(env,vMap->value);
449                (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData);
450#endif
451              }
452             
453            }
454
455#ifdef JAVA7
456            (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), scObject2);
457#else       
458            (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), scObject2);
459#endif
460
461          }
462        }
463        else
464#ifdef JAVA7
465          (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value));
466#else
467          (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
468#endif
469        tmp1=tmp1->next;
470      }
471#ifdef JAVA7
472      (*env).CallObjectMethod(scObject, put_mid, (*env).NewStringUTF(tmp->name), scObject1);
473#else
474      (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1);
475#endif
476      tmp=tmp->next;
477    }
478    return scObject;
479  }
480  else
481    return NULL;
482}
483
484/**
485 * Convert a JAVA HashMap<String,HashMap<String,String>> to a maps
486 *
487 * @param env the JNI environment pointer
488 * @param t the HashMap
489 * @param scHashMapClass the hashMap class
490 * @return a created maps containing the converted HashMap
491 * @warning make sure to free ressources returned by this function
492 */
493maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){
494#ifdef DEBUG
495  fprintf(stderr,"mapsFromHashMap start\n");
496#endif
497  /**
498   * What need to be done (in java).
499   * Set set = hm.entrySet();
500   * Iterator i = set.iterator();
501   * while(i.hasNext()){
502   *   Map.Entry me = (Map.Entry)i.next();
503   *   System.out.println(me.getKey() + " : " + me.getValue() );
504   * }
505   */
506  jclass scSetClass,scIteratorClass,scMapEntryClass;
507  jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid;
508  if(scHashMapClass==NULL){
509#ifdef DEBUG
510    fprintf(stderr,"Unable to load java.util.HashMap\n");
511#endif
512    return NULL;
513  }
514#ifdef JAVA7
515  entrySet_mid = (*env).GetMethodID(scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
516  containsKey_mid = (*env).GetMethodID(scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z");
517  get_mid = (*env).GetMethodID(scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); 
518#else
519  entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
520  containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z");
521  get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); 
522#endif
523
524  if(containsKey_mid==0){
525#ifdef DEBUG
526    fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid);
527#endif
528    return NULL;
529  }
530  if(get_mid==0){
531#ifdef DEBUG
532    fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid);
533#endif
534    return NULL;
535  }
536  if(entrySet_mid==0){
537#ifdef DEBUG
538    fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid);
539#endif
540    return NULL;
541  }
542#ifdef DEBUG
543  else
544    fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid);
545#endif
546
547#ifdef JAVA7
548  scSetClass = (*env).FindClass("java/util/Set");
549  iterator_mid = (*env).GetMethodID(scSetClass, "iterator", "()Ljava/util/Iterator;"); 
550#else
551  scSetClass = (*env)->FindClass(env, "java/util/Set");
552  iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); 
553#endif
554#ifdef DEBUG
555  fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid);
556#endif
557
558#ifdef JAVA7
559  scIteratorClass = (*env).FindClass("java/util/Iterator");
560  hasNext_mid = (*env).GetMethodID(scIteratorClass, "hasNext", "()Z");
561#else
562  scIteratorClass = (*env)->FindClass(env, "java/util/Iterator");
563  hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z");
564#endif
565
566#ifdef DEBUG
567  fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid);
568#endif
569
570#ifdef JAVA7
571  next_mid = (*env).GetMethodID(scIteratorClass, "next", "()Ljava/lang/Object;");
572#else
573  next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;");
574#endif
575#ifdef DEBUG
576  fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid);
577#endif
578
579#ifdef JAVA7
580  scMapEntryClass = (*env).FindClass("java/util/Map$Entry");
581  getKey_mid = (*env).GetMethodID(scMapEntryClass, "getKey", "()Ljava/lang/Object;");
582#else
583  scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry");
584  getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;");
585#endif
586#ifdef DEBUG
587  fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid);
588#endif
589#ifdef JAVA7
590  getValue_mid = (*env).GetMethodID(scMapEntryClass, "getValue", "()Ljava/lang/Object;");
591#else
592  getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;");
593#endif
594#ifdef DEBUG
595  fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid);
596#endif
597
598#ifdef JAVA7
599  jobject final_set=(*env).CallObjectMethod(t,entrySet_mid);
600  jobject final_iterator=(*env).CallObjectMethod(final_set,iterator_mid);
601#else
602  jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid);
603  jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid);
604#endif
605
606  maps* final_res=NULL;
607  map* res=NULL;
608#ifdef JAVA7
609  while((*env).CallBooleanMethod(final_iterator,hasNext_mid)){
610    jobject tmp=(*env).CallObjectMethod(final_iterator,next_mid);
611
612    jobject imap=(*env).CallObjectMethod(tmp,getValue_mid);
613    jobject set=(*env).CallObjectMethod(imap,entrySet_mid);
614    jobject iterator=(*env).CallObjectMethod(set,iterator_mid);
615
616#else
617  while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){
618    jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid);
619
620    jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid);
621    jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid);
622    jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid);
623
624#endif
625    int size=-1;
626#ifdef JAVA7
627    if((*env).CallBooleanMethod(imap,containsKey_mid,(*env).NewStringUTF("size"))){
628      jobject sizeV=(*env).CallObjectMethod(imap, get_mid,(*env).NewStringUTF("size"));
629      const char* sizeVS=(*env).GetStringUTFChars((jstring)sizeV, NULL);
630#else
631    if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){
632      jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size"));
633      const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL);
634#endif
635      size=atoi(sizeVS);
636      fprintf(stderr,"SIZE : %s\n",sizeVS);
637#ifdef JAVA7
638      (*env).ReleaseStringUTFChars((jstring)sizeV, sizeVS);
639#else
640      (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS);
641#endif
642    }
643   
644#ifdef JAVA7
645    while((*env).CallBooleanMethod(iterator,hasNext_mid)){
646      jobject tmp1=(*env).CallObjectMethod(iterator,next_mid);
647      jobject jk=(*env).CallObjectMethod(tmp1,getKey_mid);
648      jobject jv=(*env).CallObjectMethod(tmp1,getValue_mid);
649
650      const char* jkd=(*env).GetStringUTFChars((jstring)jk, NULL);
651#else
652    while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){
653      jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid);
654      jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid);
655      jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid);
656
657      const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL);
658#endif
659      if(size>=0 && strcmp(jkd,"value")==0){
660#ifdef JAVA7
661        jobject value=(jobject)(*env).GetByteArrayElements((jbyteArray)jv, NULL);
662#else
663        jobject value=(*env)->GetByteArrayElements(env, jv, NULL);
664#endif
665        if(res==NULL){
666          res=createMap(jkd,"");
667        }else{
668          addToMap(res,jkd,"");
669        }
670        map* tmpR=getMap(res,"value");
671        free(tmpR->value);
672        tmpR->value=(char*)malloc((size+1)*sizeof(char));
673        memmove(tmpR->value,value,size*sizeof(char));
674        tmpR->value[size]=0;
675        char tmp[128];
676        sprintf(tmp,"%d",size);
677        addToMap(res,"size",tmp);
678      }
679      else{
680#ifdef JAVA7
681        const char* jvd=(*env).GetStringUTFChars((jstring)jv, NULL);
682#else
683        const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL);
684#endif
685        if(res==NULL){
686          res=createMap(jkd,jvd);
687        }else{
688          addToMap(res,jkd,jvd);
689        }
690#ifdef JAVA7
691        (*env).ReleaseStringUTFChars((jstring)jv, jvd);
692#else
693        (*env)->ReleaseStringUTFChars(env, jv, jvd);
694#endif
695      }
696
697#ifdef JAVA7
698      (*env).ReleaseStringUTFChars((jstring)jk, jkd);
699#else
700      (*env)->ReleaseStringUTFChars(env, jk, jkd);
701#endif
702
703    }
704#ifdef JAVA7
705    jobject jk=(*env).CallObjectMethod(tmp,getKey_mid);
706#else
707    jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid);
708#endif
709    maps* cmap=(maps*)malloc(sizeof(maps));
710#ifdef JAVA7
711    cmap->name=(char*)(*env).GetStringUTFChars((jstring)jk, NULL);
712#else
713    cmap->name=(*env)->GetStringUTFChars(env, jk, NULL);
714#endif
715#ifdef DEBUG
716    fprintf(stderr," / %s \n",cmap->name);
717#endif
718    cmap->content=res;
719    cmap->next=NULL;
720    if(final_res==NULL)
721      final_res=dupMaps(&cmap);
722    else
723      addMapsToMaps(&final_res,cmap);
724    freeMaps(&cmap);
725    free(cmap);
726    cmap=NULL;
727    res=NULL;
728  }
729#ifdef DEBUG
730  fprintf(stderr,"mapsFromHashMap end\n");
731#endif
732
733  return final_res;
734}
735
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