[580] | 1 | /* |
---|
[1] | 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
[388] | 4 | * Copyright (c) 2009-2013 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 | |
---|
[580] | 27 | /** |
---|
| 28 | * Load a JAVA class then run the static public method corresponding to the |
---|
| 29 | * service by passing the conf, inputs and outputs parameters by reference. |
---|
| 30 | * |
---|
| 31 | * @param main_conf the conf maps containing the main.cfg settings |
---|
| 32 | * @param request the map containing the HTTP request |
---|
| 33 | * @param s the service structure |
---|
| 34 | * @param real_inputs the maps containing the inputs |
---|
| 35 | * @param real_outputs the maps containing the outputs |
---|
| 36 | */ |
---|
[1] | 37 | int zoo_java_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ |
---|
| 38 | maps* m=*main_conf; |
---|
| 39 | maps* inputs=*real_inputs; |
---|
| 40 | maps* outputs=*real_outputs; |
---|
| 41 | char ntmp[1024]; |
---|
| 42 | getcwd(ntmp,1024); |
---|
| 43 | map* tmp=getMap(request,"metapath"); |
---|
[355] | 44 | char *classpath; |
---|
| 45 | char *oclasspath; |
---|
[1] | 46 | int res=SERVICE_FAILED; |
---|
[352] | 47 | char *cclasspath=getenv("CLASSPATH"); |
---|
[1] | 48 | if(tmp!=NULL){ |
---|
[355] | 49 | if(cclasspath!=NULL){ |
---|
| 50 | classpath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+strlen(cclasspath)+4)*sizeof(char)); |
---|
| 51 | oclasspath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+strlen(cclasspath)+22)*sizeof(char)); |
---|
[364] | 52 | #ifndef WIN32 |
---|
[352] | 53 | sprintf(classpath,"%s/%s/:%s",ntmp,tmp->value,cclasspath); |
---|
[364] | 54 | #else |
---|
| 55 | sprintf(classpath,"%s/%s/;%s",ntmp,tmp->value,cclasspath); |
---|
| 56 | #endif |
---|
[355] | 57 | } |
---|
| 58 | else{ |
---|
| 59 | classpath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+3)*sizeof(char)); |
---|
| 60 | oclasspath=(char*) malloc((strlen(ntmp)+strlen(tmp->value)+21)*sizeof(char)); |
---|
[352] | 61 | sprintf(classpath,"%s/%s/",ntmp,tmp->value); |
---|
[355] | 62 | } |
---|
[388] | 63 | }else{ |
---|
| 64 | if(cclasspath!=NULL){ |
---|
| 65 | classpath=(char*) malloc((strlen(ntmp)+strlen(cclasspath)+3)*sizeof(char)); |
---|
| 66 | oclasspath=(char*) malloc((strlen(ntmp)+strlen(cclasspath)+21)*sizeof(char)); |
---|
| 67 | #ifndef WIN32 |
---|
| 68 | sprintf(classpath,"%s/:%s",ntmp,cclasspath); |
---|
| 69 | #else |
---|
| 70 | sprintf(classpath,"%s/;%s",ntmp,cclasspath); |
---|
| 71 | #endif |
---|
| 72 | } |
---|
| 73 | else{ |
---|
| 74 | classpath=(char*) malloc((strlen(ntmp)+2)*sizeof(char)); |
---|
| 75 | oclasspath=(char*) malloc((strlen(ntmp)+20)*sizeof(char)); |
---|
| 76 | sprintf(classpath,"%s/",ntmp); |
---|
| 77 | } |
---|
| 78 | |
---|
[1] | 79 | } |
---|
[388] | 80 | sprintf(oclasspath,"-Djava.class.path=%s",classpath); |
---|
| 81 | |
---|
[1] | 82 | #ifdef DEBUG |
---|
| 83 | fprintf(stderr,"CLASSPATH=%s\n",classpath); |
---|
[354] | 84 | fprintf(stderr,"(%s)\n",oclasspath); |
---|
[1] | 85 | #endif |
---|
| 86 | |
---|
[539] | 87 | int nb=1; |
---|
| 88 | int nbc0=0; |
---|
| 89 | maps* javaXXMap=getMaps(main_conf,"javaxx"); |
---|
| 90 | if(javaXXMap!=NULL){ |
---|
| 91 | nbc0+=count(javaXXMap->content); |
---|
| 92 | } |
---|
| 93 | int nbc1=0; |
---|
| 94 | maps* javaXMap=getMaps(main_conf,"javax"); |
---|
| 95 | if(javaXMap!=NULL){ |
---|
| 96 | nbc1+=count(javaXMap->content); |
---|
| 97 | } |
---|
[364] | 98 | #ifdef WIN32 |
---|
[539] | 99 | nb=2+nbc0+nbc1; |
---|
| 100 | JavaVMOption options[nb]; |
---|
[364] | 101 | #else |
---|
[539] | 102 | JavaVMOption options[nb+nbc0+nbc1]; |
---|
[364] | 103 | #endif |
---|
[1] | 104 | JavaVMInitArgs vm_args; |
---|
| 105 | JavaVM *jvm; |
---|
| 106 | JNIEnv *env; |
---|
| 107 | long result; |
---|
| 108 | jmethodID pmid; |
---|
| 109 | jfieldID fid; |
---|
| 110 | jobject jobj; |
---|
[364] | 111 | jclass cls; |
---|
| 112 | #ifdef JAVA7 |
---|
| 113 | jobject cls_gr; |
---|
| 114 | #else |
---|
| 115 | jclass cls_gr; |
---|
| 116 | #endif |
---|
[539] | 117 | int i,start; |
---|
| 118 | map *cursorxx=NULL; |
---|
| 119 | if(javaXXMap!=NULL) |
---|
| 120 | cursorxx=javaXXMap->content; |
---|
| 121 | map *cursorx=NULL; |
---|
| 122 | if(javaXMap!=NULL) |
---|
| 123 | cursorx=javaXMap->content; |
---|
[388] | 124 | options[0].optionString = oclasspath; |
---|
[364] | 125 | #ifdef WIN32 |
---|
[539] | 126 | start=2; |
---|
[388] | 127 | options[1].optionString = "-Xmx512m"; |
---|
[539] | 128 | #else |
---|
| 129 | start=1; |
---|
[364] | 130 | #endif |
---|
[539] | 131 | for(i=0;i<nbc0;i++){ |
---|
| 132 | char *tmp=parseJVMXXOption(cursorxx); |
---|
| 133 | options[start+i].optionString = tmp; |
---|
| 134 | free(tmp); |
---|
| 135 | cursorxx=cursorxx->next; |
---|
| 136 | } |
---|
| 137 | for(;i<nbc1+nbc0;i++){ |
---|
| 138 | char *tmp=parseJVMXOption(cursorx); |
---|
| 139 | options[start+i].optionString = tmp; |
---|
| 140 | free(tmp); |
---|
| 141 | cursorx=cursorx->next; |
---|
| 142 | } |
---|
[1] | 143 | |
---|
[59] | 144 | JNI_GetDefaultJavaVMInitArgs(&vm_args); |
---|
[352] | 145 | vm_args.version = JNI_VERSION_1_6; |
---|
[1] | 146 | vm_args.options = options; |
---|
[539] | 147 | vm_args.nOptions = start+nbc0+nbc1; |
---|
[364] | 148 | vm_args.ignoreUnrecognized = JNI_TRUE; |
---|
[1] | 149 | |
---|
| 150 | result = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args); |
---|
| 151 | if(result == JNI_ERR ) { |
---|
| 152 | fprintf(stderr,"Error invoking the JVM"); |
---|
| 153 | return -1; |
---|
| 154 | } |
---|
| 155 | #ifdef DEBUG |
---|
[364] | 156 | else |
---|
[1] | 157 | fprintf(stderr,"JAVA VM Started\n"); |
---|
| 158 | #endif |
---|
| 159 | |
---|
| 160 | tmp=getMap(s->content,"serviceProvider"); |
---|
[364] | 161 | #ifdef JAVA7 |
---|
| 162 | cls = env->FindClass(tmp->value); |
---|
| 163 | cls_gr = env->NewGlobalRef(cls); |
---|
| 164 | #else |
---|
[1] | 165 | cls = (*env)->FindClass(env,tmp->value); |
---|
| 166 | cls_gr = (*env)->NewGlobalRef(env, cls); |
---|
[364] | 167 | #endif |
---|
[1] | 168 | if( cls == NULL ) { |
---|
[388] | 169 | displayStack(env,*main_conf); |
---|
[364] | 170 | #ifdef JAVA7 |
---|
| 171 | (*jvm).DestroyJavaVM(); |
---|
| 172 | #else |
---|
[1] | 173 | (*jvm)->DestroyJavaVM(jvm); |
---|
[364] | 174 | #endif |
---|
[352] | 175 | return -1; |
---|
[1] | 176 | } |
---|
| 177 | #ifdef DEBUG |
---|
| 178 | else{ |
---|
| 179 | fprintf(stderr,"%s loaded\n",tmp->value); |
---|
| 180 | } |
---|
| 181 | #endif |
---|
| 182 | |
---|
| 183 | if (cls != NULL) { |
---|
[364] | 184 | #ifdef JAVA7 |
---|
| 185 | (*env).ExceptionClear(); |
---|
| 186 | pmid=(*env).GetStaticMethodID(cls, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I"); |
---|
| 187 | #else |
---|
[1] | 188 | (*env)->ExceptionClear(env); |
---|
| 189 | pmid=(*env)->GetStaticMethodID(env,cls_gr, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I"); |
---|
[364] | 190 | #endif |
---|
[1] | 191 | if (pmid!=0){ |
---|
| 192 | #ifdef DEBUG |
---|
| 193 | fprintf(stderr,"Function successfully loaded\n"); |
---|
| 194 | #endif |
---|
[57] | 195 | jclass scHashMapClass,scHashMap_class; |
---|
| 196 | jmethodID scHashMap_constructor; |
---|
[364] | 197 | #ifdef JAVA7 |
---|
| 198 | scHashMapClass = (*env).FindClass("java/util/HashMap"); |
---|
| 199 | scHashMap_class = (jclass)(*env).NewGlobalRef(scHashMapClass); |
---|
| 200 | scHashMap_constructor = (*env).GetMethodID(scHashMap_class, "<init>", "()V"); |
---|
| 201 | #else |
---|
[57] | 202 | scHashMapClass = (*env)->FindClass(env, "java/util/HashMap"); |
---|
| 203 | scHashMap_class = (*env)->NewGlobalRef(env, scHashMapClass); |
---|
| 204 | scHashMap_constructor = (*env)->GetMethodID(env, scHashMap_class, "<init>", "()V"); |
---|
[364] | 205 | #endif |
---|
[1] | 206 | /** |
---|
| 207 | * The 3 standard parameter for each services |
---|
| 208 | */ |
---|
[57] | 209 | jobject arg1=HashMap_FromMaps(env,m,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
| 210 | jobject arg2=HashMap_FromMaps(env,inputs,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
| 211 | jobject arg3=HashMap_FromMaps(env,outputs,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
[1] | 212 | jint pValue=0; |
---|
| 213 | |
---|
[364] | 214 | #ifdef JAVA7 |
---|
| 215 | pValue=(*env).CallStaticIntMethod(cls,pmid,arg1,arg2,arg3); |
---|
| 216 | #else |
---|
[1] | 217 | pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3); |
---|
[364] | 218 | #endif |
---|
[114] | 219 | if (pValue != (jint)NULL){ |
---|
[1] | 220 | res=pValue; |
---|
[57] | 221 | m=mapsFromHashMap(env,arg1,scHashMapClass); |
---|
| 222 | *main_conf=m; |
---|
| 223 | outputs=mapsFromHashMap(env,arg3,scHashMapClass); |
---|
[1] | 224 | *real_outputs=outputs; |
---|
| 225 | |
---|
| 226 | #ifdef DEBUG |
---|
| 227 | fprintf(stderr,"Result of call: %i\n", pValue); |
---|
| 228 | dumpMaps(inputs); |
---|
| 229 | dumpMaps(outputs); |
---|
| 230 | #endif |
---|
[388] | 231 | }else{ |
---|
| 232 | displayStack(env,*main_conf); |
---|
[364] | 233 | #ifdef JAVA7 |
---|
| 234 | (*jvm).DestroyJavaVM(); |
---|
| 235 | #else |
---|
[1] | 236 | (*jvm)->DestroyJavaVM(jvm); |
---|
[364] | 237 | #endif |
---|
[9] | 238 | return -1; |
---|
[1] | 239 | } |
---|
| 240 | } |
---|
| 241 | else{ |
---|
[388] | 242 | displayStack(env,*main_conf); |
---|
[364] | 243 | #ifdef JAVA7 |
---|
| 244 | (*jvm).DestroyJavaVM(); |
---|
| 245 | #else |
---|
[1] | 246 | (*jvm)->DestroyJavaVM(jvm); |
---|
[364] | 247 | #endif |
---|
[9] | 248 | return -1; |
---|
[1] | 249 | } |
---|
| 250 | } |
---|
[364] | 251 | #ifdef JAVA7 |
---|
| 252 | (*jvm).DestroyJavaVM(); |
---|
| 253 | #else |
---|
[1] | 254 | (*jvm)->DestroyJavaVM(jvm); |
---|
[364] | 255 | #endif |
---|
[1] | 256 | return res; |
---|
| 257 | } |
---|
| 258 | |
---|
[388] | 259 | /** |
---|
| 260 | * Error handling: display stack trace in an ExceptionReport Document |
---|
[580] | 261 | * |
---|
| 262 | * @param env the JNI environment pointer |
---|
| 263 | * @param main_conf the conf maps containing the main.cfg settings |
---|
[388] | 264 | */ |
---|
| 265 | void displayStack(JNIEnv *env,maps* main_conf){ |
---|
| 266 | map *tmpm=getMapFromMaps(main_conf,"main","tmpPath"); |
---|
| 267 | char tmps[1024]; |
---|
| 268 | sprintf(tmps,"%s/%d.ztmp",tmpm->value,getpid()); |
---|
| 269 | FILE* new_stdout=fopen(tmps,"wb+"); |
---|
[527] | 270 | if(new_stdout==NULL){ |
---|
| 271 | map* err=createMap("text","Unable to run your service, no debug informations can be provided (please verify privileges on tmpPath)"); |
---|
| 272 | addToMap(err,"code","InternalError"); |
---|
| 273 | printExceptionReportResponse(main_conf,err); |
---|
| 274 | freeMap(&err); |
---|
| 275 | free(err); |
---|
| 276 | return; |
---|
| 277 | } |
---|
[388] | 278 | fflush(stderr); |
---|
| 279 | dup2(fileno(new_stdout),fileno(stderr)); |
---|
| 280 | fprintf(stderr,"Unable to run your java process properly: "); |
---|
| 281 | fflush(stderr); |
---|
| 282 | #ifdef JAVA7 |
---|
| 283 | (*env).ExceptionDescribe(); |
---|
| 284 | #else |
---|
| 285 | (*env)->ExceptionDescribe(env); |
---|
| 286 | #endif |
---|
| 287 | fflush(new_stdout); |
---|
| 288 | fseek(new_stdout, 0, SEEK_END); |
---|
| 289 | long flen=ftell(new_stdout); |
---|
| 290 | fseek(new_stdout, 0, SEEK_SET); |
---|
| 291 | char *tmps1=(char*)malloc((flen+1)*sizeof(char)); |
---|
| 292 | fread(tmps1,flen,1,new_stdout); |
---|
| 293 | fclose(new_stdout); |
---|
| 294 | tmps1[flen]=0; |
---|
| 295 | map* err=createMap("text",tmps1); |
---|
| 296 | addToMap(err,"code","InternalError"); |
---|
| 297 | printExceptionReportResponse(main_conf,err); |
---|
| 298 | freeMap(&err); |
---|
| 299 | free(err); |
---|
| 300 | } |
---|
| 301 | |
---|
[580] | 302 | /** |
---|
| 303 | * Create a string containing the JVM -XX:* option for a given map |
---|
| 304 | * Depending on the map' name: |
---|
| 305 | * - in case the value is minus then the reult will be : -XX:-name |
---|
| 306 | * - in case the value is plus then the reult will be : -XX:+name |
---|
| 307 | * - in other cases the reult will be : -XX:name=value |
---|
| 308 | * |
---|
| 309 | * @param m the map containing the option |
---|
| 310 | * @return a char* containing the valide JVM option (-XX:*) |
---|
| 311 | */ |
---|
[539] | 312 | char *parseJVMXXOption(map* m){ |
---|
| 313 | char *res=(char*)malloc((strlen(m->name)+strlen(m->value)+5)*sizeof(char)); |
---|
| 314 | if(strncasecmp(m->value,"minus",5)==0) |
---|
| 315 | sprintf(res,"-XX:-%s",m->name); |
---|
| 316 | else if(strncasecmp(m->value,"plus",5)==0) |
---|
| 317 | sprintf(res,"-XX:+%s",m->name); |
---|
| 318 | else |
---|
| 319 | sprintf(res,"-XX:%s=%s",m->name,m->value); |
---|
| 320 | return res; |
---|
| 321 | } |
---|
| 322 | |
---|
[580] | 323 | /** |
---|
| 324 | * Create a string containing the JVM -X*:* option for a given map. |
---|
| 325 | * The reult will be in the following format: -Xname:value |
---|
| 326 | * |
---|
| 327 | * @param m the map containing the option |
---|
| 328 | * @return a char* containing the valide JVM option (-XX:*) |
---|
| 329 | */ |
---|
[539] | 330 | char *parseJVMXOption(map* m){ |
---|
| 331 | char *res=(char*)malloc((strlen(m->name)+strlen(m->value)+5)*sizeof(char)); |
---|
| 332 | sprintf(res,"-X%s:%s",m->name,m->value); |
---|
| 333 | return res; |
---|
| 334 | } |
---|
| 335 | |
---|
[580] | 336 | /** |
---|
| 337 | * Convert a maps to a JAVA HashMap<String,HashMap<String,String>> |
---|
| 338 | * |
---|
| 339 | * @param env the JNI environment pointer |
---|
| 340 | * @param t the maps to convert |
---|
| 341 | * @param scHashMapClass the HashMap class |
---|
| 342 | * @param scHashMap_class the HashMap class |
---|
| 343 | * @param scHashMap_constructor the pointer to the hashMap constructor method |
---|
| 344 | * @return a created JAVA HashMap containing the converted maps |
---|
| 345 | * @warning make sure to free ressources returned by this function |
---|
| 346 | */ |
---|
[57] | 347 | jobject HashMap_FromMaps(JNIEnv *env,maps* t,jclass scHashMapClass,jclass scHashMap_class,jmethodID scHashMap_constructor){ |
---|
[1] | 348 | jobject scObject,scObject1; |
---|
| 349 | if(scHashMap_constructor!=NULL){ |
---|
[364] | 350 | #ifdef JAVA7 |
---|
| 351 | scObject = (*env).NewObject(scHashMap_class, scHashMap_constructor); |
---|
| 352 | #else |
---|
[1] | 353 | scObject = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor); |
---|
[364] | 354 | #endif |
---|
[1] | 355 | jmethodID put_mid = 0; |
---|
| 356 | |
---|
[364] | 357 | #ifdef JAVA7 |
---|
| 358 | put_mid = (*env).GetMethodID(scHashMapClass, "put", |
---|
| 359 | "(Ljava/lang/Object;Ljava/lang/Object;)" |
---|
| 360 | "Ljava/lang/Object;"); |
---|
| 361 | #else |
---|
[1] | 362 | put_mid = (*env)->GetMethodID(env,scHashMapClass, "put", |
---|
| 363 | "(Ljava/lang/Object;Ljava/lang/Object;)" |
---|
| 364 | "Ljava/lang/Object;"); |
---|
[364] | 365 | #endif |
---|
[1] | 366 | maps* tmp=t; |
---|
| 367 | while(tmp!=NULL){ |
---|
[360] | 368 | map* tmap=getMapType(tmp->content); |
---|
[1] | 369 | map* tmp1=tmp->content; |
---|
[364] | 370 | #ifdef JAVA7 |
---|
| 371 | scObject1 = (*env).NewObject(scHashMap_class, scHashMap_constructor); |
---|
| 372 | #else |
---|
[1] | 373 | scObject1 = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor); |
---|
[364] | 374 | #endif |
---|
[68] | 375 | map* sizeV=getMap(tmp1,"size"); |
---|
[360] | 376 | map* isArray=getMap(tmp1,"isArray"); |
---|
| 377 | map* alen=getMap(tmp1,"length"); |
---|
[1] | 378 | while(tmp1!=NULL){ |
---|
[360] | 379 | if(strcmp(tmp1->name,"value")==0){ |
---|
| 380 | if(isArray==NULL){ |
---|
| 381 | if(sizeV!=NULL && strcmp(tmp1->name,"value")==0){ |
---|
[364] | 382 | #ifdef JAVA7 |
---|
| 383 | jbyteArray tmpData=(*env).NewByteArray(atoi(sizeV->value)); |
---|
| 384 | (*env).SetByteArrayRegion(tmpData,0,atoi(sizeV->value),(const jbyte *)tmp1->value); |
---|
| 385 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), tmpData); |
---|
| 386 | #else |
---|
[360] | 387 | jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sizeV->value)); |
---|
| 388 | (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sizeV->value),tmp1->value); |
---|
| 389 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), tmpData); |
---|
[364] | 390 | #endif |
---|
[360] | 391 | }else |
---|
[364] | 392 | #ifdef JAVA7 |
---|
| 393 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value)); |
---|
| 394 | #else |
---|
[360] | 395 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value)); |
---|
[364] | 396 | #endif |
---|
[360] | 397 | } |
---|
| 398 | else{ |
---|
| 399 | int alen1=atoi(alen->value); |
---|
| 400 | fprintf(stderr,"LENGTH %d \n",alen1); |
---|
| 401 | |
---|
| 402 | jclass scArrayListClass,scArrayList_class; |
---|
| 403 | jmethodID scArrayList_constructor; |
---|
[388] | 404 | jobject scObject2; |
---|
[364] | 405 | #ifdef JAVA7 |
---|
| 406 | scArrayListClass = (*env).FindClass("java/util/ArrayList"); |
---|
| 407 | scArrayList_class = (jclass)(*env).NewGlobalRef(scArrayListClass); |
---|
| 408 | scArrayList_constructor = (*env).GetMethodID(scArrayList_class, "<init>", "()V"); |
---|
| 409 | jmethodID add_mid = 0; |
---|
| 410 | scObject2 = (*env).NewObject(scArrayList_class, scArrayList_constructor); |
---|
| 411 | |
---|
| 412 | add_mid = (*env).GetMethodID(scArrayListClass, |
---|
| 413 | "add","(Ljava/lang/Object;)Z"); |
---|
| 414 | #else |
---|
[360] | 415 | scArrayListClass = (*env)->FindClass(env, "java/util/ArrayList"); |
---|
| 416 | scArrayList_class = (*env)->NewGlobalRef(env, scArrayListClass); |
---|
| 417 | scArrayList_constructor = (*env)->GetMethodID(env, scArrayList_class, "<init>", "()V"); |
---|
| 418 | jmethodID add_mid = 0; |
---|
| 419 | scObject2 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor); |
---|
| 420 | |
---|
| 421 | add_mid = (*env)->GetMethodID(env,scArrayListClass, |
---|
| 422 | "add","(Ljava/lang/Object;)Z"); |
---|
[364] | 423 | #endif |
---|
[360] | 424 | int i; |
---|
| 425 | |
---|
| 426 | for(i=0;i<alen1;i++){ |
---|
| 427 | map* vMap=getMapArray(tmp->content,"value",i); |
---|
| 428 | map* sMap=getMapArray(tmp->content,"size",i); |
---|
| 429 | map* mMap=getMapArray(tmp->content,tmap->value,i); |
---|
| 430 | |
---|
| 431 | if(sMap!=NULL && vMap!=NULL && strncmp(vMap->name,"value",5)==0){ |
---|
[364] | 432 | #ifdef JAVA7 |
---|
| 433 | jbyteArray tmpData=(*env).NewByteArray(atoi(sMap->value)); |
---|
| 434 | (*env).SetByteArrayRegion(tmpData,0,atoi(sMap->value),(const jbyte *)vMap->value); |
---|
| 435 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
| 436 | #else |
---|
[360] | 437 | jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sMap->value)); |
---|
| 438 | (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sMap->value),vMap->value); |
---|
| 439 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
[364] | 440 | #endif |
---|
[360] | 441 | }else{ |
---|
[364] | 442 | #ifdef JAVA7 |
---|
| 443 | jobject tmpData=(*env).NewStringUTF(vMap->value); |
---|
| 444 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
| 445 | #else |
---|
[360] | 446 | jobject tmpData=(*env)->NewStringUTF(env,vMap->value); |
---|
| 447 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
[364] | 448 | #endif |
---|
[360] | 449 | } |
---|
| 450 | |
---|
| 451 | } |
---|
[364] | 452 | |
---|
| 453 | #ifdef JAVA7 |
---|
| 454 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), scObject2); |
---|
| 455 | #else |
---|
[360] | 456 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), scObject2); |
---|
[364] | 457 | #endif |
---|
| 458 | |
---|
[360] | 459 | } |
---|
| 460 | } |
---|
| 461 | else |
---|
[364] | 462 | #ifdef JAVA7 |
---|
| 463 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value)); |
---|
| 464 | #else |
---|
[68] | 465 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value)); |
---|
[364] | 466 | #endif |
---|
[1] | 467 | tmp1=tmp1->next; |
---|
| 468 | } |
---|
[364] | 469 | #ifdef JAVA7 |
---|
| 470 | (*env).CallObjectMethod(scObject, put_mid, (*env).NewStringUTF(tmp->name), scObject1); |
---|
| 471 | #else |
---|
[1] | 472 | (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1); |
---|
[364] | 473 | #endif |
---|
[1] | 474 | tmp=tmp->next; |
---|
[57] | 475 | } |
---|
[1] | 476 | return scObject; |
---|
| 477 | } |
---|
| 478 | else |
---|
| 479 | return NULL; |
---|
| 480 | } |
---|
| 481 | |
---|
[580] | 482 | /** |
---|
| 483 | * Convert a JAVA HashMap<String,HashMap<String,String>> to a maps |
---|
| 484 | * |
---|
| 485 | * @param env the JNI environment pointer |
---|
| 486 | * @param t the HashMap |
---|
| 487 | * @param scHashMapClass the hashMap class |
---|
| 488 | * @return a created maps containing the converted HashMap |
---|
| 489 | * @warning make sure to free ressources returned by this function |
---|
| 490 | */ |
---|
[57] | 491 | maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){ |
---|
[1] | 492 | #ifdef DEBUG |
---|
| 493 | fprintf(stderr,"mapsFromHashMap start\n"); |
---|
| 494 | #endif |
---|
| 495 | /** |
---|
| 496 | * What need to be done (in java). |
---|
| 497 | * Set set = hm.entrySet(); |
---|
| 498 | * Iterator i = set.iterator(); |
---|
| 499 | * while(i.hasNext()){ |
---|
| 500 | * Map.Entry me = (Map.Entry)i.next(); |
---|
| 501 | * System.out.println(me.getKey() + " : " + me.getValue() ); |
---|
| 502 | * } |
---|
| 503 | */ |
---|
[57] | 504 | jclass scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass; |
---|
[68] | 505 | jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid; |
---|
[1] | 506 | jobject scObject,scObject1; |
---|
| 507 | if(scHashMapClass==NULL){ |
---|
[114] | 508 | #ifdef DEBUG |
---|
[1] | 509 | fprintf(stderr,"Unable to load java.util.HashMap\n"); |
---|
[114] | 510 | #endif |
---|
[1] | 511 | return NULL; |
---|
| 512 | } |
---|
[364] | 513 | #ifdef JAVA7 |
---|
| 514 | entrySet_mid = (*env).GetMethodID(scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
| 515 | containsKey_mid = (*env).GetMethodID(scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
| 516 | get_mid = (*env).GetMethodID(scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
| 517 | #else |
---|
[1] | 518 | entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
[68] | 519 | containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
| 520 | get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
[364] | 521 | #endif |
---|
[114] | 522 | |
---|
[68] | 523 | if(containsKey_mid==0){ |
---|
[114] | 524 | #ifdef DEBUG |
---|
[68] | 525 | fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid); |
---|
[114] | 526 | #endif |
---|
| 527 | return NULL; |
---|
[68] | 528 | } |
---|
| 529 | if(get_mid==0){ |
---|
[114] | 530 | #ifdef DEBUG |
---|
[68] | 531 | fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid); |
---|
[114] | 532 | #endif |
---|
| 533 | return NULL; |
---|
[68] | 534 | } |
---|
[1] | 535 | if(entrySet_mid==0){ |
---|
[114] | 536 | #ifdef DEBUG |
---|
[1] | 537 | fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid); |
---|
[114] | 538 | #endif |
---|
[1] | 539 | return NULL; |
---|
| 540 | } |
---|
| 541 | #ifdef DEBUG |
---|
| 542 | else |
---|
| 543 | fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid); |
---|
| 544 | #endif |
---|
| 545 | |
---|
[364] | 546 | #ifdef JAVA7 |
---|
| 547 | scSetClass = (*env).FindClass("java/util/Set"); |
---|
| 548 | iterator_mid = (*env).GetMethodID(scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
| 549 | #else |
---|
[1] | 550 | scSetClass = (*env)->FindClass(env, "java/util/Set"); |
---|
| 551 | iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
[364] | 552 | #endif |
---|
[1] | 553 | #ifdef DEBUG |
---|
| 554 | fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid); |
---|
| 555 | #endif |
---|
| 556 | |
---|
[364] | 557 | #ifdef JAVA7 |
---|
| 558 | scIteratorClass = (*env).FindClass("java/util/Iterator"); |
---|
| 559 | hasNext_mid = (*env).GetMethodID(scIteratorClass, "hasNext", "()Z"); |
---|
| 560 | #else |
---|
[1] | 561 | scIteratorClass = (*env)->FindClass(env, "java/util/Iterator"); |
---|
| 562 | hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z"); |
---|
[364] | 563 | #endif |
---|
| 564 | |
---|
[1] | 565 | #ifdef DEBUG |
---|
| 566 | fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid); |
---|
| 567 | #endif |
---|
[364] | 568 | |
---|
| 569 | #ifdef JAVA7 |
---|
| 570 | next_mid = (*env).GetMethodID(scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
| 571 | #else |
---|
[1] | 572 | next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
[364] | 573 | #endif |
---|
[1] | 574 | #ifdef DEBUG |
---|
| 575 | fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid); |
---|
| 576 | #endif |
---|
| 577 | |
---|
[364] | 578 | #ifdef JAVA7 |
---|
| 579 | scMapEntryClass = (*env).FindClass("java/util/Map$Entry"); |
---|
| 580 | getKey_mid = (*env).GetMethodID(scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
| 581 | #else |
---|
[1] | 582 | scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry"); |
---|
| 583 | getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
[364] | 584 | #endif |
---|
[1] | 585 | #ifdef DEBUG |
---|
| 586 | fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid); |
---|
| 587 | #endif |
---|
[364] | 588 | #ifdef JAVA7 |
---|
| 589 | getValue_mid = (*env).GetMethodID(scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
| 590 | #else |
---|
[1] | 591 | getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
[364] | 592 | #endif |
---|
[1] | 593 | #ifdef DEBUG |
---|
| 594 | fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid); |
---|
| 595 | #endif |
---|
| 596 | |
---|
[364] | 597 | #ifdef JAVA7 |
---|
| 598 | jobject final_set=(*env).CallObjectMethod(t,entrySet_mid); |
---|
| 599 | jobject final_iterator=(*env).CallObjectMethod(final_set,iterator_mid); |
---|
| 600 | #else |
---|
[1] | 601 | jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid); |
---|
| 602 | jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid); |
---|
[364] | 603 | #endif |
---|
[1] | 604 | |
---|
| 605 | maps* final_res=NULL; |
---|
| 606 | map* res=NULL; |
---|
[364] | 607 | #ifdef JAVA7 |
---|
| 608 | while((*env).CallBooleanMethod(final_iterator,hasNext_mid)){ |
---|
| 609 | jobject tmp=(*env).CallObjectMethod(final_iterator,next_mid); |
---|
| 610 | |
---|
| 611 | jobject imap=(*env).CallObjectMethod(tmp,getValue_mid); |
---|
| 612 | jobject set=(*env).CallObjectMethod(imap,entrySet_mid); |
---|
| 613 | jobject iterator=(*env).CallObjectMethod(set,iterator_mid); |
---|
| 614 | |
---|
| 615 | #else |
---|
[1] | 616 | while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){ |
---|
| 617 | jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid); |
---|
| 618 | |
---|
| 619 | jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid); |
---|
| 620 | jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid); |
---|
| 621 | jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid); |
---|
[57] | 622 | |
---|
[364] | 623 | #endif |
---|
[68] | 624 | int size=-1; |
---|
[364] | 625 | #ifdef JAVA7 |
---|
| 626 | if((*env).CallBooleanMethod(imap,containsKey_mid,(*env).NewStringUTF("size"))){ |
---|
| 627 | jobject sizeV=(*env).CallObjectMethod(imap, get_mid,(*env).NewStringUTF("size")); |
---|
| 628 | const char* sizeVS=(*env).GetStringUTFChars((jstring)sizeV, NULL); |
---|
| 629 | #else |
---|
[68] | 630 | if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){ |
---|
| 631 | jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size")); |
---|
[114] | 632 | const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL); |
---|
[364] | 633 | #endif |
---|
[68] | 634 | size=atoi(sizeVS); |
---|
| 635 | fprintf(stderr,"SIZE : %s\n",sizeVS); |
---|
[364] | 636 | #ifdef JAVA7 |
---|
| 637 | (*env).ReleaseStringUTFChars((jstring)sizeV, sizeVS); |
---|
| 638 | #else |
---|
[114] | 639 | (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS); |
---|
[364] | 640 | #endif |
---|
[68] | 641 | } |
---|
| 642 | |
---|
[364] | 643 | #ifdef JAVA7 |
---|
| 644 | while((*env).CallBooleanMethod(iterator,hasNext_mid)){ |
---|
| 645 | jobject tmp1=(*env).CallObjectMethod(iterator,next_mid); |
---|
| 646 | jobject jk=(*env).CallObjectMethod(tmp1,getKey_mid); |
---|
| 647 | jobject jv=(*env).CallObjectMethod(tmp1,getValue_mid); |
---|
| 648 | |
---|
| 649 | const char* jkd=(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
| 650 | #else |
---|
[1] | 651 | while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){ |
---|
| 652 | jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid); |
---|
| 653 | jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid); |
---|
| 654 | jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid); |
---|
| 655 | |
---|
[114] | 656 | const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
[364] | 657 | #endif |
---|
[68] | 658 | if(size>=0 && strcmp(jkd,"value")==0){ |
---|
[364] | 659 | #ifdef JAVA7 |
---|
| 660 | jobject value=(jobject)(*env).GetByteArrayElements((jbyteArray)jv, NULL); |
---|
| 661 | #else |
---|
[68] | 662 | jobject value=(*env)->GetByteArrayElements(env, jv, NULL); |
---|
[364] | 663 | #endif |
---|
[68] | 664 | if(res==NULL){ |
---|
| 665 | res=createMap(jkd,""); |
---|
| 666 | }else{ |
---|
| 667 | addToMap(res,jkd,""); |
---|
| 668 | } |
---|
| 669 | map* tmpR=getMap(res,"value"); |
---|
| 670 | free(tmpR->value); |
---|
| 671 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
| 672 | memmove(tmpR->value,value,size*sizeof(char)); |
---|
| 673 | tmpR->value[size]=0; |
---|
[114] | 674 | char tmp[128]; |
---|
| 675 | sprintf(tmp,"%d",size); |
---|
| 676 | addToMap(res,"size",tmp); |
---|
[68] | 677 | } |
---|
| 678 | else{ |
---|
[364] | 679 | #ifdef JAVA7 |
---|
| 680 | const char* jvd=(*env).GetStringUTFChars((jstring)jv, NULL); |
---|
| 681 | #else |
---|
[114] | 682 | const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL); |
---|
[364] | 683 | #endif |
---|
[68] | 684 | if(res==NULL){ |
---|
| 685 | res=createMap(jkd,jvd); |
---|
| 686 | }else{ |
---|
| 687 | addToMap(res,jkd,jvd); |
---|
| 688 | } |
---|
[364] | 689 | #ifdef JAVA7 |
---|
| 690 | (*env).ReleaseStringUTFChars((jstring)jv, jvd); |
---|
| 691 | #else |
---|
[114] | 692 | (*env)->ReleaseStringUTFChars(env, jv, jvd); |
---|
[364] | 693 | #endif |
---|
[68] | 694 | } |
---|
[59] | 695 | |
---|
[364] | 696 | #ifdef JAVA7 |
---|
| 697 | (*env).ReleaseStringUTFChars((jstring)jk, jkd); |
---|
| 698 | #else |
---|
[114] | 699 | (*env)->ReleaseStringUTFChars(env, jk, jkd); |
---|
[364] | 700 | #endif |
---|
[59] | 701 | |
---|
[1] | 702 | } |
---|
[364] | 703 | #ifdef JAVA7 |
---|
| 704 | jobject jk=(*env).CallObjectMethod(tmp,getKey_mid); |
---|
| 705 | #else |
---|
[1] | 706 | jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid); |
---|
[364] | 707 | #endif |
---|
[1] | 708 | maps* cmap=(maps*)malloc(sizeof(maps)); |
---|
[364] | 709 | #ifdef JAVA7 |
---|
| 710 | cmap->name=(char*)(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
| 711 | #else |
---|
[1] | 712 | cmap->name=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
[364] | 713 | #endif |
---|
[57] | 714 | #ifdef DEBUG |
---|
| 715 | fprintf(stderr," / %s \n",cmap->name); |
---|
| 716 | #endif |
---|
[1] | 717 | cmap->content=res; |
---|
| 718 | cmap->next=NULL; |
---|
[57] | 719 | if(final_res==NULL) |
---|
[9] | 720 | final_res=dupMaps(&cmap); |
---|
[57] | 721 | else |
---|
[9] | 722 | addMapsToMaps(&final_res,cmap); |
---|
| 723 | freeMaps(&cmap); |
---|
| 724 | free(cmap); |
---|
| 725 | cmap=NULL; |
---|
[1] | 726 | res=NULL; |
---|
| 727 | } |
---|
| 728 | #ifdef DEBUG |
---|
| 729 | fprintf(stderr,"mapsFromHashMap end\n"); |
---|
| 730 | #endif |
---|
| 731 | |
---|
| 732 | return final_res; |
---|
| 733 | } |
---|
[527] | 734 | |
---|