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