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

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

Update of both ZOO Kernel and ZOO Services (ogr base-vect-ops ServicesProvider?).
All the ZCFG files have been corrected to remove all references to wrong metadata (Test = Demo) to avoid validation issues.
Main Memory leaks has been removed from this version.
Addition of the Simplify Service in the C ogr base-vect-ops ServicesProvider? and addition of the Python version (without Simplify).
Update of the configure.ac and Makefile.in to follow dicussions on the mailing list and ensure to use our cgic206 and not another one, path to our cgic library is now directly in the Makefile.in file.
Accept the "-" character to name inputs, to solve issue on GRASS 7 integration.
Addition of the extension keyword for ZCFG file to be able to store resulting outputs in a file name using the extension suffix.
This version after a testing period shall be considerate as 1.0.1 version of the ZOO Project.

File size: 10.5 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
50  JavaVMOption options[2];
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  options[1].optionString = "-Djava.compiler=NONE";
63
64  vm_args.version = JNI_VERSION_1_4;
65  vm_args.options = options;
66  vm_args.nOptions = 2;
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
106      /**
107       * The 3 standard parameter for each services
108       */
109      jobject arg1=HashMap_FromMaps(env,m);
110      jobject arg2=HashMap_FromMaps(env,inputs);
111      jobject arg3=HashMap_FromMaps(env,outputs);
112      jint pValue=0;
113
114      pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3);
115      if (pValue != NULL){
116        res=pValue;
117        //inputs=mapsFromHashMap(env,arg2);
118        outputs=mapsFromHashMap(env,arg3);
119        *real_outputs=outputs;
120
121#ifdef DEBUG
122        fprintf(stderr,"Result of call: %i\n", pValue);
123        dumpMaps(inputs);
124        dumpMaps(outputs);
125        /*fprintf(stderr,"printProcessResponse(%i,\"%s\",%i,inputs,outputs);",
126          getpid(),tmp->value,PyInt_AsLong(pValue));*/
127#endif
128      }else{     
129        /**
130         * Error handling displayig stack trace in ExceptionReport
131         */
132        map *tmpm=getMapFromMaps(*main_conf,"main","tmpPath");
133        char tmps[1024];
134        sprintf(tmps,"%s/%d.ztmp",tmpm->value,getpid());
135        int old_stdout=dup(fileno(stdout));
136        FILE* new_stdout=fopen(tmps,"w+");
137        dup2(fileno(new_stdout),fileno(stdout));
138        (*env)->ExceptionDescribe(env);
139        fflush(stdout);
140        dup2(old_stdout,fileno(stdout));
141        fseek(new_stdout, 0, SEEK_END);
142        long flen=ftell(new_stdout);
143        rewind(new_stdout);
144        char tmps1[flen];
145        fread(tmps1,flen,1,new_stdout);
146        fclose(new_stdout);
147        char pbt[100+flen];
148        sprintf(pbt,"Unable to run your java process properly. Server returns : %s",tmps1);
149        map* err=createMap("text",pbt);
150        addToMap(err,"code","NoApplicableCode");
151        printExceptionReportResponse(m,err);
[9]152        freeMap(&err);
153        free(err);
[1]154        (*jvm)->DestroyJavaVM(jvm);
[9]155        return -1;
[1]156      }
157    }
158    else{
159      char tmpS[1024];
160      sprintf(tmpS, "Cannot find function %s \n", s->name);
[9]161      map* err=createMap("text",tmpS);
162      printExceptionReportResponse(m,err);
163      freeMap(&err);
164      free(err);
[1]165      (*jvm)->DestroyJavaVM(jvm);
[9]166      return -1;
[1]167    }
168  }else{
169    char tmpS[1024];
170    sprintf(tmpS, "Cannot find function %s \n", tmp->value);
[9]171    map* err=createMap("text",tmpS);
172    printExceptionReportResponse(m,err);
173    freeMap(&err);
174    free(err);
175    (*jvm)->DestroyJavaVM(jvm);
176    return -1;
[1]177  }
178  (*jvm)->DestroyJavaVM(jvm);
179  return res;
180}
181
182jobject HashMap_FromMaps(JNIEnv *env,maps* t){
183  jclass scHashMapClass,scHashMap_class;
184  jmethodID scHashMap_constructor;
185  jobject scObject,scObject1;
186  scHashMapClass = (*env)->FindClass(env, "java/util/HashMap");
187  scHashMap_class = (*env)->NewGlobalRef(env, scHashMapClass);
188  scHashMap_constructor = (*env)->GetMethodID(env, scHashMap_class, "<init>", "()V");   
189  if(scHashMap_constructor!=NULL){
190    scObject = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
191    jmethodID put_mid = 0;
192
193    put_mid = (*env)->GetMethodID(env,scHashMapClass, "put",
194                                  "(Ljava/lang/Object;Ljava/lang/Object;)"
195                                  "Ljava/lang/Object;");
196    maps* tmp=t;
197    while(tmp!=NULL){
198      map* tmp1=tmp->content;
199      scObject1 = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor);
200      while(tmp1!=NULL){
201        (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value));
202        tmp1=tmp1->next;
203      }
204      (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1);
205      tmp=tmp->next;
206    } 
207    return scObject;
208  }
209  else
210    return NULL;
211}
212
213maps* mapsFromHashMap(JNIEnv *env,jobject t){
214#ifdef DEBUG
215  fprintf(stderr,"mapsFromHashMap start\n");
216#endif
217  /**
218   * What need to be done (in java).
219   * Set set = hm.entrySet();
220   * Iterator i = set.iterator();
221   * while(i.hasNext()){
222   *   Map.Entry me = (Map.Entry)i.next();
223   *   System.out.println(me.getKey() + " : " + me.getValue() );
224   * }
225   */
226  jclass scHashMapClass,scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass;
227  jmethodID entrySet_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid;
228  jobject scObject,scObject1;
229  scHashMapClass=(*env)->GetObjectClass(env,t);
230  //scMapClass=(*env)->FindClass(env, "java/util/HashMap");
231  //scHashMapClass = (*env)->FindClass(env, "java/util/HashMap");
232  if(scHashMapClass==NULL){
233    fprintf(stderr,"Unable to load java.util.HashMap\n");
234    return NULL;
235  }
236  entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); 
237  if(entrySet_mid==0){
238    fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid);
239    return NULL;
240  }
241#ifdef DEBUG
242  else
243    fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid);
244#endif
245
246  scSetClass = (*env)->FindClass(env, "java/util/Set");
247  iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); 
248#ifdef DEBUG
249  fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid);
250#endif
251
252  scIteratorClass = (*env)->FindClass(env, "java/util/Iterator");
253  hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z");
254#ifdef DEBUG
255  fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid);
256#endif
257  next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;");
258#ifdef DEBUG
259  fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid);
260#endif
261
262  scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry");
263  getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;");
264#ifdef DEBUG
265  fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid);
266#endif
267  getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;");
268#ifdef DEBUG
269  fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid);
270#endif
271
272  jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid);
273  jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid);
274
275
276  maps* final_res=NULL;
277  map* res=NULL;
278#ifdef DEBUG
279  int i=0;
280#endif
281  while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){
282#ifdef DEBUG
283    fprintf(stderr,"mapsFromHashMap loop %d\n",i);
284    i++;
285#endif
286    jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid);
287
288    jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid);
289    jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid);
290    jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid);
291#ifdef DEBUG
292    int j=0;
293#endif
294    while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){
295#ifdef DEBUG
296      fprintf(stderr,"mapsFromHashMap internal loop %d\n",j);
297      j++;
298#endif
299      jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid);
300      jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid);
301      jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid);
302
303#ifdef DEBUG
304      jstring jkd=(*env)->GetStringUTFChars(env, jk, NULL);
305      jstring jvd=(*env)->GetStringUTFChars(env, jv, NULL);
306      fprintf(stderr,"%s %s\n",jkd,jvd);
307#endif
308
309      if(res==NULL){
310        res=createMap((*env)->GetStringUTFChars(env, jk, NULL),
311                      (*env)->GetStringUTFChars(env, jv, NULL));
312      }else
313        addToMap(res,(*env)->GetStringUTFChars(env, jk, NULL),
314                 (*env)->GetStringUTFChars(env, jv, NULL));
315    }
316    jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid);
317    maps* cmap=(maps*)malloc(sizeof(maps));
318    cmap->name=(*env)->GetStringUTFChars(env, jk, NULL);
319    cmap->content=res;
320    cmap->next=NULL;
321    if(final_res==NULL){
[9]322      final_res=dupMaps(&cmap);
[1]323    }else
[9]324      addMapsToMaps(&final_res,cmap);
[1]325    final_res->next=NULL;
[9]326    freeMaps(&cmap);
327    free(cmap);
328    cmap=NULL;
[1]329    res=NULL;
330  }
331#ifdef DEBUG
332  fprintf(stderr,"mapsFromHashMap end\n");
333#endif
334
335  return final_res;
336}
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