001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: VContextFactory.java 9280 2006-08-01 10:15:24Z japaz $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.genic;
025:
026: import java.lang.reflect.Method;
027: import java.util.ArrayList;
028: import java.util.Date;
029: import java.util.Hashtable;
030: import java.util.Iterator;
031: import java.util.Vector;
032:
033: import org.apache.velocity.VelocityContext;
034:
035: import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
036: import org.objectweb.jonas_ejb.deployment.api.EjbRelationshipRoleDesc;
037: import org.objectweb.jonas_ejb.deployment.api.EntityCmp1Desc;
038: import org.objectweb.jonas_ejb.deployment.api.EntityCmp2Desc;
039: import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
040: import org.objectweb.jonas_ejb.deployment.api.FieldDesc;
041: import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
042: import org.objectweb.jonas_ejb.lib.BeanNaming;
043: import org.objectweb.jonas_ejb.lib.JavaType;
044:
045: import org.objectweb.jonas_lib.version.Version;
046:
047: /**
048: * This class allows to create the Velocity Context used to build the generated
049: * sources with the Velocity templates.
050: *
051: * @author: Helene Joanin (Bull) : Initial developer
052: */
053:
054: public class VContextFactory {
055:
056: /**
057: * Creates the Velocity Context used to build the generated sources with the Velocity templates.
058: * There are different types of Velocity Context :
059: * HOME, LOCAL_HOME, REMOTE, LOCAL, ENTITY_HANDLE, ENTITY_CMP_JDBC, ...
060: * @see Source.java for type definitions.
061: * @param dd The bean Deployment Descriptor
062: * @param srcType the type of Velocity Context
063: * @return Return the Velocity Context associated to the gievn bean for the given source to generate
064: * @throws GenICException in error case
065: */
066: public static VelocityContext create(BeanDesc dd, int srcType)
067: throws GenICException {
068:
069: String value = null;
070: VelocityContext vc = new VelocityContext();
071:
072: vc.put("dd", dd);
073: vc.put("EntityCMP2",
074: (dd instanceof EntityCmp2Desc ? Boolean.TRUE
075: : Boolean.FALSE));
076:
077: vc.put("jVersion", Version.getNumber());
078:
079: if (dd.getHomeClass() != null) {
080: value = dd.getHomeClass().getName();
081: vc.put("home", value);
082: }
083:
084: if (dd.getLocalHomeClass() != null) {
085: value = dd.getLocalHomeClass().getName();
086: vc.put("localhome", value);
087: }
088:
089: if (dd.getRemoteClass() != null) {
090: value = dd.getRemoteClass().getName();
091: vc.put("remote", value);
092: }
093:
094: if (dd.getLocalClass() != null) {
095: value = dd.getLocalClass().getName();
096: vc.put("local", value);
097: }
098:
099: 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); // Same as "bean" if not a Entity CMP
140:
141: if (dd.getFullWrpHandleName() != null) {
142: value = dd.getFullWrpHandleName();
143: vc.put("handle", value);
144: }
145:
146: vc.put("isClusterReplicated", Boolean.valueOf(dd
147: .isClusterReplicated()));
148:
149: if (dd instanceof EntityDesc) {
150: Class c = ((EntityDesc) dd).getPrimaryKeyClass();
151: if (c.equals(Object.class)) {
152: // Auto key generated so convert into Integer
153: c = Integer.class;
154: }
155: value = c.getName();
156: vc.put("pk", value);
157: vc.put("pkIsUserClass", pkIsUserClass(value));
158: if (dd instanceof EntityCmp2Desc) {
159: if (c.equals(String.class)) {
160: vc.put("pkEncodeMethod", "encodeString");
161: vc.put("pkDecodeMethod",
162: "decodeString((java.lang.String) pk)");
163: vc.put("pkMappingToMemoryMethod", "");
164: } else if (c.equals(Character.class)) {
165: vc.put("pkEncodeMethod", "encodeOchar");
166: vc.put("pkDecodeMethod",
167: "decodeOchar((java.lang.Character) pk)");
168: vc.put("pkMappingToMemoryMethod", "");
169: } else if (c.equals(Byte.class)) {
170: vc.put("pkEncodeMethod", "encodeObyte");
171: vc.put("pkDecodeMethod",
172: "decodeObyte((java.lang.Byte) pk)");
173: vc.put("pkMappingToMemoryMethod", "");
174: } else if (c.equals(Short.class)) {
175: vc.put("pkEncodeMethod", "encodeOshort");
176: vc.put("pkDecodeMethod",
177: "decodeOshort((java.lang.Short) pk)");
178: vc.put("pkMappingToMemoryMethod", "");
179: } else if (c.equals(Integer.class)) {
180: vc.put("pkEncodeMethod", "encodeOint");
181: vc.put("pkDecodeMethod",
182: "decodeOint((java.lang.Integer) pk)");
183: vc.put("pkMappingToMemoryMethod", "");
184: } else if (c.equals(Long.class)) {
185: vc.put("pkEncodeMethod", "encodeOlong");
186: vc.put("pkDecodeMethod",
187: "decodeOlong((java.lang.Long) pk)");
188: vc.put("pkMappingToMemoryMethod", "");
189: } else if (c.equals(Date.class)) {
190: vc.put("pkEncodeMethod", "encodeDate");
191: vc.put("pkDecodeMethod",
192: "decodeDate((java.util.Date) pk)");
193: vc.put("pkMappingToMemoryMethod", "");
194: } else if (c.equals(Float.class)) {
195: vc.put("pkEncodeMethod", "encodeString");
196: vc
197: .put(
198: "pkDecodeMethod",
199: "decodeString((java.lang.String) org.objectweb.jonas_ejb.lib.FloatPkFieldMapping.toStorage(pk))");
200: vc
201: .put("pkMappingToMemoryMethod",
202: "org.objectweb.jonas_ejb.lib.FloatPkFieldMapping.toMemory");
203: }
204: }
205: }
206:
207: switch (srcType) {
208: case Source.CLUSTER_HOME:
209: value = BeanNaming.getPackageName(dd.getFullWrpHomeName());
210: vc.put("package", value);
211: value = dd.getWrpHomeName() + "_Cmi";
212: vc.put("class", value);
213: break;
214: case Source.CLUSTER_REMOTE:
215: value = BeanNaming
216: .getPackageName(dd.getFullWrpRemoteName());
217: vc.put("package", value);
218: value = dd.getWrpRemoteName() + "_Cmi";
219: vc.put("class", value);
220: break;
221: case Source.CLUSTER_HOME_SFSB:
222: value = BeanNaming.getPackageName(dd.getFullWrpHomeName());
223: vc.put("package", value);
224: value = dd.getWrpHomeName() + "_Cmi";
225: vc.put("class", value);
226: break;
227: case Source.CLUSTER_REMOTE_SFSB:
228: value = BeanNaming
229: .getPackageName(dd.getFullWrpRemoteName());
230: vc.put("package", value);
231: value = dd.getWrpRemoteName() + "_Cmi";
232: vc.put("class", value);
233: break;
234: case Source.CLUSTER_HOME_ENTITY:
235: value = BeanNaming.getPackageName(dd.getFullWrpHomeName());
236: vc.put("package", value);
237: value = dd.getWrpHomeName() + "_Cmi";
238: vc.put("class", value);
239: break;
240: case Source.CLUSTER_REMOTE_ENTITY:
241: value = BeanNaming
242: .getPackageName(dd.getFullWrpRemoteName());
243: vc.put("package", value);
244: value = dd.getWrpRemoteName() + "_Cmi";
245: vc.put("class", value);
246: break;
247: case Source.HOME:
248: value = BeanNaming.getPackageName(dd.getFullWrpHomeName());
249: vc.put("package", value);
250: value = dd.getWrpHomeName();
251: vc.put("class", value);
252: break;
253: case Source.LOCAL_HOME:
254: value = BeanNaming.getPackageName(dd
255: .getFullWrpLocalHomeName());
256: vc.put("package", value);
257: value = dd.getWrpLocalHomeName();
258: vc.put("class", value);
259: break;
260: case Source.REMOTE:
261: value = BeanNaming
262: .getPackageName(dd.getFullWrpRemoteName());
263: vc.put("package", value);
264: value = dd.getWrpRemoteName();
265: vc.put("class", value);
266: break;
267: case Source.LOCAL:
268: value = BeanNaming.getPackageName(dd.getFullWrpLocalName());
269: vc.put("package", value);
270: value = dd.getWrpLocalName();
271: vc.put("class", value);
272: break;
273: case Source.SERVICE_ENDPOINT:
274: SessionStatelessDesc ssd = (SessionStatelessDesc) dd;
275: value = BeanNaming.getPackageName(ssd
276: .getFullWrpServiceEndpointName());
277: vc.put("package", value);
278: value = ssd.getWrpServiceEndpointName();
279: vc.put("class", value);
280: break;
281: case Source.SERVICE_ENDPOINT_HOME:
282: SessionStatelessDesc ssd1 = (SessionStatelessDesc) dd;
283: value = BeanNaming.getPackageName(ssd1
284: .getFullWrpSEHomeName());
285: vc.put("package", value);
286: value = ssd1.getWrpSEHomeName();
287: vc.put("class", value);
288: break;
289: case Source.ENTITY_HANDLE:
290: value = BeanNaming
291: .getPackageName(dd.getFullWrpHandleName());
292: vc.put("package", value);
293: value = dd.getWrpHandleName();
294: vc.put("class", value);
295: break;
296: case Source.ENTITY_CMP_JDBC:
297: value = BeanNaming.getPackageName(dd
298: .getFullDerivedBeanName());
299: vc.put("package", value);
300: value = dd.getDerivedBeanName();
301: vc.put("class", value);
302: break;
303: default:
304: break;
305: }
306:
307: ArrayList prototypeMethodList = new ArrayList();
308: ArrayList vcMethodList = new ArrayList();
309: Method[] methods = null;
310: switch (srcType) {
311: case Source.CLUSTER_HOME:
312: case Source.HOME:
313: /*
314: * Add in the methods list, the Home interface methods, except
315: * - methods (other than remove) defined in javax.ejb.EJBHome,
316: * - and overriding methods.
317: */
318: methods = dd.getHomeClass().getMethods();
319: for (int i = 0; i < methods.length; i++) {
320: Method method = methods[i];
321: if (!method.getDeclaringClass().equals(
322: javax.ejb.EJBHome.class)
323: || "remove".equals(method.getName())) {
324: String pMeth = convertMethod2String(method);
325: if (!prototypeMethodList.contains(pMeth)) {
326: VcMethod vm = new VcMethod(method, dd
327: .getMethodDesc(method), dd);
328: vcMethodList.add(vm);
329: prototypeMethodList.add(pMeth);
330: }
331: }
332: }
333: break;
334: case Source.LOCAL_HOME:
335: /*
336: * Add in the methods list, the LocalHome interface methods, except
337: * - methods (other than remove) defined in javax.ejb.EJBLocalHome,
338: * - and overriding methods.
339: */
340: methods = dd.getLocalHomeClass().getMethods();
341: for (int i = 0; i < methods.length; i++) {
342: Method method = methods[i];
343: if (!method.getDeclaringClass().equals(
344: javax.ejb.EJBLocalHome.class)
345: || "remove".equals(method.getName())) {
346: String pMeth = convertMethod2String(method);
347: if (!prototypeMethodList.contains(pMeth)) {
348: VcMethod vm = new VcMethod(method, dd
349: .getMethodDesc(method), dd);
350: vcMethodList.add(vm);
351: prototypeMethodList.add(pMeth);
352: }
353: }
354: }
355: break;
356: case Source.REMOTE:
357: /*
358: * Add in the methods list, the Remote interface methods, except
359: * - methods (other than remove) defined in javax.ejb.EJBObject,
360: * - and overriding methods.
361: */
362: methods = dd.getRemoteClass().getMethods();
363: for (int i = 0; i < methods.length; i++) {
364: Method method = methods[i];
365: if (!method.getDeclaringClass().equals(
366: javax.ejb.EJBObject.class)
367: || "remove".equals(method.getName())) {
368: String pMeth = convertMethod2String(method);
369: if (!prototypeMethodList.contains(pMeth)) {
370: VcMethod vm = new VcMethod(method, dd
371: .getMethodDesc(method), dd);
372: vcMethodList.add(vm);
373: prototypeMethodList.add(pMeth);
374: }
375: }
376: }
377: break;
378: case Source.LOCAL:
379: /*
380: * Add in the methods list, the Local interface methods, except
381: * - methods (other than remove) defined in javax.ejb.EJBLocalObject,
382: * - and overriding methods.
383: */
384: methods = dd.getLocalClass().getMethods();
385: for (int i = 0; i < methods.length; i++) {
386: Method method = methods[i];
387: if (!method.getDeclaringClass().equals(
388: javax.ejb.EJBLocalObject.class)
389: || "remove".equals(method.getName())) {
390: String pMeth = convertMethod2String(method);
391: if (!prototypeMethodList.contains(pMeth)) {
392: VcMethod vm = new VcMethod(method, dd
393: .getMethodDesc(method), dd);
394: vcMethodList.add(vm);
395: prototypeMethodList.add(pMeth);
396: }
397: }
398: }
399: break;
400: case Source.SERVICE_ENDPOINT:
401: /*
402: * Add in the methods list, the ServiceEndpoint interface methods
403: */
404: SessionStatelessDesc ssd = (SessionStatelessDesc) dd;
405: methods = ssd.getServiceEndpointClass().getMethods();
406: for (int i = 0; i < methods.length; i++) {
407: Method method = methods[i];
408: String pMeth = convertMethod2String(method);
409: if (!prototypeMethodList.contains(pMeth)) {
410: VcMethod vm = new VcMethod(method, ssd
411: .getMethodDesc(method), dd);
412: vcMethodList.add(vm);
413: prototypeMethodList.add(pMeth);
414: }
415: }
416: break;
417: case Source.ENTITY_CMP_JDBC:
418: /*
419: * Add in the methods list, the Home and LocalHome interfaces methods, except
420: * - methods defined in javax.ejb.EJBHome and javax.ejb.EJBLocalHome and
421: * - and overriding methods.
422: * Same methods may be defined both in the Home interface and in the
423: * LocalHome interface. Don't add twice this method in the vcMethodList !!!
424: *
425: * Futhermore, in case of create() methods, methods added in the vcMethodList
426: * are the ejbCreate() associated bean's methods.
427: */
428: if (dd.getHomeClass() != null) {
429: methods = dd.getHomeClass().getMethods();
430: for (int i = 0; i < methods.length; i++) {
431: Method method = methods[i];
432: if (!method.getDeclaringClass().equals(
433: javax.ejb.EJBHome.class)) {
434: String pMeth = convertMethod2String(method);
435: if (!prototypeMethodList.contains(pMeth)) {
436: VcMethod vm = null;
437: if (method.getName().startsWith("create")) {
438: Method beanMethod = getBeanMethod(
439: method, dd.getEjbClass());
440: vm = new VcMethod(beanMethod, dd
441: .getMethodDesc(method), dd);
442: // add the ejbPostCreate method
443: Method m = getEjbPostCreateMethod(
444: method, dd.getEjbClass());
445: VcMethod vm2 = new VcMethod(m, dd
446: .getMethodDesc(method), dd);
447: vcMethodList.add(vm2);
448: } else {
449: vm = new VcMethod(method, dd
450: .getMethodDesc(method), dd);
451: }
452: vcMethodList.add(vm);
453: prototypeMethodList.add(pMeth);
454: }
455: }
456: }
457: }
458: if (dd.getLocalHomeClass() != null) {
459: methods = dd.getLocalHomeClass().getMethods();
460: for (int i = 0; i < methods.length; i++) {
461: Method method = methods[i];
462: if (!method.getDeclaringClass().equals(
463: javax.ejb.EJBLocalHome.class)) {
464: String pMeth = convertMethod2String(method);
465: if (!prototypeMethodList.contains(pMeth)) {
466: VcMethod vm = null;
467: if (method.getName().startsWith("create")) {
468: Method beanMethod = getBeanMethod(
469: method, dd.getEjbClass());
470: vm = new VcMethod(beanMethod, dd
471: .getMethodDesc(method), dd);
472: // add the ejbPostCreate method
473: Method m = getEjbPostCreateMethod(
474: method, dd.getEjbClass());
475: VcMethod vm2 = new VcMethod(m, dd
476: .getMethodDesc(method), dd);
477: vcMethodList.add(vm2);
478: } else {
479: vm = new VcMethod(method, dd
480: .getMethodDesc(method), dd);
481: }
482: vcMethodList.add(vm);
483: prototypeMethodList.add(pMeth);
484: }
485: }
486: }
487: }
488:
489: /*
490: * Add the
491: * - setEntityContext(javax.ejb.EntityContext),
492: * - ejbActivate(),
493: * - ejbLoad(),
494: * - ejbStore(),
495: * - ejbRemove()
496: * bean's methods.
497: */
498: try {
499: Class[] params = { javax.ejb.EntityContext.class };
500: Method beanMethod = dd.getEjbClass().getMethod(
501: "setEntityContext", params);
502: VcMethod vm = new VcMethod(beanMethod, null, dd);
503: vcMethodList.add(vm);
504: } catch (Exception e) {
505: throw new Error(
506: "setEntityContext(javax.ejb.EntityContext) method not defined in "
507: + dd.getEjbClass().getName());
508: }
509: try {
510: Method beanMethod = dd.getEjbClass().getMethod(
511: "ejbActivate", new Class[0]);
512: VcMethod vm = new VcMethod(beanMethod, null, dd);
513: vcMethodList.add(vm);
514: } catch (Exception e) {
515: throw new Error("ejbActivate() method not defined in "
516: + dd.getEjbClass().getName());
517: }
518: try {
519: Method beanMethod = dd.getEjbClass().getMethod(
520: "ejbLoad", new Class[0]);
521: VcMethod vm = new VcMethod(beanMethod, null, dd);
522: vcMethodList.add(vm);
523: } catch (Exception e) {
524: throw new Error("ejbLoad() method not defined in "
525: + dd.getEjbClass().getName());
526: }
527: try {
528: Method beanMethod = dd.getEjbClass().getMethod(
529: "ejbStore", new Class[0]);
530: VcMethod vm = new VcMethod(beanMethod, null, dd);
531: vcMethodList.add(vm);
532: } catch (Exception e) {
533: throw new Error("ejbStore() method not defined in "
534: + dd.getEjbClass().getName());
535: }
536: try {
537: Method beanMethod = dd.getEjbClass().getMethod(
538: "ejbRemove", new Class[0]);
539: VcMethod vm = new VcMethod(beanMethod, null, dd);
540: vcMethodList.add(vm);
541: } catch (Exception e) {
542: throw new Error("ejbRemove() method not defined in "
543: + dd.getEjbClass().getName());
544: }
545:
546: if (dd instanceof EntityCmp2Desc) {
547: /*
548: * Add the ejbSelect() methods defined in the bean.
549: */
550: Method[] bMeths = dd.getEjbClass().getMethods();
551: for (int i = 0; i < bMeths.length; i++) {
552: if (bMeths[i].getName().startsWith("ejbSelect")) {
553: VcMethod vm = new VcMethod(bMeths[i], dd
554: .getMethodDesc(bMeths[i]), dd);
555: vcMethodList.add(vm);
556: }
557: }
558: }
559: break;
560: default:
561: break;
562: }
563: vc.put("methodList", new Vector(vcMethodList));
564:
565: ArrayList vcFieldList = new ArrayList();
566: ArrayList vcFieldPkList = new ArrayList();
567: ArrayList vcFieldNoPkList = new ArrayList();
568: if (dd instanceof EntityCmp1Desc) {
569: EntityCmp1Desc edd = (EntityCmp1Desc) dd;
570: for (Iterator i = edd.getCmpFieldDescIterator(); i
571: .hasNext();) {
572: FieldDesc fd = (FieldDesc) i.next();
573: VcField vcf = new VcField(fd.getName(), fd
574: .getFieldType(), fd, true);
575: vcFieldList.add(vcf);
576: if (fd.isPrimaryKey()) {
577: vcFieldPkList.add(vcf);
578: } else {
579: vcFieldNoPkList.add(vcf);
580: }
581: }
582: } else if (dd instanceof EntityCmp2Desc) {
583: EntityCmp2Desc edd = (EntityCmp2Desc) dd;
584: // Define the CMP fields
585: Iterator it = edd.getCmpFieldDescIterator();
586: while (it.hasNext()) {
587: FieldDesc fd = (FieldDesc) it.next();
588: VcField vcf = new VcField(fd);
589: vcFieldList.add(vcf);
590: if (fd.isPrimaryKey()) {
591: vcFieldPkList.add(vcf);
592: } else {
593: vcFieldNoPkList.add(vcf);
594: }
595: }
596: // Define the CMR fields
597: Hashtable cmrFields = new Hashtable();
598: for (Iterator i = edd.getEjbRelationshipRoleDescIterator(); i
599: .hasNext();) {
600: EjbRelationshipRoleDesc rsr = (EjbRelationshipRoleDesc) i
601: .next();
602: String cmrname = rsr.getCmrFieldName();
603: if (!cmrFields.containsKey(cmrname)) {
604: cmrFields.put(cmrname, new VcCMRField(rsr));
605: }
606: }
607: vc.put("cmrList", cmrFields.values());
608: }
609:
610: vc.put("fieldList", new Vector(vcFieldList));
611: vc.put("fieldPkList", new Vector(vcFieldPkList));
612: vc.put("fieldNoPkList", new Vector(vcFieldNoPkList));
613:
614: return vc;
615: }
616:
617: /**
618: * Convert a method to a string depending on its name and its parameters list.
619: * @param meth method to convert
620: * @return Return a string representation of the given method
621: */
622: private static String convertMethod2String(Method meth) {
623: String value = new String(meth.getName());
624: Class[] params = meth.getParameterTypes();
625: value = value + "_" + params.length;
626: for (int p = 0; p < params.length; p++) {
627: value = value + "_" + JavaType.getName(params[p]);
628: }
629: return (value);
630: }
631:
632: /**
633: * @param method home.create() method
634: * @param bean class of the bean
635: * @return Return the associated bean's method ejbPostCreate matching the create method of the interface
636: */
637: private static Method getEjbPostCreateMethod(Method method,
638: Class bean) {
639: Method beanMethod;
640: String beanMethodName = "ejbPost"
641: + BeanNaming.firstToUpperCase(method.getName());
642: try {
643: beanMethod = bean.getMethod(beanMethodName, method
644: .getParameterTypes());
645: } catch (Exception e) {
646: throw new Error(
647: "No associated ejbPostCreate method for the interface method "
648: + method.toString());
649: }
650: return beanMethod;
651: }
652:
653: /**
654: * Return the associated bean's method of the given interface's method, ie
655: * - ejbRemove(..) for [home|object]remove(..)
656: * - ejbCreateXxx(..) for home.createXxx(..)
657: * - ejbFindXxx(..) for home.findXxx(..)
658: * - ejbHomeXxx(..) for home.xxx(..)
659: * - xxx(..) for object.xxx(..)
660: * @param method interface method
661: * @param bean class of the bean
662: * @return Return the associated bean's method to the interface method
663: */
664: private static Method getBeanMethod(Method method, Class bean) {
665:
666: String methodName = method.getName();
667: Method beanMethod;
668: boolean isMethodHome = javax.ejb.EJBHome.class
669: .isAssignableFrom(method.getDeclaringClass())
670: || javax.ejb.EJBLocalHome.class.isAssignableFrom(method
671: .getDeclaringClass());
672:
673: String beanMethodName = methodName;
674: if ("remove".equals(methodName)) {
675: beanMethodName = "ejbRemove";
676: } else if (isMethodHome) {
677: if (methodName.startsWith("create")
678: || methodName.startsWith("find")) {
679: beanMethodName = "ejb"
680: + BeanNaming.firstToUpperCase(methodName);
681: } else {
682: beanMethodName = "ejbHome"
683: + BeanNaming.firstToUpperCase(methodName);
684: }
685: }
686:
687: try {
688: beanMethod = bean.getMethod(beanMethodName, method
689: .getParameterTypes());
690: } catch (Exception e) {
691: throw new Error(
692: "No associated bean's method for the interface method "
693: + method.toString());
694: }
695: return beanMethod;
696: }
697:
698: /**
699: * @param classname name of a class
700: * @return true if the class is a 'user' class, false if it's a java class
701: */
702: public static Boolean pkIsUserClass(String classname) {
703: if (classname.startsWith("java.")) {
704: return Boolean.FALSE;
705: } else {
706: return Boolean.TRUE;
707: }
708: }
709:
710: /**
711: * @param vc VelocityContext to trace
712: * @return Return a string representation of the given VelocityContext for debug use
713: */
714: public static String toString(VelocityContext vc) {
715: StringBuffer ret = new StringBuffer();
716: Object[] keys = vc.internalGetKeys();
717: for (int i = 0; i < keys.length; i++) {
718: String key = (String) keys[i];
719: Object value = vc.internalGet(key);
720: if (i > 0) {
721: ret.append("\n");
722: }
723: if (!"dd".equals(key)) {
724: // Do not trace the BeanDesc (variable named "dd")
725: ret.append(" key = " + key + "; value = " + value);
726: }
727: }
728: return (ret.toString());
729: }
730:
731: }
|