KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > MethodMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28
29 import org.w3c.dom.Element JavaDoc;
30
31 import org.jboss.deployment.DeploymentException;
32 import org.jboss.invocation.InvocationType;
33
34 /** The combination of the method-permission, container-transaction
35  *
36  * <p>
37  * The method-permission element specifies that one or more security
38  * roles are allowed to invoke one or more enterprise bean methods. The
39  * method-permission element consists of an optional description, a list
40  * of security role names, or an indicator to specify that the methods
41  * are not to be checked for authorization, and a list of method elements.
42  * The security roles used in the method-permission element must be
43  * defined in the security-role element of the deployment descriptor,
44  * and the methods must be methods defined in the enterprise bean’s component
45  * and/or home interfaces.
46  * </p>
47  * <p>
48  * The container-transaction element specifies how the container must
49  * manage transaction scopes for the enterprise bean’s method invocations.
50  * The element consists of an optional description, a list of
51  * method elements, and a transaction attribute. The transaction
52  * attribute is to be applied to all the specified methods.
53  * </p>
54  *
55  * @author <a HREF="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
56  * @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
57  * @version $Revision: 58492 $
58  */

59 public class MethodMetaData extends MetaData
60 {
61    // Constants -----------------------------------------------------
62
// These method interface contstants are compatible with the Invocation.XXX values
63
public static final int ANY_METHOD = -1;
64    public static String JavaDoc HOME_TYPE = "Home";
65    public static String JavaDoc LOCAL_HOME_TYPE = "LocalHome";
66    public static String JavaDoc REMOTE_TYPE = "Remote";
67    public static String JavaDoc LOCAL_TYPE = "Local";
68    public static String JavaDoc SERVICE_ENDPOINT_TYPE = "ServiceEndpoint";
69
70    private static final ArrayList JavaDoc EMPTY_PARAM_LIST = new ArrayList JavaDoc();
71
72    // Attributes ----------------------------------------------------
73
/** The method-name element contains a name of an enterprise bean method,
74     * or the asterisk (*) character. The asterisk is used when the element
75     * denotes all the methods of an enterprise bean’s component and home
76     * interfaces.
77     */

78    private String JavaDoc methodName;
79    /** The ejb-name value the method applies to.
80     */

81    private String JavaDoc ejbName;
82
83    /** The method-intf element allows a method element to differentiate
84     * between the methods with the same name and signature that are multiply
85     * defined across the home and component interfaces (e.g., in both an
86     * enterprise bean’s remote and local interfaces, or in both an enter-prise
87     * bean’s home and remote interfaces, etc.)
88     * The method-intf element must be one of the following:
89     * <method-intf>Home</method-intf>
90     * <method-intf>Remote</method-intf>
91     * <method-intf>LocalHome</method-intf>
92     * <method-intf>Local</method-intf>
93     * <method-intf>ServiceEndpoint</method-intf>
94     */

95    private boolean intf = false;
96    /** One of: InvocationType
97     */

98    private InvocationType methodType = null;
99    private boolean param = false;
100    /** The unchecked element specifies that a method is not checked for
101     * authorization by the container prior to invocation of the method.
102     * Used in: method-permission
103     */

104    private boolean unchecked = false;
105    /** The exclude-list element defines a set of methods which the Assembler
106     * marks to be uncallable. It contains one or more methods. If the method
107     * permission relation contains methods that are in the exclude list, the
108     * Deployer should consider those methods to be uncallable.
109     */

110    private boolean excluded = false;
111    /** The method-params element contains a list of the fully-qualified Java
112     * type names of the method parameters.
113     */

114    private ArrayList JavaDoc paramList = EMPTY_PARAM_LIST;
115    /** The trans-attribute element specifies how the container must manage
116     * the transaction boundaries when delegating a method invocation to an
117     * enterprise bean’s business method.
118     * The value of trans-attribute must be one of the following:
119     * <trans-attribute>NotSupported</trans-attribute>
120     * <trans-attribute>Supports</trans-attribute>
121     * <trans-attribute>Required</trans-attribute>
122     * <trans-attribute>RequiresNew</trans-attribute>
123     * <trans-attribute>Mandatory</trans-attribute>
124     * <trans-attribute>Never</trans-attribute>
125     */

126    private byte transactionType;
127    /** Set<String> of the allowed role names */
128    private Set JavaDoc roles = new HashSet JavaDoc();
129    
130    // Static --------------------------------------------------------
131

132    // Constructors --------------------------------------------------
133
public MethodMetaData()
134    {
135    }
136    
137    // Public --------------------------------------------------------
138

139    public String JavaDoc getMethodName()
140    {
141       return methodName;
142    }
143
144    public void setMethodName(String JavaDoc methodName)
145    {
146       this.methodName = methodName;
147    }
148
149    public String JavaDoc getEjbName()
150    {
151       return ejbName;
152    }
153
154    public void setEjbName(String JavaDoc ejbName)
155    {
156       this.ejbName = ejbName;
157    }
158
159    public boolean isHomeMethod()
160    {
161       return methodType == InvocationType.HOME;
162    }
163
164    public boolean isRemoteMethod()
165    {
166       return methodType == InvocationType.REMOTE;
167    }
168
169    public boolean isLocalHomeMethod()
170    {
171       return methodType == InvocationType.LOCALHOME;
172    }
173
174    public boolean isLocalMethod()
175    {
176       return methodType == InvocationType.LOCAL;
177    }
178
179    public boolean isServiceEndpointMethod()
180    {
181       return methodType == InvocationType.SERVICE_ENDPOINT;
182    }
183
184    /** Return the interface type name.
185     *
186     * @return one of "Home", "LocalHome", "Remote", "Local", "ServiceEndpoint",
187     * or null if no interface was specified.
188     */

189    public String JavaDoc getInterfaceType()
190    {
191       String JavaDoc type = null;
192       if( isHomeMethod() )
193          type = HOME_TYPE;
194       if( isLocalHomeMethod() )
195          type = LOCAL_HOME_TYPE;
196       if( isRemoteMethod() )
197          type = REMOTE_TYPE;
198       if( isLocalMethod() )
199          type = LOCAL_TYPE;
200       if( isServiceEndpointMethod() )
201          type = SERVICE_ENDPOINT_TYPE;
202       return type;
203    }
204
205    public void setInterfaceType(String JavaDoc methodIntf)
206    {
207       if (methodIntf.equals("Home"))
208       {
209          methodType = InvocationType.HOME;
210       }
211       else if (methodIntf.equals("Remote"))
212       {
213          methodType = InvocationType.REMOTE;
214       }
215       else if (methodIntf.equals("LocalHome"))
216       {
217          methodType = InvocationType.LOCALHOME;
218       }
219       else if (methodIntf.equals("Local"))
220       {
221          methodType = InvocationType.LOCAL;
222       }
223       else if (methodIntf.equals("ServiceEndpoint"))
224       {
225          methodType = InvocationType.SERVICE_ENDPOINT;
226       }
227       else
228       {
229          throw new IllegalStateException JavaDoc("method-intf tag should be one of: 'Home', 'Remote', 'LocalHome', 'Local', 'ServiceEndpoint'");
230       }
231       intf = true;
232    }
233
234    public boolean isUnchecked()
235    {
236       return unchecked;
237    }
238
239    public boolean isExcluded()
240    {
241       return excluded;
242    }
243
244    public boolean isIntfGiven()
245    {
246       return intf;
247    }
248
249    public boolean isParamGiven()
250    {
251       return param;
252    }
253
254    /** The method param type names.
255     * @return
256     */

257    public Iterator JavaDoc getParams()
258    {
259       return paramList.iterator();
260    }
261    /** The
262     *
263     * @return An array of the method parameter type names
264     */

265    public String JavaDoc[] getMethodParams()
266    {
267       String JavaDoc[] params = new String JavaDoc[paramList.size()];
268       paramList.toArray(params);
269       return params;
270    }
271
272    public byte getTransactionType()
273    {
274       return transactionType;
275    }
276
277    public void setTransactionType(byte type)
278    {
279       transactionType = type;
280    }
281
282    public Set JavaDoc getRoles()
283    {
284       return roles;
285    }
286
287    public void setRoles(Set JavaDoc perm)
288    {
289       roles = perm;
290    }
291
292    public void setUnchecked()
293    {
294       unchecked = true;
295    }
296
297    public void setExcluded()
298    {
299       excluded = true;
300    }
301
302    public boolean patternMatches(String JavaDoc name, Class JavaDoc[] arg, InvocationType iface)
303    {
304       return patternMatches(name, getClassNames(arg), iface);
305    }
306
307    public boolean patternMatches(String JavaDoc name, String JavaDoc[] arg, InvocationType iface)
308    {
309       // the wildcard matches everything
310
if (getMethodName().equals("*"))
311       {
312          if (methodType != null && methodType != iface)
313             return false;
314          return true;
315       }
316
317       if (getMethodName().equals(name) == false)
318       {
319          // different names -> no
320
return false;
321       }
322       else
323       {
324          // we have the same name, next check the interface type
325
if (methodType != null && methodType != iface)
326             return false;
327
328          if (isParamGiven() == false)
329          {
330             // no param given in descriptor -> ok
331
return true;
332          }
333          else
334          {
335             // we *have* to check the parameters
336
return sameParams(arg);
337          }
338       }
339    }
340
341    public void addParam(String JavaDoc param)
342    {
343       if(paramList == null)
344       {
345          this.param = true;
346          paramList = new ArrayList JavaDoc();
347       }
348       paramList.add(param);
349    }
350
351    /**
352     * @param element method element
353     */

354    public void importEjbJarXml(Element JavaDoc element) throws DeploymentException
355    {
356       methodName = getElementContent(getUniqueChild(element, "method-name"));
357       ejbName = getElementContent(getUniqueChild(element, "ejb-name"));
358
359       Element JavaDoc intfElement = getOptionalChild(element, "method-intf");
360       if (intfElement != null)
361       {
362          String JavaDoc methodIntf = getElementContent(intfElement);
363          setInterfaceType(methodIntf);
364       }
365
366       Element JavaDoc paramsElement = getOptionalChild(element, "method-params");
367       if (paramsElement != null)
368       {
369          param = true;
370          paramList = new ArrayList JavaDoc();
371          Iterator JavaDoc paramsIterator = getChildrenByTagName(paramsElement, "method-param");
372          while (paramsIterator.hasNext())
373          {
374             paramList.add(getElementContent((Element JavaDoc) paramsIterator.next()));
375          }
376       }
377    }
378
379    // Package protected ---------------------------------------------
380

381    // Protected -----------------------------------------------------
382

383    // Private -------------------------------------------------------
384
private static String JavaDoc[] getClassNames(Class JavaDoc[] source)
385    {
386       String JavaDoc out[] = new String JavaDoc[source.length];
387       for (int i = 0; i < out.length; i++)
388       {
389          String JavaDoc brackets = "";
390          Class JavaDoc cls = source[i];
391          while (cls.isArray())
392          {
393             brackets += "[]";
394             cls = cls.getComponentType();
395          }
396          out[i] = cls.getName() + brackets;
397       }
398       return out;
399    }
400
401    private boolean sameParams(String JavaDoc[] arg)
402    {
403       if (arg.length != paramList.size()) return false;
404       for (int i = 0; i < arg.length; i++)
405          if (!arg[i].equals(paramList.get(i)))
406             return false;
407       return true;
408    }
409
410    public static byte getTransactionAttribute(String JavaDoc type)
411    {
412       if (type.equalsIgnoreCase("NotSupported") ||
413          type.equalsIgnoreCase("Not_Supported"))
414       {
415          return TX_NOT_SUPPORTED;
416       }
417       else if (type.equalsIgnoreCase("Supports"))
418       {
419          return TX_SUPPORTS;
420       }
421       else if (type.equalsIgnoreCase("Required"))
422       {
423          return TX_REQUIRED;
424       }
425       else if (type.equalsIgnoreCase("RequiresNew") ||
426          type.equalsIgnoreCase("Requires_New"))
427       {
428          return TX_REQUIRES_NEW;
429       }
430       else if (type.equalsIgnoreCase("Mandatory"))
431       {
432          return TX_MANDATORY;
433       }
434       else if (type.equalsIgnoreCase("Never"))
435       {
436          return TX_NEVER;
437       }
438
439       throw new IllegalStateException JavaDoc("invalid <transaction-attribute> : " + type);
440    }
441
442    // Inner classes -------------------------------------------------
443
}
444
Popular Tags