User: squirest Date: 02/02/25 18:02:59 Modified: src/main/org/jboss/mx/metadata MetaDataBuilder.java StandardMetaData.java XMLMetaData.java Added: src/main/org/jboss/mx/metadata AOResolver.java MBeanCapability.java MBeanInfoConversion.java MethodMapper.java Log: moved and removed stuff. new items will go into capability until a better home can be found Revision Changes Path 1.2 +3 -2 jmx/src/main/org/jboss/mx/metadata/MetaDataBuilder.java Index: MetaDataBuilder.java =================================================================== RCS file: /cvsroot/jboss/jmx/src/main/org/jboss/mx/metadata/MetaDataBuilder.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MetaDataBuilder.java 5 Dec 2001 14:22:30 -0000 1.1 +++ MetaDataBuilder.java 26 Feb 2002 02:02:59 -0000 1.2 @@ -6,9 +6,10 @@ import javax.management.MBeanInfo; import javax.management.NotCompliantMBeanException; -public interface MetaDataBuilder { +public interface MetaDataBuilder +{ public MBeanInfo build() throws NotCompliantMBeanException; - + } 1.6 +106 -123 jmx/src/main/org/jboss/mx/metadata/StandardMetaData.java Index: StandardMetaData.java =================================================================== RCS file: /cvsroot/jboss/jmx/src/main/org/jboss/mx/metadata/StandardMetaData.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- StandardMetaData.java 18 Jan 2002 16:45:32 -0000 1.5 +++ StandardMetaData.java 26 Feb 2002 02:02:59 -0000 1.6 @@ -4,33 +4,22 @@ * Distributable under LGPL license. * See terms of license at gnu.org. */ -package org.jboss.mx.metadata; - -import java.util.Map; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.ArrayList; -import java.lang.reflect.Method; -import java.lang.reflect.Constructor; - -import javax.management.NotCompliantMBeanException; -import javax.management.IntrospectionException; -import javax.management.MBeanInfo; -import javax.management.MBeanAttributeInfo; -import javax.management.MBeanOperationInfo; -import javax.management.MBeanConstructorInfo; -import javax.management.MBeanNotificationInfo; -import javax.management.NotificationBroadcaster; - -import javax.management.modelmbean.ModelMBeanAttributeInfo; -import javax.management.modelmbean.ModelMBeanOperationInfo; -import javax.management.modelmbean.ModelMBeanNotificationInfo; -import javax.management.modelmbean.ModelMBeanConstructorInfo; -import javax.management.modelmbean.ModelMBeanInfoSupport; -import javax.management.modelmbean.ModelMBeanInfo; +package org.jboss.mx.metadata; -import org.jboss.mx.server.StandardMBeanInvoker; +import javax.management.IntrospectionException; +import javax.management.MBeanAttributeInfo; +import javax.management.MBeanConstructorInfo; +import javax.management.MBeanInfo; +import javax.management.MBeanNotificationInfo; +import javax.management.MBeanOperationInfo; +import javax.management.NotCompliantMBeanException; +import javax.management.NotificationBroadcaster; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; /** * @author <a href="mailto:[EMAIL PROTECTED]">Juha Lindfors</a>. @@ -40,35 +29,46 @@ { // Attributes ---------------------------------------------------- - private Object resource = null; - private boolean isModelMetaData = false; + private Object mbeanInstance = null; + private Class mbeanClass = null; + private Class mbeanInterface = null; // Constructors -------------------------------------------------- - public StandardMetaData(Object resource) + public StandardMetaData(Object resourceInstance) { - this.resource = resource; + this(resourceInstance.getClass()); + this.mbeanInstance = resourceInstance; } - public StandardMetaData(Object resource, boolean isModelMetaData) + public StandardMetaData(Class resourceClass) { - this(resource); - this.isModelMetaData = isModelMetaData; + this.mbeanClass = resourceClass; + this.mbeanInterface = StandardMetaData.findStandardInterface(resourceClass); } // MetaDataBuilder implementation -------------------------------- public MBeanInfo build() throws NotCompliantMBeanException { - Class stdInterface = StandardMBeanInvoker.getMBeanInterface(resource); - - Method[] methods = stdInterface.getMethods(); - HashMap getters = new HashMap(); - HashMap setters = new HashMap(); - List operInfo = new ArrayList(); - List attrInfo = new ArrayList(); - MBeanConstructorInfo[] constrInfo = null; - try { + // First build the constructors + Constructor[] constructors = mbeanClass.getConstructors(); + MBeanConstructorInfo[] constructorInfo = new MBeanConstructorInfo[constructors.length]; + + for (int i = 0; i < constructors.length; ++i) + { + constructorInfo[i] = new MBeanConstructorInfo("MBean Constructor.", constructors[i]); + } + + // Next we have to figure out how the methods in the mbean class map + // to attributes and operations + Method[] methods = mbeanInterface.getMethods(); + HashMap getters = new HashMap(); + HashMap setters = new HashMap(); + + List operInfo = new ArrayList(); + List attrInfo = new ArrayList(); + for (int i = 0; i < methods.length; ++i) { String methodName = methods[i].getName(); @@ -76,55 +76,65 @@ Class returnType = methods[i].getReturnType(); if (methodName.startsWith("set") && signature.length == 1 && returnType == Void.TYPE) - setters.put(methodName.substring(3, methodName.length()), methods[i]); + { + String key = methodName.substring(3, methodName.length()); + if (setters.get(key) != null) + { + throw new IntrospectionException("overloaded type for attribute: " + key); + } + setters.put(key, methods[i]); + } else if (methodName.startsWith("get") && signature.length == 0 && returnType != Void.TYPE) + { getters.put(methodName.substring(3, methodName.length()), methods[i]); + } else if (methodName.startsWith("is") && signature.length == 0 && (returnType == Boolean.class || returnType == Boolean.TYPE)) + { getters.put(methodName.substring(2, methodName.length()), methods[i]); + } else - operInfo.add(new MBeanOperationInfo("MBean Operation.", methods[i])); + { + MBeanOperationInfo info = new MBeanOperationInfo("MBean Operation.", methods[i]); + operInfo.add(info); + } } Object[] keys = getters.keySet().toArray(); for (int i = 0; i < keys.length; ++i) { - String attrName = (String)keys[i]; - Method getter = (Method)getters.remove(attrName); - Method setter = (Method)setters.remove(attrName); - attrInfo.add(new MBeanAttributeInfo(attrName, "MBean Attribute.", getter, setter)); + String attrName = (String) keys[i]; + Method getter = (Method) getters.remove(attrName); + Method setter = (Method) setters.remove(attrName); + MBeanAttributeInfo info = new MBeanAttributeInfo(attrName, "MBean Attribute.", getter, setter); + attrInfo.add(info); } Iterator it = setters.keySet().iterator(); - while(it.hasNext()) + while (it.hasNext()) { - String attrName = (String)it.next(); - Method setter = (Method)setters.get(attrName); - attrInfo.add(new MBeanAttributeInfo(attrName, "MBean Attribute.", null, setter)); + String attrName = (String) it.next(); + Method setter = (Method) setters.get(attrName); + MBeanAttributeInfo info = new MBeanAttributeInfo(attrName, "MBean Attribute.", null, setter); + attrInfo.add(info); } - Constructor[] constructors = resource.getClass().getConstructors(); - constrInfo = new MBeanConstructorInfo[constructors.length]; + // save away the attribute and operation info objects + MBeanAttributeInfo[] attributeInfo = (MBeanAttributeInfo[]) attrInfo.toArray(new MBeanAttributeInfo[0]); + MBeanOperationInfo[] operationInfo = (MBeanOperationInfo[]) operInfo.toArray(new MBeanOperationInfo[0]); - for (int i = 0; i < constructors.length; ++i) - constrInfo[i] = new MBeanConstructorInfo("MBean Constructor.", constructors[i]); - - MBeanNotificationInfo[] notifInfo = null; - - if (resource instanceof NotificationBroadcaster) - notifInfo = ((NotificationBroadcaster)resource).getNotificationInfo(); + MBeanNotificationInfo[] notifications = null; + if (mbeanInstance instanceof NotificationBroadcaster) + { + notifications = ((NotificationBroadcaster) mbeanInstance).getNotificationInfo(); + } + else + { + notifications = new MBeanNotificationInfo[0]; + } - MBeanInfo info = new MBeanInfo( - resource.getClass().getName(), resource.getClass().getName(), - (MBeanAttributeInfo[])attrInfo.toArray(new MBeanAttributeInfo[0]), - constrInfo, - (MBeanOperationInfo[])operInfo.toArray(new MBeanOperationInfo[0]), - notifInfo - ); + return new MBeanInfo(mbeanClass.getName(), "Management Bean.", // FIXME is that a valid description? + attributeInfo, constructorInfo, operationInfo, notifications); - if (isModelMetaData) - return (MBeanInfo)convertMetaDataToModelMetaData(info); - else - return info; } catch (IntrospectionException e) { @@ -132,65 +142,38 @@ } } - // Private ------------------------------------------------------- - private ModelMBeanInfo convertMetaDataToModelMetaData(MBeanInfo mi) + public static Class findStandardInterface(Class mbeanClass) { - MBeanAttributeInfo[] attributes = mi.getAttributes(); - ModelMBeanAttributeInfo[] mmbAttributes = new ModelMBeanAttributeInfo[attributes.length]; - - for (int i = 0; i < attributes.length; ++i) + Class concrete = mbeanClass; + Class stdInterface = null; + while (null != concrete) { - mmbAttributes[i] = new ModelMBeanAttributeInfo( - attributes[i].getName(), - attributes[i].getType(), - attributes[i].getDescription(), - attributes[i].isReadable(), - attributes[i].isWritable(), - attributes[i].isIs() - ); - } - - MBeanOperationInfo[] operations = mi.getOperations(); - ModelMBeanOperationInfo[] mmbOperations = new ModelMBeanOperationInfo[operations.length]; - - for (int i = 0; i < operations.length; ++i) - { - mmbOperations[i] = new ModelMBeanOperationInfo( - operations[i].getName(), - operations[i].getDescription(), - operations[i].getSignature(), - operations[i].getReturnType(), - operations[i].getImpact() - ); - } - - MBeanConstructorInfo[] constructors = mi.getConstructors(); - ModelMBeanConstructorInfo[] mmbConstructors = new ModelMBeanConstructorInfo[constructors.length]; - - for (int i = 0; i < constructors.length; ++i) - { - mmbConstructors[i] = new ModelMBeanConstructorInfo( - constructors[i].getName(), - constructors[i].getDescription(), - constructors[i].getSignature() - ); + stdInterface = findStandardInterface(concrete, concrete.getInterfaces()); + if (null != stdInterface) + { + return stdInterface; + } + concrete = concrete.getSuperclass(); } + return null; + } - MBeanNotificationInfo[] notifications = mi.getNotifications(); - ModelMBeanNotificationInfo[] mmbNotifications = new ModelMBeanNotificationInfo[notifications.length]; + public static Class findStandardInterface(Class concrete, Class[] interfaces) + { + String stdName = concrete.getName() + "MBean"; + Class retval = null; - for (int i = 0; i < notifications.length; ++i) + // look to see if this class implements MBean std interface + for (int i = 0; i < interfaces.length; ++i) { - mmbNotifications[i] = new ModelMBeanNotificationInfo( - notifications[i].getNotifTypes(), - notifications[i].getName(), - notifications[i].getDescription() - ); + if (interfaces[i].getName().equals(stdName)) + { + retval = interfaces[i]; + break; + } } - return new ModelMBeanInfoSupport(mi.getClassName(), mi.getDescription(), - mmbAttributes, mmbConstructors, mmbOperations, mmbNotifications); + return retval; } - } 1.3 +94 -101 jmx/src/main/org/jboss/mx/metadata/XMLMetaData.java Index: XMLMetaData.java =================================================================== RCS file: /cvsroot/jboss/jmx/src/main/org/jboss/mx/metadata/XMLMetaData.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- XMLMetaData.java 21 Feb 2002 06:59:59 -0000 1.2 +++ XMLMetaData.java 26 Feb 2002 02:02:59 -0000 1.3 @@ -1,54 +1,47 @@ - package org.jboss.mx.metadata; -import java.util.List; -import java.util.Iterator; -import java.util.ArrayList; - -import java.net.MalformedURLException; -import java.net.URL; +import org.jdom.Attribute; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; -import javax.management.MBeanParameterInfo; -import javax.management.MBeanOperationInfo; -import javax.management.MBeanAttributeInfo; -import javax.management.MBeanNotificationInfo; -import javax.management.MBeanConstructorInfo; -import javax.management.MBeanInfo; import javax.management.Descriptor; +import javax.management.MBeanInfo; +import javax.management.MBeanOperationInfo; +import javax.management.MBeanParameterInfo; import javax.management.NotCompliantMBeanException; -import javax.management.MBeanException; import javax.management.modelmbean.DescriptorSupport; -import javax.management.modelmbean.ModelMBeanInfoSupport; -import javax.management.modelmbean.ModelMBeanNotificationInfo; import javax.management.modelmbean.ModelMBeanAttributeInfo; -import javax.management.modelmbean.ModelMBeanOperationInfo; import javax.management.modelmbean.ModelMBeanConstructorInfo; import javax.management.modelmbean.ModelMBeanInfo; - -import org.jdom.Element; -import org.jdom.Attribute; -import org.jdom.JDOMException; -import org.jdom.input.SAXBuilder; +import javax.management.modelmbean.ModelMBeanInfoSupport; +import javax.management.modelmbean.ModelMBeanNotificationInfo; +import javax.management.modelmbean.ModelMBeanOperationInfo; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; public class XMLMetaData implements MetaDataBuilder { // Constants ----------------------------------------------------- - private final static String GET_METHOD = "getMethod"; - private final static String SET_METHOD = "setMethod"; - private final static String PERSIST_POLICY = "persistPolicy"; - private final static String PERSIST_PERIOD = "persistPeriod"; - private final static String PERSIST_NAME = "persistName"; - private final static String PERSIST_LOCATION = "persistLocation"; + private final static String GET_METHOD = "getMethod"; + private final static String SET_METHOD = "setMethod"; + private final static String PERSIST_POLICY = "persistPolicy"; + private final static String PERSIST_PERIOD = "persistPeriod"; + private final static String PERSIST_NAME = "persistName"; + private final static String PERSIST_LOCATION = "persistLocation"; private final static String CURRENCY_TIME_LIMIT = "currencyTimeLimit"; - private final static String ON_UPDATE = "OnUpdate"; - private final static String NO_MORE_OFTEN_THAN = "NoMoreOftenThan"; - private final static String NEVER = "Never"; - private final static String ON_TIMER = "OnTimer"; - - // Attributes ---------------------------------------------------- - private URL url = null; + private final static String ON_UPDATE = "OnUpdate"; + private final static String NO_MORE_OFTEN_THAN = "NoMoreOftenThan"; + private final static String NEVER = "Never"; + private final static String ON_TIMER = "OnTimer"; + + // Attributes ---------------------------------------------------- + private URL url = null; private String className = null; // Constructors -------------------------------------------------- @@ -82,18 +75,18 @@ builder.setValidation(true); - Element root = builder.build(url).getRootElement(); - List constructors = root.getChildren("constructor"); - List operations = root.getChildren("operation"); - List attributes = root.getChildren("attribute"); + Element root = builder.build(url).getRootElement(); + List constructors = root.getChildren("constructor"); + List operations = root.getChildren("operation"); + List attributes = root.getChildren("attribute"); List notifications = root.getChildren("notifications"); String description = root.getChildText("description"); - Attribute persistPolicy = root.getAttribute(PERSIST_POLICY); - Attribute persistPeriod = root.getAttribute(PERSIST_PERIOD); + Attribute persistPolicy = root.getAttribute(PERSIST_POLICY); + Attribute persistPeriod = root.getAttribute(PERSIST_PERIOD); Attribute persistLocation = root.getAttribute(PERSIST_LOCATION); - Attribute persistName = root.getAttribute(PERSIST_NAME); - Attribute currTimeLimit = root.getAttribute(CURRENCY_TIME_LIMIT); + Attribute persistName = root.getAttribute(PERSIST_NAME); + Attribute currTimeLimit = root.getAttribute(CURRENCY_TIME_LIMIT); Descriptor descr = new DescriptorSupport(); descr.setField("name", className); @@ -111,11 +104,11 @@ descr.setField(CURRENCY_TIME_LIMIT, currTimeLimit.getValue()); ModelMBeanInfo info = buildMBeanMetaData( - description, constructors, operations, - attributes, notifications, descr - ); + description, constructors, operations, + attributes, notifications, descr + ); - return (MBeanInfo)info; + return (MBeanInfo) info; } catch (JDOMException e) { @@ -127,13 +120,13 @@ // builder methods protected ModelMBeanInfo buildMBeanMetaData(String description, - List constructors, List operations, List attributes, - List notifications, Descriptor descr) + List constructors, List operations, List attributes, + List notifications, Descriptor descr) { - ModelMBeanOperationInfo[] operInfo = + ModelMBeanOperationInfo[] operInfo = buildOperationInfo(operations); - ModelMBeanAttributeInfo[] attrInfo = + ModelMBeanAttributeInfo[] attrInfo = buildAttributeInfo(attributes); ModelMBeanConstructorInfo[] constrInfo = buildConstructorInfo(constructors); @@ -141,9 +134,9 @@ buildNotificationInfo(notifications); ModelMBeanInfo info = new ModelMBeanInfoSupport( - className, description, attrInfo, constrInfo, - operInfo, notifInfo, descr - ); + className, description, attrInfo, constrInfo, + operInfo, notifInfo, descr + ); return info; } @@ -158,39 +151,39 @@ while (it.hasNext()) { - Element constr = (Element)it.next(); - String name = constr.getChildTextTrim("name"); - String descr = constr.getChildTextTrim("description"); - List params = constr.getChildren("parameter"); + Element constr = (Element) it.next(); + String name = constr.getChildTextTrim("name"); + String descr = constr.getChildTextTrim("description"); + List params = constr.getChildren("parameter"); MBeanParameterInfo[] paramInfo = buildParameterInfo(params); - ModelMBeanConstructorInfo info = + ModelMBeanConstructorInfo info = new ModelMBeanConstructorInfo(name, descr, paramInfo); infos.add(info); } - return (ModelMBeanConstructorInfo[])infos.toArray( - new ModelMBeanConstructorInfo[0]); + return (ModelMBeanConstructorInfo[]) infos.toArray( + new ModelMBeanConstructorInfo[0]); } protected ModelMBeanOperationInfo[] - buildOperationInfo(List operations) + buildOperationInfo(List operations) { Iterator it = operations.iterator(); - List infos = new ArrayList(); + List infos = new ArrayList(); while (it.hasNext()) { - Element oper = (Element)it.next(); - String name = oper.getChildTextTrim("name"); - String descr = oper.getChildTextTrim("description"); - String type = oper.getChildTextTrim("return-type"); + Element oper = (Element) it.next(); + String name = oper.getChildTextTrim("name"); + String descr = oper.getChildTextTrim("description"); + String type = oper.getChildTextTrim("return-type"); String impact = oper.getChildTextTrim("impact"); - List params = oper.getChildren("parameter"); + List params = oper.getChildren("parameter"); MBeanParameterInfo[] paramInfo = buildParameterInfo(params); @@ -213,29 +206,29 @@ type = "void"; ModelMBeanOperationInfo info = new ModelMBeanOperationInfo( - name, descr, paramInfo, type, operImpact - ); + name, descr, paramInfo, type, operImpact + ); infos.add(info); } - return (ModelMBeanOperationInfo[])infos.toArray( - new ModelMBeanOperationInfo[0]); + return (ModelMBeanOperationInfo[]) infos.toArray( + new ModelMBeanOperationInfo[0]); } protected ModelMBeanNotificationInfo[] - buildNotificationInfo(List notifications) + buildNotificationInfo(List notifications) { Iterator it = notifications.iterator(); - List infos = new ArrayList(); + List infos = new ArrayList(); while (it.hasNext()) { - Element notif = (Element)it.next(); - String name = notif.getChildTextTrim("name"); - String descr = notif.getChildTextTrim("description"); + Element notif = (Element) it.next(); + String name = notif.getChildTextTrim("name"); + String descr = notif.getChildTextTrim("description"); List notifTypes = notif.getChildren("notification-type"); Iterator iterator = notifTypes.iterator(); @@ -243,41 +236,41 @@ while (iterator.hasNext()) { - Element type = (Element)iterator.next(); + Element type = (Element) iterator.next(); types.add(type.getTextTrim()); } ModelMBeanNotificationInfo info = new ModelMBeanNotificationInfo( - (String[])types.toArray(), name, descr - ); + (String[]) types.toArray(), name, descr + ); infos.add(info); } - return (ModelMBeanNotificationInfo[])infos.toArray( - new ModelMBeanNotificationInfo[0] - ); + return (ModelMBeanNotificationInfo[]) infos.toArray( + new ModelMBeanNotificationInfo[0] + ); } protected ModelMBeanAttributeInfo[] - buildAttributeInfo(List attributes) + buildAttributeInfo(List attributes) { Iterator it = attributes.iterator(); - List infos = new ArrayList(); + List infos = new ArrayList(); while (it.hasNext()) { - Element attr = (Element)it.next(); - String name = attr.getChildTextTrim("name"); + Element attr = (Element) it.next(); + String name = attr.getChildTextTrim("name"); String description = attr.getChildTextTrim("description"); - String type = attr.getChildTextTrim("type"); - String access = attr.getChildTextTrim("access"); + String type = attr.getChildTextTrim("type"); + String access = attr.getChildTextTrim("access"); Attribute persistPolicy = attr.getAttribute(PERSIST_POLICY); Attribute persistPeriod = attr.getAttribute(PERSIST_PERIOD); - Attribute setMethod = attr.getAttribute(SET_METHOD); - Attribute getMethod = attr.getAttribute(GET_METHOD); + Attribute setMethod = attr.getAttribute(SET_METHOD); + Attribute getMethod = attr.getAttribute(GET_METHOD); Attribute currTimeLimit = attr.getAttribute(CURRENCY_TIME_LIMIT); Descriptor descr = new DescriptorSupport(); @@ -307,15 +300,15 @@ ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo( - name, type, description, isReadable, isWritable, false, descr - ); + name, type, description, isReadable, isWritable, false, descr + ); infos.add(info); } - return (ModelMBeanAttributeInfo[])infos.toArray( - new ModelMBeanAttributeInfo[0] - ); + return (ModelMBeanAttributeInfo[]) infos.toArray( + new ModelMBeanAttributeInfo[0] + ); } @@ -327,17 +320,17 @@ while (it.hasNext()) { - Element param = (Element)it.next(); - String name = param.getChildTextTrim("name"); - String type = param.getChildTextTrim("type"); - String descr = param.getChildTextTrim("description"); + Element param = (Element) it.next(); + String name = param.getChildTextTrim("name"); + String type = param.getChildTextTrim("type"); + String descr = param.getChildTextTrim("description"); MBeanParameterInfo info = new MBeanParameterInfo(name, type, descr); infos.add(info); } - return (MBeanParameterInfo[])infos.toArray(new MBeanParameterInfo[0]); + return (MBeanParameterInfo[]) infos.toArray(new MBeanParameterInfo[0]); } } 1.1 jmx/src/main/org/jboss/mx/metadata/AOResolver.java Index: AOResolver.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.mx.metadata; import javax.management.MBeanAttributeInfo; import javax.management.MBeanInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanParameterInfo; /** * AOResolver is a modified TST for mapping an Integer code against attribute and operation keys. * * Note that this implementation was chosen to allow fast resolution of compound keys - namely the * operationName and signature[] passed to an MBean's invoke() method. For consistency it also * keeps track of attribute names (which are a single key), although for those a hashmap would * have done just as well. * * @author <a href="mailto:[EMAIL PROTECTED]">Trevor Squires</a>. */ public class AOResolver { private Node opRoot = null; private Node atRoot = null; /** * Default constructor. */ public AOResolver() { } /** * Uses the AttributeInfo and OperationInfo arrays in the MBeanInfo to configure the * resolver. Each attribute and operation will be assigned a code which corresponds * to it's position in the info array. */ public AOResolver(MBeanInfo info) { this(info.getAttributes(), info.getOperations()); } /** * Uses the AttributeInfo and OperationInfo arrays to configure the resolver. * Each attribute and operation will be assigned a code which corresponds to it's * position in the info array. */ public AOResolver(MBeanAttributeInfo[] attributes, MBeanOperationInfo[] operations) { int attributeCount = (attributes != null) ? attributes.length : 0; for (int i = 0; i < attributeCount; i++) { store(attributes[i].getName(), new Integer(i)); } int operationCount = (operations != null) ? operations.length : 0; for (int i = 0; i < operationCount; i++) { MBeanOperationInfo operation = operations[i]; MBeanParameterInfo[] params = operation.getSignature(); String[] signature = new String[params.length]; for (int j = 0; j < signature.length; j++) { signature[j] = params[j].getType(); } store(operation.getName(), signature, new Integer(i)); } } public Integer lookup(String actionName, String[] signature) { String word = actionName; int wordh = word.hashCode(); int wordpos = -1; int maxword = (signature != null) ? signature.length : 0; Node node = opRoot; Integer rval = null; OUTER_NODE: while (node != null) { if (wordh < node.hash) { node = node.loKid; } else if (wordh > node.hash) { node = node.hiKid; } else { for (int i = node.eqKid.length - 1; i > -1; i--) { if (word.equals(node.eqKid[i].val)) { if (++wordpos < maxword) { node = node.eqKid[i]; word = signature[wordpos]; wordh = word.hashCode(); continue OUTER_NODE; } else { rval = node.eqKid[i].code; break OUTER_NODE; } } } } } return rval; } public Integer lookup(String attrName) { int attrh = attrName.hashCode(); Node node = atRoot; Integer rval = null; OUTER_NODE: while (node != null) { if (attrh < node.hash) { node = node.loKid; } else if (attrh > node.hash) { node = node.hiKid; } else { for (int i = node.eqKid.length - 1; i > -1; i--) { if (attrName.equals(node.eqKid[i].val)) { rval = node.eqKid[i].code; break OUTER_NODE; } } } } return rval; } public void store(String mname, String[] signature, Integer code) { if (opRoot == null) { opRoot = createNode(mname); createValueNode(opRoot, mname); } int word = -1; int maxword = (signature != null) ? signature.length : 0; Node current = createOrGetNode(opRoot, mname); while (++word < maxword) { current = createOrGetNode(current, signature[word]); } current.code = code; } public void store(String attrName, Integer code) { Node current = null; if (atRoot == null) { atRoot = createNode(attrName); current = createValueNode(atRoot, attrName); } else { current = createOrGetNode(atRoot, attrName); } current.code = code; } protected Node createNode(String key) { Node h = new Node(); h.hash = key.hashCode(); h.val = key; return h; } protected Node createValueNode(Node parent, String key) { Node h = new Node(); h.val = key; h.hash = key.hashCode(); int insertAt = 0; if (parent.eqKid == null) { parent.eqKid = new Node[1]; } else { Node[] old = parent.eqKid; insertAt = old.length; parent.eqKid = new Node[insertAt + 1]; System.arraycopy(old, 0, parent.eqKid, 0, insertAt); } parent.eqKid[insertAt] = h; return h; } protected Node createOrGetNode(Node parent, String key) { Node realParent = parent; int keycode = key.hashCode(); while (true) { if (keycode < realParent.hash) { if (realParent.loKid == null) { realParent.loKid = createNode(key); return createValueNode(realParent.loKid, key); } realParent = realParent.loKid; } else if (keycode > realParent.hash) { if (realParent.hiKid == null) { realParent.hiKid = createNode(key); return createValueNode(realParent.hiKid, key); } realParent = realParent.hiKid; } else { if (realParent.eqKid != null) { for (int i = 0; i < realParent.eqKid.length; i++) { if (key.equals(realParent.eqKid[i].val)) { return realParent.eqKid[i]; } } } return createValueNode(realParent, key); } } } public static class Node { public int hash = 0; public String val = null; public Node hiKid = null; public Node loKid = null; public Node[] eqKid = null; public Integer code = null; } } 1.1 jmx/src/main/org/jboss/mx/metadata/MBeanCapability.java Index: MBeanCapability.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.mx.metadata; import javax.management.DynamicMBean; import javax.management.MBeanInfo; import javax.management.NotCompliantMBeanException; import java.lang.reflect.Modifier; /** * This class is a bit bogus. * * @author <a href="mailto:[EMAIL PROTECTED]">Trevor Squires</a>. */ public class MBeanCapability { public static final int DYNAMIC_MBEAN = 0x321; public static final int STANDARD_MBEAN = 0x123; public static final int NOT_AN_MBEAN = 0xc0de; protected int mbeanType = NOT_AN_MBEAN; protected Class mbeanClass = null; protected MBeanInfo standardInfo = null; public static MBeanCapability of(Class mbeanClass) throws NotCompliantMBeanException { if (null == mbeanClass) { throw new IllegalArgumentException("Class cannot be null"); } if (Modifier.isAbstract(mbeanClass.getModifiers())) { throw new NotCompliantMBeanException("Class is abstract: " + mbeanClass.getName()); } if (mbeanClass.getConstructors().length == 0) { throw new NotCompliantMBeanException("Class has no public constructors: " + mbeanClass.getName()); } if (DynamicMBean.class.isAssignableFrom(mbeanClass)) { // Compliance check (this ought to disappear in next rev of JMX spec). // You can't implement both Standard and DynamicMBean. So, if this class // is a DynamicMBean and it directly implements a standard MBean interface // then it's not compliant. if (StandardMetaData.findStandardInterface(mbeanClass, mbeanClass.getInterfaces()) != null) { throw new NotCompliantMBeanException("Class supplies a standard MBean interface and is a DynamicMBean: " + mbeanClass.getName()); } return new MBeanCapability(mbeanClass); } else if (StandardMetaData.findStandardInterface(mbeanClass) != null) { return new MBeanCapability(mbeanClass, new StandardMetaData(mbeanClass).build()); } throw new NotCompliantMBeanException("Class does not expose a management interface: " + mbeanClass.getName()); } protected MBeanCapability(Class mbeanClass) { mbeanType = DYNAMIC_MBEAN; } protected MBeanCapability(Class mbeanClass, MBeanInfo info) { mbeanType = STANDARD_MBEAN; standardInfo = info; } public int getMBeanType() { return mbeanType; } public MBeanInfo getStandardMBeanInfo() { return standardInfo; } } 1.1 jmx/src/main/org/jboss/mx/metadata/MBeanInfoConversion.java Index: MBeanInfoConversion.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.mx.metadata; import javax.management.MBeanAttributeInfo; import javax.management.MBeanConstructorInfo; import javax.management.MBeanException; import javax.management.MBeanInfo; import javax.management.MBeanNotificationInfo; import javax.management.MBeanOperationInfo; import javax.management.modelmbean.ModelMBeanAttributeInfo; import javax.management.modelmbean.ModelMBeanConstructorInfo; import javax.management.modelmbean.ModelMBeanInfo; import javax.management.modelmbean.ModelMBeanInfoSupport; import javax.management.modelmbean.ModelMBeanNotificationInfo; import javax.management.modelmbean.ModelMBeanOperationInfo; import java.util.HashMap; import java.util.Iterator; /** * Routines for converting MBeanInfo to ModelMBeanInfoSupport and stripping ModelMBeanOperationInfos that * are referred to in ModelMBeanAttributeInfos */ public class MBeanInfoConversion { /** * Convert regular MBeanInfo into ModelMBeanInfo */ public static ModelMBeanInfoSupport toModelMBeanInfo(MBeanInfo info) { MBeanAttributeInfo[] attributes = info.getAttributes(); ModelMBeanAttributeInfo[] mmbAttributes = new ModelMBeanAttributeInfo[attributes.length]; for (int i = 0; i < attributes.length; ++i) { mmbAttributes[i] = new ModelMBeanAttributeInfo( attributes[i].getName(), attributes[i].getType(), attributes[i].getDescription(), attributes[i].isReadable(), attributes[i].isWritable(), attributes[i].isIs() ); } MBeanOperationInfo[] operations = info.getOperations(); ModelMBeanOperationInfo[] mmbOperations = new ModelMBeanOperationInfo[operations.length]; for (int i = 0; i < operations.length; ++i) { mmbOperations[i] = new ModelMBeanOperationInfo( operations[i].getName(), operations[i].getDescription(), operations[i].getSignature(), operations[i].getReturnType(), operations[i].getImpact() ); } MBeanConstructorInfo[] constructors = info.getConstructors(); ModelMBeanConstructorInfo[] mmbConstructors = new ModelMBeanConstructorInfo[constructors.length]; for (int i = 0; i < constructors.length; ++i) { mmbConstructors[i] = new ModelMBeanConstructorInfo( constructors[i].getName(), constructors[i].getDescription(), constructors[i].getSignature() ); } MBeanNotificationInfo[] notifications = info.getNotifications(); ModelMBeanNotificationInfo[] mmbNotifications = new ModelMBeanNotificationInfo[notifications.length]; for (int i = 0; i < notifications.length; ++i) { mmbNotifications[i] = new ModelMBeanNotificationInfo( notifications[i].getNotifTypes(), notifications[i].getName(), notifications[i].getDescription() ); } return new ModelMBeanInfoSupport(info.getClassName(), info.getDescription(), mmbAttributes, mmbConstructors, mmbOperations, mmbNotifications); } /** * Returns a ModelMBeanInfoSupport where ModelMBeanOperationInfos that are referred to by ModelMBeanAttributeInfo * getMethod or setMethod descriptor fields are stripped out. If the stripAllRoles parameter is true * then all the referred-to operations will be stripped. Otherwise only referred-to operations with a * role of "getter" or "setter" will be stripped. */ public static ModelMBeanInfoSupport stripAttributeOperations(ModelMBeanInfo info, boolean stripAllRoles) throws MBeanException { HashMap opsMap = new HashMap(); ModelMBeanOperationInfo[] operations = (ModelMBeanOperationInfo[]) info.getOperations(); for (int i = 0; i < operations.length; i++) { opsMap.put(MethodMapper.operationSignature(operations[i]), operations[i]); } ModelMBeanAttributeInfo[] attributes = (ModelMBeanAttributeInfo[]) info.getAttributes(); for (int i = 0; i < attributes.length; i++) { if (attributes[i].isReadable() && (attributes[i].getDescriptor().getFieldValue("getMethod") != null)) { String key = MethodMapper.getterSignature(attributes[i]); ModelMBeanOperationInfo opinfo = (ModelMBeanOperationInfo) opsMap.get(key); String role = (String) opinfo.getDescriptor().getFieldValue("role"); if ("getter".equals(role) || stripAllRoles) { opsMap.remove(key); } } if (attributes[i].isWritable() && (attributes[i].getDescriptor().getFieldValue("setMethod") != null)) { String key = MethodMapper.getterSignature(attributes[i]); ModelMBeanOperationInfo opinfo = (ModelMBeanOperationInfo) opsMap.get(key); String role = (String) opinfo.getDescriptor().getFieldValue("role"); if ("setter".equals(role) || stripAllRoles) { opsMap.remove(key); } } } operations = new ModelMBeanOperationInfo[opsMap.size()]; int position = 0; for (Iterator iterator = opsMap.values().iterator(); iterator.hasNext(); position++) { operations[position] = (ModelMBeanOperationInfo) iterator.next(); } return new ModelMBeanInfoSupport(info.getClassName(), info.getDescription(), (ModelMBeanAttributeInfo[]) info.getAttributes(), (ModelMBeanConstructorInfo[]) info.getConstructors(), operations, (ModelMBeanNotificationInfo[]) info.getNotifications(), info.getMBeanDescriptor()); } } 1.1 jmx/src/main/org/jboss/mx/metadata/MethodMapper.java Index: MethodMapper.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.mx.metadata; import javax.management.MBeanAttributeInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanParameterInfo; import javax.management.modelmbean.ModelMBeanAttributeInfo; import java.lang.reflect.Method; import java.util.HashMap; /** * Helper class for resolving JMX *Info objects against Method objects. It's typically * used during the construction of dispatchers during MBean registration/creation. * * If you're looking for a fast resolver of JMX operation signatures see AOResolver. * * FIXME - this class is not aware of multiple target objects (as in modelmbeans) * however I'm half expecting that feature to disappear in JMX 1.1 anyhow. * * @author <a href="mailto:[EMAIL PROTECTED]">Trevor Squires</a>. */ public class MethodMapper { private HashMap map = null; /** * Constructs a mapper by reflecting on the class. */ public MethodMapper(Class resourceClass) { if (null == resourceClass) { throw new IllegalArgumentException("resourceClass cannot be null"); } map = createMap(resourceClass); } /** * Return a method matching the signature defined in the operation info */ public Method lookupOperation(MBeanOperationInfo info) { if (null == info) { throw new IllegalArgumentException("MBeanOperationInfo cannot be null"); } return (Method) map.get(operationSignature(info)); } /** * Return a method matching the getter signature expected for an attribute. */ public Method lookupGetter(MBeanAttributeInfo info) { if (null == info) { throw new IllegalArgumentException("MBeanAttributeInfo cannot be null"); } return (Method) map.get(getterSignature(info)); } /** * Return a method matching the getter signature expected for a ModelMBean attribute. */ public Method lookupGetter(ModelMBeanAttributeInfo info) { if (null == info) { throw new IllegalArgumentException("MBeanAttributeInfo cannot be null"); } return (Method) map.get(getterSignature(info)); } /** * Return a method matching the setter signature expected for an attribute */ public Method lookupSetter(MBeanAttributeInfo info) { if (null == info) { throw new IllegalArgumentException("MBeanAttributeInfo cannot be null"); } return (Method) map.get(setterSignature(info)); } /** * Return a method matching the setter signature expected for a ModelMBean attribute */ public Method lookupSetter(ModelMBeanAttributeInfo info) { if (null == info) { throw new IllegalArgumentException("ModelMBeanAttributeInfo cannot be null"); } return (Method) map.get(setterSignature(info)); } /** * Return a method matching the specified signature */ public Method lookupMethod(String returnType, String name, String[] signature) { if (null == returnType) { throw new IllegalArgumentException("returnType cannot be null"); } if (null == name) { throw new IllegalArgumentException("method name cannot be null"); } return (Method) map.get(methodSignature(returnType, name, signature)); } /** * Generates a signature string for an attribute getter method using standard rules */ public static String getterSignature(MBeanAttributeInfo info) { if (null == info) { throw new IllegalArgumentException("MBeanAttributeInfo cannot be null"); } String prefix = (info.isIs()) ? "is" : "get"; return methodSignature(info.getType(), prefix + info.getName(), null); } /** * Generates a getter signature string for a ModelMBean attribute by checking the * descriptor for getMethod. */ public static String getterSignature(ModelMBeanAttributeInfo info) { if (null == info) { throw new IllegalArgumentException("ModelMBeanAttributeInfo cannot be null"); } String methodName = (String) info.getDescriptor().getFieldValue("getMethod"); if (null == methodName) { throw new IllegalArgumentException("getMethod not defined in ModelMBeanAttributeInfo descriptor"); } return methodSignature(info.getType(), methodName, null); } /** * Generates a signature string for an attribute setter method using standard rules */ public static String setterSignature(MBeanAttributeInfo info) { if (null == info) { throw new IllegalArgumentException("MBeanAttributeInfo cannot be null"); } return methodSignature(Void.TYPE.getName(), "set" + info.getName(), new String[]{info.getType()}); } /** * Generates a setter signature string for a ModelMBean attribute by checking the * descriptor for setMethod. */ public static String setterSignature(ModelMBeanAttributeInfo info) { if (null == info) { throw new IllegalArgumentException("ModelMBeanAttributeInfo cannot be null"); } String methodName = (String) info.getDescriptor().getFieldValue("setMethod"); if (null == methodName) { throw new IllegalArgumentException("setMethod not defined in ModelMBeanAttributeInfo descriptor"); } return methodSignature(Void.TYPE.getName(), methodName, new String[]{info.getType()}); } /** * Generates a signature string using the operation info. */ public static String operationSignature(MBeanOperationInfo info) { if (null == info) { throw new IllegalArgumentException("MBeanOperationInfo cannot be null"); } MBeanParameterInfo[] params = info.getSignature(); String[] signature = new String[params.length]; for (int i = 0; i < signature.length; i++) { signature[i] = params[i].getType(); } return methodSignature(info.getReturnType(), info.getName(), signature); } /** * Generates a signature string using a Method object. */ public static String methodSignature(Method method) { if (null == method) { throw new IllegalArgumentException("Method cannot be null"); } Class[] paramtypes = method.getParameterTypes(); String[] signature = new String[paramtypes.length]; for (int i = 0; i < signature.length; i++) { signature[i] = paramtypes[i].getName(); } return methodSignature(method.getReturnType().getName(), method.getName(), signature); } /** * Generates a signature string using the supplied signature arguments. */ public static String methodSignature(String returnType, String name, String[] signature) { if (null == returnType) { throw new IllegalArgumentException("returnType cannot be null"); } if (null == name) { throw new IllegalArgumentException("method name cannot be null"); } StringBuffer buf = new StringBuffer(returnType).append(';').append(name); if (null == signature) { return buf.toString(); } for (int i = 0; i < signature.length; i++) { buf.append(';').append(signature[i]); // the ; char ensures uniqueness } return buf.toString(); } /** * creates the signature string to Method HashMap. */ protected HashMap createMap(Class resourceClass) { HashMap cmap = new HashMap(); Method[] methods = resourceClass.getMethods(); for (int i = 0; i < methods.length; i++) { cmap.put(methodSignature(methods[i]), methods[i]); } return cmap; } }
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development