KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > deployment > api > MethodDesc


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: MethodDesc.java,v 1.20 2005/03/11 12:34:28 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27
28 package org.objectweb.jonas_ejb.deployment.api;
29
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.objectweb.jonas_ejb.deployment.xml.MethodParams;
36 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
37
38 /**
39  * Class to hold meta-information related to bean and home methods.
40  * @author Christophe Ney [cney@batisseurs.com] : Initial developer
41  * @author Helene Joanin: fix a bug about select method select method is a bean's method which name begins with ejbSelect,
42  * (and not home interface method as finder method).
43  */

44 public class MethodDesc {
45
46     /**
47      * Set of constants for method transaction attribute
48      */

49     public static final int TX_NULL = -1;
50     public static final int TX_NOT_SET = 0;
51     public static final int TX_NOT_SUPPORTED = 1;
52     public static final int TX_REQUIRED = 2;
53     public static final int TX_SUPPORTS = 3;
54     public static final int TX_REQUIRES_NEW = 4;
55     public static final int TX_MANDATORY = 5;
56     public static final int TX_NEVER = 6;
57
58
59     private static final String JavaDoc[] ATTR = {
60         "TX_NOT_SET",
61         "TX_NOT_SUPPORTED",
62         "TX_REQUIRED",
63         "TX_SUPPORTS",
64         "TX_REQUIRES_NEW",
65         "TX_MANDATORY",
66         "TX_NEVER"
67     };
68
69     /**
70      * default value for undefined transaction attribute for sessions and entities
71      */

72     protected static final String JavaDoc TX_STR_DEFAULT_VALUE = "Supports";
73
74     /**
75      * default value for undefined transaction attribute for message driven beans
76      */

77     protected static final String JavaDoc TX_STR_DEFAULT_VALUE_4_MDB = "NotSupported";
78
79     /**
80      * value for undefined transaction attribute
81      */

82     private int txAttribute = TX_NOT_SET;
83     private int txAttributeStatus = APPLY_TO_BEAN;
84
85     // Array of String representing the roles which can execute the method of this MethodDesc
86
private HashSet JavaDoc roleName = new HashSet JavaDoc();
87
88     public static final int APPLY_TO_NOTHING = 0;
89     public static final int APPLY_TO_BEAN = 1;
90     public static final int APPLY_TO_CLASS = 2;
91     public static final int APPLY_TO_BEAN_METHOD_NAME = 3;
92     public static final int APPLY_TO_CLASS_METHOD_NAME = 4;
93     public static final int APPLY_TO_BEAN_METHOD = 5;
94     public static final int APPLY_TO_CLASS_METHOD = 6;
95
96     protected static final String JavaDoc[] APPLY_TO = {
97         "APPLY_TO_NOTHING",
98         "APPLY_TO_BEAN",
99         "APPLY_TO_CLASS",
100         "APPLY_TO_BEAN_METHOD_NAME",
101         "APPLY_TO_CLASS_METHOD_NAME",
102         "APPLY_TO_BEAN_METHOD",
103         "APPLY_TO_CLASS_METHOD"
104     };
105
106     private Method JavaDoc meth;
107     private Class JavaDoc classDef;
108     private int index;
109
110     protected BeanDesc beanDesc;
111
112     private boolean isFinder = false;
113     private boolean isEjbSelect = false;
114     private boolean isFindByPrimaryKey = false;
115
116     /**
117      * This method was marked as excluded in ejb-jar.xml ?
118      */

119     private boolean excluded = false;
120
121     /**
122      * constructor to be used by parent node
123      */

124     public MethodDesc(BeanDesc beanDesc, Method JavaDoc meth, Class JavaDoc clDef, int index) {
125         this.meth = meth;
126         this.classDef = clDef;
127         this.index = index;
128         this.beanDesc = beanDesc;
129         isFindByPrimaryKey = MethodDesc.isFindByPrimaryKey(meth);
130         isFinder = isFinder(meth);
131         isEjbSelect = isEjbSelect(meth);
132     }
133
134     /**
135      * get a unique index of the method for the bean
136      */

137     public int getIndex() {
138         return index;
139     }
140
141     public void setIndex(int idx) {
142         index = idx;
143     }
144
145     /**
146      * access if the method is a finder
147      * @return true for finder methods
148      */

149     public boolean isFinder() {
150         return isFinder;
151     }
152
153     /**
154      * access if the method is <code>findByPrimaryKey</code>
155      * @return true for the <code>findByPrimaryKey</code> method
156      */

157     public boolean isFindByPrimaryKey() {
158         return isFindByPrimaryKey;
159     }
160
161
162     /**
163      * access if the method is a select
164      * @return true for select methods
165      */

166     public boolean isEjbSelect() {
167         return isEjbSelect;
168     }
169
170
171     /**
172      * Overwrite TxAttribute
173      * @param transAttribute new value for txAttribute
174      * @param status applicability of given transAttribute parameter
175      */

176     void overwriteTxAttribute(String JavaDoc transAttribute, int status) throws DeploymentDescException {
177         // overwrite only if numerical value greater than existing one
178
if (status < this.txAttributeStatus) {
179             return;
180         }
181         setTxAttribute(transAttribute);
182         txAttributeStatus = status;
183     }
184
185     /**
186      * Set TxAttribute with given value
187      */

188     void setTxAttribute(String JavaDoc transAttribute) throws DeploymentDescException {
189         if (transAttribute.equals("NotSupported")) {
190             txAttribute = TX_NOT_SUPPORTED;
191         } else if (transAttribute.equals("Required")) {
192             txAttribute = TX_REQUIRED;
193         } else if (transAttribute.equals("Supports")) {
194             txAttribute = TX_SUPPORTS;
195         } else if (transAttribute.equals("RequiresNew")) {
196             txAttribute = TX_REQUIRES_NEW;
197         } else if (transAttribute.equals("Mandatory")) {
198             txAttribute = TX_MANDATORY;
199         } else if (transAttribute.equals("Never")) {
200             txAttribute = TX_NEVER;
201         } else {
202             throw new DeploymentDescException(transAttribute
203                                               + " is not a valid trans-attribute value");
204         }
205     }
206
207     /**
208      * Add a role name to the role names which can execute the method
209      * @param rn role name to add
210      */

211     void addRoleName (String JavaDoc rn) {
212         roleName.add(rn);
213     }
214
215     /**
216      * Evaluate method pattern maching as defined in the EJB specifications
217      * @return one of the <code>APPLY_TO_*</code> values.
218      */

219     public int matchPattern(Class JavaDoc pclass, String JavaDoc mName, MethodParams patternMethodParams) {
220         return matchPattern(getMethod(), classDef, pclass, mName, patternMethodParams);
221     }
222
223     /**
224      * Get the status of applicability for a given pattern to a method
225      * @return status of applicability APPLY_TO_NOTHING,APPLY_TO_BEAN,APPLY_TO_CLASS,APPLY_TO_METHOD_NAME,APPLY_TO_METHOD
226      */

227     public static int matchPattern(java.lang.reflect.Method JavaDoc meth,
228                                    Class JavaDoc classMeth,
229                                    Class JavaDoc pclass,
230                                    String JavaDoc mName,
231                                    MethodParams patternMethodParams) {
232
233         // If pclass don't match -> APPLY_TO_NOTHING
234
if (pclass != null && !pclass.isAssignableFrom(classMeth)) {
235             return APPLY_TO_NOTHING;
236         }
237
238         // class is enough
239
if (mName.equals("*")) {
240             return (pclass == null) ? APPLY_TO_BEAN : APPLY_TO_CLASS;
241         }
242
243         // method name does not match
244
if (!mName.equals(meth.getName())) {
245             return APPLY_TO_NOTHING;
246         }
247
248         // no params specified (name test is enough)
249
if (patternMethodParams == null) {
250             return (pclass == null) ? APPLY_TO_BEAN_METHOD_NAME : APPLY_TO_CLASS_METHOD_NAME;
251         }
252
253         Class JavaDoc pars[] = meth.getParameterTypes();
254         List JavaDoc pattPars = patternMethodParams.getMethodParamList();
255         // number of parameters does not match
256
if (pars.length != pattPars.size()) {
257             return APPLY_TO_NOTHING;
258         }
259         Iterator JavaDoc i = pattPars.iterator();
260         for (int ii = 0; ii < pars.length; ii++) {
261             String JavaDoc cName = (String JavaDoc) i.next();
262             if (!getClassName(pars[ii]).equals(cName)) {
263                 return APPLY_TO_NOTHING;
264             }
265         }
266         return (pclass == null) ? APPLY_TO_BEAN_METHOD : APPLY_TO_CLASS_METHOD;
267     }
268
269     /**
270      * Returns common name of a given type <BR>
271      * For example it returns int[] for an array of int
272      * @return String with the name of the given type
273      */

274     private static String JavaDoc getClassName(Class JavaDoc c) {
275         String JavaDoc name;
276         if (c.isArray()) {
277             name = getClassName(c.getComponentType()) + "[]";
278         } else {
279             name = c.getName();
280         }
281         return (name);
282     }
283
284
285
286     /**
287      * Get the container transaction attribute that match the method
288      * @return Constant value within list :
289      * TX_NOT_SUPPORTED,TX_REQUIRED,TX_SUPPORTS,TX_REQUIRES_NEW,TX_MANDATORY,TX_NEVER,TX_NOT_SET
290      */

291     public int getTxAttribute() {
292         return txAttribute;
293     }
294
295     /**
296      * Get the container transaction attribute that match the method
297      * @return Constant value within list :
298      * APPLY_TO_NOTHING, APPLY_TO_BEAN, APPLY_TO_CLASS, APPLY_TO_BEAN_METHOD_NAME,
299      * APPLY_TO_CLASS_METHOD_NAME, APPLY_TO_BEAN_METHOD, APPLY_TO_CLASS_METHOD
300      */

301     public int getTxAttributeStatus() {
302         return txAttributeStatus;
303     }
304
305     /**
306      * String representation of the transactionnal attribute
307      * @return String representation of this transactionnal attribute
308      */

309     public static String JavaDoc getTxAttributeName(int value) {
310         if ((value < 0) || (value > ATTR.length)) {
311             throw new Error JavaDoc(value + " is not a valid TxAttribute");
312         }
313         return ATTR[value];
314     }
315
316     /**
317      * String representation of the transactionnal attribute
318      * @return String representation of this transactionnal attribute
319      */

320     public String JavaDoc getTxAttributeName() {
321         return ATTR[txAttribute];
322     }
323
324     /**
325      * String representation of the roles which can execute the method
326      * @return Array of String representing the roles which can execute the method
327      */

328     public String JavaDoc[] getRoleName () {
329         if (roleName.isEmpty()) {
330             return new String JavaDoc[0];
331         }
332         Object JavaDoc[] o = roleName.toArray();
333         String JavaDoc[] rn = new String JavaDoc[o.length];
334         for (int i = 0; i < rn.length; i++) {
335             rn[i] = (String JavaDoc) o[i];
336         }
337         return rn;
338     }
339
340     /**
341      * String representation of the given element <method>
342      * @param m an element <method>
343      * @return String representation of the given element method
344      */

345     public static String JavaDoc methodElementToString(org.objectweb.jonas_ejb.deployment.xml.Method m) {
346         return methodElementToString(m.getMethodIntf(), m.getMethodName(), m.getMethodParams());
347     }
348
349     /**
350      * get a String representation of a method from it's XML representation
351      */

352     protected static String JavaDoc methodElementToString(String JavaDoc intf, String JavaDoc name, MethodParams params) {
353         String JavaDoc s = new String JavaDoc();
354         if (intf != null) {
355             s = s.concat(intf + ".");
356         }
357         s = s.concat(name);
358         if (params != null) {
359             s = s.concat("(");
360             for (Iterator JavaDoc i = params.getMethodParamList().iterator(); i.hasNext();) {
361                 s = s.concat((String JavaDoc) i.next());
362                 if (i.hasNext()) {
363                     s = s.concat(",");
364                 }
365             }
366             s = s.concat(")");
367         }
368         return (s);
369     }
370
371     /**
372      * get a String representation of a method from the reflection object
373      */

374     public static String JavaDoc toString(Method JavaDoc m) {
375         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
376         ret.append(m.getDeclaringClass().getName());
377         ret.append('.');
378         ret.append(m.getName());
379         ret.append('(');
380         Class JavaDoc[] params = m.getParameterTypes();
381         for (int i = 0; i < params.length; i++) {
382             ret.append(getClassName(params[i]));
383             if (i == params.length - 1) {
384                 break;
385             }
386             ret.append(',');
387         }
388         ret.append(')');
389         return ret.toString();
390     }
391
392     /**
393      * return the method to which the meta-information applies
394      */

395     public Method JavaDoc getMethod() {
396         return meth;
397     }
398
399     /**
400      * get the parent node
401      */

402     public BeanDesc getBeanDesc() {
403         return beanDesc;
404     }
405
406     /**
407      * access if a method is a finder
408      */

409     public static boolean isFinder(Method JavaDoc meth) {
410         return (meth.getName().startsWith("find")
411                 && (javax.ejb.EJBHome JavaDoc.class.isAssignableFrom(meth.getDeclaringClass())
412                     || javax.ejb.EJBLocalHome JavaDoc.class.isAssignableFrom(meth.getDeclaringClass())));
413     }
414
415     /**
416      * access if a method is findByPrimaryKey
417      */

418     public static boolean isFindByPrimaryKey(Method JavaDoc meth) {
419         return (meth.getName().equals("findByPrimaryKey")
420                 && (javax.ejb.EJBHome JavaDoc.class.isAssignableFrom(meth.getDeclaringClass())
421                     || javax.ejb.EJBLocalHome JavaDoc.class.isAssignableFrom(meth.getDeclaringClass())));
422     }
423
424     /**
425      * access if a method is a select
426      */

427     public static boolean isEjbSelect(Method JavaDoc meth) {
428         return (meth.getName().startsWith("ejbSelect")
429                 && javax.ejb.EntityBean JavaDoc.class.isAssignableFrom(meth.getDeclaringClass()));
430     }
431
432     /**
433      * String representation of the object for test purpose
434      * @return String representation of this object
435      */

436     public String JavaDoc toString() {
437         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
438         ret.append("\ngetTxAttribute() = " + ATTR[getTxAttribute()]);
439         ret.append("\ngetTxAttributeStatus() = " + APPLY_TO[getTxAttributeStatus()]);
440         ret.append("\nMethodIndex = " + index);
441         ret.append("\nmeth = " + toString(meth));
442         ret.append("\nisFinder = " + isFinder);
443         ret.append("\nisEjbSelect = " + isEjbSelect);
444         ret.append("\nisFindByPrimaryKey = " + isFindByPrimaryKey);
445         if (!roleName.isEmpty()) {
446             ret.append("\ngetRoleName() = [");
447             String JavaDoc [] rn = getRoleName();
448             for (int i = 0; i < rn.length - 1; i++) {
449                 ret.append (rn[i] + ", ");
450             }
451             ret.append(rn[rn.length - 1] + "]");
452             ret.append("\n");
453         }
454         return ret.toString();
455     }
456
457     /**
458      * @return true if this method is excluded (in DD), else false
459      */

460     public boolean isExcluded() {
461         return excluded;
462     }
463     /**
464      * Sets the excluded attribute.
465      * @param excluded true of false
466      */

467     public void setExcluded(boolean excluded) {
468         this.excluded = excluded;
469     }
470 }
471
472
473
474
475
476
477
Popular Tags