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

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

Remove the default java.library.path definition and use an optional [java] section instead (where the key java.library.path, amongst other, may be defined). Change _ function name to translate in the Java ZOO-API. Use a ogr.zoo_project package containing the ZOO class. Fix the hello-java service and update doc consequently

  • 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  }
83  sprintf(oclasspath,"-Djava.class.path=%s",classpath);
84#ifdef DEBUG
85  fprintf(stderr,"CLASSPATH=%s\n",classpath);
86  fprintf(stderr,"(%s)\n",oclasspath);
87#endif
88#ifndef USE_JDB
89  int njdb=0;
90#else
91  int njdb=2;
92#endif
93#ifndef WIN32
94  int nb=2+njdb;
95#endif
96  int nbs[3]={0,0,0};
97  maps* javaMap=getMaps(*main_conf,"java");
98  if(javaMap!=NULL){
99    nbs[0]+=count(javaMap->content);
100  }
101  int nbc1=0;
102  maps* javaXMap=getMaps(*main_conf,"javax");
103  if(javaXMap!=NULL){
104    nbs[1]+=count(javaXMap->content);
105  }
106  int nbc0=0;
107  maps* javaXXMap=getMaps(*main_conf,"javaxx");
108  if(javaXXMap!=NULL){
109    nbs[2]+=count(javaXXMap->content);
110  }
111#ifdef WIN32
112  const int nb=2+nbs[1]+nbs[2]+nbs[0];
113  JavaVMOption *options=(JavaVMOption*)malloc(nb*sizeof(JavaVMOption));
114#else
115  JavaVMOption options[nb+nbs[1]+nbs[2]+nbs[0]+1];
116#endif
117  JavaVMInitArgs vm_args;
118  JavaVM *jvm;
119  JNIEnv *env;
120  long result;
121  jmethodID pmid;
122  jclass cls;
123#ifdef JAVA7
124  jobject cls_gr;
125#else
126  jclass cls_gr;
127#endif
128  int i,start;
129  options[0].optionString = oclasspath;
130  options[0].extraInfo=NULL;
131  options[1].optionString = "-server";
132  options[1].extraInfo=NULL;
133#ifdef USE_JDB
134  options[2].optionString = "-Xdebug";
135  options[2].extraInfo=NULL;
136  options[3].optionString = "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7896";
137  options[3].extraInfo=NULL;
138#endif
139#ifdef WIN32
140  start=2;
141  options[1].optionString = "-Xmx512m";
142#else
143  start=2+njdb;
144#endif
145  map *cursors[3]={NULL,NULL,NULL};
146  if(javaMap!=NULL)
147    cursors[0]=javaMap->content;
148  if(javaXMap!=NULL)
149    cursors[1]=javaXMap->content;
150  if(javaXXMap!=NULL)
151    cursors[2]=javaXXMap->content;
152  for(i=0;i<nbs[0];i++){
153    char *tmp=parseJVMOption(cursors[0]);
154    options[start+i].optionString = zStrdup(tmp);
155    options[start+i].extraInfo=NULL;
156    free(tmp);
157    cursors[0]=cursors[0]->next;
158  }
159  for(;i<nbs[0]+nbs[1];i++){
160    char *tmp=parseJVMXOption(cursors[1]);
161    options[start+i].optionString = zStrdup(tmp);
162    options[start+i].extraInfo=NULL;
163    free(tmp);
164    cursors[1]=cursors[1]->next;
165  }
166  for(;i<nbs[0]+nbs[2]+nbs[1];i++){
167    char *tmp=parseJVMXXOption(cursors[2]);
168    options[start+i].optionString = zStrdup(tmp);
169    options[start+i].extraInfo=NULL;
170    free(tmp);
171    cursors[2]=cursors[2]->next;
172  }
173
174  JNI_GetDefaultJavaVMInitArgs(&vm_args);
175  vm_args.version = JNI_VERSION_1_6;
176  vm_args.options = options;
177  vm_args.nOptions = start+nbs[0]+nbs[1]+nbs[2];
178  vm_args.ignoreUnrecognized = JNI_TRUE;
179
180  result = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args);
181  if(result == JNI_ERR ) {
182    fprintf(stderr,"Error invoking the JVM");
183    return -1;
184  }
185#ifdef DEBUG
186  else
187    fprintf(stderr,"JAVA VM Started\n");
188#endif
189
190  tmp=getMap(s->content,"serviceProvider");
191#ifdef JAVA7
192  cls = (*env).FindClass(tmp->value);
193#else
194  cls = (*env)->FindClass(env,tmp->value);
195#endif
196  if( cls == NULL ) {
197    displayStack(env,*main_conf);
198#ifdef JAVA7
199    (*jvm).DestroyJavaVM();
200#else
201    (*jvm)->DestroyJavaVM(jvm);
202#endif
203    return -1;
204  }
205#ifdef DEBUG
206  else{
207    fprintf(stderr,"%s loaded\n",tmp->value);
208  }
209#endif
210#ifdef JAVA7
211  cls_gr = (*env)NewGlobalRef(cls);
212#else
213  cls_gr = (*env)->NewGlobalRef(env, cls);
214#endif
215
216  if (cls != NULL) {
217#ifdef JAVA7
218@    (*env).ExceptionClear();
219    pmid=(*env).GetStaticMethodID(cls, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I");
220#else
221    (*env)->ExceptionClear(env);
222    pmid=(*env)->GetStaticMethodID(env,cls_gr, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I");
223#endif
224    if (pmid!=0){
225#ifdef DEBUG
226      fprintf(stderr,"Function successfully loaded\n");
227#endif
228      jclass scHashMapClass,scHashMap_class;
229      jmethodID scHashMap_constructor;
230#ifdef JAVA7
231      scHashMapClass = (*env).FindClass("java/util/HashMap");
232      scHashMap_class = (jclass)(*env).NewGlobalRef(scHashMapClass);
233      scHashMap_constructor = (*env).GetMethodID(scHashMap_class, "<init>", "()V");
234#else
235      scHashMapClass = (*env)->FindClass(env, "java/util/HashMap");
236      scHashMap_class = (*env)->NewGlobalRef(env, scHashMapClass);
237      scHashMap_constructor = (*env)->GetMethodID(env, scHashMap_class, "<init>", "()V");
238#endif
239      /**
240       * The 3 standard parameter for each services
241       */
242      jobject arg1=HashMap_FromMaps(env,m,scHashMapClass,scHashMap_class,scHashMap_constructor);
243      jobject arg2=HashMap_FromMaps(env,inputs,scHashMapClass,scHashMap_class,scHashMap_constructor);
244      jobject arg3=HashMap_FromMaps(env,outputs,scHashMapClass,scHashMap_class,scHashMap_constructor);
245      jint pValue=0;
246
247#ifdef JAVA7
248      pValue=(*env).CallStaticIntMethod(cls,pmid,arg1,arg2,arg3);
249#else
250      pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3);
251#endif
252      if (pValue != (jint)NULL){
253        res=pValue;
254        m=mapsFromHashMap(env,arg1,scHashMapClass);
255        *main_conf=m;
256        outputs=mapsFromHashMap(env,arg3,scHashMapClass);
257        *real_outputs=outputs;
258
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 informations 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 ressources 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 ressources 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=(maps*)malloc(sizeof(maps));
761#ifdef JAVA7
762    cmap->name=(char*)(*env).GetStringUTFChars((jstring)jk, NULL);
763#else
764    cmap->name=(*env)->GetStringUTFChars(env, jk, NULL);
765#endif
766#ifdef DEBUG
767    fprintf(stderr," / %s \n",cmap->name);
768#endif
769    cmap->content=res;
770    cmap->next=NULL;
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
784  return final_res;
785}
786
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