001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx;
009:
010: import java.util.Map;
011: import java.util.HashMap;
012: import java.util.List;
013: import java.util.ArrayList;
014: import java.lang.reflect.Method;
015:
016: import javax.management.MBeanOperationInfo;
017: import javax.management.MBeanAttributeInfo;
018:
019: //import javax.management.AttributeNotFoundException;
020:
021: /**
022: *
023: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
024: */
025:
026: public class MBeanInterfaceMetaDataSupport implements
027: MBeanInterfaceMetaData {
028:
029: private static Map cache = new HashMap(); // cache the mbean interface instance, it must be static
030: private Class interfaceClass;
031:
032: // hold MBeanOperationInfo objects
033: private MBeanOperationInfo[] operations = null;
034: // hold MBeanAttributeInfo objects
035: private MBeanAttributeInfo[] attributes = null;
036:
037: private MBeanInterfaceMetaDataSupport(Class c) {
038: interfaceClass = c;
039: }
040:
041: public static MBeanInterfaceMetaData getInstance(Class c) {
042: if (cache.containsKey(c)) {
043: return (MBeanInterfaceMetaData) cache.get(c);
044: } else {
045: MBeanInterfaceMetaDataSupport me = new MBeanInterfaceMetaDataSupport(
046: c);
047: // me.constructMethods();
048: me.constructOperationInfos();
049: me.constructAttributeInfos();
050: cache.put(c, me);
051: return me;
052: }
053: }
054:
055: public Class getInterfaceClass() {
056: return interfaceClass;
057: }
058:
059: public MBeanAttributeInfo[] getAttributes() {
060: return attributes;
061: }
062:
063: public MBeanOperationInfo[] getOperations() {
064: return operations;
065: }
066:
067: // public MBeanOperationInfo getOperationInfo(String operation, Class[] signature) {
068: // return null;
069: // }
070:
071: //find the specified attribute info, return null when not found
072: public MBeanAttributeInfo getAttributeInfo(String attribute) {
073: // System.out.println("Debug:MBeanInterfaceMetaDataSupport 67: " + attribute);
074: MBeanAttributeInfo attributeInfo = null;
075: for (int i = 0; i < attributes.length; i++) {
076: MBeanAttributeInfo info = attributes[i];
077: if (info.getName().equals(attribute)) {
078: attributeInfo = info;
079: break;
080: }
081: }
082: // if(attributeInfo == null) throw new AttributeNotFoundException(attribute);
083: return attributeInfo;
084: }
085:
086: /* ############ private methods ####################### */
087:
088: private void constructOperationInfos() {
089: List operationInfos = new ArrayList();
090: Method[] methods = interfaceClass.getMethods();
091: for (int i = 0; i < methods.length; i++) {
092: Method method = methods[i];
093: if (method == null)
094: continue;
095:
096: // normal method
097: if (!MethodHelper.isGetterMethod(method)
098: && !MethodHelper.isSetterMethod(method)) {
099: MBeanOperationInfo operationInfo = new MBeanOperationInfo(
100: "Operation exposed for management", method);
101: operationInfos.add(operationInfo);
102: }
103: }
104: operations = (MBeanOperationInfo[]) operationInfos
105: .toArray(new MBeanOperationInfo[0]);
106: }
107:
108: private void constructAttributeInfos() {
109: Map attributeInfos = new HashMap();
110: // List setters = new ArrayList();
111: // List getters = new ArrayList();
112: String descr = "Attribute exposed for management";
113: Method method = null;
114: MBeanAttributeInfo attributeInfo = null;
115:
116: Method[] methods = interfaceClass.getMethods();
117: for (int i = 0; i < methods.length; i++) {
118: method = methods[i];
119: if (method == null)
120: continue;
121:
122: String attribute = null;
123: String name = method.getName();
124: boolean isIs = name.startsWith("is") && name.length() > 2;
125: if (isIs)
126: attribute = name.substring(2);
127: else
128: attribute = name.substring(3);
129:
130: attributeInfo = (MBeanAttributeInfo) attributeInfos
131: .get(attribute);
132:
133: if (MethodHelper.isGetterMethod(method)) { // is getter method
134: if (attributeInfo == null) {
135: attributeInfo = new MBeanAttributeInfo(attribute,
136: method.getReturnType().getName(), descr,
137: true, false, isIs);
138: } else {
139: attributeInfo = new MBeanAttributeInfo(attribute,
140: method.getReturnType().getName(),
141: attributeInfo.getDescription(), true,
142: attributeInfo.isWritable(), isIs);
143: }
144: // Replace if exists
145: attributeInfos.put(attribute, attributeInfo);
146: } else if (MethodHelper.isSetterMethod(method)) { // is setter method
147: if (attributeInfo == null) {
148: attributeInfo = new MBeanAttributeInfo(attribute,
149: method.getParameterTypes()[0].getName(),
150: descr, false, true, false);
151: } else {
152: attributeInfo = new MBeanAttributeInfo(
153: attributeInfo.getName(), attributeInfo
154: .getType(), attributeInfo
155: .getDescription(), attributeInfo
156: .isReadable(), true, attributeInfo
157: .isIs());
158: }
159: // Replace if exists
160: attributeInfos.put(attribute, attributeInfo);
161: }
162:
163: }
164: attributes = (MBeanAttributeInfo[]) attributeInfos.values()
165: .toArray(new MBeanAttributeInfo[0]);
166: }
167:
168: }
|