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

Last change on this file since 537 was 527, checked in by djay, 10 years ago

Fix #106 and #108

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 20.3 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
27int zoo_java_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
28  maps* m=*main_conf;
29  maps* inputs=*real_inputs;
30  maps* outputs=*real_outputs;
31  char ntmp[1024];
32  getcwd(ntmp,1024);
33  map* tmp=getMap(request,"metapath");
34  char *classpath;
35  char *oclasspath;
36  int res=SERVICE_FAILED;
37  char *cclasspath=getenv("CLASSPATH");
38  if(tmp!=NULL){
39    if(cclasspath!=NULL){
40      classpath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+strlen(cclasspath)+4)*sizeof(char));
41      oclasspath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+strlen(cclasspath)+22)*sizeof(char));
42#ifndef WIN32
43      sprintf(classpath,"%s/%s/:%s",ntmp,tmp->value,cclasspath);
44#else
45      sprintf(classpath,"%s/%s/;%s",ntmp,tmp->value,cclasspath);
46#endif
47    }
48    else{
49      classpath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+3)*sizeof(char));
50      oclasspath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+21)*sizeof(char));
51      sprintf(classpath,"%s/%s/",ntmp,tmp->value);
52    }
53    sprintf(oclasspath,"-Djava.class.path=%s",classpath);
54  }else{
55    if(cclasspath!=NULL){
56      classpath=(char*) malloc((strlen(ntmp)+strlen(cclasspath)+3)*sizeof(char));
57      oclasspath=(char*) malloc((strlen(ntmp)+strlen(cclasspath)+21)*sizeof(char));
58#ifndef WIN32
59      sprintf(classpath,"%s/:%s",ntmp,cclasspath);
60#else
61      sprintf(classpath,"%s/;%s",ntmp,cclasspath);
62#endif
63    }
64    else{
65      classpath=(char*) malloc((strlen(ntmp)+2)*sizeof(char));
66      oclasspath=(char*) malloc((strlen(ntmp)+20)*sizeof(char));
67      sprintf(classpath,"%s/",ntmp);
68    }
69    sprintf(oclasspath,"-Djava.class.path=%s",classpath);
70
71  }
72  sprintf(oclasspath,"-Djava.class.path=%s",classpath);
73
74#ifdef DEBUG
75  fprintf(stderr,"CLASSPATH=%s\n",classpath);
76  fprintf(stderr,"(%s)\n",oclasspath);
77#endif
78
79#ifdef WIN32
80  JavaVMOption options[2];
81#else
82  JavaVMOption options[1];
83#endif
84  JavaVMInitArgs vm_args;
85  JavaVM *jvm;
86  JNIEnv *env;
87  long result;
88  jmethodID pmid;
89  jfieldID fid;
90  jobject jobj;
91  jclass cls;
92#ifdef JAVA7
93  jobject cls_gr;
94#else
95  jclass cls_gr;
96#endif
97  int i;
98
99  options[0].optionString = oclasspath;
100#ifdef WIN32
101  options[1].optionString = "-Xmx512m";
102  /*options[2].optionString = "-Xms128m";
103  options[3].optionString = "-XX:MaxPermSize=256m";
104  options[4].optionString = "-XX:MaxHeapFreeRatio=70";*/
105#endif
106
107  JNI_GetDefaultJavaVMInitArgs(&vm_args);
108  vm_args.version = JNI_VERSION_1_6;
109  vm_args.options = options;
110#ifdef WIN32
111  vm_args.nOptions = 2;
112#else
113  vm_args.nOptions = 1;
114#endif
115  vm_args.ignoreUnrecognized = JNI_TRUE;
116
117  result = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args);
118  if(result == JNI_ERR ) {
119    fprintf(stderr,"Error invoking the JVM");
120    return -1;
121  }
122#ifdef DEBUG
123  else
124    fprintf(stderr,"JAVA VM Started\n");
125#endif
126
127  tmp=getMap(s->content,"serviceProvider");
128#ifdef JAVA7
129  cls = env->FindClass(tmp->value);
130  cls_gr = env->NewGlobalRef(cls);
131#else
132  cls = (*env)->FindClass(env,tmp->value);
133  cls_gr = (*env)->NewGlobalRef(env, cls);
134#endif
135  if( cls == NULL ) {
136    displayStack(env,*main_conf);
137#ifdef JAVA7
138    (*jvm).DestroyJavaVM();
139#else
140    (*jvm)->DestroyJavaVM(jvm);
141#endif
142    return -1;
143  }
144#ifdef DEBUG
145  else{
146    fprintf(stderr,"%s loaded\n",tmp->value);
147  }
148#endif
149
150  if (cls != NULL) {
151#ifdef JAVA7
152    (*env).ExceptionClear();
153    pmid=(*env).GetStaticMethodID(cls, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I");
154#else
155    (*env)->ExceptionClear(env);
156    pmid=(*env)->GetStaticMethodID(env,cls_gr, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I");
157#endif
158    if (pmid!=0){
159#ifdef DEBUG
160      fprintf(stderr,"Function successfully loaded\n");
161#endif
162      jclass scHashMapClass,scHashMap_class;
163      jmethodID scHashMap_constructor;
164#ifdef JAVA7
165      scHashMapClass = (*env).FindClass("java/util/HashMap");
166      scHashMap_class = (jclass)(*env).NewGlobalRef(scHashMapClass);
167      scHashMap_constructor = (*env).GetMethodID(scHashMap_class, "<init>", "()V");
168#else
169      scHashMapClass = (*env)->FindClass(env, "java/util/HashMap");
170      scHashMap_class = (*env)->NewGlobalRef(env, scHashMapClass);
171      scHashMap_constructor = (*env)->GetMethodID(env, scHashMap_class, "<init>", "()V");
172#endif
173      /**
174       * The 3 standard parameter for each services
175       */
176      jobject arg1=HashMap_FromMaps(env,m,scHashMapClass,scHashMap_class,scHashMap_constructor);
177      jobject arg2=HashMap_FromMaps(env,inputs,scHashMapClass,scHashMap_class,scHashMap_constructor);
178      jobject arg3=HashMap_FromMaps(env,outputs,scHashMapClass,scHashMap_class,scHashMap_constructor);
179      jint pValue=0;
180
181#ifdef JAVA7
182      pValue=(*env).CallStaticIntMethod(cls,pmid,arg1,arg2,arg3);
183#else
184      pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3);
185#endif
186      if (pValue != (jint)NULL){
187        res=pValue;
188        m=mapsFromHashMap(env,arg1,scHashMapClass);
189        *main_conf=m;
190        outputs=mapsFromHashMap(env,arg3,scHashMapClass);
191        *real_outputs=outputs;
192
193#ifdef DEBUG
194        fprintf(stderr,"Result of call: %i\n", pValue);
195        dumpMaps(inputs);
196        dumpMaps(outputs);
197#endif
198      }else{
199        displayStack(env,*main_conf);
200#ifdef JAVA7
201        (*jvm).DestroyJavaVM();
202#else
203        (*jvm)->DestroyJavaVM(jvm);
204#endif
205        return -1;
206      }
207    }
208    else{
209      displayStack(env,*main_conf);
210#ifdef JAVA7
211      (*jvm).DestroyJavaVM();
212#else
213      (*jvm)->DestroyJavaVM(jvm);
214#endif
215      return -1;
216    }
217  }
218#ifdef JAVA7
219      (*jvm).DestroyJavaVM();
220#else
221  (*jvm)->DestroyJavaVM(jvm);
222#endif
223  return res;
224}
225
226/**
227 * Error handling: display stack trace in an ExceptionReport Document
228 */
229void displayStack(JNIEnv *env,maps* main_conf){
230  map *tmpm=getMapFromMaps(main_conf,"main","tmpPath");
231  char tmps[1024];
232  sprintf(tmps,"%s/%d.ztmp",tmpm->value,getpid());
233  FILE* new_stdout=fopen(tmps,"wb+");
234  if(new_stdout==NULL){
235    map* err=createMap("text","Unable to run your service, no debug informations can be provided (please verify privileges on tmpPath)");
236    addToMap(err,"code","InternalError");
237    printExceptionReportResponse(main_conf,err);
238    freeMap(&err);
239    free(err);
240    return;
241  }
242  fflush(stderr);
243  dup2(fileno(new_stdout),fileno(stderr));
244  fprintf(stderr,"Unable to run your java process properly: ");
245  fflush(stderr);
246#ifdef JAVA7
247  (*env).ExceptionDescribe();
248#else
249  (*env)->ExceptionDescribe(env);
250#endif
251  fflush(new_stdout);
252  fseek(new_stdout, 0, SEEK_END);
253  long flen=ftell(new_stdout);
254  fseek(new_stdout, 0, SEEK_SET);
255  char *tmps1=(char*)malloc((flen+1)*sizeof(char));
256  fread(tmps1,flen,1,new_stdout);
257  fclose(new_stdout);
258  tmps1[flen]=0;
259  map* err=createMap("text",tmps1);
260  addToMap(err,"code","InternalError");
261  printExceptionReportResponse(main_conf,err);
262  freeMap(&err);
263  free(err);
264}
265
266jobject HashMap_FromMaps(JNIEnv *env,maps* t,jclass scHashMapClass,jclass scHashMap_class,jmethodID scHashMap_constructor){
267  jobject scObject,scObject1;
268  if(scHashMap_constructor!=NULL){
269#ifdef JAVA7
270    scObject = (*env).NewObject(scHashMap_class, scHashMap_constructor);
271#else
272    scObject = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
273#endif
274    jmethodID put_mid = 0;
275
276#ifdef JAVA7
277    put_mid = (*env).GetMethodID(scHashMapClass, "put",
278                                  "(Ljava/lang/Object;Ljava/lang/Object;)"
279                                  "Ljava/lang/Object;");
280#else
281    put_mid = (*env)->GetMethodID(env,scHashMapClass, "put",
282                                  "(Ljava/lang/Object;Ljava/lang/Object;)"
283                                  "Ljava/lang/Object;");
284#endif
285    maps* tmp=t;
286    while(tmp!=NULL){
287      map* tmap=getMapType(tmp->content);
288      map* tmp1=tmp->content;
289#ifdef JAVA7
290      scObject1 = (*env).NewObject(scHashMap_class, scHashMap_constructor);
291#else
292      scObject1 = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
293#endif
294      map* sizeV=getMap(tmp1,"size");
295      map* isArray=getMap(tmp1,"isArray");
296      map* alen=getMap(tmp1,"length");
297      while(tmp1!=NULL){
298        if(strcmp(tmp1->name,"value")==0){
299          if(isArray==NULL){
300            if(sizeV!=NULL && strcmp(tmp1->name,"value")==0){
301#ifdef JAVA7
302              jbyteArray tmpData=(*env).NewByteArray(atoi(sizeV->value));
303              (*env).SetByteArrayRegion(tmpData,0,atoi(sizeV->value),(const jbyte *)tmp1->value);
304              (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), tmpData);
305#else
306              jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sizeV->value));
307              (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sizeV->value),tmp1->value);
308              (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), tmpData);
309#endif
310            }else
311#ifdef JAVA7
312              (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value));
313#else
314              (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
315#endif
316          }
317          else{
318            int alen1=atoi(alen->value);
319            fprintf(stderr,"LENGTH %d \n",alen1);
320           
321            jclass scArrayListClass,scArrayList_class;
322            jmethodID scArrayList_constructor;
323            jobject scObject2;
324#ifdef JAVA7
325            scArrayListClass = (*env).FindClass("java/util/ArrayList");
326            scArrayList_class = (jclass)(*env).NewGlobalRef(scArrayListClass);
327            scArrayList_constructor = (*env).GetMethodID(scArrayList_class, "<init>", "()V");
328            jmethodID add_mid = 0;
329            scObject2 = (*env).NewObject(scArrayList_class, scArrayList_constructor);
330
331            add_mid = (*env).GetMethodID(scArrayListClass,
332                                          "add","(Ljava/lang/Object;)Z");
333#else
334            scArrayListClass = (*env)->FindClass(env, "java/util/ArrayList");
335            scArrayList_class = (*env)->NewGlobalRef(env, scArrayListClass);
336            scArrayList_constructor = (*env)->GetMethodID(env, scArrayList_class, "<init>", "()V");
337            jmethodID add_mid = 0;
338            scObject2 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor);
339
340            add_mid = (*env)->GetMethodID(env,scArrayListClass,
341                                          "add","(Ljava/lang/Object;)Z");
342#endif     
343            int i;
344           
345            for(i=0;i<alen1;i++){
346              map* vMap=getMapArray(tmp->content,"value",i);       
347              map* sMap=getMapArray(tmp->content,"size",i);
348              map* mMap=getMapArray(tmp->content,tmap->value,i);
349             
350              if(sMap!=NULL && vMap!=NULL && strncmp(vMap->name,"value",5)==0){
351#ifdef JAVA7
352                jbyteArray tmpData=(*env).NewByteArray(atoi(sMap->value));
353                (*env).SetByteArrayRegion(tmpData,0,atoi(sMap->value),(const jbyte *)vMap->value);
354                (*env).CallObjectMethod(scObject2, add_mid, tmpData);
355#else
356                jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sMap->value));
357                (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sMap->value),vMap->value);
358                (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData);
359#endif
360              }else{
361#ifdef JAVA7
362                jobject tmpData=(*env).NewStringUTF(vMap->value);
363                (*env).CallObjectMethod(scObject2, add_mid, tmpData);
364#else
365                jobject tmpData=(*env)->NewStringUTF(env,vMap->value);
366                (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData);
367#endif
368              }
369             
370            }
371
372#ifdef JAVA7
373            (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), scObject2);
374#else       
375            (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), scObject2);
376#endif
377
378          }
379        }
380        else
381#ifdef JAVA7
382          (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value));
383#else
384          (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
385#endif
386        tmp1=tmp1->next;
387      }
388#ifdef JAVA7
389      (*env).CallObjectMethod(scObject, put_mid, (*env).NewStringUTF(tmp->name), scObject1);
390#else
391      (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1);
392#endif
393      tmp=tmp->next;
394    }
395    return scObject;
396  }
397  else
398    return NULL;
399}
400
401maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){
402#ifdef DEBUG
403  fprintf(stderr,"mapsFromHashMap start\n");
404#endif
405  /**
406   * What need to be done (in java).
407   * Set set = hm.entrySet();
408   * Iterator i = set.iterator();
409   * while(i.hasNext()){
410   *   Map.Entry me = (Map.Entry)i.next();
411   *   System.out.println(me.getKey() + " : " + me.getValue() );
412   * }
413   */
414  jclass scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass;
415  jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid;
416  jobject scObject,scObject1;
417  if(scHashMapClass==NULL){
418#ifdef DEBUG
419    fprintf(stderr,"Unable to load java.util.HashMap\n");
420#endif
421    return NULL;
422  }
423#ifdef JAVA7
424  entrySet_mid = (*env).GetMethodID(scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
425  containsKey_mid = (*env).GetMethodID(scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z");
426  get_mid = (*env).GetMethodID(scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); 
427#else
428  entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
429  containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z");
430  get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); 
431#endif
432
433  if(containsKey_mid==0){
434#ifdef DEBUG
435    fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid);
436#endif
437    return NULL;
438  }
439  if(get_mid==0){
440#ifdef DEBUG
441    fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid);
442#endif
443    return NULL;
444  }
445  if(entrySet_mid==0){
446#ifdef DEBUG
447    fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid);
448#endif
449    return NULL;
450  }
451#ifdef DEBUG
452  else
453    fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid);
454#endif
455
456#ifdef JAVA7
457  scSetClass = (*env).FindClass("java/util/Set");
458  iterator_mid = (*env).GetMethodID(scSetClass, "iterator", "()Ljava/util/Iterator;"); 
459#else
460  scSetClass = (*env)->FindClass(env, "java/util/Set");
461  iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); 
462#endif
463#ifdef DEBUG
464  fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid);
465#endif
466
467#ifdef JAVA7
468  scIteratorClass = (*env).FindClass("java/util/Iterator");
469  hasNext_mid = (*env).GetMethodID(scIteratorClass, "hasNext", "()Z");
470#else
471  scIteratorClass = (*env)->FindClass(env, "java/util/Iterator");
472  hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z");
473#endif
474
475#ifdef DEBUG
476  fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid);
477#endif
478
479#ifdef JAVA7
480  next_mid = (*env).GetMethodID(scIteratorClass, "next", "()Ljava/lang/Object;");
481#else
482  next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;");
483#endif
484#ifdef DEBUG
485  fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid);
486#endif
487
488#ifdef JAVA7
489  scMapEntryClass = (*env).FindClass("java/util/Map$Entry");
490  getKey_mid = (*env).GetMethodID(scMapEntryClass, "getKey", "()Ljava/lang/Object;");
491#else
492  scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry");
493  getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;");
494#endif
495#ifdef DEBUG
496  fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid);
497#endif
498#ifdef JAVA7
499  getValue_mid = (*env).GetMethodID(scMapEntryClass, "getValue", "()Ljava/lang/Object;");
500#else
501  getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;");
502#endif
503#ifdef DEBUG
504  fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid);
505#endif
506
507#ifdef JAVA7
508  jobject final_set=(*env).CallObjectMethod(t,entrySet_mid);
509  jobject final_iterator=(*env).CallObjectMethod(final_set,iterator_mid);
510#else
511  jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid);
512  jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid);
513#endif
514
515  maps* final_res=NULL;
516  map* res=NULL;
517#ifdef JAVA7
518  while((*env).CallBooleanMethod(final_iterator,hasNext_mid)){
519    jobject tmp=(*env).CallObjectMethod(final_iterator,next_mid);
520
521    jobject imap=(*env).CallObjectMethod(tmp,getValue_mid);
522    jobject set=(*env).CallObjectMethod(imap,entrySet_mid);
523    jobject iterator=(*env).CallObjectMethod(set,iterator_mid);
524
525#else
526  while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){
527    jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid);
528
529    jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid);
530    jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid);
531    jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid);
532
533#endif
534    int size=-1;
535#ifdef JAVA7
536    if((*env).CallBooleanMethod(imap,containsKey_mid,(*env).NewStringUTF("size"))){
537      jobject sizeV=(*env).CallObjectMethod(imap, get_mid,(*env).NewStringUTF("size"));
538      const char* sizeVS=(*env).GetStringUTFChars((jstring)sizeV, NULL);
539#else
540    if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){
541      jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size"));
542      const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL);
543#endif
544      size=atoi(sizeVS);
545      fprintf(stderr,"SIZE : %s\n",sizeVS);
546#ifdef JAVA7
547      (*env).ReleaseStringUTFChars((jstring)sizeV, sizeVS);
548#else
549      (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS);
550#endif
551    }
552   
553#ifdef JAVA7
554    while((*env).CallBooleanMethod(iterator,hasNext_mid)){
555      jobject tmp1=(*env).CallObjectMethod(iterator,next_mid);
556      jobject jk=(*env).CallObjectMethod(tmp1,getKey_mid);
557      jobject jv=(*env).CallObjectMethod(tmp1,getValue_mid);
558
559      const char* jkd=(*env).GetStringUTFChars((jstring)jk, NULL);
560#else
561    while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){
562      jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid);
563      jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid);
564      jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid);
565
566      const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL);
567#endif
568      if(size>=0 && strcmp(jkd,"value")==0){
569#ifdef JAVA7
570        jobject value=(jobject)(*env).GetByteArrayElements((jbyteArray)jv, NULL);
571#else
572        jobject value=(*env)->GetByteArrayElements(env, jv, NULL);
573#endif
574        if(res==NULL){
575          res=createMap(jkd,"");
576        }else{
577          addToMap(res,jkd,"");
578        }
579        map* tmpR=getMap(res,"value");
580        free(tmpR->value);
581        tmpR->value=(char*)malloc((size+1)*sizeof(char));
582        memmove(tmpR->value,value,size*sizeof(char));
583        tmpR->value[size]=0;
584        char tmp[128];
585        sprintf(tmp,"%d",size);
586        addToMap(res,"size",tmp);
587      }
588      else{
589#ifdef JAVA7
590        const char* jvd=(*env).GetStringUTFChars((jstring)jv, NULL);
591#else
592        const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL);
593#endif
594        if(res==NULL){
595          res=createMap(jkd,jvd);
596        }else{
597          addToMap(res,jkd,jvd);
598        }
599#ifdef JAVA7
600        (*env).ReleaseStringUTFChars((jstring)jv, jvd);
601#else
602        (*env)->ReleaseStringUTFChars(env, jv, jvd);
603#endif
604      }
605
606#ifdef JAVA7
607      (*env).ReleaseStringUTFChars((jstring)jk, jkd);
608#else
609      (*env)->ReleaseStringUTFChars(env, jk, jkd);
610#endif
611
612    }
613#ifdef JAVA7
614    jobject jk=(*env).CallObjectMethod(tmp,getKey_mid);
615#else
616    jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid);
617#endif
618    maps* cmap=(maps*)malloc(sizeof(maps));
619#ifdef JAVA7
620    cmap->name=(char*)(*env).GetStringUTFChars((jstring)jk, NULL);
621#else
622    cmap->name=(*env)->GetStringUTFChars(env, jk, NULL);
623#endif
624#ifdef DEBUG
625    fprintf(stderr," / %s \n",cmap->name);
626#endif
627    cmap->content=res;
628    cmap->next=NULL;
629    if(final_res==NULL)
630      final_res=dupMaps(&cmap);
631    else
632      addMapsToMaps(&final_res,cmap);
633    freeMaps(&cmap);
634    free(cmap);
635    cmap=NULL;
636    res=NULL;
637  }
638#ifdef DEBUG
639  fprintf(stderr,"mapsFromHashMap end\n");
640#endif
641
642  return final_res;
643}
644
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