001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.metadata;
023:
024: import javax.management.MBeanAttributeInfo;
025: import javax.management.MBeanOperationInfo;
026: import javax.management.MBeanParameterInfo;
027: import javax.management.modelmbean.ModelMBeanAttributeInfo;
028: import java.lang.reflect.Method;
029: import java.util.HashMap;
030:
031: /**
032: * Helper class for resolving JMX *Info objects against Method objects. It's typically
033: * used during the construction of dispatchers during MBean registration/creation.
034: *
035: * If you're looking for a fast resolver of JMX operation signatures see AttributeOperationResolver.
036: *
037: * FIXME - this class is not aware of multiple target objects (as in modelmbeans)
038: * however I'm half expecting that feature to disappear in JMX 1.1 anyhow.
039: *
040: * @author <a href="mailto:trevor@protocool.com">Trevor Squires</a>.
041: */
042:
043: public class MethodMapper {
044: private HashMap map = null;
045:
046: /**
047: * Constructs a mapper by reflecting on the class.
048: */
049: public MethodMapper(Class resourceClass) {
050: if (null == resourceClass) {
051: throw new IllegalArgumentException(
052: "resourceClass cannot be null");
053: }
054:
055: map = createMap(resourceClass);
056: }
057:
058: /**
059: * Return a method matching the signature defined in the operation info
060: */
061: public Method lookupOperation(MBeanOperationInfo info) {
062: if (null == info) {
063: throw new IllegalArgumentException(
064: "MBeanOperationInfo cannot be null");
065: }
066:
067: return (Method) map.get(operationSignature(info));
068: }
069:
070: /**
071: * Return a method matching the getter signature expected for an attribute.
072: */
073: public Method lookupGetter(MBeanAttributeInfo info) {
074: if (null == info) {
075: throw new IllegalArgumentException(
076: "MBeanAttributeInfo cannot be null");
077: }
078:
079: return (Method) map.get(getterSignature(info));
080: }
081:
082: /**
083: * Return a method matching the getter signature expected for a ModelMBean attribute.
084: */
085: public Method lookupGetter(ModelMBeanAttributeInfo info) {
086: if (null == info) {
087: throw new IllegalArgumentException(
088: "MBeanAttributeInfo cannot be null");
089: }
090:
091: return (Method) map.get(getterSignature(info));
092: }
093:
094: /**
095: * Return a method matching the setter signature expected for an attribute
096: */
097: public Method lookupSetter(MBeanAttributeInfo info) {
098: if (null == info) {
099: throw new IllegalArgumentException(
100: "MBeanAttributeInfo cannot be null");
101: }
102:
103: return (Method) map.get(setterSignature(info));
104: }
105:
106: /**
107: * Return a method matching the setter signature expected for a ModelMBean attribute
108: */
109: public Method lookupSetter(ModelMBeanAttributeInfo info) {
110: if (null == info) {
111: throw new IllegalArgumentException(
112: "ModelMBeanAttributeInfo cannot be null");
113: }
114:
115: return (Method) map.get(setterSignature(info));
116: }
117:
118: /**
119: * Return a method matching the specified signature
120: */
121: public Method lookupMethod(String returnType, String name,
122: String[] signature) {
123: if (null == returnType) {
124: throw new IllegalArgumentException(
125: "returnType cannot be null");
126: }
127:
128: if (null == name) {
129: throw new IllegalArgumentException(
130: "method name cannot be null");
131: }
132:
133: return (Method) map.get(methodSignature(returnType, name,
134: signature));
135: }
136:
137: /**
138: * Generates a signature string for an attribute getter method using standard rules
139: */
140: public static String getterSignature(MBeanAttributeInfo info) {
141: if (null == info) {
142: throw new IllegalArgumentException(
143: "MBeanAttributeInfo cannot be null");
144: }
145:
146: String prefix = (info.isIs()) ? "is" : "get";
147: return methodSignature(info.getType(), prefix + info.getName(),
148: null);
149: }
150:
151: /**
152: * Generates a getter signature string for a ModelMBean attribute by checking the
153: * descriptor for getMethod.
154: */
155: public static String getterSignature(ModelMBeanAttributeInfo info) {
156: if (null == info) {
157: throw new IllegalArgumentException(
158: "ModelMBeanAttributeInfo cannot be null");
159: }
160:
161: String methodName = (String) info.getDescriptor()
162: .getFieldValue("getMethod");
163:
164: if (null == methodName) {
165: throw new IllegalArgumentException(
166: "getMethod not defined in ModelMBeanAttributeInfo descriptor");
167: }
168:
169: return methodSignature(info.getType(), methodName, null);
170: }
171:
172: /**
173: * Generates a signature string for an attribute setter method using standard rules
174: */
175: public static String setterSignature(MBeanAttributeInfo info) {
176: if (null == info) {
177: throw new IllegalArgumentException(
178: "MBeanAttributeInfo cannot be null");
179: }
180:
181: return methodSignature(Void.TYPE.getName(), "set"
182: + info.getName(), new String[] { info.getType() });
183: }
184:
185: /**
186: * Generates a setter signature string for a ModelMBean attribute by checking the
187: * descriptor for setMethod.
188: */
189: public static String setterSignature(ModelMBeanAttributeInfo info) {
190: if (null == info) {
191: throw new IllegalArgumentException(
192: "ModelMBeanAttributeInfo cannot be null");
193: }
194:
195: String methodName = (String) info.getDescriptor()
196: .getFieldValue("setMethod");
197:
198: if (null == methodName) {
199: throw new IllegalArgumentException(
200: "setMethod not defined in ModelMBeanAttributeInfo descriptor");
201: }
202:
203: return methodSignature(Void.TYPE.getName(), methodName,
204: new String[] { info.getType() });
205: }
206:
207: /**
208: * Generates a signature string using the operation info.
209: */
210: public static String operationSignature(MBeanOperationInfo info) {
211: if (null == info) {
212: throw new IllegalArgumentException(
213: "MBeanOperationInfo cannot be null");
214: }
215:
216: MBeanParameterInfo[] params = info.getSignature();
217: String[] signature = new String[params.length];
218: for (int i = 0; i < signature.length; i++) {
219: signature[i] = params[i].getType();
220: }
221: return methodSignature(info.getReturnType(), info.getName(),
222: signature);
223: }
224:
225: /**
226: * Generates a signature string using a Method object.
227: */
228: public static String methodSignature(Method method) {
229: if (null == method) {
230: throw new IllegalArgumentException("Method cannot be null");
231: }
232:
233: Class[] paramtypes = method.getParameterTypes();
234: String[] signature = new String[paramtypes.length];
235: for (int i = 0; i < signature.length; i++) {
236: signature[i] = paramtypes[i].getName();
237: }
238: return methodSignature(method.getReturnType().getName(), method
239: .getName(), signature);
240: }
241:
242: /**
243: * Generates a signature string using the supplied signature arguments.
244: */
245: public static String methodSignature(String returnType,
246: String name, String[] signature) {
247: if (null == returnType) {
248: throw new IllegalArgumentException(
249: "returnType cannot be null");
250: }
251: if (null == name) {
252: throw new IllegalArgumentException(
253: "method name cannot be null");
254: }
255:
256: StringBuffer buf = new StringBuffer(returnType).append(';')
257: .append(name);
258: if (null == signature) {
259: return buf.toString();
260: }
261:
262: for (int i = 0; i < signature.length; i++) {
263: buf.append(';').append(signature[i]); // the ; char ensures uniqueness
264: }
265:
266: return buf.toString();
267: }
268:
269: /**
270: * creates the signature string to Method HashMap.
271: */
272: protected HashMap createMap(Class resourceClass) {
273: HashMap cmap = new HashMap();
274: Method[] methods = resourceClass.getMethods();
275: for (int i = 0; i < methods.length; i++) {
276: cmap.put(methodSignature(methods[i]), methods[i]);
277: }
278: return cmap;
279: }
280: }
|