FileDocCategorySizeDatePackage
Server.javaAPI DocJBoss 4.2.19759Fri Jul 13 21:02:14 BST 2007org.jboss.console.plugins.helpers.jmx

Server

public class Server extends Object
Utility methods related to the MBeanServer interface
author
Scott.Stark@jboss.org
version
$Revision: 57191 $

Fields Summary
static Logger
log
Constructors Summary
Methods Summary
public static java.util.IteratorgetDomainData(java.lang.String filter)

      MBeanServer server = getMBeanServer();
      TreeMap domainData = new TreeMap();
      if( server != null )
      {
         ObjectName filterName = null;
         if( filter != null )
            filterName = new ObjectName(filter);
         Set objectNames = server.queryNames(filterName, null);
         Iterator objectNamesIter = objectNames.iterator();
         while( objectNamesIter.hasNext() )
         {
            ObjectName name = (ObjectName) objectNamesIter.next();
            MBeanInfo info = server.getMBeanInfo(name);
            String domainName = name.getDomain();
            MBeanData mbeanData = new MBeanData(name, info);
            DomainData data = (DomainData) domainData.get(domainName);
            if( data == null )
            {
               data = new DomainData(domainName);
               domainData.put(domainName, data);
            }
            data.addData(mbeanData);
         }
      }
      Iterator domainDataIter = domainData.values().iterator();
      return domainDataIter;
   
public static java.lang.StringgetMBeanAttribute(java.lang.String name, java.lang.String attrName)

      MBeanServer server = getMBeanServer();
      ObjectName objName = new ObjectName(name);
      String value = null;
      try
      {
         Object attr = server.getAttribute(objName, attrName);
         if( attr != null )
            value = attr.toString();
      }
      catch(JMException e)
      {
         value = e.getMessage();
      }
      return value;
   
public static java.lang.ObjectgetMBeanAttributeObject(java.lang.String name, java.lang.String attrName)

      MBeanServer server = getMBeanServer();
      ObjectName objName = new ObjectName(name);
      Object value = server.getAttribute(objName, attrName);
      return value;
   
public static AttrResultInfogetMBeanAttributeResultInfo(java.lang.String name, javax.management.MBeanAttributeInfo attrInfo)

      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      MBeanServer server = getMBeanServer();
      ObjectName objName = new ObjectName(name);
      String attrName = attrInfo.getName();
      String attrType = attrInfo.getType();
      Object value = null;
      Throwable throwable = null;
      if( attrInfo.isReadable() == true )
      {
         try
         {
            value = server.getAttribute(objName, attrName);
         }
         catch (Throwable t)
         {
            throwable = t;
         }
      }
      Class typeClass = null;
      try
      {
         typeClass = getPrimativeClass(attrType);
         if( typeClass == null )
            typeClass = loader.loadClass(attrType);
      }
      catch(ClassNotFoundException ignore)
      {
      }
      PropertyEditor editor = null;
      if( typeClass != null )
         editor = PropertyEditorManager.findEditor(typeClass);

      return new AttrResultInfo(attrName, editor, value, throwable);
   
public static MBeanDatagetMBeanData(java.lang.String name)

      MBeanServer server = getMBeanServer();
      ObjectName objName = new ObjectName(name);
      MBeanInfo info = server.getMBeanInfo(objName);
      MBeanData mbeanData = new MBeanData(objName, info);
      return mbeanData;
   
public static javax.management.MBeanServergetMBeanServer()


        
   
      return MBeanServerLocator.locateJBoss();
   
static java.lang.ClassgetPrimativeClass(java.lang.String type)

      String[] primatives = {
            "boolean", "byte", "char", "int", "short",
            "float", "double", "long"
         };
      Class[] primativeTypes = {
         boolean.class, byte.class, char.class, int.class, short.class,
         float.class, double.class, long.class
      };
      Class c = null;
      for(int p = 0; p < primatives.length; p ++)
      {
         if( type.equals(primatives[p]) )
            c = primativeTypes[p];
      }
      return c;
   
public static OpResultInfoinvokeOp(java.lang.String name, int index, java.lang.String[] args)

      MBeanServer server = getMBeanServer();
      ObjectName objName = new ObjectName(name);
      MBeanInfo info = server.getMBeanInfo(objName);
      MBeanOperationInfo[] opInfo = info.getOperations();
      MBeanOperationInfo op = opInfo[index];
      MBeanParameterInfo[] paramInfo = op.getSignature();
      String[] argTypes = new String[paramInfo.length];
      for(int p = 0; p < paramInfo.length; p ++)
         argTypes[p] = paramInfo[p].getType();
      return invokeOpByName(name, op.getName(), argTypes, args);
   
public static OpResultInfoinvokeOpByName(java.lang.String name, java.lang.String opName, java.lang.String[] argTypes, java.lang.String[] args)

      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      MBeanServer server = getMBeanServer();
      ObjectName objName = new ObjectName(name);
      int length = argTypes != null ? argTypes.length : 0;
      Object[] typedArgs = new Object[length];
      for(int p = 0; p < typedArgs.length; p ++)
      {
         String arg = args[p];
         try
         {
            Class argType = getPrimativeClass(argTypes[p]);
            if( argType == null )
               argType = loader.loadClass(argTypes[p]);
            PropertyEditor editor = PropertyEditorManager.findEditor(argType);
            if( editor == null )
               throw new IntrospectionException("Failed to find PropertyEditor for type: "+argTypes[p]);
            editor.setAsText(arg);
            typedArgs[p] = editor.getValue();
         }
         catch(ClassNotFoundException e)
         {
            log.trace("Failed to load class for arg"+p, e);
            throw new ReflectionException(e, "Failed to load class for arg"+p);
         }
      }
      Object opReturn = server.invoke(objName, opName, typedArgs, argTypes);
      return new OpResultInfo(opName, argTypes, opReturn);
   
public static javax.management.AttributeListsetAttributes(java.lang.String name, java.util.HashMap attributes)

      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      MBeanServer server = getMBeanServer();
      ObjectName objName = new ObjectName(name);
      MBeanInfo info = server.getMBeanInfo(objName);
      MBeanAttributeInfo[] attributesInfo = info.getAttributes();
      AttributeList newAttributes = new AttributeList();
      for(int a = 0; a < attributesInfo.length; a ++)
      {
         MBeanAttributeInfo attrInfo = attributesInfo[a];
         String attrName = attrInfo.getName();
         if( attributes.containsKey(attrName) == false )
            continue;
         String value = (String) attributes.get(attrName);
         String attrType = attrInfo.getType();
         Attribute attr = null;
         try
         {
            Class argType = getPrimativeClass(attrType);
            if( argType == null )
               argType = loader.loadClass(attrType);
            PropertyEditor editor = PropertyEditorManager.findEditor(argType);
            // Ignore attributes without valid editors
            if( editor == null )
            {
               log.trace("Failed to find PropertyEditor for type: "+attrType);
               continue;
            }
            editor.setAsText(value);
            attr = new Attribute(attrName, editor.getValue());
         }
         catch(ClassNotFoundException e)
         {
            log.trace("Failed to load class for attribute: "+ (attr==null?"null":attr.getName()), e);
            throw new ReflectionException(e, "Failed to load class for attribute: " + (attr==null?"null":attr.getName()));
         }

         server.setAttribute(objName, attr);
         newAttributes.add(attr);
      }
      return newAttributes;