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