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

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

JAVA Supprot : binary files support for data in both inputs and outputs.

File size: 11.8 KB
RevLine 
[1]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
[62]50  JavaVMOption options[1];
[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
[59]63  vm_args.version = JNI_VERSION_1_2;
64  JNI_GetDefaultJavaVMInitArgs(&vm_args);
[1]65  vm_args.options = options;
[62]66  vm_args.nOptions = 1;
[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);
[9]88    freeMap(&err);
89    free(err);
[1]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
[57]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");
[1]111      /**
112       * The 3 standard parameter for each services
113       */
[57]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);
[1]117      jint pValue=0;
118
119      pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3);
120      if (pValue != NULL){
121        res=pValue;
[57]122        m=mapsFromHashMap(env,arg1,scHashMapClass);
123        *main_conf=m;
124        outputs=mapsFromHashMap(env,arg3,scHashMapClass);
[1]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);
[9]156        freeMap(&err);
157        free(err);
[1]158        (*jvm)->DestroyJavaVM(jvm);
[9]159        return -1;
[1]160      }
161    }
162    else{
163      char tmpS[1024];
164      sprintf(tmpS, "Cannot find function %s \n", s->name);
[9]165      map* err=createMap("text",tmpS);
166      printExceptionReportResponse(m,err);
167      freeMap(&err);
168      free(err);
[1]169      (*jvm)->DestroyJavaVM(jvm);
[9]170      return -1;
[1]171    }
172  }else{
173    char tmpS[1024];
174    sprintf(tmpS, "Cannot find function %s \n", tmp->value);
[9]175    map* err=createMap("text",tmpS);
176    printExceptionReportResponse(m,err);
177    freeMap(&err);
178    free(err);
179    (*jvm)->DestroyJavaVM(jvm);
180    return -1;
[1]181  }
182  (*jvm)->DestroyJavaVM(jvm);
183  return res;
184}
185
[57]186jobject HashMap_FromMaps(JNIEnv *env,maps* t,jclass scHashMapClass,jclass scHashMap_class,jmethodID scHashMap_constructor){
[1]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);
[68]199      map* sizeV=getMap(tmp1,"size");
[1]200      while(tmp1!=NULL){
[68]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));
[1]207        tmp1=tmp1->next;
208      }
209      (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1);
210      tmp=tmp->next;
[57]211    }
[1]212    return scObject;
213  }
214  else
215    return NULL;
216}
217
[57]218maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){
[1]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   */
[57]231  jclass scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass;
[68]232  jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid;
[1]233  jobject scObject,scObject1;
234  if(scHashMapClass==NULL){
235    fprintf(stderr,"Unable to load java.util.HashMap\n");
236    return NULL;
237  }
238  entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
[68]239  containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z");
240  get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); 
241  if(containsKey_mid==0){
242    fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid);
243  }
244  if(get_mid==0){
245    fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid);
246  }
[1]247  if(entrySet_mid==0){
248    fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid);
249    return NULL;
250  }
251#ifdef DEBUG
252  else
253    fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid);
254#endif
255
256  scSetClass = (*env)->FindClass(env, "java/util/Set");
257  iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); 
258#ifdef DEBUG
259  fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid);
260#endif
261
262  scIteratorClass = (*env)->FindClass(env, "java/util/Iterator");
263  hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z");
264#ifdef DEBUG
265  fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid);
266#endif
267  next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;");
268#ifdef DEBUG
269  fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid);
270#endif
271
272  scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry");
273  getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;");
274#ifdef DEBUG
275  fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid);
276#endif
277  getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;");
278#ifdef DEBUG
279  fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid);
280#endif
281
282  jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid);
283  jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid);
284
285
286  maps* final_res=NULL;
287  map* res=NULL;
288  while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){
289    jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid);
290
291    jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid);
292    jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid);
293    jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid);
[57]294
[68]295    int size=-1;
296    if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){
297      jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size"));
298      jstring sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL);
299      size=atoi(sizeVS);
300      fprintf(stderr,"SIZE : %s\n",sizeVS);
301    }
302   
[1]303    while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){
304      jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid);
305      jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid);
306      jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid);
307
308      jstring jkd=(*env)->GetStringUTFChars(env, jk, NULL);
[68]309      if(size>=0 && strcmp(jkd,"value")==0){
310        fprintf(stderr,"%s\n",jkd);
311        jobject value=(*env)->GetByteArrayElements(env, jv, NULL);
312        if(res==NULL){
313          res=createMap(jkd,"");
314        }else{
315          addToMap(res,jkd,"");
316        }
317        fprintf(stderr,"/%s\n",jkd);
318        map* tmpR=getMap(res,"value");
319        free(tmpR->value);
320        tmpR->value=(char*)malloc((size+1)*sizeof(char));
321        memmove(tmpR->value,value,size*sizeof(char));
322        tmpR->value[size]=0;
323        fprintf(stderr,"/%s\n",jkd);
324      }
325      else{
326        jstring jvd=(*env)->GetStringUTFChars(env, jv, NULL);
327        if(res==NULL){
328          res=createMap(jkd,jvd);
329        }else{
330          addToMap(res,jkd,jvd);
331        }
332        (*env)->ReleaseStringChars(env, jv, jvd);
333      }
[59]334
335#ifdef DEBUG
[1]336      fprintf(stderr,"%s %s\n",jkd,jvd);
337#endif
338
[59]339      (*env)->ReleaseStringChars(env, jk, jkd);
340
[1]341    }
342    jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid);
343    maps* cmap=(maps*)malloc(sizeof(maps));
344    cmap->name=(*env)->GetStringUTFChars(env, jk, NULL);
[57]345#ifdef DEBUG
346    fprintf(stderr," / %s \n",cmap->name);
347#endif
[1]348    cmap->content=res;
349    cmap->next=NULL;
[57]350    if(final_res==NULL)
[9]351      final_res=dupMaps(&cmap);
[57]352    else
[9]353      addMapsToMaps(&final_res,cmap);
354    freeMaps(&cmap);
355    free(cmap);
356    cmap=NULL;
[1]357    res=NULL;
358  }
359#ifdef DEBUG
360  fprintf(stderr,"mapsFromHashMap end\n");
361#endif
362
363  return final_res;
364}
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