00001
00026 package org.objectweb.jonas_ejb.genic;
00027
00028 import java.lang.reflect.Method;
00029 import java.util.ArrayList;
00030 import java.util.Date;
00031 import java.util.Hashtable;
00032 import java.util.Iterator;
00033 import java.util.Vector;
00034
00035 import org.apache.velocity.VelocityContext;
00036
00037 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
00038 import org.objectweb.jonas_ejb.deployment.api.EjbRelationshipRoleDesc;
00039 import org.objectweb.jonas_ejb.deployment.api.EntityCmp1Desc;
00040 import org.objectweb.jonas_ejb.deployment.api.EntityCmp2Desc;
00041 import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
00042 import org.objectweb.jonas_ejb.deployment.api.FieldDesc;
00043 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
00044 import org.objectweb.jonas_ejb.lib.BeanNaming;
00045 import org.objectweb.jonas_ejb.lib.JavaType;
00046
00047 import org.objectweb.jonas_lib.version.Version;
00048
00049
00057 public class VContextFactory {
00058
00069 public static VelocityContext create(BeanDesc dd, int srcType) throws GenICException {
00070
00071 String value = null;
00072 VelocityContext vc = new VelocityContext();
00073
00074 vc.put("dd", dd);
00075 vc.put("EntityCMP2", (dd instanceof EntityCmp2Desc ? Boolean.TRUE : Boolean.FALSE));
00076
00077 vc.put("jVersion", Version.NUMBER);
00078
00079 if (dd.getHomeClass() != null) {
00080 value = dd.getHomeClass().getName();
00081 vc.put("home", value);
00082 }
00083
00084 if (dd.getLocalHomeClass() != null) {
00085 value = dd.getLocalHomeClass().getName();
00086 vc.put("localhome", value);
00087 }
00088
00089 if (dd.getRemoteClass() != null) {
00090 value = dd.getRemoteClass().getName();
00091 vc.put("remote", value);
00092 }
00093
00094 if (dd.getLocalClass() != null) {
00095 value = dd.getLocalClass().getName();
00096 vc.put("local", value);
00097 }
00098
00099 if (dd instanceof SessionStatelessDesc) {
00100 SessionStatelessDesc ssd = (SessionStatelessDesc) dd;
00101 if (ssd.getServiceEndpointClass() != null) {
00102 value = ssd.getServiceEndpointClass().getName();
00103 vc.put("serviceEndpoint", value);
00104 }
00105 if (ssd.getFullWrpServiceEndpointName() != null) {
00106 value = ssd.getFullWrpServiceEndpointName();
00107 vc.put("service-endpoint_wrp", value);
00108 }
00109 if (ssd.getFullWrpSEHomeName() != null) {
00110 value = ssd.getFullWrpSEHomeName();
00111 vc.put("service-endpoint-home_wrp", value);
00112 }
00113 }
00114
00115 if (dd.getFullWrpHomeName() != null) {
00116 value = dd.getFullWrpHomeName();
00117 vc.put("home_wrp", value);
00118 }
00119
00120 if (dd.getFullWrpLocalHomeName() != null) {
00121 value = dd.getFullWrpLocalHomeName();
00122 vc.put("localhome_wrp", value);
00123 }
00124
00125 if (dd.getFullWrpRemoteName() != null) {
00126 value = dd.getFullWrpRemoteName();
00127 vc.put("remote_wrp", value);
00128 }
00129
00130 if (dd.getFullWrpLocalName() != null) {
00131 value = dd.getFullWrpLocalName();
00132 vc.put("local_wrp", value);
00133 }
00134
00135 value = dd.getEjbClass().getName();
00136 vc.put("bean", value);
00137
00138 value = dd.getFullDerivedBeanName();
00139 vc.put("bean_wrp", value);
00140
00141 if (dd.getFullWrpHandleName() != null) {
00142 value = dd.getFullWrpHandleName();
00143 vc.put("handle", value);
00144 }
00145
00146 if (dd instanceof EntityDesc) {
00147 Class c = ((EntityDesc) dd).getPrimaryKeyClass();
00148 if (c.equals(Object.class)) {
00149
00150 c = Integer.class;
00151 }
00152 value = c.getName();
00153 vc.put("pk", value);
00154 vc.put("pkIsUserClass", pkIsUserClass(value));
00155 if (dd instanceof EntityCmp2Desc) {
00156 if (c.equals(String.class)) {
00157 vc.put("pkEncodeMethod", "encodeString");
00158 vc.put("pkDecodeMethod", "decodeString((java.lang.String) pk)");
00159 vc.put("pkMappingToMemoryMethod", "");
00160 } else if (c.equals(Character.class)) {
00161 vc.put("pkEncodeMethod", "encodeOchar");
00162 vc.put("pkDecodeMethod", "decodeOchar((java.lang.Character) pk)");
00163 vc.put("pkMappingToMemoryMethod", "");
00164 } else if (c.equals(Byte.class)) {
00165 vc.put("pkEncodeMethod", "encodeObyte");
00166 vc.put("pkDecodeMethod", "decodeObyte((java.lang.Byte) pk)");
00167 vc.put("pkMappingToMemoryMethod", "");
00168 } else if (c.equals(Short.class)) {
00169 vc.put("pkEncodeMethod", "encodeOshort");
00170 vc.put("pkDecodeMethod", "decodeOshort((java.lang.Short) pk)");
00171 vc.put("pkMappingToMemoryMethod", "");
00172 } else if (c.equals(Integer.class)) {
00173 vc.put("pkEncodeMethod", "encodeOint");
00174 vc.put("pkDecodeMethod", "decodeOint((java.lang.Integer) pk)");
00175 vc.put("pkMappingToMemoryMethod", "");
00176 } else if (c.equals(Long.class)) {
00177 vc.put("pkEncodeMethod", "encodeOlong");
00178 vc.put("pkDecodeMethod", "decodeOlong((java.lang.Long) pk)");
00179 vc.put("pkMappingToMemoryMethod", "");
00180 } else if (c.equals(Date.class)) {
00181 vc.put("pkEncodeMethod", "encodeDate");
00182 vc.put("pkDecodeMethod", "decodeDate((java.util.Date) pk)");
00183 vc.put("pkMappingToMemoryMethod", "");
00184 } else if (c.equals(Float.class)) {
00185 vc.put("pkEncodeMethod", "encodeString");
00186 vc.put("pkDecodeMethod", "decodeString((java.lang.String) org.objectweb.jonas_ejb.lib.FloatPkFieldMapping.toStorage(pk))");
00187 vc.put("pkMappingToMemoryMethod", "org.objectweb.jonas_ejb.lib.FloatPkFieldMapping.toMemory");
00188 }
00189 }
00190 }
00191
00192 switch (srcType) {
00193 case Source.CLUSTER_HOME:
00194 case Source.HOME:
00195 value = BeanNaming.getPackageName(dd.getFullWrpHomeName());
00196 vc.put("package", value);
00197 value = dd.getWrpHomeName();
00198 vc.put("class", value);
00199 break;
00200 case Source.LOCAL_HOME:
00201 value = BeanNaming.getPackageName(dd.getFullWrpLocalHomeName());
00202 vc.put("package", value);
00203 value = dd.getWrpLocalHomeName();
00204 vc.put("class", value);
00205 break;
00206 case Source.REMOTE:
00207 value = BeanNaming.getPackageName(dd.getFullWrpRemoteName());
00208 vc.put("package", value);
00209 value = dd.getWrpRemoteName();
00210 vc.put("class", value);
00211 break;
00212 case Source.LOCAL:
00213 value = BeanNaming.getPackageName(dd.getFullWrpLocalName());
00214 vc.put("package", value);
00215 value = dd.getWrpLocalName();
00216 vc.put("class", value);
00217 break;
00218 case Source.SERVICE_ENDPOINT:
00219 SessionStatelessDesc ssd = (SessionStatelessDesc) dd;
00220 value = BeanNaming.getPackageName(ssd.getFullWrpServiceEndpointName());
00221 vc.put("package", value);
00222 value = ssd.getWrpServiceEndpointName();
00223 vc.put("class", value);
00224 break;
00225 case Source.SERVICE_ENDPOINT_HOME:
00226 SessionStatelessDesc ssd1 = (SessionStatelessDesc) dd;
00227 value = BeanNaming.getPackageName(ssd1.getFullWrpSEHomeName());
00228 vc.put("package", value);
00229 value = ssd1.getWrpSEHomeName();
00230 vc.put("class", value);
00231 break;
00232 case Source.ENTITY_HANDLE:
00233 value = BeanNaming.getPackageName(dd.getFullWrpHandleName());
00234 vc.put("package", value);
00235 value = dd.getWrpHandleName();
00236 vc.put("class", value);
00237 break;
00238 case Source.ENTITY_CMP_JDBC:
00239 value = BeanNaming.getPackageName(dd.getFullDerivedBeanName());
00240 vc.put("package", value);
00241 value = dd.getDerivedBeanName();
00242 vc.put("class", value);
00243 break;
00244 default:
00245 break;
00246 }
00247
00248 ArrayList prototypeMethodList = new ArrayList();
00249 ArrayList vcMethodList = new ArrayList();
00250 Method [] methods = null;
00251 switch (srcType) {
00252 case Source.CLUSTER_HOME:
00253 case Source.HOME:
00254
00255
00256
00257
00258
00259 methods = dd.getHomeClass().getMethods();
00260 for (int i = 0; i < methods.length; i++) {
00261 Method method = methods[i];
00262 if (!method.getDeclaringClass().equals(javax.ejb.EJBHome.class)
00263 || "remove".equals(method.getName())) {
00264 String pMeth = convertMethod2String(method);
00265 if (!prototypeMethodList.contains(pMeth)) {
00266 VcMethod vm = new VcMethod(method, dd.getMethodDesc(method), dd);
00267 vcMethodList.add(vm);
00268 prototypeMethodList.add(pMeth);
00269 }
00270 }
00271 }
00272 break;
00273 case Source.LOCAL_HOME:
00274
00275
00276
00277
00278
00279 methods = dd.getLocalHomeClass().getMethods();
00280 for (int i = 0; i < methods.length; i++) {
00281 Method method = methods[i];
00282 if (!method.getDeclaringClass().equals(javax.ejb.EJBLocalHome.class)
00283 || "remove".equals(method.getName())) {
00284 String pMeth = convertMethod2String(method);
00285 if (!prototypeMethodList.contains(pMeth)) {
00286 VcMethod vm = new VcMethod(method, dd.getMethodDesc(method), dd);
00287 vcMethodList.add(vm);
00288 prototypeMethodList.add(pMeth);
00289 }
00290 }
00291 }
00292 break;
00293 case Source.REMOTE:
00294
00295
00296
00297
00298
00299 methods = dd.getRemoteClass().getMethods();
00300 for (int i = 0; i < methods.length; i++) {
00301 Method method = methods[i];
00302 if (!method.getDeclaringClass().equals(javax.ejb.EJBObject.class)
00303 || "remove".equals(method.getName())) {
00304 String pMeth = convertMethod2String(method);
00305 if (!prototypeMethodList.contains(pMeth)) {
00306 VcMethod vm = new VcMethod(method, dd.getMethodDesc(method), dd);
00307 vcMethodList.add(vm);
00308 prototypeMethodList.add(pMeth);
00309 }
00310 }
00311 }
00312 break;
00313 case Source.LOCAL:
00314
00315
00316
00317
00318
00319 methods = dd.getLocalClass().getMethods();
00320 for (int i = 0; i < methods.length; i++) {
00321 Method method = methods[i];
00322 if (!method.getDeclaringClass().equals(javax.ejb.EJBLocalObject.class)
00323 || "remove".equals(method.getName())) {
00324 String pMeth = convertMethod2String(method);
00325 if (!prototypeMethodList.contains(pMeth)) {
00326 VcMethod vm = new VcMethod(method, dd.getMethodDesc(method), dd);
00327 vcMethodList.add(vm);
00328 prototypeMethodList.add(pMeth);
00329 }
00330 }
00331 }
00332 break;
00333 case Source.SERVICE_ENDPOINT:
00334
00335
00336
00337 SessionStatelessDesc ssd = (SessionStatelessDesc) dd;
00338 methods = ssd.getServiceEndpointClass().getMethods();
00339 for (int i = 0; i < methods.length; i++) {
00340 Method method = methods[i];
00341 String pMeth = convertMethod2String(method);
00342 if (!prototypeMethodList.contains(pMeth)) {
00343 VcMethod vm = new VcMethod(method, ssd.getMethodDesc(method), dd);
00344 vcMethodList.add(vm);
00345 prototypeMethodList.add(pMeth);
00346 }
00347 }
00348 break;
00349 case Source.ENTITY_CMP_JDBC:
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 if (dd.getHomeClass() != null) {
00361 methods = dd.getHomeClass().getMethods();
00362 for (int i = 0; i < methods.length; i++) {
00363 Method method = methods[i];
00364 if (!method.getDeclaringClass().equals(javax.ejb.EJBHome.class)) {
00365 String pMeth = convertMethod2String(method);
00366 if (!prototypeMethodList.contains(pMeth)) {
00367 VcMethod vm = null;
00368 if (method.getName().startsWith("create")) {
00369 Method beanMethod = getBeanMethod(method, dd.getEjbClass());
00370 vm = new VcMethod(beanMethod, dd.getMethodDesc(method), dd);
00371
00372 Method m = getEjbPostCreateMethod(method, dd.getEjbClass());
00373 VcMethod vm2 = new VcMethod(m, dd.getMethodDesc(method), dd);
00374 vcMethodList.add(vm2);
00375 } else {
00376 vm = new VcMethod(method, dd.getMethodDesc(method), dd);
00377 }
00378 vcMethodList.add(vm);
00379 prototypeMethodList.add(pMeth);
00380 }
00381 }
00382 }
00383 }
00384 if (dd.getLocalHomeClass() != null) {
00385 methods = dd.getLocalHomeClass().getMethods();
00386 for (int i = 0; i < methods.length; i++) {
00387 Method method = methods[i];
00388 if (!method.getDeclaringClass().equals(javax.ejb.EJBLocalHome.class)) {
00389 String pMeth = convertMethod2String(method);
00390 if (!prototypeMethodList.contains(pMeth)) {
00391 VcMethod vm = null;
00392 if (method.getName().startsWith("create")) {
00393 Method beanMethod = getBeanMethod(method, dd.getEjbClass());
00394 vm = new VcMethod(beanMethod, dd.getMethodDesc(method), dd);
00395
00396 Method m = getEjbPostCreateMethod(method, dd.getEjbClass());
00397 VcMethod vm2 = new VcMethod(m, dd.getMethodDesc(method), dd);
00398 vcMethodList.add(vm2);
00399 } else {
00400 vm = new VcMethod(method, dd.getMethodDesc(method), dd);
00401 }
00402 vcMethodList.add(vm);
00403 prototypeMethodList.add(pMeth);
00404 }
00405 }
00406 }
00407 }
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 try {
00419 Class[] params = {javax.ejb.EntityContext.class};
00420 Method beanMethod = dd.getEjbClass().getMethod("setEntityContext",
00421 params);
00422 VcMethod vm = new VcMethod(beanMethod, null, dd);
00423 vcMethodList.add(vm);
00424 } catch (Exception e) {
00425 throw new Error("setEntityContext(javax.ejb.EntityContext) method not defined in "
00426 + dd.getEjbClass().getName());
00427 }
00428 try {
00429 Method beanMethod = dd.getEjbClass().getMethod("ejbActivate", new Class[0]);
00430 VcMethod vm = new VcMethod(beanMethod, null, dd);
00431 vcMethodList.add(vm);
00432 } catch (Exception e) {
00433 throw new Error("ejbActivate() method not defined in "
00434 + dd.getEjbClass().getName());
00435 }
00436 try {
00437 Method beanMethod = dd.getEjbClass().getMethod("ejbLoad", new Class[0]);
00438 VcMethod vm = new VcMethod(beanMethod, null, dd);
00439 vcMethodList.add(vm);
00440 } catch (Exception e) {
00441 throw new Error("ejbLoad() method not defined in "
00442 + dd.getEjbClass().getName());
00443 }
00444 try {
00445 Method beanMethod = dd.getEjbClass().getMethod("ejbStore", new Class[0]);
00446 VcMethod vm = new VcMethod(beanMethod, null, dd);
00447 vcMethodList.add(vm);
00448 } catch (Exception e) {
00449 throw new Error("ejbStore() method not defined in "
00450 + dd.getEjbClass().getName());
00451 }
00452 try {
00453 Method beanMethod = dd.getEjbClass().getMethod("ejbRemove", new Class[0]);
00454 VcMethod vm = new VcMethod(beanMethod, null, dd);
00455 vcMethodList.add(vm);
00456 } catch (Exception e) {
00457 throw new Error("ejbRemove() method not defined in "
00458 + dd.getEjbClass().getName());
00459 }
00460
00461 if (dd instanceof EntityCmp2Desc) {
00462
00463
00464
00465 Method[] bMeths = dd.getEjbClass().getMethods();
00466 for (int i = 0; i < bMeths.length; i++) {
00467 if (bMeths[i].getName().startsWith("ejbSelect")) {
00468 VcMethod vm = new VcMethod(bMeths[i], dd.getMethodDesc(bMeths[i]), dd);
00469 vcMethodList.add(vm);
00470 }
00471 }
00472 }
00473 break;
00474 default:
00475 break;
00476 }
00477 vc.put("methodList", new Vector(vcMethodList));
00478
00479 ArrayList vcFieldList = new ArrayList();
00480 ArrayList vcFieldPkList = new ArrayList();
00481 ArrayList vcFieldNoPkList = new ArrayList();
00482 if (dd instanceof EntityCmp1Desc) {
00483 EntityCmp1Desc edd = (EntityCmp1Desc) dd;
00484 for (Iterator i = edd.getCmpFieldDescIterator(); i.hasNext();) {
00485 FieldDesc fd = (FieldDesc) i.next();
00486 VcField vcf = new VcField(fd.getName(), fd.getFieldType(), fd, true);
00487 vcFieldList.add(vcf);
00488 if (fd.isPrimaryKey()) {
00489 vcFieldPkList.add(vcf);
00490 } else {
00491 vcFieldNoPkList.add(vcf);
00492 }
00493 }
00494 } else if (dd instanceof EntityCmp2Desc) {
00495 EntityCmp2Desc edd = (EntityCmp2Desc) dd;
00496
00497 Iterator it = edd.getCmpFieldDescIterator();
00498 while (it.hasNext()) {
00499 FieldDesc fd = (FieldDesc) it.next();
00500 VcField vcf = new VcField(fd);
00501 vcFieldList.add(vcf);
00502 if (fd.isPrimaryKey()) {
00503 vcFieldPkList.add(vcf);
00504 } else {
00505 vcFieldNoPkList.add(vcf);
00506 }
00507 }
00508
00509 Hashtable cmrFields = new Hashtable();
00510 for (Iterator i = edd.getEjbRelationshipRoleDescIterator(); i.hasNext();) {
00511 EjbRelationshipRoleDesc rsr = (EjbRelationshipRoleDesc) i.next();
00512 String cmrname = rsr.getCmrFieldName();
00513 if (!cmrFields.containsKey(cmrname)) {
00514 cmrFields.put(cmrname, new VcCMRField(rsr));
00515 }
00516 }
00517 vc.put("cmrList", cmrFields.values());
00518 }
00519
00520 vc.put("fieldList", new Vector(vcFieldList));
00521 vc.put("fieldPkList", new Vector(vcFieldPkList));
00522 vc.put("fieldNoPkList", new Vector(vcFieldNoPkList));
00523
00524 return vc;
00525 }
00526
00532 private static String convertMethod2String(Method meth) {
00533 String value = new String(meth.getName());
00534 Class[] params = meth.getParameterTypes();
00535 value = value + "_" + params.length;
00536 for (int p = 0; p < params.length; p++) {
00537 value = value + "_" + JavaType.getName(params[p]);
00538 }
00539 return (value);
00540 }
00541
00547 private static Method getEjbPostCreateMethod(Method method, Class bean) {
00548 Method beanMethod;
00549 String beanMethodName = "ejbPost" + BeanNaming.firstToUpperCase(method.getName());
00550 try {
00551 beanMethod = bean.getMethod(beanMethodName, method.getParameterTypes());
00552 } catch (Exception e) {
00553 throw new Error("No associated ejbPostCreate method for the interface method " + method.toString());
00554 }
00555 return beanMethod;
00556 }
00557
00569 private static Method getBeanMethod(Method method, Class bean) {
00570
00571 String methodName = method.getName();
00572 Method beanMethod;
00573 boolean isMethodHome =
00574 javax.ejb.EJBHome.class.isAssignableFrom(method.getDeclaringClass())
00575 || javax.ejb.EJBLocalHome.class.isAssignableFrom(method.getDeclaringClass());
00576
00577 String beanMethodName = methodName;
00578 if ("remove".equals(methodName)) {
00579 beanMethodName = "ejbRemove";
00580 } else if (isMethodHome) {
00581 if (methodName.startsWith("create") || methodName.startsWith("find")) {
00582 beanMethodName = "ejb" + BeanNaming.firstToUpperCase(methodName);
00583 } else {
00584 beanMethodName = "ejbHome" + BeanNaming.firstToUpperCase(methodName);
00585 }
00586 }
00587
00588 try {
00589 beanMethod = bean.getMethod(beanMethodName, method.getParameterTypes());
00590 } catch (Exception e) {
00591 throw new Error("No associated bean's method for the interface method " + method.toString());
00592 }
00593 return beanMethod;
00594 }
00595
00600 public static Boolean pkIsUserClass(String classname) {
00601 if (classname.startsWith("java.")) {
00602 return Boolean.FALSE;
00603 } else {
00604 return Boolean.TRUE;
00605 }
00606 }
00607
00612 public static String toString(VelocityContext vc) {
00613 StringBuffer ret = new StringBuffer();
00614 Object [] keys = vc.internalGetKeys();
00615 for (int i = 0; i < keys.length; i++) {
00616 String key = (String) keys[i];
00617 Object value = vc.internalGet(key);
00618 if (i > 0) {
00619 ret.append("\n");
00620 }
00621 if (!"dd".equals(key)) {
00622
00623 ret.append(" key = " + key + "; value = " + value);
00624 }
00625 }
00626 return (ret.toString());
00627 }
00628
00629 }