KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > metadata > 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.metadata;
23
24 import javax.management.MBeanAttributeInfo JavaDoc;
25 import javax.management.MBeanOperationInfo JavaDoc;
26 import javax.management.MBeanParameterInfo JavaDoc;
27 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 /**
32  * Helper class for resolving JMX *Info objects against Method objects. It's typically
33  * used during the construction of dispatchers during MBean registration/creation.
34  *
35  * If you're looking for a fast resolver of JMX operation signatures see AttributeOperationResolver.
36  *
37  * FIXME - this class is not aware of multiple target objects (as in modelmbeans)
38  * however I'm half expecting that feature to disappear in JMX 1.1 anyhow.
39  *
40  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
41  */

42
43 public class MethodMapper
44 {
45    private HashMap JavaDoc map = null;
46
47    /**
48     * Constructs a mapper by reflecting on the class.
49     */

50    public MethodMapper(Class JavaDoc resourceClass)
51    {
52       if (null == resourceClass)
53       {
54          throw new IllegalArgumentException JavaDoc("resourceClass cannot be null");
55       }
56
57       map = createMap(resourceClass);
58    }
59
60    /**
61     * Return a method matching the signature defined in the operation info
62     */

63    public Method JavaDoc lookupOperation(MBeanOperationInfo JavaDoc info)
64    {
65       if (null == info)
66       {
67          throw new IllegalArgumentException JavaDoc("MBeanOperationInfo cannot be null");
68       }
69
70       return (Method JavaDoc) map.get(operationSignature(info));
71    }
72
73    /**
74     * Return a method matching the getter signature expected for an attribute.
75     */

76    public Method JavaDoc lookupGetter(MBeanAttributeInfo JavaDoc info)
77    {
78       if (null == info)
79       {
80          throw new IllegalArgumentException JavaDoc("MBeanAttributeInfo cannot be null");
81       }
82
83       return (Method JavaDoc) map.get(getterSignature(info));
84    }
85
86    /**
87     * Return a method matching the getter signature expected for a ModelMBean attribute.
88     */

89    public Method JavaDoc lookupGetter(ModelMBeanAttributeInfo JavaDoc info)
90    {
91       if (null == info)
92       {
93          throw new IllegalArgumentException JavaDoc("MBeanAttributeInfo cannot be null");
94       }
95
96       return (Method JavaDoc) map.get(getterSignature(info));
97    }
98
99    /**
100     * Return a method matching the setter signature expected for an attribute
101     */

102    public Method JavaDoc lookupSetter(MBeanAttributeInfo JavaDoc info)
103    {
104       if (null == info)
105       {
106          throw new IllegalArgumentException JavaDoc("MBeanAttributeInfo cannot be null");
107       }
108
109       return (Method JavaDoc) map.get(setterSignature(info));
110    }
111
112    /**
113     * Return a method matching the setter signature expected for a ModelMBean attribute
114     */

115    public Method JavaDoc lookupSetter(ModelMBeanAttributeInfo JavaDoc info)
116    {
117       if (null == info)
118       {
119          throw new IllegalArgumentException JavaDoc("ModelMBeanAttributeInfo cannot be null");
120       }
121
122       return (Method JavaDoc) map.get(setterSignature(info));
123    }
124
125    /**
126     * Return a method matching the specified signature
127     */

128    public Method JavaDoc lookupMethod(String JavaDoc returnType, String JavaDoc name, String JavaDoc[] signature)
129    {
130       if (null == returnType)
131       {
132          throw new IllegalArgumentException JavaDoc("returnType cannot be null");
133       }
134
135       if (null == name)
136       {
137          throw new IllegalArgumentException JavaDoc("method name cannot be null");
138       }
139
140       return (Method JavaDoc) map.get(methodSignature(returnType, name, signature));
141    }
142
143    /**
144     * Generates a signature string for an attribute getter method using standard rules
145     */

146    public static String JavaDoc getterSignature(MBeanAttributeInfo JavaDoc info)
147    {
148       if (null == info)
149       {
150          throw new IllegalArgumentException JavaDoc("MBeanAttributeInfo cannot be null");
151       }
152
153       String JavaDoc prefix = (info.isIs()) ? "is" : "get";
154       return methodSignature(info.getType(), prefix + info.getName(), null);
155    }
156
157    /**
158     * Generates a getter signature string for a ModelMBean attribute by checking the
159     * descriptor for getMethod.
160     */

161    public static String JavaDoc getterSignature(ModelMBeanAttributeInfo JavaDoc info)
162    {
163       if (null == info)
164       {
165          throw new IllegalArgumentException JavaDoc("ModelMBeanAttributeInfo cannot be null");
166       }
167
168       String JavaDoc methodName = (String JavaDoc) info.getDescriptor().getFieldValue("getMethod");
169
170       if (null == methodName)
171       {
172          throw new IllegalArgumentException JavaDoc("getMethod not defined in ModelMBeanAttributeInfo descriptor");
173       }
174
175       return methodSignature(info.getType(), methodName, null);
176    }
177
178    /**
179     * Generates a signature string for an attribute setter method using standard rules
180     */

181    public static String JavaDoc setterSignature(MBeanAttributeInfo JavaDoc info)
182    {
183       if (null == info)
184       {
185          throw new IllegalArgumentException JavaDoc("MBeanAttributeInfo cannot be null");
186       }
187
188       return methodSignature(Void.TYPE.getName(), "set" + info.getName(), new String JavaDoc[]{info.getType()});
189    }
190
191    /**
192     * Generates a setter signature string for a ModelMBean attribute by checking the
193     * descriptor for setMethod.
194     */

195    public static String JavaDoc setterSignature(ModelMBeanAttributeInfo JavaDoc info)
196    {
197       if (null == info)
198       {
199          throw new IllegalArgumentException JavaDoc("ModelMBeanAttributeInfo cannot be null");
200       }
201
202       String JavaDoc methodName = (String JavaDoc) info.getDescriptor().getFieldValue("setMethod");
203
204       if (null == methodName)
205       {
206          throw new IllegalArgumentException JavaDoc("setMethod not defined in ModelMBeanAttributeInfo descriptor");
207       }
208
209       return methodSignature(Void.TYPE.getName(), methodName, new String JavaDoc[]{info.getType()});
210    }
211
212    /**
213     * Generates a signature string using the operation info.
214     */

215    public static String JavaDoc operationSignature(MBeanOperationInfo JavaDoc info)
216    {
217       if (null == info)
218       {
219          throw new IllegalArgumentException JavaDoc("MBeanOperationInfo cannot be null");
220       }
221
222       MBeanParameterInfo JavaDoc[] params = info.getSignature();
223       String JavaDoc[] signature = new String JavaDoc[params.length];
224       for (int i = 0; i < signature.length; i++)
225       {
226          signature[i] = params[i].getType();
227       }
228       return methodSignature(info.getReturnType(), info.getName(), signature);
229    }
230
231    /**
232     * Generates a signature string using a Method object.
233     */

234    public static String JavaDoc methodSignature(Method JavaDoc method)
235    {
236       if (null == method)
237       {
238          throw new IllegalArgumentException JavaDoc("Method cannot be null");
239       }
240
241       Class JavaDoc[] paramtypes = method.getParameterTypes();
242       String JavaDoc[] signature = new String JavaDoc[paramtypes.length];
243       for (int i = 0; i < signature.length; i++)
244       {
245          signature[i] = paramtypes[i].getName();
246       }
247       return methodSignature(method.getReturnType().getName(), method.getName(), signature);
248    }
249
250    /**
251     * Generates a signature string using the supplied signature arguments.
252     */

253    public static String JavaDoc methodSignature(String JavaDoc returnType, String JavaDoc name, String JavaDoc[] signature)
254    {
255       if (null == returnType)
256       {
257          throw new IllegalArgumentException JavaDoc("returnType cannot be null");
258       }
259       if (null == name)
260       {
261          throw new IllegalArgumentException JavaDoc("method name cannot be null");
262       }
263
264       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(returnType).append(';').append(name);
265       if (null == signature)
266       {
267          return buf.toString();
268       }
269
270       for (int i = 0; i < signature.length; i++)
271       {
272          buf.append(';').append(signature[i]); // the ; char ensures uniqueness
273
}
274
275       return buf.toString();
276    }
277
278    /**
279     * creates the signature string to Method HashMap.
280     */

281    protected HashMap JavaDoc createMap(Class JavaDoc resourceClass)
282    {
283       HashMap JavaDoc cmap = new HashMap JavaDoc();
284       Method JavaDoc[] methods = resourceClass.getMethods();
285       for (int i = 0; i < methods.length; i++)
286       {
287          cmap.put(methodSignature(methods[i]), methods[i]);
288       }
289       return cmap;
290    }
291 }
292
Popular Tags