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