001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jmx;
030:
031: import com.caucho.util.L10N;
032:
033: import javax.management.*;
034: import java.lang.reflect.InvocationTargetException;
035: import java.lang.reflect.Method;
036: import java.lang.reflect.Modifier;
037: import java.util.ArrayList;
038: import java.util.HashMap;
039:
040: /**
041: * Skeleton for an standard MBean.
042: */
043: public class MBeanSkeleton {
044: private static L10N L = new L10N(MBeanSkeleton.class);
045:
046: private Class _mbeanClass;
047:
048: private MBeanInfo _mbeanInfo;
049:
050: private HashMap<String, Method> _getAttributes = new HashMap<String, Method>();
051: private HashMap<String, Method> _setAttributes = new HashMap<String, Method>();
052: private Method[] _operations;
053:
054: /**
055: * Creates the skeleton based on the MBean class.
056: *
057: * @param mbeanClass the _MBean class
058: */
059: MBeanSkeleton(Class mbeanClass) throws IntrospectionException {
060: _mbeanClass = mbeanClass;
061:
062: introspect();
063: }
064:
065: /**
066: * Returns the mbean class.
067: */
068: public Class getMBeanClass() {
069: return _mbeanClass;
070: }
071:
072: /**
073: * Returns the MBeanInfo for the class.
074: */
075: public MBeanInfo getMBeanInfo() {
076: return _mbeanInfo;
077: }
078:
079: /**
080: * Returns the attribute value.
081: */
082: public Object getAttribute(Object object, String name)
083: throws AttributeNotFoundException, ReflectionException {
084: Method method = _getAttributes.get(name);
085:
086: if (method != null) {
087: try {
088: return method.invoke(object, (Object[]) null);
089: } catch (IllegalAccessException e) {
090: throw new ReflectionException(e);
091: } catch (InvocationTargetException e) {
092: throw new ReflectionException(e);
093: }
094: } else if (name.equals("resin-api"))
095: return _mbeanClass.getName();
096: else
097: throw new AttributeNotFoundException(L.l(
098: "MBean class `{0}' has no read attribute `{1}'.",
099: _mbeanClass.getName(), name));
100:
101: }
102:
103: /**
104: * Sets the attribute value.
105: */
106: public void setAttribute(Object object, String name, Object value)
107: throws AttributeNotFoundException, ReflectionException {
108: Method method = _setAttributes.get(name);
109:
110: if (method == null)
111: throw new AttributeNotFoundException(L.l(
112: "MBean class `{0}' has no write attribute `{1}'.",
113: _mbeanClass.getName(), name));
114:
115: try {
116: method.invoke(object, new Object[] { value });
117: } catch (IllegalAccessException e) {
118: throw new ReflectionException(e);
119: } catch (InvocationTargetException e) {
120: throw new ReflectionException(e);
121: }
122: }
123:
124: /**
125: * Invokes an operation.
126: */
127: public Object invoke(Object object, String name, Object[] args,
128: String[] sig) throws MBeanException, ReflectionException {
129: Method method = findMethod(name, sig);
130:
131: if (method == null)
132: throw new MBeanException(
133: null,
134: L
135: .l(
136: "MBean class `{0}' has no matching operation `{1}'.",
137: _mbeanClass.getName(), name));
138:
139: try {
140: return method.invoke(object, args);
141: } catch (IllegalAccessException e) {
142: throw new ReflectionException(e);
143: } catch (InvocationTargetException e) {
144: throw new ReflectionException(e);
145: }
146: }
147:
148: private Method findMethod(String name, String[] sig) {
149: loop: for (int i = 0; i < _operations.length; i++) {
150: Method method = _operations[i];
151:
152: if (!method.getName().equals(name))
153: continue;
154:
155: Class[] param = method.getParameterTypes();
156:
157: if (param.length != sig.length)
158: continue;
159:
160: for (int j = 0; j < sig.length; j++)
161: if (!param[j].getName().equals(sig[j]))
162: continue loop;
163:
164: return method;
165: }
166:
167: return null;
168: }
169:
170: /**
171: * Introspects the class.
172: */
173: private void introspect() throws IntrospectionException {
174: Method[] methods = _mbeanClass.getMethods();
175:
176: ArrayList<Method> publicMethods = new ArrayList<Method>();
177:
178: ArrayList<MBeanAttributeInfo> attributes;
179: attributes = new ArrayList<MBeanAttributeInfo>();
180: ArrayList<MBeanOperationInfo> operations;
181: operations = new ArrayList<MBeanOperationInfo>();
182:
183: for (int i = 0; i < methods.length; i++) {
184: Method method = methods[i];
185:
186: if (!Modifier.isPublic(method.getModifiers()))
187: continue;
188:
189: if (method.getDeclaringClass().getName().startsWith(
190: "javax.management."))
191: continue;
192:
193: String name = method.getName();
194: Class[] param = method.getParameterTypes();
195: Class retType = method.getReturnType();
196:
197: if (name.startsWith("get") && param.length == 0
198: && !void.class.equals(retType)) {
199: String attributeName = name.substring(3);
200:
201: Method setter = getSetter(attributeName, retType);
202:
203: _getAttributes.put(attributeName, method);
204:
205: attributes.add(new MBeanAttributeInfo(attributeName,
206: attributeName, method, setter));
207: } else if (name.startsWith("is")
208: && param.length == 0
209: && (boolean.class.equals(retType) || Boolean.class
210: .equals(retType))) {
211: String attributeName = name.substring(2);
212:
213: Method setter = getSetter(attributeName, retType);
214:
215: attributes.add(new MBeanAttributeInfo(attributeName,
216: attributeName, method, setter));
217:
218: _getAttributes.put(attributeName, method);
219: } else if (name.startsWith("set") && param.length == 1
220: && void.class.equals(retType)) {
221: String attributeName = name.substring(3);
222:
223: _setAttributes.put(attributeName, method);
224: } else {
225: operations.add(new MBeanOperationInfo(method.getName(),
226: method));
227: publicMethods.add(method);
228: }
229: }
230:
231: _operations = new Method[publicMethods.size()];
232: publicMethods.toArray(_operations);
233:
234: MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[attributes
235: .size()];
236: attributes.toArray(attrs);
237:
238: MBeanConstructorInfo[] makers = new MBeanConstructorInfo[0];
239:
240: MBeanOperationInfo[] ops = new MBeanOperationInfo[operations
241: .size()];
242: operations.toArray(ops);
243:
244: MBeanNotificationInfo[] notifs = new MBeanNotificationInfo[0];
245:
246: _mbeanInfo = new MBeanInfo(_mbeanClass.getName(), _mbeanClass
247: .getName(), attrs, makers, ops, notifs);
248:
249: }
250:
251: private Method getSetter(String attributeName, Class type) {
252: String setName = "set" + attributeName;
253:
254: Method[] methods = _mbeanClass.getMethods();
255:
256: for (int i = 0; i < methods.length; i++) {
257: Method method = methods[i];
258:
259: if (!Modifier.isPublic(method.getModifiers()))
260: continue;
261:
262: String name = method.getName();
263:
264: if (!setName.equals(name))
265: continue;
266:
267: Class[] param = method.getParameterTypes();
268: Class ret = method.getReturnType();
269:
270: if (param.length != 1)
271: continue;
272:
273: if (!void.class.equals(ret))
274: continue;
275:
276: if (param[0].equals(type))
277: return method;
278: else
279: return null; // XXX: s/b error?
280: }
281:
282: return null;
283: }
284: }
|