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