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

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

Use dynamically allocated memory for Java CLASSPATH

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