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

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

Add support for wildcard character in main.cfg file.

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