[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+"); |
---|
[527] | 234 | if(new_stdout==NULL){ |
---|
| 235 | map* err=createMap("text","Unable to run your service, no debug informations can be provided (please verify privileges on tmpPath)"); |
---|
| 236 | addToMap(err,"code","InternalError"); |
---|
| 237 | printExceptionReportResponse(main_conf,err); |
---|
| 238 | freeMap(&err); |
---|
| 239 | free(err); |
---|
| 240 | return; |
---|
| 241 | } |
---|
[388] | 242 | fflush(stderr); |
---|
| 243 | dup2(fileno(new_stdout),fileno(stderr)); |
---|
| 244 | fprintf(stderr,"Unable to run your java process properly: "); |
---|
| 245 | fflush(stderr); |
---|
| 246 | #ifdef JAVA7 |
---|
| 247 | (*env).ExceptionDescribe(); |
---|
| 248 | #else |
---|
| 249 | (*env)->ExceptionDescribe(env); |
---|
| 250 | #endif |
---|
| 251 | fflush(new_stdout); |
---|
| 252 | fseek(new_stdout, 0, SEEK_END); |
---|
| 253 | long flen=ftell(new_stdout); |
---|
| 254 | fseek(new_stdout, 0, SEEK_SET); |
---|
| 255 | char *tmps1=(char*)malloc((flen+1)*sizeof(char)); |
---|
| 256 | fread(tmps1,flen,1,new_stdout); |
---|
| 257 | fclose(new_stdout); |
---|
| 258 | tmps1[flen]=0; |
---|
| 259 | map* err=createMap("text",tmps1); |
---|
| 260 | addToMap(err,"code","InternalError"); |
---|
| 261 | printExceptionReportResponse(main_conf,err); |
---|
| 262 | freeMap(&err); |
---|
| 263 | free(err); |
---|
| 264 | } |
---|
| 265 | |
---|
[57] | 266 | jobject HashMap_FromMaps(JNIEnv *env,maps* t,jclass scHashMapClass,jclass scHashMap_class,jmethodID scHashMap_constructor){ |
---|
[1] | 267 | jobject scObject,scObject1; |
---|
| 268 | if(scHashMap_constructor!=NULL){ |
---|
[364] | 269 | #ifdef JAVA7 |
---|
| 270 | scObject = (*env).NewObject(scHashMap_class, scHashMap_constructor); |
---|
| 271 | #else |
---|
[1] | 272 | scObject = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor); |
---|
[364] | 273 | #endif |
---|
[1] | 274 | jmethodID put_mid = 0; |
---|
| 275 | |
---|
[364] | 276 | #ifdef JAVA7 |
---|
| 277 | put_mid = (*env).GetMethodID(scHashMapClass, "put", |
---|
| 278 | "(Ljava/lang/Object;Ljava/lang/Object;)" |
---|
| 279 | "Ljava/lang/Object;"); |
---|
| 280 | #else |
---|
[1] | 281 | put_mid = (*env)->GetMethodID(env,scHashMapClass, "put", |
---|
| 282 | "(Ljava/lang/Object;Ljava/lang/Object;)" |
---|
| 283 | "Ljava/lang/Object;"); |
---|
[364] | 284 | #endif |
---|
[1] | 285 | maps* tmp=t; |
---|
| 286 | while(tmp!=NULL){ |
---|
[360] | 287 | map* tmap=getMapType(tmp->content); |
---|
[1] | 288 | map* tmp1=tmp->content; |
---|
[364] | 289 | #ifdef JAVA7 |
---|
| 290 | scObject1 = (*env).NewObject(scHashMap_class, scHashMap_constructor); |
---|
| 291 | #else |
---|
[1] | 292 | scObject1 = (*env)->NewObject(env, scHashMap_class, scHashMap_constructor); |
---|
[364] | 293 | #endif |
---|
[68] | 294 | map* sizeV=getMap(tmp1,"size"); |
---|
[360] | 295 | map* isArray=getMap(tmp1,"isArray"); |
---|
| 296 | map* alen=getMap(tmp1,"length"); |
---|
[1] | 297 | while(tmp1!=NULL){ |
---|
[360] | 298 | if(strcmp(tmp1->name,"value")==0){ |
---|
| 299 | if(isArray==NULL){ |
---|
| 300 | if(sizeV!=NULL && strcmp(tmp1->name,"value")==0){ |
---|
[364] | 301 | #ifdef JAVA7 |
---|
| 302 | jbyteArray tmpData=(*env).NewByteArray(atoi(sizeV->value)); |
---|
| 303 | (*env).SetByteArrayRegion(tmpData,0,atoi(sizeV->value),(const jbyte *)tmp1->value); |
---|
| 304 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), tmpData); |
---|
| 305 | #else |
---|
[360] | 306 | jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sizeV->value)); |
---|
| 307 | (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sizeV->value),tmp1->value); |
---|
| 308 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), tmpData); |
---|
[364] | 309 | #endif |
---|
[360] | 310 | }else |
---|
[364] | 311 | #ifdef JAVA7 |
---|
| 312 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value)); |
---|
| 313 | #else |
---|
[360] | 314 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value)); |
---|
[364] | 315 | #endif |
---|
[360] | 316 | } |
---|
| 317 | else{ |
---|
| 318 | int alen1=atoi(alen->value); |
---|
| 319 | fprintf(stderr,"LENGTH %d \n",alen1); |
---|
| 320 | |
---|
| 321 | jclass scArrayListClass,scArrayList_class; |
---|
| 322 | jmethodID scArrayList_constructor; |
---|
[388] | 323 | jobject scObject2; |
---|
[364] | 324 | #ifdef JAVA7 |
---|
| 325 | scArrayListClass = (*env).FindClass("java/util/ArrayList"); |
---|
| 326 | scArrayList_class = (jclass)(*env).NewGlobalRef(scArrayListClass); |
---|
| 327 | scArrayList_constructor = (*env).GetMethodID(scArrayList_class, "<init>", "()V"); |
---|
| 328 | jmethodID add_mid = 0; |
---|
| 329 | scObject2 = (*env).NewObject(scArrayList_class, scArrayList_constructor); |
---|
| 330 | |
---|
| 331 | add_mid = (*env).GetMethodID(scArrayListClass, |
---|
| 332 | "add","(Ljava/lang/Object;)Z"); |
---|
| 333 | #else |
---|
[360] | 334 | scArrayListClass = (*env)->FindClass(env, "java/util/ArrayList"); |
---|
| 335 | scArrayList_class = (*env)->NewGlobalRef(env, scArrayListClass); |
---|
| 336 | scArrayList_constructor = (*env)->GetMethodID(env, scArrayList_class, "<init>", "()V"); |
---|
| 337 | jmethodID add_mid = 0; |
---|
| 338 | scObject2 = (*env)->NewObject(env, scArrayList_class, scArrayList_constructor); |
---|
| 339 | |
---|
| 340 | add_mid = (*env)->GetMethodID(env,scArrayListClass, |
---|
| 341 | "add","(Ljava/lang/Object;)Z"); |
---|
[364] | 342 | #endif |
---|
[360] | 343 | int i; |
---|
| 344 | |
---|
| 345 | for(i=0;i<alen1;i++){ |
---|
| 346 | map* vMap=getMapArray(tmp->content,"value",i); |
---|
| 347 | map* sMap=getMapArray(tmp->content,"size",i); |
---|
| 348 | map* mMap=getMapArray(tmp->content,tmap->value,i); |
---|
| 349 | |
---|
| 350 | if(sMap!=NULL && vMap!=NULL && strncmp(vMap->name,"value",5)==0){ |
---|
[364] | 351 | #ifdef JAVA7 |
---|
| 352 | jbyteArray tmpData=(*env).NewByteArray(atoi(sMap->value)); |
---|
| 353 | (*env).SetByteArrayRegion(tmpData,0,atoi(sMap->value),(const jbyte *)vMap->value); |
---|
| 354 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
| 355 | #else |
---|
[360] | 356 | jbyteArray tmpData=(*env)->NewByteArray(env,atoi(sMap->value)); |
---|
| 357 | (*env)->SetByteArrayRegion(env,tmpData,0,atoi(sMap->value),vMap->value); |
---|
| 358 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
[364] | 359 | #endif |
---|
[360] | 360 | }else{ |
---|
[364] | 361 | #ifdef JAVA7 |
---|
| 362 | jobject tmpData=(*env).NewStringUTF(vMap->value); |
---|
| 363 | (*env).CallObjectMethod(scObject2, add_mid, tmpData); |
---|
| 364 | #else |
---|
[360] | 365 | jobject tmpData=(*env)->NewStringUTF(env,vMap->value); |
---|
| 366 | (*env)->CallObjectMethod(env,scObject2, add_mid, tmpData); |
---|
[364] | 367 | #endif |
---|
[360] | 368 | } |
---|
| 369 | |
---|
| 370 | } |
---|
[364] | 371 | |
---|
| 372 | #ifdef JAVA7 |
---|
| 373 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), scObject2); |
---|
| 374 | #else |
---|
[360] | 375 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), scObject2); |
---|
[364] | 376 | #endif |
---|
| 377 | |
---|
[360] | 378 | } |
---|
| 379 | } |
---|
| 380 | else |
---|
[364] | 381 | #ifdef JAVA7 |
---|
| 382 | (*env).CallObjectMethod(scObject1, put_mid, (*env).NewStringUTF(tmp1->name), (*env).NewStringUTF(tmp1->value)); |
---|
| 383 | #else |
---|
[68] | 384 | (*env)->CallObjectMethod(env,scObject1, put_mid, (*env)->NewStringUTF(env,tmp1->name), (*env)->NewStringUTF(env,tmp1->value)); |
---|
[364] | 385 | #endif |
---|
[1] | 386 | tmp1=tmp1->next; |
---|
| 387 | } |
---|
[364] | 388 | #ifdef JAVA7 |
---|
| 389 | (*env).CallObjectMethod(scObject, put_mid, (*env).NewStringUTF(tmp->name), scObject1); |
---|
| 390 | #else |
---|
[1] | 391 | (*env)->CallObjectMethod(env,scObject, put_mid, (*env)->NewStringUTF(env,tmp->name), scObject1); |
---|
[364] | 392 | #endif |
---|
[1] | 393 | tmp=tmp->next; |
---|
[57] | 394 | } |
---|
[1] | 395 | return scObject; |
---|
| 396 | } |
---|
| 397 | else |
---|
| 398 | return NULL; |
---|
| 399 | } |
---|
| 400 | |
---|
[57] | 401 | maps* mapsFromHashMap(JNIEnv *env,jobject t,jclass scHashMapClass){ |
---|
[1] | 402 | #ifdef DEBUG |
---|
| 403 | fprintf(stderr,"mapsFromHashMap start\n"); |
---|
| 404 | #endif |
---|
| 405 | /** |
---|
| 406 | * What need to be done (in java). |
---|
| 407 | * Set set = hm.entrySet(); |
---|
| 408 | * Iterator i = set.iterator(); |
---|
| 409 | * while(i.hasNext()){ |
---|
| 410 | * Map.Entry me = (Map.Entry)i.next(); |
---|
| 411 | * System.out.println(me.getKey() + " : " + me.getValue() ); |
---|
| 412 | * } |
---|
| 413 | */ |
---|
[57] | 414 | jclass scHashMap_class,scSetClass,scIteratorClass,scMapEntryClass,scSet_class,scMapClass; |
---|
[68] | 415 | jmethodID entrySet_mid,containsKey_mid,get_mid,iterator_mid,hasNext_mid,next_mid,getKey_mid,getValue_mid; |
---|
[1] | 416 | jobject scObject,scObject1; |
---|
| 417 | if(scHashMapClass==NULL){ |
---|
[114] | 418 | #ifdef DEBUG |
---|
[1] | 419 | fprintf(stderr,"Unable to load java.util.HashMap\n"); |
---|
[114] | 420 | #endif |
---|
[1] | 421 | return NULL; |
---|
| 422 | } |
---|
[364] | 423 | #ifdef JAVA7 |
---|
| 424 | entrySet_mid = (*env).GetMethodID(scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
| 425 | containsKey_mid = (*env).GetMethodID(scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
| 426 | get_mid = (*env).GetMethodID(scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
| 427 | #else |
---|
[1] | 428 | entrySet_mid = (*env)->GetMethodID(env, scHashMapClass, "entrySet", "()Ljava/util/Set;"); |
---|
[68] | 429 | containsKey_mid = (*env)->GetMethodID(env, scHashMapClass, "containsKey", "(Ljava/lang/Object;)Z"); |
---|
| 430 | get_mid = (*env)->GetMethodID(env, scHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); |
---|
[364] | 431 | #endif |
---|
[114] | 432 | |
---|
[68] | 433 | if(containsKey_mid==0){ |
---|
[114] | 434 | #ifdef DEBUG |
---|
[68] | 435 | fprintf(stderr,"unable to load containsKey from HashMap object (%d) \n",entrySet_mid); |
---|
[114] | 436 | #endif |
---|
| 437 | return NULL; |
---|
[68] | 438 | } |
---|
| 439 | if(get_mid==0){ |
---|
[114] | 440 | #ifdef DEBUG |
---|
[68] | 441 | fprintf(stderr,"unable to load get from HashMap object (%d) \n",entrySet_mid); |
---|
[114] | 442 | #endif |
---|
| 443 | return NULL; |
---|
[68] | 444 | } |
---|
[1] | 445 | if(entrySet_mid==0){ |
---|
[114] | 446 | #ifdef DEBUG |
---|
[1] | 447 | fprintf(stderr,"unable to load entrySet from HashMap object (%d) \n",entrySet_mid); |
---|
[114] | 448 | #endif |
---|
[1] | 449 | return NULL; |
---|
| 450 | } |
---|
| 451 | #ifdef DEBUG |
---|
| 452 | else |
---|
| 453 | fprintf(stderr,"entrySet loaded from HashMap object (%d) \n",entrySet_mid); |
---|
| 454 | #endif |
---|
| 455 | |
---|
[364] | 456 | #ifdef JAVA7 |
---|
| 457 | scSetClass = (*env).FindClass("java/util/Set"); |
---|
| 458 | iterator_mid = (*env).GetMethodID(scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
| 459 | #else |
---|
[1] | 460 | scSetClass = (*env)->FindClass(env, "java/util/Set"); |
---|
| 461 | iterator_mid = (*env)->GetMethodID(env, scSetClass, "iterator", "()Ljava/util/Iterator;"); |
---|
[364] | 462 | #endif |
---|
[1] | 463 | #ifdef DEBUG |
---|
| 464 | fprintf(stderr,"mapsFromHashMap 1 (%d) \n",iterator_mid); |
---|
| 465 | #endif |
---|
| 466 | |
---|
[364] | 467 | #ifdef JAVA7 |
---|
| 468 | scIteratorClass = (*env).FindClass("java/util/Iterator"); |
---|
| 469 | hasNext_mid = (*env).GetMethodID(scIteratorClass, "hasNext", "()Z"); |
---|
| 470 | #else |
---|
[1] | 471 | scIteratorClass = (*env)->FindClass(env, "java/util/Iterator"); |
---|
| 472 | hasNext_mid = (*env)->GetMethodID(env, scIteratorClass, "hasNext", "()Z"); |
---|
[364] | 473 | #endif |
---|
| 474 | |
---|
[1] | 475 | #ifdef DEBUG |
---|
| 476 | fprintf(stderr,"mapsFromHashMap 2 (%d)\n",hasNext_mid); |
---|
| 477 | #endif |
---|
[364] | 478 | |
---|
| 479 | #ifdef JAVA7 |
---|
| 480 | next_mid = (*env).GetMethodID(scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
| 481 | #else |
---|
[1] | 482 | next_mid = (*env)->GetMethodID(env, scIteratorClass, "next", "()Ljava/lang/Object;"); |
---|
[364] | 483 | #endif |
---|
[1] | 484 | #ifdef DEBUG |
---|
| 485 | fprintf(stderr,"mapsFromHashMap 3 (%d)\n",next_mid); |
---|
| 486 | #endif |
---|
| 487 | |
---|
[364] | 488 | #ifdef JAVA7 |
---|
| 489 | scMapEntryClass = (*env).FindClass("java/util/Map$Entry"); |
---|
| 490 | getKey_mid = (*env).GetMethodID(scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
| 491 | #else |
---|
[1] | 492 | scMapEntryClass = (*env)->FindClass(env, "java/util/Map$Entry"); |
---|
| 493 | getKey_mid = (*env)->GetMethodID(env, scMapEntryClass, "getKey", "()Ljava/lang/Object;"); |
---|
[364] | 494 | #endif |
---|
[1] | 495 | #ifdef DEBUG |
---|
| 496 | fprintf(stderr,"mapsFromHashMap 4 (%d)\n",getKey_mid); |
---|
| 497 | #endif |
---|
[364] | 498 | #ifdef JAVA7 |
---|
| 499 | getValue_mid = (*env).GetMethodID(scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
| 500 | #else |
---|
[1] | 501 | getValue_mid = (*env)->GetMethodID(env, scMapEntryClass, "getValue", "()Ljava/lang/Object;"); |
---|
[364] | 502 | #endif |
---|
[1] | 503 | #ifdef DEBUG |
---|
| 504 | fprintf(stderr,"mapsFromHashMap 5 (%d)\n",getValue_mid); |
---|
| 505 | #endif |
---|
| 506 | |
---|
[364] | 507 | #ifdef JAVA7 |
---|
| 508 | jobject final_set=(*env).CallObjectMethod(t,entrySet_mid); |
---|
| 509 | jobject final_iterator=(*env).CallObjectMethod(final_set,iterator_mid); |
---|
| 510 | #else |
---|
[1] | 511 | jobject final_set=(*env)->CallObjectMethod(env,t,entrySet_mid); |
---|
| 512 | jobject final_iterator=(*env)->CallObjectMethod(env,final_set,iterator_mid); |
---|
[364] | 513 | #endif |
---|
[1] | 514 | |
---|
| 515 | maps* final_res=NULL; |
---|
| 516 | map* res=NULL; |
---|
[364] | 517 | #ifdef JAVA7 |
---|
| 518 | while((*env).CallBooleanMethod(final_iterator,hasNext_mid)){ |
---|
| 519 | jobject tmp=(*env).CallObjectMethod(final_iterator,next_mid); |
---|
| 520 | |
---|
| 521 | jobject imap=(*env).CallObjectMethod(tmp,getValue_mid); |
---|
| 522 | jobject set=(*env).CallObjectMethod(imap,entrySet_mid); |
---|
| 523 | jobject iterator=(*env).CallObjectMethod(set,iterator_mid); |
---|
| 524 | |
---|
| 525 | #else |
---|
[1] | 526 | while((*env)->CallBooleanMethod(env,final_iterator,hasNext_mid)){ |
---|
| 527 | jobject tmp=(*env)->CallObjectMethod(env,final_iterator,next_mid); |
---|
| 528 | |
---|
| 529 | jobject imap=(*env)->CallObjectMethod(env,tmp,getValue_mid); |
---|
| 530 | jobject set=(*env)->CallObjectMethod(env,imap,entrySet_mid); |
---|
| 531 | jobject iterator=(*env)->CallObjectMethod(env,set,iterator_mid); |
---|
[57] | 532 | |
---|
[364] | 533 | #endif |
---|
[68] | 534 | int size=-1; |
---|
[364] | 535 | #ifdef JAVA7 |
---|
| 536 | if((*env).CallBooleanMethod(imap,containsKey_mid,(*env).NewStringUTF("size"))){ |
---|
| 537 | jobject sizeV=(*env).CallObjectMethod(imap, get_mid,(*env).NewStringUTF("size")); |
---|
| 538 | const char* sizeVS=(*env).GetStringUTFChars((jstring)sizeV, NULL); |
---|
| 539 | #else |
---|
[68] | 540 | if((*env)->CallBooleanMethod(env,imap,containsKey_mid,(*env)->NewStringUTF(env,"size"))){ |
---|
| 541 | jobject sizeV=(*env)->CallObjectMethod(env, imap, get_mid,(*env)->NewStringUTF(env,"size")); |
---|
[114] | 542 | const char* sizeVS=(*env)->GetStringUTFChars(env, sizeV, NULL); |
---|
[364] | 543 | #endif |
---|
[68] | 544 | size=atoi(sizeVS); |
---|
| 545 | fprintf(stderr,"SIZE : %s\n",sizeVS); |
---|
[364] | 546 | #ifdef JAVA7 |
---|
| 547 | (*env).ReleaseStringUTFChars((jstring)sizeV, sizeVS); |
---|
| 548 | #else |
---|
[114] | 549 | (*env)->ReleaseStringUTFChars(env, sizeV, sizeVS); |
---|
[364] | 550 | #endif |
---|
[68] | 551 | } |
---|
| 552 | |
---|
[364] | 553 | #ifdef JAVA7 |
---|
| 554 | while((*env).CallBooleanMethod(iterator,hasNext_mid)){ |
---|
| 555 | jobject tmp1=(*env).CallObjectMethod(iterator,next_mid); |
---|
| 556 | jobject jk=(*env).CallObjectMethod(tmp1,getKey_mid); |
---|
| 557 | jobject jv=(*env).CallObjectMethod(tmp1,getValue_mid); |
---|
| 558 | |
---|
| 559 | const char* jkd=(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
| 560 | #else |
---|
[1] | 561 | while((*env)->CallBooleanMethod(env,iterator,hasNext_mid)){ |
---|
| 562 | jobject tmp1=(*env)->CallObjectMethod(env,iterator,next_mid); |
---|
| 563 | jobject jk=(*env)->CallObjectMethod(env,tmp1,getKey_mid); |
---|
| 564 | jobject jv=(*env)->CallObjectMethod(env,tmp1,getValue_mid); |
---|
| 565 | |
---|
[114] | 566 | const char* jkd=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
[364] | 567 | #endif |
---|
[68] | 568 | if(size>=0 && strcmp(jkd,"value")==0){ |
---|
[364] | 569 | #ifdef JAVA7 |
---|
| 570 | jobject value=(jobject)(*env).GetByteArrayElements((jbyteArray)jv, NULL); |
---|
| 571 | #else |
---|
[68] | 572 | jobject value=(*env)->GetByteArrayElements(env, jv, NULL); |
---|
[364] | 573 | #endif |
---|
[68] | 574 | if(res==NULL){ |
---|
| 575 | res=createMap(jkd,""); |
---|
| 576 | }else{ |
---|
| 577 | addToMap(res,jkd,""); |
---|
| 578 | } |
---|
| 579 | map* tmpR=getMap(res,"value"); |
---|
| 580 | free(tmpR->value); |
---|
| 581 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
| 582 | memmove(tmpR->value,value,size*sizeof(char)); |
---|
| 583 | tmpR->value[size]=0; |
---|
[114] | 584 | char tmp[128]; |
---|
| 585 | sprintf(tmp,"%d",size); |
---|
| 586 | addToMap(res,"size",tmp); |
---|
[68] | 587 | } |
---|
| 588 | else{ |
---|
[364] | 589 | #ifdef JAVA7 |
---|
| 590 | const char* jvd=(*env).GetStringUTFChars((jstring)jv, NULL); |
---|
| 591 | #else |
---|
[114] | 592 | const char* jvd=(*env)->GetStringUTFChars(env, jv, NULL); |
---|
[364] | 593 | #endif |
---|
[68] | 594 | if(res==NULL){ |
---|
| 595 | res=createMap(jkd,jvd); |
---|
| 596 | }else{ |
---|
| 597 | addToMap(res,jkd,jvd); |
---|
| 598 | } |
---|
[364] | 599 | #ifdef JAVA7 |
---|
| 600 | (*env).ReleaseStringUTFChars((jstring)jv, jvd); |
---|
| 601 | #else |
---|
[114] | 602 | (*env)->ReleaseStringUTFChars(env, jv, jvd); |
---|
[364] | 603 | #endif |
---|
[68] | 604 | } |
---|
[59] | 605 | |
---|
[364] | 606 | #ifdef JAVA7 |
---|
| 607 | (*env).ReleaseStringUTFChars((jstring)jk, jkd); |
---|
| 608 | #else |
---|
[114] | 609 | (*env)->ReleaseStringUTFChars(env, jk, jkd); |
---|
[364] | 610 | #endif |
---|
[59] | 611 | |
---|
[1] | 612 | } |
---|
[364] | 613 | #ifdef JAVA7 |
---|
| 614 | jobject jk=(*env).CallObjectMethod(tmp,getKey_mid); |
---|
| 615 | #else |
---|
[1] | 616 | jobject jk=(*env)->CallObjectMethod(env,tmp,getKey_mid); |
---|
[364] | 617 | #endif |
---|
[1] | 618 | maps* cmap=(maps*)malloc(sizeof(maps)); |
---|
[364] | 619 | #ifdef JAVA7 |
---|
| 620 | cmap->name=(char*)(*env).GetStringUTFChars((jstring)jk, NULL); |
---|
| 621 | #else |
---|
[1] | 622 | cmap->name=(*env)->GetStringUTFChars(env, jk, NULL); |
---|
[364] | 623 | #endif |
---|
[57] | 624 | #ifdef DEBUG |
---|
| 625 | fprintf(stderr," / %s \n",cmap->name); |
---|
| 626 | #endif |
---|
[1] | 627 | cmap->content=res; |
---|
| 628 | cmap->next=NULL; |
---|
[57] | 629 | if(final_res==NULL) |
---|
[9] | 630 | final_res=dupMaps(&cmap); |
---|
[57] | 631 | else |
---|
[9] | 632 | addMapsToMaps(&final_res,cmap); |
---|
| 633 | freeMaps(&cmap); |
---|
| 634 | free(cmap); |
---|
| 635 | cmap=NULL; |
---|
[1] | 636 | res=NULL; |
---|
| 637 | } |
---|
| 638 | #ifdef DEBUG |
---|
| 639 | fprintf(stderr,"mapsFromHashMap end\n"); |
---|
| 640 | #endif |
---|
| 641 | |
---|
| 642 | return final_res; |
---|
| 643 | } |
---|
[527] | 644 | |
---|