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

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

Special thanks to Yoshida-san for pointing us this out. Remove the deactivation of the JIT (Just In Time) compiler from the ZOO Java Support. Solve issue when using heavy Java Services like GoGPS is.

File size: 10.2 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2010 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 != 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      while(tmp1!=NULL){
200        (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
201        tmp1=tmp1->next;
202      }
203      (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1);
204      tmp=tmp->next;
205    }
206    return scObject;
207  }
208  else
209    return NULL;
210}
211
212maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){
213#ifdef DEBUG
214  fprintf(stderr,"mapsFromHashMap start\n");
215#endif
216  /**
217   * What need to be done (in java).
218   * Set set = hm.entrySet();
219   * Iterator i = set.iterator();
220   * while(i.hasNext()){
221   *   Map.Entry me = (Map.Entry)i.next();
222   *   System.out.println(me.getKey() + " : " + me.getValue() );
223   * }
224   */
225  jclass scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass;
226  jmethodID entrySet_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid;
227  jobject scObject,scObject1;
228  if(scHashMapClass==NULL){
229    fprintf(stderr,"Unable to load java.util.HashMap\n");
230    return NULL;
231  }
232  entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
233  if(entrySet_mid==0){
234    fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid);
235    return NULL;
236  }
237#ifdef DEBUG
238  else
239    fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid);
240#endif
241
242  scSetClass = (*env)->FindClass(env, "java/util/Set");
243  iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); 
244#ifdef DEBUG
245  fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid);
246#endif
247
248  scIteratorClass = (*env)->FindClass(env, "java/util/Iterator");
249  hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z");
250#ifdef DEBUG
251  fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid);
252#endif
253  next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;");
254#ifdef DEBUG
255  fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid);
256#endif
257
258  scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry");
259  getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;");
260#ifdef DEBUG
261  fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid);
262#endif
263  getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;");
264#ifdef DEBUG
265  fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid);
266#endif
267
268  jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid);
269  jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid);
270
271
272  maps* final_res=NULL;
273  map* res=NULL;
274  while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){
275    jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid);
276
277    jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid);
278    jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid);
279    jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid);
280
281    while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){
282      jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid);
283      jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid);
284      jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid);
285
286      jstring jkd=(*env)->GetStringUTFChars(env, jk, NULL);
287      jstring jvd=(*env)->GetStringUTFChars(env, jv, NULL);
288
289#ifdef DEBUG
290      fprintf(stderr,"%s %s\n",jkd,jvd);
291#endif
292
293      if(res==NULL){
294        res=createMap(jkd,jvd);
295      }else{
296        addToMap(res,jkd,jvd);
297      }
298      (*env)->ReleaseStringChars(env, jk, jkd);
299      (*env)->ReleaseStringChars(env, jv, jvd);
300
301    }
302    jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid);
303    maps* cmap=(maps*)malloc(sizeof(maps));
304    cmap->name=(*env)->GetStringUTFChars(env, jk, NULL);
305#ifdef DEBUG
306    fprintf(stderr," / %s \n",cmap->name);
307#endif
308    cmap->content=res;
309    cmap->next=NULL;
310    if(final_res==NULL)
311      final_res=dupMaps(&cmap);
312    else
313      addMapsToMaps(&final_res,cmap);
314    freeMaps(&cmap);
315    free(cmap);
316    cmap=NULL;
317    res=NULL;
318  }
319#ifdef DEBUG
320  fprintf(stderr,"mapsFromHashMap end\n");
321#endif
322
323  return final_res;
324}
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