1 22 package org.jboss.console.plugins.helpers.jmx; 23 24 import org.jboss.logging.Logger; 25 import org.jboss.mx.util.MBeanServerLocator; 26 27 import javax.management.Attribute ; 28 import javax.management.AttributeList ; 29 import javax.management.IntrospectionException ; 30 import javax.management.JMException ; 31 import javax.management.MBeanAttributeInfo ; 32 import javax.management.MBeanInfo ; 33 import javax.management.MBeanOperationInfo ; 34 import javax.management.MBeanParameterInfo ; 35 import javax.management.MBeanServer ; 36 import javax.management.ObjectName ; 37 import javax.management.ReflectionException ; 38 import java.beans.PropertyEditor ; 39 import java.beans.PropertyEditorManager ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.Set ; 43 import java.util.TreeMap ; 44 45 50 public class Server 51 { 52 static Logger log = Logger.getLogger(Server.class); 53 54 public static MBeanServer getMBeanServer() throws JMException 55 { 56 return MBeanServerLocator.locateJBoss(); 57 } 58 59 public static Iterator getDomainData(String filter) throws JMException 60 { 61 MBeanServer server = getMBeanServer(); 62 TreeMap domainData = new TreeMap (); 63 if( server != null ) 64 { 65 ObjectName filterName = null; 66 if( filter != null ) 67 filterName = new ObjectName (filter); 68 Set objectNames = server.queryNames(filterName, null); 69 Iterator objectNamesIter = objectNames.iterator(); 70 while( objectNamesIter.hasNext() ) 71 { 72 ObjectName name = (ObjectName ) objectNamesIter.next(); 73 MBeanInfo info = server.getMBeanInfo(name); 74 String domainName = name.getDomain(); 75 MBeanData mbeanData = new MBeanData(name, info); 76 DomainData data = (DomainData) domainData.get(domainName); 77 if( data == null ) 78 { 79 data = new DomainData(domainName); 80 domainData.put(domainName, data); 81 } 82 data.addData(mbeanData); 83 } 84 } 85 Iterator domainDataIter = domainData.values().iterator(); 86 return domainDataIter; 87 } 88 89 public static MBeanData getMBeanData(String name) throws JMException 90 { 91 MBeanServer server = getMBeanServer(); 92 ObjectName objName = new ObjectName (name); 93 MBeanInfo info = server.getMBeanInfo(objName); 94 MBeanData mbeanData = new MBeanData(objName, info); 95 return mbeanData; 96 } 97 98 public static Object getMBeanAttributeObject(String name, String attrName) 99 throws JMException 100 { 101 MBeanServer server = getMBeanServer(); 102 ObjectName objName = new ObjectName (name); 103 Object value = server.getAttribute(objName, attrName); 104 return value; 105 } 106 107 public static String getMBeanAttribute(String name, String attrName) throws JMException 108 { 109 MBeanServer server = getMBeanServer(); 110 ObjectName objName = new ObjectName (name); 111 String value = null; 112 try 113 { 114 Object attr = server.getAttribute(objName, attrName); 115 if( attr != null ) 116 value = attr.toString(); 117 } 118 catch(JMException e) 119 { 120 value = e.getMessage(); 121 } 122 return value; 123 } 124 125 public static AttrResultInfo getMBeanAttributeResultInfo(String name, MBeanAttributeInfo attrInfo) 126 throws JMException 127 { 128 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 129 MBeanServer server = getMBeanServer(); 130 ObjectName objName = new ObjectName (name); 131 String attrName = attrInfo.getName(); 132 String attrType = attrInfo.getType(); 133 Object value = null; 134 Throwable throwable = null; 135 if( attrInfo.isReadable() == true ) 136 { 137 try 138 { 139 value = server.getAttribute(objName, attrName); 140 } 141 catch (Throwable t) 142 { 143 throwable = t; 144 } 145 } 146 Class typeClass = null; 147 try 148 { 149 typeClass = getPrimativeClass(attrType); 150 if( typeClass == null ) 151 typeClass = loader.loadClass(attrType); 152 } 153 catch(ClassNotFoundException ignore) 154 { 155 } 156 PropertyEditor editor = null; 157 if( typeClass != null ) 158 editor = PropertyEditorManager.findEditor(typeClass); 159 160 return new AttrResultInfo(attrName, editor, value, throwable); 161 } 162 163 public static AttributeList setAttributes(String name, HashMap attributes) throws JMException 164 { 165 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 166 MBeanServer server = getMBeanServer(); 167 ObjectName objName = new ObjectName (name); 168 MBeanInfo info = server.getMBeanInfo(objName); 169 MBeanAttributeInfo [] attributesInfo = info.getAttributes(); 170 AttributeList newAttributes = new AttributeList (); 171 for(int a = 0; a < attributesInfo.length; a ++) 172 { 173 MBeanAttributeInfo attrInfo = attributesInfo[a]; 174 String attrName = attrInfo.getName(); 175 if( attributes.containsKey(attrName) == false ) 176 continue; 177 String value = (String ) attributes.get(attrName); 178 String attrType = attrInfo.getType(); 179 Attribute attr = null; 180 try 181 { 182 Class argType = getPrimativeClass(attrType); 183 if( argType == null ) 184 argType = loader.loadClass(attrType); 185 PropertyEditor editor = PropertyEditorManager.findEditor(argType); 186 if( editor == null ) 188 { 189 log.trace("Failed to find PropertyEditor for type: "+attrType); 190 continue; 191 } 192 editor.setAsText(value); 193 attr = new Attribute (attrName, editor.getValue()); 194 } 195 catch(ClassNotFoundException e) 196 { 197 log.trace("Failed to load class for attribute: "+ (attr==null?"null":attr.getName()), e); 198 throw new ReflectionException (e, "Failed to load class for attribute: " + (attr==null?"null":attr.getName())); 199 } 200 201 server.setAttribute(objName, attr); 202 newAttributes.add(attr); 203 } 204 return newAttributes; 205 } 206 207 public static OpResultInfo invokeOp(String name, int index, String [] args) throws JMException 208 { 209 MBeanServer server = getMBeanServer(); 210 ObjectName objName = new ObjectName (name); 211 MBeanInfo info = server.getMBeanInfo(objName); 212 MBeanOperationInfo [] opInfo = info.getOperations(); 213 MBeanOperationInfo op = opInfo[index]; 214 MBeanParameterInfo [] paramInfo = op.getSignature(); 215 String [] argTypes = new String [paramInfo.length]; 216 for(int p = 0; p < paramInfo.length; p ++) 217 argTypes[p] = paramInfo[p].getType(); 218 return invokeOpByName(name, op.getName(), argTypes, args); 219 } 220 221 public static OpResultInfo invokeOpByName(String name, String opName, String [] argTypes, String [] args) 222 throws JMException 223 { 224 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 225 MBeanServer server = getMBeanServer(); 226 ObjectName objName = new ObjectName (name); 227 int length = argTypes != null ? argTypes.length : 0; 228 Object [] typedArgs = new Object [length]; 229 for(int p = 0; p < typedArgs.length; p ++) 230 { 231 String arg = args[p]; 232 try 233 { 234 Class argType = getPrimativeClass(argTypes[p]); 235 if( argType == null ) 236 argType = loader.loadClass(argTypes[p]); 237 PropertyEditor editor = PropertyEditorManager.findEditor(argType); 238 if( editor == null ) 239 throw new IntrospectionException ("Failed to find PropertyEditor for type: "+argTypes[p]); 240 editor.setAsText(arg); 241 typedArgs[p] = editor.getValue(); 242 } 243 catch(ClassNotFoundException e) 244 { 245 log.trace("Failed to load class for arg"+p, e); 246 throw new ReflectionException (e, "Failed to load class for arg"+p); 247 } 248 } 249 Object opReturn = server.invoke(objName, opName, typedArgs, argTypes); 250 return new OpResultInfo(opName, argTypes, opReturn); 251 } 252 253 static Class getPrimativeClass(String type) 254 { 255 String [] primatives = { 256 "boolean", "byte", "char", "int", "short", 257 "float", "double", "long" 258 }; 259 Class [] primativeTypes = { 260 boolean.class, byte.class, char.class, int.class, short.class, 261 float.class, double.class, long.class 262 }; 263 Class c = null; 264 for(int p = 0; p < primatives.length; p ++) 265 { 266 if( type.equals(primatives[p]) ) 267 c = primativeTypes[p]; 268 } 269 return c; 270 } 271 } 272 | Popular Tags |