KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > metadata > MethodConfig


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.aop.metadata;
23
24 import javassist.CtClass;
25 import javassist.CtMethod;
26 import javassist.NotFoundException;
27 import org.jboss.aop.util.XmlHelper;
28 import org.w3c.dom.Element JavaDoc;
29
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.Iterator JavaDoc;
32
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  *
48  * @author <a HREF="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
49  * @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
50  * @version $Revision: 37406 $
51  */

52 public class MethodConfig
53 {
54    String JavaDoc methodName = null;
55    String JavaDoc signature = null;
56    // Constructors --------------------------------------------------
57
public MethodConfig()
58    {
59    }
60
61    // Public --------------------------------------------------------
62

63    public boolean patternMatches(Method method)
64    {
65       // the wildcard matches everything
66
if (methodName.equals("*"))
67       {
68          return true;
69       }
70
71       if ( methodName.equals(method.getName()) == false )
72       {
73          // different names -> no
74
return false;
75       }
76       if (signature == null) return true;
77
78       String JavaDoc sig = getSignature(method.getParameterTypes());
79       return sig.equals(signature);
80    }
81
82    public boolean patternMatches(CtMethod method) throws NotFoundException
83    {
84       // the wildcard matches everything
85
if (methodName.equals("*"))
86       {
87          return true;
88       }
89
90       if ( methodName.equals(method.getName()) == false )
91       {
92          // different names -> no
93
return false;
94       }
95       if (signature == null) return true;
96
97       String JavaDoc sig = getSignature(method.getParameterTypes());
98       return sig.equals(signature);
99    }
100
101    public void importXml(Element JavaDoc element)
102       throws Exception JavaDoc
103    {
104       methodName = XmlHelper.getElementContent(XmlHelper.getUniqueChild(element, "method-name"));
105       signature = "(";
106       Element JavaDoc paramsElement = XmlHelper.getOptionalChild(element, "method-params");
107       if (paramsElement != null)
108       {
109          Iterator JavaDoc paramsIterator = XmlHelper.getChildrenByTagName(paramsElement,
110                                                                   "method-param");
111          while (paramsIterator.hasNext())
112          {
113             signature += XmlHelper.getElementContent((Element JavaDoc)paramsIterator.next());
114             signature += " ";
115          }
116       }
117       signature += ")";
118    }
119
120    public static String JavaDoc toGenericString(Method method)
121    {
122       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
123       buf.append(method.getName());
124       buf.append("(");
125       getSignature(method.getParameterTypes());
126       buf.append(")");
127       return buf.toString();
128    }
129
130    public static String JavaDoc getSignature(Class JavaDoc[] source)
131    {
132       StringBuffer JavaDoc sig = new StringBuffer JavaDoc("(");
133       for( int i=0; i < source.length; i++ )
134       {
135          String JavaDoc brackets = "";
136          Class JavaDoc cls = source[i];
137          while(cls.isArray())
138          {
139             brackets += "[]";
140             cls = cls.getComponentType();
141          }
142          sig.append(cls.getName());
143          sig.append(brackets);
144          sig.append(" ");
145       }
146       sig.append(")");
147       return sig.toString();
148    }
149
150    public static String JavaDoc getSignature(CtClass[] source)
151    {
152       StringBuffer JavaDoc sig = new StringBuffer JavaDoc("(");
153       for( int i=0; i < source.length; i++ )
154       {
155          CtClass cls = source[i];
156          sig.append(cls.getName());
157          sig.append(" ");
158       }
159       sig.append(")");
160       return sig.toString();
161    }
162
163    // Inner classes -------------------------------------------------
164
}
165
Popular Tags