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

Last change on this file since 353 was 353, checked in by djay, 9 years ago

Fix previous commit

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 12.1 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2011 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[2048];
35  char oclasspath[2068];
36  int res=SERVICE_FAILED;
37  char *cclasspath=getenv("CLASSPATH");
38  if(tmp!=NULL){
39    if(cclasspath!=NULL)
40      sprintf(classpath,"%s/%s/:%s",ntmp,tmp->value,cclasspath);
41    else
42      sprintf(classpath,"%s/%s/",ntmp,tmp->value);
43    sprintf(oclasspath,"-Djava.class.path=%s",classpath);
44  }
45  else{
46    if(cclasspath!=NULL)
47      sprintf(classpath,"%s:%s",ntmp,cclasspath);
48    else
49      sprintf(classpath,"%s",ntmp);
50    sprintf(oclasspath,"-Djava.class.path=%s",classpath);
51  }
52#ifdef DEBUG
53  fprintf(stderr,"CLASSPATH=%s\n",classpath);
54#endif
55  setenv("CLASSPATH",classpath,1);
56
57  JavaVMOption options[1];
58  JavaVMInitArgs vm_args;
59  JavaVM *jvm;
60  JNIEnv *env;
61  long result;
62  jmethodID pmid;
63  jfieldID fid;
64  jobject jobj;
65  jclass cls,cls_gr;
66  int i;
67
68  options[0].optionString = strdup(oclasspath);
69
70  JNI_GetDefaultJavaVMInitArgs(&vm_args);
71  vm_args.version = JNI_VERSION_1_6;
72  vm_args.options = options;
73  vm_args.nOptions = 1;
74  vm_args.ignoreUnrecognized = JNI_FALSE;
75
76  result = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args);
77  if(result == JNI_ERR ) {
78    fprintf(stderr,"Error invoking the JVM");
79    return -1;
80  }
81#ifdef DEBUG
82  else 
83    fprintf(stderr,"JAVA VM Started\n");
84#endif
85
86  tmp=getMap(s->content,"serviceProvider");
87  cls = (*env)->FindClass(env,tmp->value);
88  cls_gr = (*env)->NewGlobalRef(env, cls);
89  if( cls == NULL ) {
90    char pbt[10240];
91    sprintf(pbt,"can't find class %s\n",tmp->value);
92    map* err=createMap("text",pbt);
93    addToMap(err,"code","NoApplicableCode");
94    printExceptionReportResponse(m,err);
95    freeMap(&err);
96    free(err);
97    (*jvm)->DestroyJavaVM(jvm);
98    return -1;
99  }
100#ifdef DEBUG
101  else{
102    fprintf(stderr,"%s loaded\n",tmp->value);
103  }
104#endif
105
106  if (cls != NULL) {
107    (*env)->ExceptionClear(env);
108    pmid=(*env)->GetStaticMethodID(env,cls_gr, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I");
109    if (pmid!=0){
110#ifdef DEBUG
111      fprintf(stderr,"Function successfully loaded\n");
112#endif
113      jclass scHashMapClass,scHashMap_class;
114      jmethodID scHashMap_constructor;
115      scHashMapClass = (*env)->FindClass(env, "java/util/HashMap");
116      scHashMap_class = (*env)->NewGlobalRef(env, scHashMapClass);
117      scHashMap_constructor = (*env)->GetMethodID(env, scHashMap_class, "<init>", "()V");
118      /**
119       * The 3 standard parameter for each services
120       */
121      jobject arg1=HashMap_FromMaps(env,m,scHashMapClass,scHashMap_class,scHashMap_constructor);
122      jobject arg2=HashMap_FromMaps(env,inputs,scHashMapClass,scHashMap_class,scHashMap_constructor);
123      jobject arg3=HashMap_FromMaps(env,outputs,scHashMapClass,scHashMap_class,scHashMap_constructor);
124      jint pValue=0;
125
126      pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3);
127      if (pValue != (jint)NULL){
128        res=pValue;
129        m=mapsFromHashMap(env,arg1,scHashMapClass);
130        *main_conf=m;
131        outputs=mapsFromHashMap(env,arg3,scHashMapClass);
132        *real_outputs=outputs;
133
134#ifdef DEBUG
135        fprintf(stderr,"Result of call: %i\n", pValue);
136        dumpMaps(inputs);
137        dumpMaps(outputs);
138#endif
139      }else{     
140        /**
141         * Error handling displayig stack trace in ExceptionReport
142         */
143        map *tmpm=getMapFromMaps(*main_conf,"main","tmpPath");
144        char tmps[1024];
145        sprintf(tmps,"%s/%d.ztmp",tmpm->value,getpid());
146        int old_stdout=dup(fileno(stdout));
147        FILE* new_stdout=fopen(tmps,"w+");
148        dup2(fileno(new_stdout),fileno(stdout));
149        (*env)->ExceptionDescribe(env);
150        fflush(stdout);
151        dup2(old_stdout,fileno(stdout));
152        fseek(new_stdout, 0, SEEK_END);
153        long flen=ftell(new_stdout);
154        rewind(new_stdout);
155        char tmps1[flen];
156        fread(tmps1,flen,1,new_stdout);
157        fclose(new_stdout);
158        char pbt[100+flen];
159        sprintf(pbt,"Unable to run your java process properly. Server returns : %s",tmps1);
160        map* err=createMap("text",pbt);
161        addToMap(err,"code","NoApplicableCode");
162        printExceptionReportResponse(m,err);
163        freeMap(&err);
164        free(err);
165        (*jvm)->DestroyJavaVM(jvm);
166        return -1;
167      }
168    }
169    else{
170      char tmpS[1024];
171      sprintf(tmpS, "Cannot find function %s \n", s->name);
172      map* err=createMap("text",tmpS);
173      printExceptionReportResponse(m,err);
174      freeMap(&err);
175      free(err);
176      (*jvm)->DestroyJavaVM(jvm);
177      return -1;
178    }
179  }else{
180    char tmpS[1024];
181    sprintf(tmpS, "Cannot find function %s \n", tmp->value);
182    map* err=createMap("text",tmpS);
183    printExceptionReportResponse(m,err);
184    freeMap(&err);
185    free(err);
186    (*jvm)->DestroyJavaVM(jvm);
187    return -1;
188  }
189  (*jvm)->DestroyJavaVM(jvm);
190  return res;
191}
192
193jobject HashMap_FromMaps(JNIEnv *env,maps* t,jclass scHashMapClass,jclass scHashMap_class,jmethodID scHashMap_constructor){
194  jobject scObject,scObject1;
195  if(scHashMap_constructor!=NULL){
196    scObject = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
197    jmethodID put_mid = 0;
198
199    put_mid = (*env)->GetMethodID(env,scHashMapClass, "put",
200                                  "(Ljava/lang/Object;Ljava/lang/Object;)"
201                                  "Ljava/lang/Object;");
202    maps* tmp=t;
203    while(tmp!=NULL){
204      map* tmp1=tmp->content;
205      scObject1 = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
206      map* sizeV=getMap(tmp1,"size");
207      while(tmp1!=NULL){
208        if(sizeV!=NULL && strcmp(tmp1->name,"value")==0){
209          jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sizeV->value));
210          (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sizeV->value),tmp1->value);
211          (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), tmpData);
212        }else
213          (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
214        tmp1=tmp1->next;
215      }
216      (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1);
217      tmp=tmp->next;
218    }
219    return scObject;
220  }
221  else
222    return NULL;
223}
224
225maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){
226#ifdef DEBUG
227  fprintf(stderr,"mapsFromHashMap start\n");
228#endif
229  /**
230   * What need to be done (in java).
231   * Set set = hm.entrySet();
232   * Iterator i = set.iterator();
233   * while(i.hasNext()){
234   *   Map.Entry me = (Map.Entry)i.next();
235   *   System.out.println(me.getKey() + " : " + me.getValue() );
236   * }
237   */
238  jclass scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass;
239  jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid;
240  jobject scObject,scObject1;
241  if(scHashMapClass==NULL){
242#ifdef DEBUG
243    fprintf(stderr,"Unable to load java.util.HashMap\n");
244#endif
245    return NULL;
246  }
247  entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
248  containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z");
249  get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); 
250
251  if(containsKey_mid==0){
252#ifdef DEBUG
253    fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid);
254#endif
255    return NULL;
256  }
257  if(get_mid==0){
258#ifdef DEBUG
259    fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid);
260#endif
261    return NULL;
262  }
263  if(entrySet_mid==0){
264#ifdef DEBUG
265    fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid);
266#endif
267    return NULL;
268  }
269#ifdef DEBUG
270  else
271    fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid);
272#endif
273
274  scSetClass = (*env)->FindClass(env, "java/util/Set");
275  iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); 
276#ifdef DEBUG
277  fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid);
278#endif
279
280  scIteratorClass = (*env)->FindClass(env, "java/util/Iterator");
281  hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z");
282#ifdef DEBUG
283  fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid);
284#endif
285  next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;");
286#ifdef DEBUG
287  fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid);
288#endif
289
290  scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry");
291  getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;");
292#ifdef DEBUG
293  fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid);
294#endif
295  getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;");
296#ifdef DEBUG
297  fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid);
298#endif
299
300  jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid);
301  jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid);
302
303
304  maps* final_res=NULL;
305  map* res=NULL;
306  while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){
307    jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid);
308
309    jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid);
310    jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid);
311    jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid);
312
313    int size=-1;
314    if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){
315      jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size"));
316      const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL);
317      size=atoi(sizeVS);
318      fprintf(stderr,"SIZE : %s\n",sizeVS);
319      (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS);
320    }
321   
322    while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){
323      jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid);
324      jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid);
325      jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid);
326
327      const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL);
328      if(size>=0 && strcmp(jkd,"value")==0){
329        jobject value=(*env)->GetByteArrayElements(env, jv, NULL);
330        if(res==NULL){
331          res=createMap(jkd,"");
332        }else{
333          addToMap(res,jkd,"");
334        }
335        map* tmpR=getMap(res,"value");
336        free(tmpR->value);
337        tmpR->value=(char*)malloc((size+1)*sizeof(char));
338        memmove(tmpR->value,value,size*sizeof(char));
339        tmpR->value[size]=0;
340        char tmp[128];
341        sprintf(tmp,"%d",size);
342        addToMap(res,"size",tmp);
343      }
344      else{
345        const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL);
346        if(res==NULL){
347          res=createMap(jkd,jvd);
348        }else{
349          addToMap(res,jkd,jvd);
350        }
351        (*env)->ReleaseStringUTFChars(env, jv, jvd);
352      }
353
354      (*env)->ReleaseStringUTFChars(env, jk, jkd);
355
356    }
357    jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid);
358    maps* cmap=(maps*)malloc(sizeof(maps));
359    cmap->name=(*env)->GetStringUTFChars(env, jk, NULL);
360#ifdef DEBUG
361    fprintf(stderr," / %s \n",cmap->name);
362#endif
363    cmap->content=res;
364    cmap->next=NULL;
365    if(final_res==NULL)
366      final_res=dupMaps(&cmap);
367    else
368      addMapsToMaps(&final_res,cmap);
369    freeMaps(&cmap);
370    free(cmap);
371    cmap=NULL;
372    res=NULL;
373  }
374#ifdef DEBUG
375  fprintf(stderr,"mapsFromHashMap end\n");
376#endif
377
378  return final_res;
379}
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