[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 | } |
---|
[352] | 53 | sprintf(oclasspath,"-Djava.class.path=%s",classpath); |
---|
[388] | 54 | }else{ |
---|
| 55 | if(cclasspath!=NULL){ |
---|
| 56 | classpath=(char*) malloc((strlen(ntmp)+strlen(cclasspath)+3)*sizeof(char)); |
---|
| 57 | oclasspath=(char*) malloc((strlen(ntmp)+strlen(cclasspath)+21)*sizeof(char)); |
---|
| 58 | #ifndef WIN32 |
---|
| 59 | sprintf(classpath,"%s/:%s",ntmp,cclasspath); |
---|
| 60 | #else |
---|
| 61 | sprintf(classpath,"%s/;%s",ntmp,cclasspath); |
---|
| 62 | #endif |
---|
| 63 | } |
---|
| 64 | else{ |
---|
| 65 | classpath=(char*) malloc((strlen(ntmp)+2)*sizeof(char)); |
---|
| 66 | oclasspath=(char*) malloc((strlen(ntmp)+20)*sizeof(char)); |
---|
| 67 | sprintf(classpath,"%s/",ntmp); |
---|
| 68 | } |
---|
| 69 | sprintf(oclasspath,"-Djava.class.path=%s",classpath); |
---|
| 70 | |
---|
[1] | 71 | } |
---|
[388] | 72 | sprintf(oclasspath,"-Djava.class.path=%s",classpath); |
---|
| 73 | |
---|
[1] | 74 | #ifdef DEBUG |
---|
| 75 | fprintf(stderr,"CLASSPATH=%s\n",classpath); |
---|
[354] | 76 | fprintf(stderr,"(%s)\n",oclasspath); |
---|
[1] | 77 | #endif |
---|
| 78 | |
---|
[364] | 79 | #ifdef WIN32 |
---|
| 80 | JavaVMOption options[2]; |
---|
| 81 | #else |
---|
[62] | 82 | JavaVMOption options[1]; |
---|
[364] | 83 | #endif |
---|
[1] | 84 | JavaVMInitArgs vm_args; |
---|
| 85 | JavaVM *jvm; |
---|
| 86 | JNIEnv *env; |
---|
| 87 | long result; |
---|
| 88 | jmethodID pmid; |
---|
| 89 | jfieldID fid; |
---|
| 90 | jobject jobj; |
---|
[364] | 91 | jclass cls; |
---|
| 92 | #ifdef JAVA7 |
---|
| 93 | jobject cls_gr; |
---|
| 94 | #else |
---|
| 95 | jclass cls_gr; |
---|
| 96 | #endif |
---|
[1] | 97 | int i; |
---|
| 98 | |
---|
[388] | 99 | options[0].optionString = oclasspath; |
---|
[364] | 100 | #ifdef WIN32 |
---|
[388] | 101 | options[1].optionString = "-Xmx512m"; |
---|
| 102 | /*options[2].optionString = "-Xms128m"; |
---|
| 103 | options[3].optionString = "-XX:MaxPermSize=256m"; |
---|
| 104 | options[4].optionString = "-XX:MaxHeapFreeRatio=70";*/ |
---|
[364] | 105 | #endif |
---|
[1] | 106 | |
---|
[59] | 107 | JNI_GetDefaultJavaVMInitArgs(&vm_args); |
---|
[352] | 108 | vm_args.version = JNI_VERSION_1_6; |
---|
[1] | 109 | vm_args.options = options; |
---|
[364] | 110 | #ifdef WIN32 |
---|
| 111 | vm_args.nOptions = 2; |
---|
| 112 | #else |
---|
[62] | 113 | vm_args.nOptions = 1; |
---|
[364] | 114 | #endif |
---|
| 115 | vm_args.ignoreUnrecognized = JNI_TRUE; |
---|
[1] | 116 | |
---|
| 117 | result = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args); |
---|
| 118 | if(result == JNI_ERR ) { |
---|
| 119 | fprintf(stderr,"Error invoking the JVM"); |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
| 122 | #ifdef DEBUG |
---|
[364] | 123 | else |
---|
[1] | 124 | fprintf(stderr,"JAVA VM Started\n"); |
---|
| 125 | #endif |
---|
| 126 | |
---|
| 127 | tmp=getMap(s->content,"serviceProvider"); |
---|
[364] | 128 | #ifdef JAVA7 |
---|
| 129 | cls = env->FindClass(tmp->value); |
---|
| 130 | cls_gr = env->NewGlobalRef(cls); |
---|
| 131 | #else |
---|
[1] | 132 | cls = (*env)->FindClass(env,tmp->value); |
---|
| 133 | cls_gr = (*env)->NewGlobalRef(env, cls); |
---|
[364] | 134 | #endif |
---|
[1] | 135 | if( cls == NULL ) { |
---|
[388] | 136 | displayStack(env,*main_conf); |
---|
[364] | 137 | #ifdef JAVA7 |
---|
| 138 | (*jvm).DestroyJavaVM(); |
---|
| 139 | #else |
---|
[1] | 140 | (*jvm)->DestroyJavaVM(jvm); |
---|
[364] | 141 | #endif |
---|
[352] | 142 | return -1; |
---|
[1] | 143 | } |
---|
| 144 | #ifdef DEBUG |
---|
| 145 | else{ |
---|
| 146 | fprintf(stderr,"%s loaded\n",tmp->value); |
---|
| 147 | } |
---|
| 148 | #endif |
---|
| 149 | |
---|
| 150 | if (cls != NULL) { |
---|
[364] | 151 | #ifdef JAVA7 |
---|
| 152 | (*env).ExceptionClear(); |
---|
| 153 | pmid=(*env).GetStaticMethodID(cls, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I"); |
---|
| 154 | #else |
---|
[1] | 155 | (*env)->ExceptionClear(env); |
---|
| 156 | pmid=(*env)->GetStaticMethodID(env,cls_gr, s->name, "(Ljava/util/HashMap;Ljava/util/HashMap;Ljava/util/HashMap;)I"); |
---|
[364] | 157 | #endif |
---|
[1] | 158 | if (pmid!=0){ |
---|
| 159 | #ifdef DEBUG |
---|
| 160 | fprintf(stderr,"Function successfully loaded\n"); |
---|
| 161 | #endif |
---|
[57] | 162 | jclass scHashMapClass,scHashMap_class; |
---|
| 163 | jmethodID scHashMap_constructor; |
---|
[364] | 164 | #ifdef JAVA7 |
---|
| 165 | scHashMapClass = (*env).FindClass("java/util/HashMap"); |
---|
| 166 | scHashMap_class = (jclass)(*env).NewGlobalRef(scHashMapClass); |
---|
| 167 | scHashMap_constructor = (*env).GetMethodID(scHashMap_class, "<init>", "()V"); |
---|
| 168 | #else |
---|
[57] | 169 | scHashMapClass = (*env)->FindClass(env, "java/util/HashMap"); |
---|
| 170 | scHashMap_class = (*env)->NewGlobalRef(env, scHashMapClass); |
---|
| 171 | scHashMap_constructor = (*env)->GetMethodID(env, scHashMap_class, "<init>", "()V"); |
---|
[364] | 172 | #endif |
---|
[1] | 173 | /** |
---|
| 174 | * The 3 standard parameter for each services |
---|
| 175 | */ |
---|
[57] | 176 | jobject arg1=HashMap_FromMaps(env,m,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
| 177 | jobject arg2=HashMap_FromMaps(env,inputs,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
| 178 | jobject arg3=HashMap_FromMaps(env,outputs,scHashMapClass,scHashMap_class,scHashMap_constructor); |
---|
[1] | 179 | jint pValue=0; |
---|
| 180 | |
---|
[364] | 181 | #ifdef JAVA7 |
---|
| 182 | pValue=(*env).CallStaticIntMethod(cls,pmid,arg1,arg2,arg3); |
---|
| 183 | #else |
---|
[1] | 184 | pValue=(*env)->CallStaticIntMethod(env,cls,pmid,arg1,arg2,arg3); |
---|
[364] | 185 | #endif |
---|
[114] | 186 | if (pValue != (jint)NULL){ |
---|
[1] | 187 | res=pValue; |
---|
[57] | 188 | m=mapsFromHashMap(env,arg1,scHashMapClass); |
---|
| 189 | *main_conf=m; |
---|
| 190 | outputs=mapsFromHashMap(env,arg3,scHashMapClass); |
---|
[1] | 191 | *real_outputs=outputs; |
---|
| 192 | |
---|
| 193 | #ifdef DEBUG |
---|
| 194 | fprintf(stderr,"Result of call: %i\n", pValue); |
---|
| 195 | dumpMaps(inputs); |
---|
| 196 | dumpMaps(outputs); |
---|
| 197 | #endif |
---|
[388] | 198 | }else{ |
---|
| 199 | displayStack(env,*main_conf); |
---|
[364] | 200 | #ifdef JAVA7 |
---|
| 201 | (*jvm).DestroyJavaVM(); |
---|
| 202 | #else |
---|
[1] | 203 | (*jvm)->DestroyJavaVM(jvm); |
---|
[364] | 204 | #endif |
---|
[9] | 205 | return -1; |
---|
[1] | 206 | } |
---|
| 207 | } |
---|
| 208 | else{ |
---|
[388] | 209 | displayStack(env,*main_conf); |
---|
[364] | 210 | #ifdef JAVA7 |
---|
| 211 | (*jvm).DestroyJavaVM(); |
---|
| 212 | #else |
---|
[1] | 213 | (*jvm)->DestroyJavaVM(jvm); |
---|
[364] | 214 | #endif |
---|
[9] | 215 | return -1; |
---|
[1] | 216 | } |
---|
| 217 | } |
---|
[364] | 218 | #ifdef JAVA7 |
---|
| 219 | (*jvm).DestroyJavaVM(); |
---|
| 220 | #else |
---|
[1] | 221 | (*jvm)->DestroyJavaVM(jvm); |
---|
[364] | 222 | #endif |
---|
[1] | 223 | return res; |
---|
| 224 | } |
---|
| 225 | |
---|
[388] | 226 | /** |
---|
| 227 | * Error handling: display stack trace in an ExceptionReport Document |
---|
| 228 | */ |
---|
| 229 | void displayStack(JNIEnv *env,maps* main_conf){ |
---|
| 230 | map *tmpm=getMapFromMaps(main_conf,"main","tmpPath"); |
---|
| 231 | char tmps[1024]; |
---|
| 232 | sprintf(tmps,"%s/%d.ztmp",tmpm->value,getpid()); |
---|
| 233 | FILE* new_stdout=fopen(tmps,"wb+"); |
---|
| 234 | fflush(stderr); |
---|
| 235 | dup2(fileno(new_stdout),fileno(stderr)); |
---|
| 236 | fprintf(stderr,"Unable to run your java process properly: "); |
---|
| 237 | fflush(stderr); |
---|
| 238 | #ifdef JAVA7 |
---|
| 239 | (*env).ExceptionDescribe(); |
---|
| 240 | #else |
---|
| 241 | (*env)->ExceptionDescribe(env); |
---|
| 242 | #endif |
---|
| 243 | fflush(new_stdout); |
---|
| 244 | fseek(new_stdout, 0, SEEK_END); |
---|
| 245 | long flen=ftell(new_stdout); |
---|
| 246 | fseek(new_stdout, 0, SEEK_SET); |
---|
| 247 | char *tmps1=(char*)malloc((flen+1)*sizeof(char)); |
---|
| 248 | fread(tmps1,flen,1,new_stdout); |
---|
| 249 | fclose(new_stdout); |
---|
| 250 | tmps1[flen]=0; |
---|
| 251 | map* err=createMap("text",tmps1); |
---|
| 252 | addToMap(err,"code","InternalError"); |
---|
| 253 | printExceptionReportResponse(main_conf,err); |
---|
| 254 | freeMap(&err); |
---|
| 255 | free(err); |
---|
| 256 | } |
---|
| 257 | |
---|
[57] | 258 | jobject HashMap_FromMaps(JNIEnv *env,maps* t,jclass scHashMapClass,jclass scHashMap_class,jmethodID scHashMap_constructor){ |
---|
[1] | 259 | jobject scObject,scObject1; |
---|
| 260 | if(scHashMap_constructor!=NULL){ |
---|
[364] | 261 | #ifdef JAVA7 |
---|
| 262 | scObject = (*env).NewObject(scHashMap_class, scHashMap_constructor); |
---|
| 263 | #else |
---|
[1] | 264 | scObject = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor); |
---|
[364] | 265 | #endif |
---|
[1] | 266 | jmethodID put_mid = 0; |
---|
| 267 | |
---|
[364] | 268 | #ifdef JAVA7 |
---|
| 269 | put_mid = (*env).GetMethodID(scHashMapClass, "put", |
---|
| 270 | "(Ljava/lang/Object;Ljava/lang/Object;)" |
---|
| 271 | "Ljava/lang/Object;"); |
---|
| 272 | #else |
---|
[1] | 273 | put_mid = (*env)->GetMethodID(env,scHashMapClass, "put", |
---|
| 274 | "(Ljava/lang/Object;Ljava/lang/Object;)" |
---|
| 275 | "Ljava/lang/Object;"); |
---|
[364] | 276 | #endif |
---|
[1] | 277 | maps* tmp=t; |
---|
| 278 | while(tmp!=NULL){ |
---|
[360] | 279 | map* tmap=getMapType(tmp->content); |
---|
[1] | 280 | map* tmp1=tmp->content; |
---|
[364] | 281 | #ifdef JAVA7 |
---|
| 282 | scObject1 = (*env).NewObject(scHashMap_class, scHashMap_constructor); |
---|
| 283 | #else |
---|
[1] | 284 | scObject1 = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor); |
---|
[364] | 285 | #endif |
---|
[68] | 286 | map* sizeV=getMap(tmp1,"size"); |
---|
[360] | 287 | map* isArray=getMap(tmp1,"isArray"); |
---|
| 288 | map* alen=getMap(tmp1,"length"); |
---|
[1] | 289 | while(tmp1!=NULL){ |
---|
[360] | 290 | if(strcmp(tmp1->name,"value")==0){ |
---|
| 291 | if(isArray==NULL){ |
---|
| 292 | if(sizeV!=NULL && strcmp(tmp1->name,"value")==0){ |
---|
[364] | 293 | #ifdef JAVA7 |
---|
| 294 | jbyteArray tmpData=(*env).NewByteArray(atoi(sizeV->value)); |
---|
| 295 | (*env).SetByteArrayRegion(tmpData,0,atoi(sizeV->value),(const jbyte *)tmp1->value); |
---|
| 296 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), tmpData); |
---|
| 297 | #else |
---|
[360] | 298 | jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sizeV->value)); |
---|
| 299 | (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sizeV->value),tmp1->value); |
---|
| 300 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), tmpData); |
---|
[364] | 301 | #endif |
---|
[360] | 302 | }else |
---|
[364] | 303 | #ifdef JAVA7 |
---|
| 304 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value)); |
---|
| 305 | #else |
---|
[360] | 306 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value)); |
---|
[364] | 307 | #endif |
---|
[360] | 308 | } |
---|
| 309 | else{ |
---|
| 310 | int alen1=atoi(alen->value); |
---|
| 311 | fprintf(stderr,"LENGTH %d \n",alen1); |
---|
| 312 | |
---|
| 313 | jclass scArrayListClass,scArrayList_class; |
---|
| 314 | jmethodID scArrayList_constructor; |
---|
[388] | 315 | jobject scObject2; |
---|
[364] | 316 | #ifdef JAVA7 |
---|
| 317 | scArrayListClass = (*env).FindClass("java/util/ArrayList"); |
---|
| 318 | scArrayList_class = (jclass)(*env).NewGlobalRef(scArrayListClass); |
---|
| 319 | scArrayList_constructor = (*env).GetMethodID(scArrayList_class, "<init>", "()V"); |
---|
| 320 | jmethodID add_mid = 0; |
---|
| 321 | scObject2 = (*env).NewObject(scArrayList_class, scArrayList_constructor); |
---|
| 322 | |
---|
| 323 | add_mid = (*env).GetMethodID(scArrayListClass, |
---|
| 324 | "add","(Ljava/lang/Object;)Z"); |
---|
| 325 | #else |
---|
[360] | 326 | scArrayListClass = (*env)->FindClass(env, "java/util/ArrayList"); |
---|
| 327 | scArrayList_class = (*env)->NewGlobalRef(env, scArrayListClass); |
---|
| 328 | scArrayList_constructor = (*env)->GetMethodID(env, scArrayList_class, "<init>", "()V"); |
---|
| 329 | jmethodID add_mid = 0; |
---|
| 330 | scObject2 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor); |
---|
| 331 | |
---|
| 332 | add_mid = (*env)->GetMethodID(env,scArrayListClass, |
---|
| 333 | "add","(Ljava/lang/Object;)Z"); |
---|
[364] | 334 | #endif |
---|
[360] | 335 | int i; |
---|
| 336 | |
---|
| 337 | for(i=0;i<alen1;i++){ |
---|
| 338 | map* vMap=getMapArray(tmp->content,"value",i); |
---|
| 339 | map* sMap=getMapArray(tmp->content,"size",i); |
---|
| 340 | map* mMap=getMapArray(tmp->content,tmap->value,i); |
---|
| 341 | |
---|
| 342 | if(sMap!=NULL && vMap!=NULL && strncmp(vMap->name,"value",5)==0){ |
---|
[364] | 343 | #ifdef JAVA7 |
---|
| 344 | jbyteArray tmpData=(*env).NewByteArray(atoi(sMap->value)); |
---|
| 345 | (*env).SetByteArrayRegion(tmpData,0,atoi(sMap->value),(const jbyte *)vMap->value); |
---|
| 346 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
| 347 | #else |
---|
[360] | 348 | jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sMap->value)); |
---|
| 349 | (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sMap->value),vMap->value); |
---|
| 350 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
[364] | 351 | #endif |
---|
[360] | 352 | }else{ |
---|
[364] | 353 | #ifdef JAVA7 |
---|
| 354 | jobject tmpData=(*env).NewStringUTF(vMap->value); |
---|
| 355 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
| 356 | #else |
---|
[360] | 357 | jobject tmpData=(*env)->NewStringUTF(env,vMap->value); |
---|
| 358 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
[364] | 359 | #endif |
---|
[360] | 360 | } |
---|
| 361 | |
---|
| 362 | } |
---|
[364] | 363 | |
---|
| 364 | #ifdef JAVA7 |
---|
| 365 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), scObject2); |
---|
| 366 | #else |
---|
[360] | 367 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), scObject2); |
---|
[364] | 368 | #endif |
---|
| 369 | |
---|
[360] | 370 | } |
---|
| 371 | } |
---|
| 372 | else |
---|
[364] | 373 | #ifdef JAVA7 |
---|
| 374 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value)); |
---|
| 375 | #else |
---|
[68] | 376 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value)); |
---|
[364] | 377 | #endif |
---|
[1] | 378 | tmp1=tmp1->next; |
---|
| 379 | } |
---|
[364] | 380 | #ifdef JAVA7 |
---|
| 381 | (*env).CallObjectMethod(scObject, put_mid, (*env).NewStringUTF(tmp->name), scObject1); |
---|
| 382 | #else |
---|
[1] | 383 | (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1); |
---|
[364] | 384 | #endif |
---|
[1] | 385 | tmp=tmp->next; |
---|
[57] | 386 | } |
---|
[1] | 387 | return scObject; |
---|
| 388 | } |
---|
| 389 | else |
---|
| 390 | return NULL; |
---|
| 391 | } |
---|
| 392 | |
---|
[57] | 393 | maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){ |
---|
[1] | 394 | #ifdef DEBUG |
---|
| 395 | fprintf(stderr,"mapsFromHashMap start\n"); |
---|
| 396 | #endif |
---|
| 397 | /** |
---|
| 398 | * What need to be done (in java). |
---|
| 399 | * Set set = hm.entrySet(); |
---|
| 400 | * Iterator i = set.iterator(); |
---|
| 401 | * while(i.hasNext()){ |
---|
| 402 | * Map.Entry me = (Map.Entry)i.next(); |
---|
| 403 | * System.out.println(me.getKey() + " : " + me.getValue() ); |
---|
| 404 | * } |
---|
| 405 | */ |
---|
[57] | 406 | jclass scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass; |
---|
[68] | 407 | jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid; |
---|
[1] | 408 | jobject scObject,scObject1; |
---|
| 409 | if(scHashMapClass==NULL){ |
---|
[114] | 410 | #ifdef DEBUG |
---|
[1] | 411 | fprintf(stderr,"Unable to load java.util.HashMap\n"); |
---|
[114] | 412 | #endif |
---|
[1] | 413 | return NULL; |
---|
| 414 | } |
---|
[364] | 415 | #ifdef JAVA7 |
---|
| 416 | entrySet_mid = (*env).GetMethodID(scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
| 417 | containsKey_mid = (*env).GetMethodID(scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
| 418 | get_mid = (*env).GetMethodID(scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
| 419 | #else |
---|
[1] | 420 | entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
[68] | 421 | containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
| 422 | get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
[364] | 423 | #endif |
---|
[114] | 424 | |
---|
[68] | 425 | if(containsKey_mid==0){ |
---|
[114] | 426 | #ifdef DEBUG |
---|
[68] | 427 | fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid); |
---|
[114] | 428 | #endif |
---|
| 429 | return NULL; |
---|
[68] | 430 | } |
---|
| 431 | if(get_mid==0){ |
---|
[114] | 432 | #ifdef DEBUG |
---|
[68] | 433 | fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid); |
---|
[114] | 434 | #endif |
---|
| 435 | return NULL; |
---|
[68] | 436 | } |
---|
[1] | 437 | if(entrySet_mid==0){ |
---|
[114] | 438 | #ifdef DEBUG |
---|
[1] | 439 | fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid); |
---|
[114] | 440 | #endif |
---|
[1] | 441 | return NULL; |
---|
| 442 | } |
---|
| 443 | #ifdef DEBUG |
---|
| 444 | else |
---|
| 445 | fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid); |
---|
| 446 | #endif |
---|
| 447 | |
---|
[364] | 448 | #ifdef JAVA7 |
---|
| 449 | scSetClass = (*env).FindClass("java/util/Set"); |
---|
| 450 | iterator_mid = (*env).GetMethodID(scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
| 451 | #else |
---|
[1] | 452 | scSetClass = (*env)->FindClass(env, "java/util/Set"); |
---|
| 453 | iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
[364] | 454 | #endif |
---|
[1] | 455 | #ifdef DEBUG |
---|
| 456 | fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid); |
---|
| 457 | #endif |
---|
| 458 | |
---|
[364] | 459 | #ifdef JAVA7 |
---|
| 460 | scIteratorClass = (*env).FindClass("java/util/Iterator"); |
---|
| 461 | hasNext_mid = (*env).GetMethodID(scIteratorClass, "hasNext", "()Z"); |
---|
| 462 | #else |
---|
[1] | 463 | scIteratorClass = (*env)->FindClass(env, "java/util/Iterator"); |
---|
| 464 | hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z"); |
---|
[364] | 465 | #endif |
---|
| 466 | |
---|
[1] | 467 | #ifdef DEBUG |
---|
| 468 | fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid); |
---|
| 469 | #endif |
---|
[364] | 470 | |
---|
| 471 | #ifdef JAVA7 |
---|
| 472 | next_mid = (*env).GetMethodID(scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
| 473 | #else |
---|
[1] | 474 | next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
[364] | 475 | #endif |
---|
[1] | 476 | #ifdef DEBUG |
---|
| 477 | fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid); |
---|
| 478 | #endif |
---|
| 479 | |
---|
[364] | 480 | #ifdef JAVA7 |
---|
| 481 | scMapEntryClass = (*env).FindClass("java/util/Map$Entry"); |
---|
| 482 | getKey_mid = (*env).GetMethodID(scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
| 483 | #else |
---|
[1] | 484 | scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry"); |
---|
| 485 | getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
[364] | 486 | #endif |
---|
[1] | 487 | #ifdef DEBUG |
---|
| 488 | fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid); |
---|
| 489 | #endif |
---|
[364] | 490 | #ifdef JAVA7 |
---|
| 491 | getValue_mid = (*env).GetMethodID(scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
| 492 | #else |
---|
[1] | 493 | getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
[364] | 494 | #endif |
---|
[1] | 495 | #ifdef DEBUG |
---|
| 496 | fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid); |
---|
| 497 | #endif |
---|
| 498 | |
---|
[364] | 499 | #ifdef JAVA7 |
---|
| 500 | jobject final_set=(*env).CallObjectMethod(t,entrySet_mid); |
---|
| 501 | jobject final_iterator=(*env).CallObjectMethod(final_set,iterator_mid); |
---|
| 502 | #else |
---|
[1] | 503 | jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid); |
---|
| 504 | jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid); |
---|
[364] | 505 | #endif |
---|
[1] | 506 | |
---|
| 507 | maps* final_res=NULL; |
---|
| 508 | map* res=NULL; |
---|
[364] | 509 | #ifdef JAVA7 |
---|
| 510 | while((*env).CallBooleanMethod(final_iterator,hasNext_mid)){ |
---|
| 511 | jobject tmp=(*env).CallObjectMethod(final_iterator,next_mid); |
---|
| 512 | |
---|
| 513 | jobject imap=(*env).CallObjectMethod(tmp,getValue_mid); |
---|
| 514 | jobject set=(*env).CallObjectMethod(imap,entrySet_mid); |
---|
| 515 | jobject iterator=(*env).CallObjectMethod(set,iterator_mid); |
---|
| 516 | |
---|
| 517 | #else |
---|
[1] | 518 | while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){ |
---|
| 519 | jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid); |
---|
| 520 | |
---|
| 521 | jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid); |
---|
| 522 | jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid); |
---|
| 523 | jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid); |
---|
[57] | 524 | |
---|
[364] | 525 | #endif |
---|
[68] | 526 | int size=-1; |
---|
[364] | 527 | #ifdef JAVA7 |
---|
| 528 | if((*env).CallBooleanMethod(imap,containsKey_mid,(*env).NewStringUTF("size"))){ |
---|
| 529 | jobject sizeV=(*env).CallObjectMethod(imap, get_mid,(*env).NewStringUTF("size")); |
---|
| 530 | const char* sizeVS=(*env).GetStringUTFChars((jstring)sizeV, NULL); |
---|
| 531 | #else |
---|
[68] | 532 | if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){ |
---|
| 533 | jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size")); |
---|
[114] | 534 | const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL); |
---|
[364] | 535 | #endif |
---|
[68] | 536 | size=atoi(sizeVS); |
---|
| 537 | fprintf(stderr,"SIZE : %s\n",sizeVS); |
---|
[364] | 538 | #ifdef JAVA7 |
---|
| 539 | (*env).ReleaseStringUTFChars((jstring)sizeV, sizeVS); |
---|
| 540 | #else |
---|
[114] | 541 | (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS); |
---|
[364] | 542 | #endif |
---|
[68] | 543 | } |
---|
| 544 | |
---|
[364] | 545 | #ifdef JAVA7 |
---|
| 546 | while((*env).CallBooleanMethod(iterator,hasNext_mid)){ |
---|
| 547 | jobject tmp1=(*env).CallObjectMethod(iterator,next_mid); |
---|
| 548 | jobject jk=(*env).CallObjectMethod(tmp1,getKey_mid); |
---|
| 549 | jobject jv=(*env).CallObjectMethod(tmp1,getValue_mid); |
---|
| 550 | |
---|
| 551 | const char* jkd=(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
| 552 | #else |
---|
[1] | 553 | while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){ |
---|
| 554 | jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid); |
---|
| 555 | jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid); |
---|
| 556 | jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid); |
---|
| 557 | |
---|
[114] | 558 | const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
[364] | 559 | #endif |
---|
[68] | 560 | if(size>=0 && strcmp(jkd,"value")==0){ |
---|
[364] | 561 | #ifdef JAVA7 |
---|
| 562 | jobject value=(jobject)(*env).GetByteArrayElements((jbyteArray)jv, NULL); |
---|
| 563 | #else |
---|
[68] | 564 | jobject value=(*env)->GetByteArrayElements(env, jv, NULL); |
---|
[364] | 565 | #endif |
---|
[68] | 566 | if(res==NULL){ |
---|
| 567 | res=createMap(jkd,""); |
---|
| 568 | }else{ |
---|
| 569 | addToMap(res,jkd,""); |
---|
| 570 | } |
---|
| 571 | map* tmpR=getMap(res,"value"); |
---|
| 572 | free(tmpR->value); |
---|
| 573 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
| 574 | memmove(tmpR->value,value,size*sizeof(char)); |
---|
| 575 | tmpR->value[size]=0; |
---|
[114] | 576 | char tmp[128]; |
---|
| 577 | sprintf(tmp,"%d",size); |
---|
| 578 | addToMap(res,"size",tmp); |
---|
[68] | 579 | } |
---|
| 580 | else{ |
---|
[364] | 581 | #ifdef JAVA7 |
---|
| 582 | const char* jvd=(*env).GetStringUTFChars((jstring)jv, NULL); |
---|
| 583 | #else |
---|
[114] | 584 | const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL); |
---|
[364] | 585 | #endif |
---|
[68] | 586 | if(res==NULL){ |
---|
| 587 | res=createMap(jkd,jvd); |
---|
| 588 | }else{ |
---|
| 589 | addToMap(res,jkd,jvd); |
---|
| 590 | } |
---|
[364] | 591 | #ifdef JAVA7 |
---|
| 592 | (*env).ReleaseStringUTFChars((jstring)jv, jvd); |
---|
| 593 | #else |
---|
[114] | 594 | (*env)->ReleaseStringUTFChars(env, jv, jvd); |
---|
[364] | 595 | #endif |
---|
[68] | 596 | } |
---|
[59] | 597 | |
---|
[364] | 598 | #ifdef JAVA7 |
---|
| 599 | (*env).ReleaseStringUTFChars((jstring)jk, jkd); |
---|
| 600 | #else |
---|
[114] | 601 | (*env)->ReleaseStringUTFChars(env, jk, jkd); |
---|
[364] | 602 | #endif |
---|
[59] | 603 | |
---|
[1] | 604 | } |
---|
[364] | 605 | #ifdef JAVA7 |
---|
| 606 | jobject jk=(*env).CallObjectMethod(tmp,getKey_mid); |
---|
| 607 | #else |
---|
[1] | 608 | jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid); |
---|
[364] | 609 | #endif |
---|
[1] | 610 | maps* cmap=(maps*)malloc(sizeof(maps)); |
---|
[364] | 611 | #ifdef JAVA7 |
---|
| 612 | cmap->name=(char*)(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
| 613 | #else |
---|
[1] | 614 | cmap->name=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
[364] | 615 | #endif |
---|
[57] | 616 | #ifdef DEBUG |
---|
| 617 | fprintf(stderr," / %s \n",cmap->name); |
---|
| 618 | #endif |
---|
[1] | 619 | cmap->content=res; |
---|
| 620 | cmap->next=NULL; |
---|
[57] | 621 | if(final_res==NULL) |
---|
[9] | 622 | final_res=dupMaps(&cmap); |
---|
[57] | 623 | else |
---|
[9] | 624 | addMapsToMaps(&final_res,cmap); |
---|
| 625 | freeMaps(&cmap); |
---|
| 626 | free(cmap); |
---|
| 627 | cmap=NULL; |
---|
[1] | 628 | res=NULL; |
---|
| 629 | } |
---|
| 630 | #ifdef DEBUG |
---|
| 631 | fprintf(stderr,"mapsFromHashMap end\n"); |
---|
| 632 | #endif |
---|
| 633 | |
---|
| 634 | return final_res; |
---|
| 635 | } |
---|