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

Last change on this file since 863 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
RevLine 
[580]1/*
[1]2 * Author : Gérald FENOY
3 *
[388]4 * Copyright (c) 2009-2013 GeoLabs SARL
[1]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"
[640]26#include "response_print.h"
[1]27
[580]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 */
[1]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;
[767]42  map* cwdMap=getMapFromMaps(*main_conf,"lenv","cwd");
43  char *ntmp=NULL;
44  if(cwdMap!=NULL)
45    ntmp=zStrdup(cwdMap->value);
[1]46  map* tmp=getMap(request,"metapath");
[355]47  char *classpath;
48  char *oclasspath;
[1]49  int res=SERVICE_FAILED;
[352]50  char *cclasspath=getenv("CLASSPATH");
[1]51  if(tmp!=NULL){
[355]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));
[364]55#ifndef WIN32
[352]56      sprintf(classpath,"%s/%s/:%s",ntmp,tmp->value,cclasspath);
[364]57#else
58      sprintf(classpath,"%s/%s/;%s",ntmp,tmp->value,cclasspath);
59#endif
[355]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));
[352]64      sprintf(classpath,"%s/%s/",ntmp,tmp->value);
[355]65    }
[388]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    }
[1]81  }
[388]82  sprintf(oclasspath,"-Djava.class.path=%s",classpath);
[1]83#ifdef DEBUG
84  fprintf(stderr,"CLASSPATH=%s\n",classpath);
[354]85  fprintf(stderr,"(%s)\n",oclasspath);
[1]86#endif
[767]87#ifndef USE_JDB
88  int njdb=0;
89#else
90  int njdb=2;
91#endif
[765]92#ifndef WIN32
[771]93  int nb=2+njdb;
[765]94#endif
[771]95  int nbs[3]={0,0,0};
96  maps* javaMap=getMaps(*main_conf,"java");
97  if(javaMap!=NULL){
98    nbs[0]+=count(javaMap->content);
[539]99  }
100  int nbc1=0;
[640]101  maps* javaXMap=getMaps(*main_conf,"javax");
[539]102  if(javaXMap!=NULL){
[771]103    nbs[1]+=count(javaXMap->content);
[539]104  }
[771]105  int nbc0=0;
106  maps* javaXXMap=getMaps(*main_conf,"javaxx");
107  if(javaXXMap!=NULL){
108    nbs[2]+=count(javaXXMap->content);
109  }
[364]110#ifdef WIN32
[771]111  const int nb=2+nbs[1]+nbs[2]+nbs[0];
[765]112  JavaVMOption *options=(JavaVMOption*)malloc(nb*sizeof(JavaVMOption));
[364]113#else
[771]114  JavaVMOption options[nb+nbs[1]+nbs[2]+nbs[0]+1];
[364]115#endif
[1]116  JavaVMInitArgs vm_args;
117  JavaVM *jvm;
118  JNIEnv *env;
119  long result;
120  jmethodID pmid;
[364]121  jclass cls;
122#ifdef JAVA7
123  jobject cls_gr;
124#else
125  jclass cls_gr;
126#endif
[539]127  int i,start;
[748]128  options[0].optionString = oclasspath;
[747]129  options[0].extraInfo=NULL;
[767]130  options[1].optionString = "-server";
131  options[1].extraInfo=NULL;
[771]132#ifdef USE_JDB
133  options[2].optionString = "-Xdebug";
[767]134  options[2].extraInfo=NULL;
[771]135  options[3].optionString = "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7896";
[767]136  options[3].extraInfo=NULL;
137#endif
[364]138#ifdef WIN32
[539]139  start=2;
[388]140  options[1].optionString = "-Xmx512m";
[539]141#else
[771]142  start=2+njdb;
[364]143#endif
[771]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]);
[748]153    options[start+i].optionString = zStrdup(tmp);
[747]154    options[start+i].extraInfo=NULL;
[539]155    free(tmp);
[771]156    cursors[0]=cursors[0]->next;
[539]157  }
[771]158  for(;i<nbs[0]+nbs[1];i++){
159    char *tmp=parseJVMXOption(cursors[1]);
[748]160    options[start+i].optionString = zStrdup(tmp);
[747]161    options[start+i].extraInfo=NULL;
[539]162    free(tmp);
[771]163    cursors[1]=cursors[1]->next;
[539]164  }
[771]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  }
[1]172
[59]173  JNI_GetDefaultJavaVMInitArgs(&vm_args);
[352]174  vm_args.version = JNI_VERSION_1_6;
[1]175  vm_args.options = options;
[771]176  vm_args.nOptions = start+nbs[0]+nbs[1]+nbs[2];
[364]177  vm_args.ignoreUnrecognized = JNI_TRUE;
[1]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
[364]185  else
[1]186    fprintf(stderr,"JAVA VM Started\n");
187#endif
188
189  tmp=getMap(s->content,"serviceProvider");
[364]190#ifdef JAVA7
[767]191  cls = (*env).FindClass(tmp->value);
[364]192#else
[1]193  cls = (*env)->FindClass(env,tmp->value);
[364]194#endif
[1]195  if( cls == NULL ) {
[388]196    displayStack(env,*main_conf);
[364]197#ifdef JAVA7
198    (*jvm).DestroyJavaVM();
199#else
[1]200    (*jvm)->DestroyJavaVM(jvm);
[364]201#endif
[352]202    return -1;
[1]203  }
204#ifdef DEBUG
205  else{
206    fprintf(stderr,"%s loaded\n",tmp->value);
207  }
208#endif
[767]209#ifdef JAVA7
210  cls_gr = (*env)NewGlobalRef(cls);
211#else
212  cls_gr = (*env)->NewGlobalRef(env, cls);
213#endif
[1]214
215  if (cls != NULL) {
[364]216#ifdef JAVA7
[771]217@    (*env).ExceptionClear();
[364]218    pmid=(*env).GetStaticMethodID(cls, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I");
219#else
[1]220    (*env)->ExceptionClear(env);
221    pmid=(*env)->GetStaticMethodID(env,cls_gr, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I");
[364]222#endif
[1]223    if (pmid!=0){
224#ifdef DEBUG
225      fprintf(stderr,"Function successfully loaded\n");
226#endif
[57]227      jclass scHashMapClass,scHashMap_class;
228      jmethodID scHashMap_constructor;
[364]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
[57]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");
[364]237#endif
[1]238      /**
239       * The 3 standard parameter for each services
240       */
[57]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);
[1]244      jint pValue=0;
245
[364]246#ifdef JAVA7
247      pValue=(*env).CallStaticIntMethod(cls,pmid,arg1,arg2,arg3);
248#else
[1]249      pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3);
[364]250#endif
[114]251      if (pValue != (jint)NULL){
[1]252        res=pValue;
[790]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);
[1]259#ifdef DEBUG
260        fprintf(stderr,"Result of call: %i\n", pValue);
261        dumpMaps(inputs);
262        dumpMaps(outputs);
263#endif
[388]264      }else{
265        displayStack(env,*main_conf);
[364]266#ifdef JAVA7
267        (*jvm).DestroyJavaVM();
268#else
[1]269        (*jvm)->DestroyJavaVM(jvm);
[364]270#endif
[9]271        return -1;
[1]272      }
273    }
274    else{
[388]275      displayStack(env,*main_conf);
[364]276#ifdef JAVA7
[767]277      (*env).ExceptionDescribe();
[364]278      (*jvm).DestroyJavaVM();
279#else
[1]280      (*jvm)->DestroyJavaVM(jvm);
[364]281#endif
[9]282      return -1;
[1]283    }
284  }
[364]285#ifdef JAVA7
286      (*jvm).DestroyJavaVM();
287#else
[1]288  (*jvm)->DestroyJavaVM(jvm);
[364]289#endif
[771]290  for(i=0;i<nbs[2]+nbs[1]+nbs[0];i++){
[748]291    free(options[start+i].optionString);
292  }
[765]293#ifdef WIN32
294  free(options);
295#endif
[1]296  return res;
297}
298
[388]299/**
300 * Error handling: display stack trace in an ExceptionReport Document
[580]301 *
302 * @param env the JNI environment pointer
303 * @param main_conf the conf maps containing the main.cfg settings
[388]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+");
[527]310  if(new_stdout==NULL){
[781]311    map* err=createMap("text","Unable to run your service, no debug information can be provided (please verify privileges on tmpPath)");
[527]312    addToMap(err,"code","InternalError");
313    printExceptionReportResponse(main_conf,err);
314    freeMap(&err);
315    free(err);
316    return;
317  }
[388]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
[580]342/**
[771]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/**
[580]356 * Create a string containing the JVM -XX:* option for a given map
357 * Depending on the map' name:
[613]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
[580]361 *
362 * @param m the map containing the option
363 * @return a char* containing the valide JVM option (-XX:*)
364 */
[539]365char *parseJVMXXOption(map* m){
[747]366  char *res=(char*)malloc((strlen(m->name)+strlen(m->value)+6)*sizeof(char));
[539]367  if(strncasecmp(m->value,"minus",5)==0)
368    sprintf(res,"-XX:-%s",m->name);
[767]369  else if(strncasecmp(m->value,"plus",4)==0)
[539]370    sprintf(res,"-XX:+%s",m->name);
371  else
372    sprintf(res,"-XX:%s=%s",m->name,m->value);
373  return res;
374}
375
[580]376/**
[613]377 * Create a string containing the JVM -X** option for a given map.
378 * The result will be in the following format: -Xnamevalue
[580]379 *
380 * @param m the map containing the option
[613]381 * @return a char* containing the valide JVM option (-X**)
[580]382 */
[539]383char *parseJVMXOption(map* m){
384  char *res=(char*)malloc((strlen(m->name)+strlen(m->value)+5)*sizeof(char));
[613]385  sprintf(res,"-X%s%s",m->name,m->value);
[539]386  return res;
387}
388
[580]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
[781]398 * @warning make sure to free resources returned by this function
[580]399 */
[57]400jobject HashMap_FromMaps(JNIEnv *env,maps* t,jclass scHashMapClass,jclass scHashMap_class,jmethodID scHashMap_constructor){
[1]401  jobject scObject,scObject1;
402  if(scHashMap_constructor!=NULL){
[364]403#ifdef JAVA7
404    scObject = (*env).NewObject(scHashMap_class, scHashMap_constructor);
405#else
[1]406    scObject = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
[364]407#endif
[1]408    jmethodID put_mid = 0;
409
[364]410#ifdef JAVA7
411    put_mid = (*env).GetMethodID(scHashMapClass, "put",
412                                  "(Ljava/lang/Object;Ljava/lang/Object;)"
413                                  "Ljava/lang/Object;");
414#else
[1]415    put_mid = (*env)->GetMethodID(env,scHashMapClass, "put",
416                                  "(Ljava/lang/Object;Ljava/lang/Object;)"
417                                  "Ljava/lang/Object;");
[364]418#endif
[1]419    maps* tmp=t;
420    while(tmp!=NULL){
[360]421      map* tmap=getMapType(tmp->content);
[1]422      map* tmp1=tmp->content;
[364]423#ifdef JAVA7
424      scObject1 = (*env).NewObject(scHashMap_class, scHashMap_constructor);
425#else
[1]426      scObject1 = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
[364]427#endif
[68]428      map* sizeV=getMap(tmp1,"size");
[360]429      map* isArray=getMap(tmp1,"isArray");
430      map* alen=getMap(tmp1,"length");
[1]431      while(tmp1!=NULL){
[360]432        if(strcmp(tmp1->name,"value")==0){
433          if(isArray==NULL){
434            if(sizeV!=NULL && strcmp(tmp1->name,"value")==0){
[364]435#ifdef JAVA7
436              jbyteArray tmpData=(*env).NewByteArray(atoi(sizeV->value));
437              (*env).SetByteArrayRegion(tmpData,0,atoi(sizeV->value),(const jbyte *)tmp1->value);
[767]438              (*env).CallObjectMethod(env,scObject1, put_mid, (*env).NewStringUTF(env,tmp1->name), tmpData);
[364]439#else
[360]440              jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sizeV->value));
[767]441              (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sizeV->value),(jbyte*) tmp1->value);
[360]442              (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), tmpData);
[364]443#endif
[360]444            }else
[364]445#ifdef JAVA7
446              (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value));
447#else
[360]448              (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
[364]449#endif
[360]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;
[388]457            jobject scObject2;
[364]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
[360]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");
[364]476#endif     
[360]477            int i;
478           
479            for(i=0;i<alen1;i++){
[771]480              map* vMap=getMapArray(tmp->content,"value",i);
[360]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){
[364]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
[360]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);
[364]493#endif
[360]494              }else{
[364]495#ifdef JAVA7
496                jobject tmpData=(*env).NewStringUTF(vMap->value);
497                (*env).CallObjectMethod(scObject2, add_mid, tmpData);
498#else
[360]499                jobject tmpData=(*env)->NewStringUTF(env,vMap->value);
500                (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData);
[364]501#endif
[360]502              }
503             
504            }
[364]505
506#ifdef JAVA7
507            (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), scObject2);
508#else       
[360]509            (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), scObject2);
[364]510#endif
511
[360]512          }
513        }
514        else
[364]515#ifdef JAVA7
516          (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value));
517#else
[68]518          (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
[364]519#endif
[1]520        tmp1=tmp1->next;
521      }
[364]522#ifdef JAVA7
523      (*env).CallObjectMethod(scObject, put_mid, (*env).NewStringUTF(tmp->name), scObject1);
524#else
[1]525      (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1);
[364]526#endif
[1]527      tmp=tmp->next;
[57]528    }
[1]529    return scObject;
530  }
531  else
532    return NULL;
533}
534
[580]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
[781]542 * @warning make sure to free resources returned by this function
[580]543 */
[57]544maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){
[1]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   */
[640]557  jclass scSetClass,scIteratorClass,scMapEntryClass;
[68]558  jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid;
[1]559  if(scHashMapClass==NULL){
[114]560#ifdef DEBUG
[1]561    fprintf(stderr,"Unable to load java.util.HashMap\n");
[114]562#endif
[1]563    return NULL;
564  }
[364]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
[1]570  entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
[68]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;"); 
[364]573#endif
[114]574
[68]575  if(containsKey_mid==0){
[114]576#ifdef DEBUG
[68]577    fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid);
[114]578#endif
579    return NULL;
[68]580  }
581  if(get_mid==0){
[114]582#ifdef DEBUG
[68]583    fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid);
[114]584#endif
585    return NULL;
[68]586  }
[1]587  if(entrySet_mid==0){
[114]588#ifdef DEBUG
[1]589    fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid);
[114]590#endif
[1]591    return NULL;
592  }
593#ifdef DEBUG
594  else
595    fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid);
596#endif
597
[364]598#ifdef JAVA7
599  scSetClass = (*env).FindClass("java/util/Set");
600  iterator_mid = (*env).GetMethodID(scSetClass, "iterator", "()Ljava/util/Iterator;"); 
601#else
[1]602  scSetClass = (*env)->FindClass(env, "java/util/Set");
603  iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); 
[364]604#endif
[1]605#ifdef DEBUG
606  fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid);
607#endif
608
[364]609#ifdef JAVA7
610  scIteratorClass = (*env).FindClass("java/util/Iterator");
611  hasNext_mid = (*env).GetMethodID(scIteratorClass, "hasNext", "()Z");
612#else
[1]613  scIteratorClass = (*env)->FindClass(env, "java/util/Iterator");
614  hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z");
[364]615#endif
616
[1]617#ifdef DEBUG
618  fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid);
619#endif
[364]620
621#ifdef JAVA7
622  next_mid = (*env).GetMethodID(scIteratorClass, "next", "()Ljava/lang/Object;");
623#else
[1]624  next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;");
[364]625#endif
[1]626#ifdef DEBUG
627  fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid);
628#endif
629
[364]630#ifdef JAVA7
631  scMapEntryClass = (*env).FindClass("java/util/Map$Entry");
632  getKey_mid = (*env).GetMethodID(scMapEntryClass, "getKey", "()Ljava/lang/Object;");
633#else
[1]634  scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry");
635  getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;");
[364]636#endif
[1]637#ifdef DEBUG
638  fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid);
639#endif
[364]640#ifdef JAVA7
641  getValue_mid = (*env).GetMethodID(scMapEntryClass, "getValue", "()Ljava/lang/Object;");
642#else
[1]643  getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;");
[364]644#endif
[1]645#ifdef DEBUG
646  fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid);
647#endif
648
[364]649#ifdef JAVA7
650  jobject final_set=(*env).CallObjectMethod(t,entrySet_mid);
651  jobject final_iterator=(*env).CallObjectMethod(final_set,iterator_mid);
652#else
[1]653  jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid);
654  jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid);
[364]655#endif
[1]656
657  maps* final_res=NULL;
658  map* res=NULL;
[364]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
[1]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);
[57]674
[364]675#endif
[68]676    int size=-1;
[364]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
[68]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"));
[114]684      const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL);
[364]685#endif
[68]686      size=atoi(sizeVS);
687      fprintf(stderr,"SIZE : %s\n",sizeVS);
[364]688#ifdef JAVA7
689      (*env).ReleaseStringUTFChars((jstring)sizeV, sizeVS);
690#else
[114]691      (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS);
[364]692#endif
[68]693    }
694   
[364]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
[1]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
[114]708      const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL);
[364]709#endif
[68]710      if(size>=0 && strcmp(jkd,"value")==0){
[364]711#ifdef JAVA7
712        jobject value=(jobject)(*env).GetByteArrayElements((jbyteArray)jv, NULL);
713#else
[68]714        jobject value=(*env)->GetByteArrayElements(env, jv, NULL);
[364]715#endif
[68]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;
[114]726        char tmp[128];
727        sprintf(tmp,"%d",size);
728        addToMap(res,"size",tmp);
[68]729      }
730      else{
[364]731#ifdef JAVA7
732        const char* jvd=(*env).GetStringUTFChars((jstring)jv, NULL);
733#else
[114]734        const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL);
[364]735#endif
[68]736        if(res==NULL){
737          res=createMap(jkd,jvd);
738        }else{
739          addToMap(res,jkd,jvd);
740        }
[364]741#ifdef JAVA7
742        (*env).ReleaseStringUTFChars((jstring)jv, jvd);
743#else
[114]744        (*env)->ReleaseStringUTFChars(env, jv, jvd);
[364]745#endif
[68]746      }
[59]747
[364]748#ifdef JAVA7
749      (*env).ReleaseStringUTFChars((jstring)jk, jkd);
750#else
[114]751      (*env)->ReleaseStringUTFChars(env, jk, jkd);
[364]752#endif
[59]753
[1]754    }
[364]755#ifdef JAVA7
756    jobject jk=(*env).CallObjectMethod(tmp,getKey_mid);
757#else
[1]758    jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid);
[364]759#endif
[790]760    maps* cmap=createMaps(
[364]761#ifdef JAVA7
[790]762      (char*)(*env).GetStringUTFChars((jstring)jk, NULL)
[364]763#else
[790]764      (*env)->GetStringUTFChars(env, jk, NULL)
[364]765#endif
[790]766                          );
[57]767#ifdef DEBUG
768    fprintf(stderr," / %s \n",cmap->name);
769#endif
[1]770    cmap->content=res;
[57]771    if(final_res==NULL)
[9]772      final_res=dupMaps(&cmap);
[57]773    else
[9]774      addMapsToMaps(&final_res,cmap);
775    freeMaps(&cmap);
776    free(cmap);
777    cmap=NULL;
[1]778    res=NULL;
779  }
780#ifdef DEBUG
781  fprintf(stderr,"mapsFromHashMap end\n");
782#endif
783  return final_res;
784}
[527]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