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