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