KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > genic > VContextFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: VContextFactory.java,v 1.37 2005/07/27 13:36:18 pelletib Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.genic;
27
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Vector JavaDoc;
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 /**
51  * This class allows to create the Velocity Context used to build the generated
52  * sources with the Velocity templates.
53  *
54  * @author: Helene Joanin (Bull) : Initial developer
55  */

56
57 public class VContextFactory {
58
59     /**
60      * Creates the Velocity Context used to build the generated sources with the Velocity templates.
61      * There are different types of Velocity Context :
62      * HOME, LOCAL_HOME, REMOTE, LOCAL, ENTITY_HANDLE, ENTITY_CMP_JDBC, ...
63      * @see Source.java for type definitions.
64      * @param dd The bean Deployment Descriptor
65      * @param srcType the type of Velocity Context
66      * @return Return the Velocity Context associated to the gievn bean for the given source to generate
67      * @throws GenICException in error case
68      */

69     public static VelocityContext create(BeanDesc dd, int srcType) throws GenICException {
70
71         String JavaDoc 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); // 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         if (dd instanceof EntityDesc) {
147             Class JavaDoc c = ((EntityDesc) dd).getPrimaryKeyClass();
148             if (c.equals(Object JavaDoc.class)) {
149                 // Auto key generated so convert into Integer
150
c = Integer JavaDoc.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 JavaDoc.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 JavaDoc.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 JavaDoc.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 JavaDoc.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 JavaDoc.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 JavaDoc.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 JavaDoc.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 JavaDoc.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 JavaDoc prototypeMethodList = new ArrayList JavaDoc();
254         ArrayList JavaDoc vcMethodList = new ArrayList JavaDoc();
255         Method JavaDoc [] methods = null;
256         switch (srcType) {
257         case Source.CLUSTER_HOME:
258         case Source.HOME:
259             /*
260              * Add in the methods list, the Home interface methods, except
261              * - methods (other than remove) defined in javax.ejb.EJBHome,
262              * - and overriding methods.
263              */

264             methods = dd.getHomeClass().getMethods();
265             for (int i = 0; i < methods.length; i++) {
266                 Method JavaDoc method = methods[i];
267                 if (!method.getDeclaringClass().equals(javax.ejb.EJBHome JavaDoc.class)
268                     || "remove".equals(method.getName())) {
269                     String JavaDoc 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             /*
280              * Add in the methods list, the LocalHome interface methods, except
281              * - methods (other than remove) defined in javax.ejb.EJBLocalHome,
282              * - and overriding methods.
283              */

284             methods = dd.getLocalHomeClass().getMethods();
285             for (int i = 0; i < methods.length; i++) {
286                 Method JavaDoc method = methods[i];
287                 if (!method.getDeclaringClass().equals(javax.ejb.EJBLocalHome JavaDoc.class)
288                     || "remove".equals(method.getName())) {
289                     String JavaDoc 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             /*
300              * Add in the methods list, the Remote interface methods, except
301              * - methods (other than remove) defined in javax.ejb.EJBObject,
302              * - and overriding methods.
303              */

304             methods = dd.getRemoteClass().getMethods();
305             for (int i = 0; i < methods.length; i++) {
306                 Method JavaDoc method = methods[i];
307                 if (!method.getDeclaringClass().equals(javax.ejb.EJBObject JavaDoc.class)
308                     || "remove".equals(method.getName())) {
309                     String JavaDoc 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             /*
320              * Add in the methods list, the Local interface methods, except
321              * - methods (other than remove) defined in javax.ejb.EJBLocalObject,
322              * - and overriding methods.
323              */

324             methods = dd.getLocalClass().getMethods();
325             for (int i = 0; i < methods.length; i++) {
326                 Method JavaDoc method = methods[i];
327                 if (!method.getDeclaringClass().equals(javax.ejb.EJBLocalObject JavaDoc.class)
328                     || "remove".equals(method.getName())) {
329                     String JavaDoc 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             /*
340              * Add in the methods list, the ServiceEndpoint interface methods
341              */

342             SessionStatelessDesc ssd = (SessionStatelessDesc) dd;
343             methods = ssd.getServiceEndpointClass().getMethods();
344             for (int i = 0; i < methods.length; i++) {
345                 Method JavaDoc method = methods[i];
346                 String JavaDoc 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             /*
356              * Add in the methods list, the Home and LocalHome interfaces methods, except
357              * - methods defined in javax.ejb.EJBHome and javax.ejb.EJBLocalHome and
358              * - and overriding methods.
359              * Same methods may be defined both in the Home interface and in the
360              * LocalHome interface. Don't add twice this method in the vcMethodList !!!
361              *
362              * Futhermore, in case of create() methods, methods added in the vcMethodList
363              * are the ejbCreate() associated bean's methods.
364              */

365             if (dd.getHomeClass() != null) {
366                 methods = dd.getHomeClass().getMethods();
367                 for (int i = 0; i < methods.length; i++) {
368                     Method JavaDoc method = methods[i];
369                     if (!method.getDeclaringClass().equals(javax.ejb.EJBHome JavaDoc.class)) {
370                         String JavaDoc pMeth = convertMethod2String(method);
371                         if (!prototypeMethodList.contains(pMeth)) {
372                             VcMethod vm = null;
373                             if (method.getName().startsWith("create")) {
374                                 Method JavaDoc beanMethod = getBeanMethod(method, dd.getEjbClass());
375                                 vm = new VcMethod(beanMethod, dd.getMethodDesc(method), dd);
376                                 // add the ejbPostCreate method
377
Method JavaDoc 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 JavaDoc method = methods[i];
393                     if (!method.getDeclaringClass().equals(javax.ejb.EJBLocalHome JavaDoc.class)) {
394                         String JavaDoc pMeth = convertMethod2String(method);
395                         if (!prototypeMethodList.contains(pMeth)) {
396                             VcMethod vm = null;
397                             if (method.getName().startsWith("create")) {
398                                 Method JavaDoc beanMethod = getBeanMethod(method, dd.getEjbClass());
399                                 vm = new VcMethod(beanMethod, dd.getMethodDesc(method), dd);
400                                 // add the ejbPostCreate method
401
Method JavaDoc 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             /*
415              * Add the
416              * - setEntityContext(javax.ejb.EntityContext),
417              * - ejbActivate(),
418              * - ejbLoad(),
419              * - ejbStore(),
420              * - ejbRemove()
421              * bean's methods.
422              */

423             try {
424                 Class JavaDoc[] params = {javax.ejb.EntityContext JavaDoc.class};
425                 Method JavaDoc beanMethod = dd.getEjbClass().getMethod("setEntityContext",
426                                                                params);
427                 VcMethod vm = new VcMethod(beanMethod, null, dd);
428                 vcMethodList.add(vm);
429             } catch (Exception JavaDoc e) {
430                 throw new Error JavaDoc("setEntityContext(javax.ejb.EntityContext) method not defined in "
431                                 + dd.getEjbClass().getName());
432             }
433             try {
434                 Method JavaDoc beanMethod = dd.getEjbClass().getMethod("ejbActivate", new Class JavaDoc[0]);
435                 VcMethod vm = new VcMethod(beanMethod, null, dd);
436                 vcMethodList.add(vm);
437             } catch (Exception JavaDoc e) {
438                 throw new Error JavaDoc("ejbActivate() method not defined in "
439                                 + dd.getEjbClass().getName());
440             }
441             try {
442                 Method JavaDoc beanMethod = dd.getEjbClass().getMethod("ejbLoad", new Class JavaDoc[0]);
443                 VcMethod vm = new VcMethod(beanMethod, null, dd);
444                 vcMethodList.add(vm);
445             } catch (Exception JavaDoc e) {
446                 throw new Error JavaDoc("ejbLoad() method not defined in "
447                                 + dd.getEjbClass().getName());
448             }
449             try {
450                 Method JavaDoc beanMethod = dd.getEjbClass().getMethod("ejbStore", new Class JavaDoc[0]);
451                 VcMethod vm = new VcMethod(beanMethod, null, dd);
452                 vcMethodList.add(vm);
453             } catch (Exception JavaDoc e) {
454                 throw new Error JavaDoc("ejbStore() method not defined in "
455                                 + dd.getEjbClass().getName());
456             }
457             try {
458                 Method JavaDoc beanMethod = dd.getEjbClass().getMethod("ejbRemove", new Class JavaDoc[0]);
459                 VcMethod vm = new VcMethod(beanMethod, null, dd);
460                 vcMethodList.add(vm);
461             } catch (Exception JavaDoc e) {
462                 throw new Error JavaDoc("ejbRemove() method not defined in "
463                                 + dd.getEjbClass().getName());
464             }
465
466             if (dd instanceof EntityCmp2Desc) {
467                 /*
468                  * Add the ejbSelect() methods defined in the bean.
469                  */

470                 Method JavaDoc[] 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 JavaDoc(vcMethodList));
483
484         ArrayList JavaDoc vcFieldList = new ArrayList JavaDoc();
485         ArrayList JavaDoc vcFieldPkList = new ArrayList JavaDoc();
486         ArrayList JavaDoc vcFieldNoPkList = new ArrayList JavaDoc();
487         if (dd instanceof EntityCmp1Desc) {
488             EntityCmp1Desc edd = (EntityCmp1Desc) dd;
489             for (Iterator JavaDoc 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             // Define the CMP fields
502
Iterator JavaDoc 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             // Define the CMR fields
514
Hashtable JavaDoc cmrFields = new Hashtable JavaDoc();
515             for (Iterator JavaDoc i = edd.getEjbRelationshipRoleDescIterator(); i.hasNext();) {
516                 EjbRelationshipRoleDesc rsr = (EjbRelationshipRoleDesc) i.next();
517                 String JavaDoc 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 JavaDoc(vcFieldList));
526         vc.put("fieldPkList", new Vector JavaDoc(vcFieldPkList));
527         vc.put("fieldNoPkList", new Vector JavaDoc(vcFieldNoPkList));
528
529         return vc;
530     }
531
532     /**
533      * Convert a method to a string depending on its name and its parameters list.
534      * @param meth method to convert
535      * @return Return a string representation of the given method
536      */

537     private static String JavaDoc convertMethod2String(Method JavaDoc meth) {
538         String JavaDoc value = new String JavaDoc(meth.getName());
539         Class JavaDoc[] 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     /**
548      * @param method home.create() method
549      * @param bean class of the bean
550      * @return Return the associated bean's method ejbPostCreate matching the create method of the interface
551      */

552     private static Method JavaDoc getEjbPostCreateMethod(Method JavaDoc method, Class JavaDoc bean) {
553         Method JavaDoc beanMethod;
554         String JavaDoc beanMethodName = "ejbPost" + BeanNaming.firstToUpperCase(method.getName());
555         try {
556             beanMethod = bean.getMethod(beanMethodName, method.getParameterTypes());
557         } catch (Exception JavaDoc e) {
558             throw new Error JavaDoc("No associated ejbPostCreate method for the interface method " + method.toString());
559         }
560         return beanMethod;
561     }
562
563     /**
564      * Return the associated bean's method of the given interface's method, ie
565      * - ejbRemove(..) for [home|object]remove(..)
566      * - ejbCreateXxx(..) for home.createXxx(..)
567      * - ejbFindXxx(..) for home.findXxx(..)
568      * - ejbHomeXxx(..) for home.xxx(..)
569      * - xxx(..) for object.xxx(..)
570      * @param method interface method
571      * @param bean class of the bean
572      * @return Return the associated bean's method to the interface method
573      */

574     private static Method JavaDoc getBeanMethod(Method JavaDoc method, Class JavaDoc bean) {
575
576         String JavaDoc methodName = method.getName();
577         Method JavaDoc beanMethod;
578         boolean isMethodHome =
579             javax.ejb.EJBHome JavaDoc.class.isAssignableFrom(method.getDeclaringClass())
580             || javax.ejb.EJBLocalHome JavaDoc.class.isAssignableFrom(method.getDeclaringClass());
581
582         String JavaDoc 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 JavaDoc e) {
596             throw new Error JavaDoc("No associated bean's method for the interface method " + method.toString());
597         }
598         return beanMethod;
599     }
600
601     /**
602      * @param classname name of a class
603      * @return true if the class is a 'user' class, false if it's a java class
604      */

605     public static Boolean JavaDoc pkIsUserClass(String JavaDoc classname) {
606         if (classname.startsWith("java.")) {
607             return Boolean.FALSE;
608         } else {
609             return Boolean.TRUE;
610         }
611     }
612
613     /**
614      * @param vc VelocityContext to trace
615      * @return Return a string representation of the given VelocityContext for debug use
616      */

617     public static String JavaDoc toString(VelocityContext vc) {
618         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
619         Object JavaDoc [] keys = vc.internalGetKeys();
620         for (int i = 0; i < keys.length; i++) {
621             String JavaDoc key = (String JavaDoc) keys[i];
622             Object JavaDoc value = vc.internalGet(key);
623             if (i > 0) {
624                 ret.append("\n");
625             }
626             if (!"dd".equals(key)) {
627                 // Do not trace the BeanDesc (variable named "dd")
628
ret.append(" key = " + key + "; value = " + value);
629             }
630         }
631         return (ret.toString());
632     }
633
634 }
635
Popular Tags