KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > server > MethodMapper


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.mx.server;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import javax.management.MBeanAttributeInfo JavaDoc;
28 import javax.management.MBeanOperationInfo JavaDoc;
29 import javax.management.MBeanParameterInfo JavaDoc;
30 import javax.management.Descriptor JavaDoc;
31 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
32 import org.jboss.mx.modelmbean.ModelMBeanConstants;
33
34 /**
35  * Helper class for resolving JMX *Info objects against Method objects. It's typically
36  * used during the construction of dispatchers during MBean registration/creation.
37  *
38  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
39  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>
40  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
41  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
42  * @version $Revision: 43655 $
43  */

44 public class MethodMapper
45 {
46    // Attributes ----------------------------------------------------
47

48    private HashMap JavaDoc map = null;
49    
50    // Static --------------------------------------------------------
51

52    /**
53     * Generates a signature string for an attribute getter method using standard rules
54     */

55    public static String JavaDoc getterSignature(MBeanAttributeInfo JavaDoc info)
56    {
57       if (null == info)
58       {
59          throw new IllegalArgumentException JavaDoc("MBeanAttributeInfo cannot be null");
60       }
61
62       String JavaDoc sig = null;
63       if( info instanceof ModelMBeanAttributeInfo JavaDoc )
64       {
65          ModelMBeanAttributeInfo JavaDoc minfo = (ModelMBeanAttributeInfo JavaDoc) info;
66          Descriptor JavaDoc desc = minfo.getDescriptor();
67          String JavaDoc methodName = (String JavaDoc) desc.getFieldValue(ModelMBeanConstants.GET_METHOD);
68          if( methodName != null )
69             sig = methodSignature(info.getType(), methodName, null);
70       }
71       if( sig == null )
72       {
73          String JavaDoc prefix = (info.isIs()) ? "is" : "get";
74          sig = methodSignature(info.getType(), prefix + info.getName(), null);
75       }
76       return sig;
77    }
78
79    /**
80     * Generates a signature string for an attribute setter method using standard rules
81     */

82    public static String JavaDoc setterSignature(MBeanAttributeInfo JavaDoc info)
83    {
84       if (null == info)
85       {
86          throw new IllegalArgumentException JavaDoc("MBeanAttributeInfo cannot be null");
87       }
88
89       String JavaDoc sig = null;
90       if( info instanceof ModelMBeanAttributeInfo JavaDoc )
91       {
92          ModelMBeanAttributeInfo JavaDoc minfo = (ModelMBeanAttributeInfo JavaDoc) info;
93          Descriptor JavaDoc desc = minfo.getDescriptor();
94          String JavaDoc methodName = (String JavaDoc) desc.getFieldValue(ModelMBeanConstants.SET_METHOD);
95          String JavaDoc[] typeSig = {info.getType()};
96          if( methodName != null )
97             sig = methodSignature(Void.TYPE.getName(), methodName, typeSig);
98       }
99       if( sig == null )
100       {
101          String JavaDoc[] typeSig = {info.getType()};
102          sig = methodSignature(Void.TYPE.getName(), "set" + info.getName(), typeSig);
103       }
104       return sig;
105    }
106
107    /**
108     * Generates a signature string using the operation info.
109     */

110    public static String JavaDoc operationSignature(MBeanOperationInfo JavaDoc info)
111    {
112       if (null == info)
113       {
114          throw new IllegalArgumentException JavaDoc("MBeanOperationInfo cannot be null");
115       }
116
117       MBeanParameterInfo JavaDoc[] params = info.getSignature();
118       String JavaDoc[] signature = new String JavaDoc[params.length];
119       for (int i = 0; i < signature.length; i++)
120       {
121          signature[i] = params[i].getType();
122       }
123       return methodSignature(info.getReturnType(), info.getName(), signature);
124    }
125
126    /**
127     * Generates a signature string using a Method object.
128     */

129    public static String JavaDoc methodSignature(Method JavaDoc method)
130    {
131       if (null == method)
132       {
133          throw new IllegalArgumentException JavaDoc("Method cannot be null");
134       }
135
136       Class JavaDoc[] paramtypes = method.getParameterTypes();
137       String JavaDoc[] signature = new String JavaDoc[paramtypes.length];
138       for (int i = 0; i < signature.length; i++)
139       {
140          signature[i] = paramtypes[i].getName();
141       }
142       return methodSignature(method.getReturnType().getName(), method.getName(), signature);
143    }
144
145    /**
146     * Generates a signature string using the supplied signature arguments.
147     */

148    public static String JavaDoc methodSignature(String JavaDoc returnType, String JavaDoc name, String JavaDoc[] signature)
149    {
150       if (null == returnType)
151       {
152          throw new IllegalArgumentException JavaDoc("returnType cannot be null");
153       }
154       if (null == name)
155       {
156          throw new IllegalArgumentException JavaDoc("method name cannot be null");
157       }
158
159       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(returnType).append(';').append(name);
160       if (null == signature)
161       {
162          return buf.toString();
163       }
164
165       for (int i = 0; i < signature.length; i++)
166       {
167          buf.append(';').append(signature[i]); // the ; char ensures uniqueness
168
}
169
170       return buf.toString();
171    }
172
173    /** Used to see if a ModelMBean has the operation in question.
174     *
175     * @param info
176     * @param mbean
177     * @return The mbean method if found, null otherwise
178     */

179    public static Method JavaDoc lookupOperation(MBeanOperationInfo JavaDoc info, Object JavaDoc mbean)
180    {
181       Class JavaDoc mbeanClass = mbean.getClass();
182       Method JavaDoc m = null;
183       try
184       {
185          ClassLoader JavaDoc loader = mbeanClass.getClassLoader();
186          MBeanParameterInfo JavaDoc[] params = info.getSignature();
187          Class JavaDoc[] signature = new Class JavaDoc[params.length];
188          for (int i = 0; i < signature.length; i++)
189          {
190             Class JavaDoc type = loader.loadClass(params[i].getType());
191             signature[i] = type;
192          }
193          m = mbeanClass.getMethod(info.getName(), signature);
194          // The modelmbean does not provide methods from object
195
if (Object JavaDoc.class.equals(m.getDeclaringClass()))
196             m = null;
197       }
198       catch(Exception JavaDoc e)
199       {
200       }
201
202       return m;
203    }
204    
205    // Constructors --------------------------------------------------
206

207    /**
208     * Constructs a mapper by reflecting on the class.
209     */

210    public MethodMapper(Class JavaDoc resourceClass)
211    {
212       map = createMap(resourceClass);
213    }
214
215    
216    // Public --------------------------------------------------------
217

218    /**
219     * Return a method matching the signature defined in the operation info
220     */

221    public Method JavaDoc lookupOperation(MBeanOperationInfo JavaDoc info)
222    {
223       if (null == info)
224       {
225          throw new IllegalArgumentException JavaDoc("MBeanOperationInfo cannot be null");
226       }
227
228       return (Method JavaDoc) map.get(operationSignature(info));
229    }
230
231    /**
232     * Return a method matching the getter signature expected for an attribute.
233     */

234    public Method JavaDoc lookupGetter(MBeanAttributeInfo JavaDoc info)
235    {
236       if (null == info)
237       {
238          throw new IllegalArgumentException JavaDoc("MBeanAttributeInfo cannot be null");
239       }
240
241       return (Method JavaDoc) map.get(getterSignature(info));
242    }
243
244    /**
245     * Return a method matching the setter signature expected for an attribute
246     */

247    public Method JavaDoc lookupSetter(MBeanAttributeInfo JavaDoc info)
248    {
249       if (null == info)
250       {
251          throw new IllegalArgumentException JavaDoc("MBeanAttributeInfo cannot be null");
252       }
253
254       return (Method JavaDoc) map.get(setterSignature(info));
255    }
256
257    /**
258     * Return a method matching the specified signature
259     */

260    public Method JavaDoc lookupMethod(String JavaDoc returnType, String JavaDoc name, String JavaDoc[] signature)
261    {
262       if (null == returnType)
263       {
264          throw new IllegalArgumentException JavaDoc("returnType cannot be null");
265       }
266
267       if (null == name)
268       {
269          throw new IllegalArgumentException JavaDoc("method name cannot be null");
270       }
271
272       return (Method JavaDoc) map.get(methodSignature(returnType, name, signature));
273    }
274
275    public String JavaDoc toString()
276    {
277       return map.toString();
278    }
279    
280    // Protected -----------------------------------------------------
281

282    /**
283     * creates the signature string to Method HashMap.
284     */

285    protected HashMap JavaDoc createMap(Class JavaDoc resourceClass)
286    {
287       HashMap JavaDoc cmap = new HashMap JavaDoc();
288       if (resourceClass != null)
289       {
290          Method JavaDoc[] methods = resourceClass.getMethods();
291          for (int i = 0; i < methods.length; i++)
292          {
293             Method JavaDoc method = methods[i];
294             cmap.put(methodSignature(method), method);
295          }
296       }
297       return cmap;
298    }
299    
300 }
301
Popular Tags