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

Last change on this file since 712 was 640, checked in by djay, 10 years ago

First version including zoo_service shared library

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