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

Last change on this file since 927 was 917, checked in by djay, 6 years ago

Merge prototype-v0 branch in trunk

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 25.9 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* useFile=getMap(tmp1,"use_file");
430      map* cacheFile=getMap(tmp1,"cache_file");
431      map* isArray=getMap(tmp1,"isArray");
432      map* alen=getMap(tmp1,"length");
433      while(tmp1!=NULL){
434        if(strcmp(tmp1->name,"value")==0){
435          if(isArray==NULL){
436            if(sizeV!=NULL && strcmp(tmp1->name,"value")==0 && useFile==NULL){
437#ifdef JAVA7
438              jbyteArray tmpData=(*env).NewByteArray(atoi(sizeV->value));
439              (*env).SetByteArrayRegion(tmpData,0,atoi(sizeV->value),(const jbyte *)tmp1->value);
440              (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), tmpData);
441#else
442              jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sizeV->value));
443              (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sizeV->value),(jbyte*) tmp1->value);
444              (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), tmpData);
445#endif
446            }else
447#ifdef JAVA7
448              (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value));
449#else
450              (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
451#endif
452              if(useFile!=NULL)
453#ifdef JAVA7
454                (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF("cache_file"), (*env).NewStringUTF(cacheFile->value));
455#else
456                (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,"cache_file"), (*env)->NewStringUTF(env,cacheFile->value));
457#endif
458             
459          }
460          else{
461            int alen1=atoi(alen->value);
462            fprintf(stderr,"LENGTH %d \n",alen1);
463           
464            jclass scArrayListClass,scArrayList_class;
465            jmethodID scArrayList_constructor;
466            jobject scObject2,scObject3;
467#ifdef JAVA7
468            scArrayListClass = (*env).FindClass("java/util/ArrayList");
469            scArrayList_class = (jclass)(*env).NewGlobalRef(scArrayListClass);
470            scArrayList_constructor = (*env).GetMethodID(scArrayList_class, "<init>", "()V");
471            jmethodID add_mid = 0;
472            scObject2 = (*env).NewObject(scArrayList_class, scArrayList_constructor);
473            scObject3 = (*env).NewObject(scArrayList_class, scArrayList_constructor);
474
475            add_mid = (*env).GetMethodID(scArrayListClass,
476                                          "add","(Ljava/lang/Object;)Z");
477#else
478            scArrayListClass = (*env)->FindClass(env, "java/util/ArrayList");
479            scArrayList_class = (*env)->NewGlobalRef(env, scArrayListClass);
480            scArrayList_constructor = (*env)->GetMethodID(env, scArrayList_class, "<init>", "()V");
481            jmethodID add_mid = 0;
482            scObject2 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor);
483            scObject3 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor);
484
485            add_mid = (*env)->GetMethodID(env,scArrayListClass,
486                                          "add","(Ljava/lang/Object;)Z");
487#endif     
488            int i;
489           
490            for(i=0;i<alen1;i++){
491              map* cMap=getMapArray(tmp->content,"cache_file",i);
492              map* uMap=getMapArray(tmp->content,"use_file",i);
493              map* vMap=getMapArray(tmp->content,"value",i);
494              map* sMap=getMapArray(tmp->content,"size",i);
495              map* mMap=getMapArray(tmp->content,tmap->value,i);
496             
497              if(sMap!=NULL && vMap!=NULL && strncmp(vMap->name,"value",5)==0 && uMap==NULL){
498#ifdef JAVA7
499                jbyteArray tmpData=(*env).NewByteArray(atoi(sMap->value));
500                (*env).SetByteArrayRegion(tmpData,0,atoi(sMap->value),(const jbyte *)vMap->value);
501                (*env).CallObjectMethod(scObject2, add_mid, tmpData);
502#else
503                jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sMap->value));
504                (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sMap->value),vMap->value);
505                (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData);
506#endif
507              }else{
508#ifdef JAVA7
509                jobject tmpData=(*env).NewStringUTF(vMap->value);
510                (*env).CallObjectMethod(scObject2, add_mid, tmpData);
511#else
512                jobject tmpData=(*env)->NewStringUTF(env,vMap->value);
513                (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData);
514#endif
515              }
516
517              if(cMap!=NULL){
518#ifdef JAVA7
519                jobject tmpData=(*env).NewStringUTF(cMap->value);
520                (*env).CallObjectMethod(scObject3, add_mid, tmpData);
521#else
522                jobject tmpData=(*env)->NewStringUTF(env,cMap->value);
523                (*env)->CallObjectMethod(env,scObject3, add_mid, tmpData);
524#endif
525
526              }
527            }
528
529#ifdef JAVA7
530            (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), scObject2);
531            (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF("cache_file"), scObject3);
532#else       
533            (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), scObject2);
534            (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,"cache_file"), scObject3);
535#endif
536
537          }
538        }
539        else
540#ifdef JAVA7
541          (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value));
542#else
543          (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
544#endif
545        tmp1=tmp1->next;
546      }
547#ifdef JAVA7
548      (*env).CallObjectMethod(scObject, put_mid, (*env).NewStringUTF(tmp->name), scObject1);
549#else
550      (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1);
551#endif
552      tmp=tmp->next;
553    }
554    return scObject;
555  }
556  else
557    return NULL;
558}
559
560/**
561 * Convert a JAVA HashMap<String,HashMap<String,String>> to a maps
562 *
563 * @param env the JNI environment pointer
564 * @param t the HashMap
565 * @param scHashMapClass the hashMap class
566 * @return a created maps containing the converted HashMap
567 * @warning make sure to free resources returned by this function
568 */
569maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){
570#ifdef DEBUG
571  fprintf(stderr,"mapsFromHashMap start\n");
572#endif
573  /**
574   * What need to be done (in java).
575   * Set set = hm.entrySet();
576   * Iterator i = set.iterator();
577   * while(i.hasNext()){
578   *   Map.Entry me = (Map.Entry)i.next();
579   *   System.out.println(me.getKey() + " : " + me.getValue() );
580   * }
581   */
582  jclass scSetClass,scIteratorClass,scMapEntryClass;
583  jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid;
584  if(scHashMapClass==NULL){
585#ifdef DEBUG
586    fprintf(stderr,"Unable to load java.util.HashMap\n");
587#endif
588    return NULL;
589  }
590#ifdef JAVA7
591  entrySet_mid = (*env).GetMethodID(scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
592  containsKey_mid = (*env).GetMethodID(scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z");
593  get_mid = (*env).GetMethodID(scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); 
594#else
595  entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
596  containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z");
597  get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); 
598#endif
599
600  if(containsKey_mid==0){
601#ifdef DEBUG
602    fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid);
603#endif
604    return NULL;
605  }
606  if(get_mid==0){
607#ifdef DEBUG
608    fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid);
609#endif
610    return NULL;
611  }
612  if(entrySet_mid==0){
613#ifdef DEBUG
614    fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid);
615#endif
616    return NULL;
617  }
618#ifdef DEBUG
619  else
620    fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid);
621#endif
622
623#ifdef JAVA7
624  scSetClass = (*env).FindClass("java/util/Set");
625  iterator_mid = (*env).GetMethodID(scSetClass, "iterator", "()Ljava/util/Iterator;"); 
626#else
627  scSetClass = (*env)->FindClass(env, "java/util/Set");
628  iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); 
629#endif
630#ifdef DEBUG
631  fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid);
632#endif
633
634#ifdef JAVA7
635  scIteratorClass = (*env).FindClass("java/util/Iterator");
636  hasNext_mid = (*env).GetMethodID(scIteratorClass, "hasNext", "()Z");
637#else
638  scIteratorClass = (*env)->FindClass(env, "java/util/Iterator");
639  hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z");
640#endif
641
642#ifdef DEBUG
643  fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid);
644#endif
645
646#ifdef JAVA7
647  next_mid = (*env).GetMethodID(scIteratorClass, "next", "()Ljava/lang/Object;");
648#else
649  next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;");
650#endif
651#ifdef DEBUG
652  fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid);
653#endif
654
655#ifdef JAVA7
656  scMapEntryClass = (*env).FindClass("java/util/Map$Entry");
657  getKey_mid = (*env).GetMethodID(scMapEntryClass, "getKey", "()Ljava/lang/Object;");
658#else
659  scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry");
660  getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;");
661#endif
662#ifdef DEBUG
663  fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid);
664#endif
665#ifdef JAVA7
666  getValue_mid = (*env).GetMethodID(scMapEntryClass, "getValue", "()Ljava/lang/Object;");
667#else
668  getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;");
669#endif
670#ifdef DEBUG
671  fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid);
672#endif
673
674#ifdef JAVA7
675  jobject final_set=(*env).CallObjectMethod(t,entrySet_mid);
676  jobject final_iterator=(*env).CallObjectMethod(final_set,iterator_mid);
677#else
678  jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid);
679  jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid);
680#endif
681
682  maps* final_res=NULL;
683  map* res=NULL;
684#ifdef JAVA7
685  while((*env).CallBooleanMethod(final_iterator,hasNext_mid)){
686    jobject tmp=(*env).CallObjectMethod(final_iterator,next_mid);
687
688    jobject imap=(*env).CallObjectMethod(tmp,getValue_mid);
689    jobject set=(*env).CallObjectMethod(imap,entrySet_mid);
690    jobject iterator=(*env).CallObjectMethod(set,iterator_mid);
691
692#else
693  while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){
694    jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid);
695
696    jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid);
697    jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid);
698    jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid);
699
700#endif
701    int size=-1;
702#ifdef JAVA7
703    if((*env).CallBooleanMethod(imap,containsKey_mid,(*env).NewStringUTF("size"))){
704      jobject sizeV=(*env).CallObjectMethod(imap, get_mid,(*env).NewStringUTF("size"));
705      const char* sizeVS=(*env).GetStringUTFChars((jstring)sizeV, NULL);
706#else
707    if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){
708      jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size"));
709      const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL);
710#endif
711      size=atoi(sizeVS);
712      fprintf(stderr,"SIZE : %s\n",sizeVS);
713#ifdef JAVA7
714      (*env).ReleaseStringUTFChars((jstring)sizeV, sizeVS);
715#else
716      (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS);
717#endif
718    }
719   
720#ifdef JAVA7
721    while((*env).CallBooleanMethod(iterator,hasNext_mid)){
722      jobject tmp1=(*env).CallObjectMethod(iterator,next_mid);
723      jobject jk=(*env).CallObjectMethod(tmp1,getKey_mid);
724      jobject jv=(*env).CallObjectMethod(tmp1,getValue_mid);
725
726      const char* jkd=(*env).GetStringUTFChars((jstring)jk, NULL);
727#else
728    while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){
729      jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid);
730      jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid);
731      jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid);
732
733      const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL);
734#endif
735      if(size>=0 && strcmp(jkd,"value")==0){
736#ifdef JAVA7
737        jobject value=(jobject)(*env).GetByteArrayElements((jbyteArray)jv, NULL);
738#else
739        jobject value=(*env)->GetByteArrayElements(env, jv, NULL);
740#endif
741        if(res==NULL){
742          res=createMap(jkd,"");
743        }else{
744          addToMap(res,jkd,"");
745        }
746        map* tmpR=getMap(res,"value");
747        free(tmpR->value);
748        tmpR->value=(char*)malloc((size+1)*sizeof(char));
749        memmove(tmpR->value,value,size*sizeof(char));
750        tmpR->value[size]=0;
751        char tmp[128];
752        sprintf(tmp,"%d",size);
753        addToMap(res,"size",tmp);
754      }
755      else{
756#ifdef JAVA7
757        const char* jvd=(*env).GetStringUTFChars((jstring)jv, NULL);
758#else
759        const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL);
760#endif
761        if(res==NULL){
762          res=createMap(jkd,jvd);
763        }else{
764          addToMap(res,jkd,jvd);
765        }
766#ifdef JAVA7
767        (*env).ReleaseStringUTFChars((jstring)jv, jvd);
768#else
769        (*env)->ReleaseStringUTFChars(env, jv, jvd);
770#endif
771      }
772
773#ifdef JAVA7
774      (*env).ReleaseStringUTFChars((jstring)jk, jkd);
775#else
776      (*env)->ReleaseStringUTFChars(env, jk, jkd);
777#endif
778
779    }
780#ifdef JAVA7
781    jobject jk=(*env).CallObjectMethod(tmp,getKey_mid);
782#else
783    jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid);
784#endif
785    maps* cmap=createMaps(
786#ifdef JAVA7
787      (char*)(*env).GetStringUTFChars((jstring)jk, NULL)
788#else
789      (*env)->GetStringUTFChars(env, jk, NULL)
790#endif
791                          );
792#ifdef DEBUG
793    fprintf(stderr," / %s \n",cmap->name);
794#endif
795    cmap->content=res;
796    if(final_res==NULL)
797      final_res=dupMaps(&cmap);
798    else
799      addMapsToMaps(&final_res,cmap);
800    freeMaps(&cmap);
801    free(cmap);
802    cmap=NULL;
803    res=NULL;
804  }
805#ifdef DEBUG
806  fprintf(stderr,"mapsFromHashMap end\n");
807#endif
808  return final_res;
809}
810
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

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