source: trunk/zoo-kernel/service_internal_java.c @ 114

Last change on this file since 114 was 114, checked in by djay, 14 years ago

Code cleanup to avoid most of the warning messages at compilation time.

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