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

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

Fix issue with headers located in extern C. Define java.library.path at runtime to search for libZOO.so from the Java zoo-api.

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