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