1 25 26 package org.objectweb.jonas_ejb.genic; 27 28 import java.lang.reflect.Method ; 29 import java.util.ArrayList ; 30 import java.util.Date ; 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import java.util.Vector ; 34 35 import org.apache.velocity.VelocityContext; 36 37 import org.objectweb.jonas_ejb.deployment.api.BeanDesc; 38 import org.objectweb.jonas_ejb.deployment.api.EjbRelationshipRoleDesc; 39 import org.objectweb.jonas_ejb.deployment.api.EntityCmp1Desc; 40 import org.objectweb.jonas_ejb.deployment.api.EntityCmp2Desc; 41 import org.objectweb.jonas_ejb.deployment.api.EntityDesc; 42 import org.objectweb.jonas_ejb.deployment.api.FieldDesc; 43 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc; 44 import org.objectweb.jonas_ejb.lib.BeanNaming; 45 import org.objectweb.jonas_ejb.lib.JavaType; 46 47 import org.objectweb.jonas_lib.version.Version; 48 49 50 56 57 public class VContextFactory { 58 59 69 public static VelocityContext create(BeanDesc dd, int srcType) throws GenICException { 70 71 String value = null; 72 VelocityContext vc = new VelocityContext(); 73 74 vc.put("dd", dd); 75 vc.put("EntityCMP2", (dd instanceof EntityCmp2Desc ? Boolean.TRUE : Boolean.FALSE)); 76 77 vc.put("jVersion", Version.NUMBER); 78 79 if (dd.getHomeClass() != null) { 80 value = dd.getHomeClass().getName(); 81 vc.put("home", value); 82 } 83 84 if (dd.getLocalHomeClass() != null) { 85 value = dd.getLocalHomeClass().getName(); 86 vc.put("localhome", value); 87 } 88 89 if (dd.getRemoteClass() != null) { 90 value = dd.getRemoteClass().getName(); 91 vc.put("remote", value); 92 } 93 94 if (dd.getLocalClass() != null) { 95 value = dd.getLocalClass().getName(); 96 vc.put("local", value); 97 } 98 99 if (dd instanceof SessionStatelessDesc) { 100 SessionStatelessDesc ssd = (SessionStatelessDesc) dd; 101 if (ssd.getServiceEndpointClass() != null) { 102 value = ssd.getServiceEndpointClass().getName(); 103 vc.put("serviceEndpoint", value); 104 } 105 if (ssd.getFullWrpServiceEndpointName() != null) { 106 value = ssd.getFullWrpServiceEndpointName(); 107 vc.put("service-endpoint_wrp", value); 108 } 109 if (ssd.getFullWrpSEHomeName() != null) { 110 value = ssd.getFullWrpSEHomeName(); 111 vc.put("service-endpoint-home_wrp", value); 112 } 113 } 114 115 if (dd.getFullWrpHomeName() != null) { 116 value = dd.getFullWrpHomeName(); 117 vc.put("home_wrp", value); 118 } 119 120 if (dd.getFullWrpLocalHomeName() != null) { 121 value = dd.getFullWrpLocalHomeName(); 122 vc.put("localhome_wrp", value); 123 } 124 125 if (dd.getFullWrpRemoteName() != null) { 126 value = dd.getFullWrpRemoteName(); 127 vc.put("remote_wrp", value); 128 } 129 130 if (dd.getFullWrpLocalName() != null) { 131 value = dd.getFullWrpLocalName(); 132 vc.put("local_wrp", value); 133 } 134 135 value = dd.getEjbClass().getName(); 136 vc.put("bean", value); 137 138 value = dd.getFullDerivedBeanName(); 139 vc.put("bean_wrp", value); 141 if (dd.getFullWrpHandleName() != null) { 142 value = dd.getFullWrpHandleName(); 143 vc.put("handle", value); 144 } 145 146 if (dd instanceof EntityDesc) { 147 Class c = ((EntityDesc) dd).getPrimaryKeyClass(); 148 if (c.equals(Object .class)) { 149 c = Integer .class; 151 } 152 value = c.getName(); 153 vc.put("pk", value); 154 vc.put("pkIsUserClass", pkIsUserClass(value)); 155 if (dd instanceof EntityCmp2Desc) { 156 if (c.equals(String .class)) { 157 vc.put("pkEncodeMethod", "encodeString"); 158 vc.put("pkDecodeMethod", "decodeString((java.lang.String) pk)"); 159 vc.put("pkMappingToMemoryMethod", ""); 160 } else if (c.equals(Character .class)) { 161 vc.put("pkEncodeMethod", "encodeOchar"); 162 vc.put("pkDecodeMethod", "decodeOchar((java.lang.Character) pk)"); 163 vc.put("pkMappingToMemoryMethod", ""); 164 } else if (c.equals(Byte .class)) { 165 vc.put("pkEncodeMethod", "encodeObyte"); 166 vc.put("pkDecodeMethod", "decodeObyte((java.lang.Byte) pk)"); 167 vc.put("pkMappingToMemoryMethod", ""); 168 } else if (c.equals(Short .class)) { 169 vc.put("pkEncodeMethod", "encodeOshort"); 170 vc.put("pkDecodeMethod", "decodeOshort((java.lang.Short) pk)"); 171 vc.put("pkMappingToMemoryMethod", ""); 172 } else if (c.equals(Integer .class)) { 173 vc.put("pkEncodeMethod", "encodeOint"); 174 vc.put("pkDecodeMethod", "decodeOint((java.lang.Integer) pk)"); 175 vc.put("pkMappingToMemoryMethod", ""); 176 } else if (c.equals(Long .class)) { 177 vc.put("pkEncodeMethod", "encodeOlong"); 178 vc.put("pkDecodeMethod", "decodeOlong((java.lang.Long) pk)"); 179 vc.put("pkMappingToMemoryMethod", ""); 180 } else if (c.equals(Date .class)) { 181 vc.put("pkEncodeMethod", "encodeDate"); 182 vc.put("pkDecodeMethod", "decodeDate((java.util.Date) pk)"); 183 vc.put("pkMappingToMemoryMethod", ""); 184 } else if (c.equals(Float .class)) { 185 vc.put("pkEncodeMethod", "encodeString"); 186 vc.put("pkDecodeMethod", "decodeString((java.lang.String) org.objectweb.jonas_ejb.lib.FloatPkFieldMapping.toStorage(pk))"); 187 vc.put("pkMappingToMemoryMethod", "org.objectweb.jonas_ejb.lib.FloatPkFieldMapping.toMemory"); 188 } 189 } 190 } 191 192 switch (srcType) { 193 case Source.CLUSTER_HOME: 194 value = BeanNaming.getPackageName(dd.getFullWrpHomeName()); 195 vc.put("package", value); 196 value = dd.getWrpHomeName()+"_Cmi"; 197 vc.put("class", value); 198 break; 199 case Source.HOME: 200 value = BeanNaming.getPackageName(dd.getFullWrpHomeName()); 201 vc.put("package", value); 202 value = dd.getWrpHomeName(); 203 vc.put("class", value); 204 break; 205 case Source.LOCAL_HOME: 206 value = BeanNaming.getPackageName(dd.getFullWrpLocalHomeName()); 207 vc.put("package", value); 208 value = dd.getWrpLocalHomeName(); 209 vc.put("class", value); 210 break; 211 case Source.REMOTE: 212 value = BeanNaming.getPackageName(dd.getFullWrpRemoteName()); 213 vc.put("package", value); 214 value = dd.getWrpRemoteName(); 215 vc.put("class", value); 216 break; 217 case Source.LOCAL: 218 value = BeanNaming.getPackageName(dd.getFullWrpLocalName()); 219 vc.put("package", value); 220 value = dd.getWrpLocalName(); 221 vc.put("class", value); 222 break; 223 case Source.SERVICE_ENDPOINT: 224 SessionStatelessDesc ssd = (SessionStatelessDesc) dd; 225 value = BeanNaming.getPackageName(ssd.getFullWrpServiceEndpointName()); 226 vc.put("package", value); 227 value = ssd.getWrpServiceEndpointName(); 228 vc.put("class", value); 229 break; 230 case Source.SERVICE_ENDPOINT_HOME: 231 SessionStatelessDesc ssd1 = (SessionStatelessDesc) dd; 232 value = BeanNaming.getPackageName(ssd1.getFullWrpSEHomeName()); 233 vc.put("package", value); 234 value = ssd1.getWrpSEHomeName(); 235 vc.put("class", value); 236 break; 237 case Source.ENTITY_HANDLE: 238 value = BeanNaming.getPackageName(dd.getFullWrpHandleName()); 239 vc.put("package", value); 240 value = dd.getWrpHandleName(); 241 vc.put("class", value); 242 break; 243 case Source.ENTITY_CMP_JDBC: 244 value = BeanNaming.getPackageName(dd.getFullDerivedBeanName()); 245 vc.put("package", value); 246 value = dd.getDerivedBeanName(); 247 vc.put("class", value); 248 break; 249 default: 250 break; 251 } 252 253 ArrayList prototypeMethodList = new ArrayList (); 254 ArrayList vcMethodList = new ArrayList (); 255 Method [] methods = null; 256 switch (srcType) { 257 case Source.CLUSTER_HOME: 258 case Source.HOME: 259 264 methods = dd.getHomeClass().getMethods(); 265 for (int i = 0; i < methods.length; i++) { 266 Method method = methods[i]; 267 if (!method.getDeclaringClass().equals(javax.ejb.EJBHome .class) 268 || "remove".equals(method.getName())) { 269 String pMeth = convertMethod2String(method); 270 if (!prototypeMethodList.contains(pMeth)) { 271 VcMethod vm = new VcMethod(method, dd.getMethodDesc(method), dd); 272 vcMethodList.add(vm); 273 prototypeMethodList.add(pMeth); 274 } 275 } 276 } 277 break; 278 case Source.LOCAL_HOME: 279 284 methods = dd.getLocalHomeClass().getMethods(); 285 for (int i = 0; i < methods.length; i++) { 286 Method method = methods[i]; 287 if (!method.getDeclaringClass().equals(javax.ejb.EJBLocalHome .class) 288 || "remove".equals(method.getName())) { 289 String pMeth = convertMethod2String(method); 290 if (!prototypeMethodList.contains(pMeth)) { 291 VcMethod vm = new VcMethod(method, dd.getMethodDesc(method), dd); 292 vcMethodList.add(vm); 293 prototypeMethodList.add(pMeth); 294 } 295 } 296 } 297 break; 298 case Source.REMOTE: 299 304 methods = dd.getRemoteClass().getMethods(); 305 for (int i = 0; i < methods.length; i++) { 306 Method method = methods[i]; 307 if (!method.getDeclaringClass().equals(javax.ejb.EJBObject .class) 308 || "remove".equals(method.getName())) { 309 String pMeth = convertMethod2String(method); 310 if (!prototypeMethodList.contains(pMeth)) { 311 VcMethod vm = new VcMethod(method, dd.getMethodDesc(method), dd); 312 vcMethodList.add(vm); 313 prototypeMethodList.add(pMeth); 314 } 315 } 316 } 317 break; 318 case Source.LOCAL: 319 324 methods = dd.getLocalClass().getMethods(); 325 for (int i = 0; i < methods.length; i++) { 326 Method method = methods[i]; 327 if (!method.getDeclaringClass().equals(javax.ejb.EJBLocalObject .class) 328 || "remove".equals(method.getName())) { 329 String pMeth = convertMethod2String(method); 330 if (!prototypeMethodList.contains(pMeth)) { 331 VcMethod vm = new VcMethod(method, dd.getMethodDesc(method), dd); 332 vcMethodList.add(vm); 333 prototypeMethodList.add(pMeth); 334 } 335 } 336 } 337 break; 338 case Source.SERVICE_ENDPOINT: 339 342 SessionStatelessDesc ssd = (SessionStatelessDesc) dd; 343 methods = ssd.getServiceEndpointClass().getMethods(); 344 for (int i = 0; i < methods.length; i++) { 345 Method method = methods[i]; 346 String pMeth = convertMethod2String(method); 347 if (!prototypeMethodList.contains(pMeth)) { 348 VcMethod vm = new VcMethod(method, ssd.getMethodDesc(method), dd); 349 vcMethodList.add(vm); 350 prototypeMethodList.add(pMeth); 351 } 352 } 353 break; 354 case Source.ENTITY_CMP_JDBC: 355 365 if (dd.getHomeClass() != null) { 366 methods = dd.getHomeClass().getMethods(); 367 for (int i = 0; i < methods.length; i++) { 368 Method method = methods[i]; 369 if (!method.getDeclaringClass().equals(javax.ejb.EJBHome .class)) { 370 String pMeth = convertMethod2String(method); 371 if (!prototypeMethodList.contains(pMeth)) { 372 VcMethod vm = null; 373 if (method.getName().startsWith("create")) { 374 Method beanMethod = getBeanMethod(method, dd.getEjbClass()); 375 vm = new VcMethod(beanMethod, dd.getMethodDesc(method), dd); 376 Method m = getEjbPostCreateMethod(method, dd.getEjbClass()); 378 VcMethod vm2 = new VcMethod(m, dd.getMethodDesc(method), dd); 379 vcMethodList.add(vm2); 380 } else { 381 vm = new VcMethod(method, dd.getMethodDesc(method), dd); 382 } 383 vcMethodList.add(vm); 384 prototypeMethodList.add(pMeth); 385 } 386 } 387 } 388 } 389 if (dd.getLocalHomeClass() != null) { 390 methods = dd.getLocalHomeClass().getMethods(); 391 for (int i = 0; i < methods.length; i++) { 392 Method method = methods[i]; 393 if (!method.getDeclaringClass().equals(javax.ejb.EJBLocalHome .class)) { 394 String pMeth = convertMethod2String(method); 395 if (!prototypeMethodList.contains(pMeth)) { 396 VcMethod vm = null; 397 if (method.getName().startsWith("create")) { 398 Method beanMethod = getBeanMethod(method, dd.getEjbClass()); 399 vm = new VcMethod(beanMethod, dd.getMethodDesc(method), dd); 400 Method m = getEjbPostCreateMethod(method, dd.getEjbClass()); 402 VcMethod vm2 = new VcMethod(m, dd.getMethodDesc(method), dd); 403 vcMethodList.add(vm2); 404 } else { 405 vm = new VcMethod(method, dd.getMethodDesc(method), dd); 406 } 407 vcMethodList.add(vm); 408 prototypeMethodList.add(pMeth); 409 } 410 } 411 } 412 } 413 414 423 try { 424 Class [] params = {javax.ejb.EntityContext .class}; 425 Method beanMethod = dd.getEjbClass().getMethod("setEntityContext", 426 params); 427 VcMethod vm = new VcMethod(beanMethod, null, dd); 428 vcMethodList.add(vm); 429 } catch (Exception e) { 430 throw new Error ("setEntityContext(javax.ejb.EntityContext) method not defined in " 431 + dd.getEjbClass().getName()); 432 } 433 try { 434 Method beanMethod = dd.getEjbClass().getMethod("ejbActivate", new Class [0]); 435 VcMethod vm = new VcMethod(beanMethod, null, dd); 436 vcMethodList.add(vm); 437 } catch (Exception e) { 438 throw new Error ("ejbActivate() method not defined in " 439 + dd.getEjbClass().getName()); 440 } 441 try { 442 Method beanMethod = dd.getEjbClass().getMethod("ejbLoad", new Class [0]); 443 VcMethod vm = new VcMethod(beanMethod, null, dd); 444 vcMethodList.add(vm); 445 } catch (Exception e) { 446 throw new Error ("ejbLoad() method not defined in " 447 + dd.getEjbClass().getName()); 448 } 449 try { 450 Method beanMethod = dd.getEjbClass().getMethod("ejbStore", new Class [0]); 451 VcMethod vm = new VcMethod(beanMethod, null, dd); 452 vcMethodList.add(vm); 453 } catch (Exception e) { 454 throw new Error ("ejbStore() method not defined in " 455 + dd.getEjbClass().getName()); 456 } 457 try { 458 Method beanMethod = dd.getEjbClass().getMethod("ejbRemove", new Class [0]); 459 VcMethod vm = new VcMethod(beanMethod, null, dd); 460 vcMethodList.add(vm); 461 } catch (Exception e) { 462 throw new Error ("ejbRemove() method not defined in " 463 + dd.getEjbClass().getName()); 464 } 465 466 if (dd instanceof EntityCmp2Desc) { 467 470 Method [] bMeths = dd.getEjbClass().getMethods(); 471 for (int i = 0; i < bMeths.length; i++) { 472 if (bMeths[i].getName().startsWith("ejbSelect")) { 473 VcMethod vm = new VcMethod(bMeths[i], dd.getMethodDesc(bMeths[i]), dd); 474 vcMethodList.add(vm); 475 } 476 } 477 } 478 break; 479 default: 480 break; 481 } 482 vc.put("methodList", new Vector (vcMethodList)); 483 484 ArrayList vcFieldList = new ArrayList (); 485 ArrayList vcFieldPkList = new ArrayList (); 486 ArrayList vcFieldNoPkList = new ArrayList (); 487 if (dd instanceof EntityCmp1Desc) { 488 EntityCmp1Desc edd = (EntityCmp1Desc) dd; 489 for (Iterator i = edd.getCmpFieldDescIterator(); i.hasNext();) { 490 FieldDesc fd = (FieldDesc) i.next(); 491 VcField vcf = new VcField(fd.getName(), fd.getFieldType(), fd, true); 492 vcFieldList.add(vcf); 493 if (fd.isPrimaryKey()) { 494 vcFieldPkList.add(vcf); 495 } else { 496 vcFieldNoPkList.add(vcf); 497 } 498 } 499 } else if (dd instanceof EntityCmp2Desc) { 500 EntityCmp2Desc edd = (EntityCmp2Desc) dd; 501 Iterator it = edd.getCmpFieldDescIterator(); 503 while (it.hasNext()) { 504 FieldDesc fd = (FieldDesc) it.next(); 505 VcField vcf = new VcField(fd); 506 vcFieldList.add(vcf); 507 if (fd.isPrimaryKey()) { 508 vcFieldPkList.add(vcf); 509 } else { 510 vcFieldNoPkList.add(vcf); 511 } 512 } 513 Hashtable cmrFields = new Hashtable (); 515 for (Iterator i = edd.getEjbRelationshipRoleDescIterator(); i.hasNext();) { 516 EjbRelationshipRoleDesc rsr = (EjbRelationshipRoleDesc) i.next(); 517 String cmrname = rsr.getCmrFieldName(); 518 if (!cmrFields.containsKey(cmrname)) { 519 cmrFields.put(cmrname, new VcCMRField(rsr)); 520 } 521 } 522 vc.put("cmrList", cmrFields.values()); 523 } 524 525 vc.put("fieldList", new Vector (vcFieldList)); 526 vc.put("fieldPkList", new Vector (vcFieldPkList)); 527 vc.put("fieldNoPkList", new Vector (vcFieldNoPkList)); 528 529 return vc; 530 } 531 532 537 private static String convertMethod2String(Method meth) { 538 String value = new String (meth.getName()); 539 Class [] params = meth.getParameterTypes(); 540 value = value + "_" + params.length; 541 for (int p = 0; p < params.length; p++) { 542 value = value + "_" + JavaType.getName(params[p]); 543 } 544 return (value); 545 } 546 547 552 private static Method getEjbPostCreateMethod(Method method, Class bean) { 553 Method beanMethod; 554 String beanMethodName = "ejbPost" + BeanNaming.firstToUpperCase(method.getName()); 555 try { 556 beanMethod = bean.getMethod(beanMethodName, method.getParameterTypes()); 557 } catch (Exception e) { 558 throw new Error ("No associated ejbPostCreate method for the interface method " + method.toString()); 559 } 560 return beanMethod; 561 } 562 563 574 private static Method getBeanMethod(Method method, Class bean) { 575 576 String methodName = method.getName(); 577 Method beanMethod; 578 boolean isMethodHome = 579 javax.ejb.EJBHome .class.isAssignableFrom(method.getDeclaringClass()) 580 || javax.ejb.EJBLocalHome .class.isAssignableFrom(method.getDeclaringClass()); 581 582 String beanMethodName = methodName; 583 if ("remove".equals(methodName)) { 584 beanMethodName = "ejbRemove"; 585 } else if (isMethodHome) { 586 if (methodName.startsWith("create") || methodName.startsWith("find")) { 587 beanMethodName = "ejb" + BeanNaming.firstToUpperCase(methodName); 588 } else { 589 beanMethodName = "ejbHome" + BeanNaming.firstToUpperCase(methodName); 590 } 591 } 592 593 try { 594 beanMethod = bean.getMethod(beanMethodName, method.getParameterTypes()); 595 } catch (Exception e) { 596 throw new Error ("No associated bean's method for the interface method " + method.toString()); 597 } 598 return beanMethod; 599 } 600 601 605 public static Boolean pkIsUserClass(String classname) { 606 if (classname.startsWith("java.")) { 607 return Boolean.FALSE; 608 } else { 609 return Boolean.TRUE; 610 } 611 } 612 613 617 public static String toString(VelocityContext vc) { 618 StringBuffer ret = new StringBuffer (); 619 Object [] keys = vc.internalGetKeys(); 620 for (int i = 0; i < keys.length; i++) { 621 String key = (String ) keys[i]; 622 Object value = vc.internalGet(key); 623 if (i > 0) { 624 ret.append("\n"); 625 } 626 if (!"dd".equals(key)) { 627 ret.append(" key = " + key + "; value = " + value); 629 } 630 } 631 return (ret.toString()); 632 } 633 634 } 635 | Popular Tags |