1 18 package org.apache.activemq.console.filter; 19 20 import javax.management.remote.JMXServiceURL ; 21 import javax.management.remote.JMXConnectorFactory ; 22 import javax.management.remote.JMXConnector ; 23 import javax.management.ObjectInstance ; 24 import javax.management.ObjectName ; 25 import javax.management.MBeanServerConnection ; 26 import javax.management.ReflectionException ; 27 import javax.management.InstanceNotFoundException ; 28 import javax.management.AttributeList ; 29 import javax.management.Attribute ; 30 import javax.management.MBeanAttributeInfo ; 31 import javax.management.IntrospectionException ; 32 import java.util.Set ; 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 import java.util.ArrayList ; 36 import java.util.List ; 37 import java.io.IOException ; 38 39 public class MBeansAttributeQueryFilter extends AbstractQueryFilter { 40 public static final String KEY_OBJECT_NAME_ATTRIBUTE = "Attribute:ObjectName:"; 41 42 private JMXServiceURL jmxServiceUrl; 43 private Set attribView; 44 45 51 public MBeansAttributeQueryFilter(JMXServiceURL jmxServiceUrl, Set attribView, MBeansObjectNameQueryFilter next) { 52 super(next); 53 this.jmxServiceUrl = jmxServiceUrl; 54 this.attribView = attribView; 55 } 56 57 63 public List query(List queries) throws Exception { 64 return getMBeanAttributesCollection(next.query(queries)); 65 } 66 67 76 protected List getMBeanAttributesCollection(Collection result) throws IOException , ReflectionException , InstanceNotFoundException , NoSuchMethodException , IntrospectionException { 77 List mbeansCollection = new ArrayList (); 78 79 for (Iterator i=result.iterator(); i.hasNext();) { 80 Object mbean = i.next(); 81 if (mbean instanceof ObjectInstance ) { 82 mbeansCollection.add(getMBeanAttributes(((ObjectInstance )mbean).getObjectName(), attribView)); 83 } else if (mbean instanceof ObjectName ) { 84 mbeansCollection.add(getMBeanAttributes((ObjectName )mbean, attribView)); 85 } else { 86 throw new NoSuchMethodException ("Cannot get the mbean attributes for class: " + mbean.getClass().getName()); 87 } 88 } 89 90 return mbeansCollection; 91 } 92 93 102 protected AttributeList getMBeanAttributes(ObjectInstance obj, Set attrView) throws ReflectionException , InstanceNotFoundException , IOException , IntrospectionException { 103 return getMBeanAttributes(obj.getObjectName(), attrView); 104 } 105 106 115 protected AttributeList getMBeanAttributes(ObjectName objName, Set attrView) throws IOException , ReflectionException , InstanceNotFoundException , IntrospectionException { 116 JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceUrl); 117 MBeanServerConnection server = jmxConnector.getMBeanServerConnection(); 118 119 String [] attribs; 121 if (attrView == null || attrView.isEmpty()) { 122 MBeanAttributeInfo [] infos = server.getMBeanInfo(objName).getAttributes(); 123 attribs = new String [infos.length]; 124 125 for (int i=0; i<infos.length; i++) { 126 if (infos[i].isReadable()) { 127 attribs[i] = infos[i].getName(); 128 } 129 } 130 131 } else { 133 134 attribs = new String [attrView.size()]; 135 int count = 0; 136 for (Iterator i=attrView.iterator(); i.hasNext();) { 137 attribs[count++] = (String )i.next(); 138 } 139 } 140 141 AttributeList attribList = server.getAttributes(objName, attribs); 142 143 jmxConnector.close(); 144 145 attribList.add(0, new Attribute (KEY_OBJECT_NAME_ATTRIBUTE, objName)); 146 147 return attribList; 148 } 149 } 150 | Popular Tags |