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

Last change on this file since 828 was 790, checked in by djay, 8 years ago

Add support for nested inputs and outputs.

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