FileDocCategorySizeDatePackage
XMBeanCommand.javaAPI DocJBoss 4.2.17693Fri Jul 13 21:02:16 BST 2007org.jboss.console.twiddle.command

XMBeanCommand

public class XMBeanCommand extends MBeanServerCommand
Command to print out mbean metadata as an xmbean descriptor. The idea is to redirect and use the output as the skeleton for writing an xmbean descriptor for an existing mbean, since this can be very tedious to write by hand.
author
Dimitris Andreadis
version
$Revision: 57191 $

Fields Summary
Constructors Summary
public XMBeanCommand()
Default CTOR

      super("xmbean", "Print out mbean metadata as an xmbean descriptor");
   
Methods Summary
public voiddisplayHelp()

      PrintWriter out = context.getWriter();

      out.println(desc);
      out.println();      
      out.println("Redirect the output and use it as a skeleton for");
      out.println("writing an xmbean descriptor for an existing mbean.");
      out.println();
      out.println("Usage: " + name + " <object-name>");
      out.println();

      out.flush();
   
public voidexecute(java.lang.String[] args)

      if (args.length != 1)
      {
         throw new CommandException("Missing object name");
      }
      ObjectName target = super.createObjectName(args[0]);

      MBeanInfo mbeanInfo = getMBeanServer().getMBeanInfo(target);
      MBeanConstructorInfo[] ctors = mbeanInfo.getConstructors();
      MBeanAttributeInfo[] attrs = mbeanInfo.getAttributes();
      MBeanOperationInfo[] ops = mbeanInfo.getOperations();
      MBeanNotificationInfo[] notifs = mbeanInfo.getNotifications();
      
      PrintWriter out = context.getWriter();

      // header
      out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
      out.println("<!DOCTYPE mbean PUBLIC");
      out.println("   \"-//JBoss//DTD JBOSS XMBEAN 1.2//EN\"");
      out.println("   \"http://www.jboss.org/j2ee/dtd/jboss_xmbean_1_2.dtd\">");
      out.println("<!--");
      out.println("   xmbean descriptor generated by 'twiddle'");
      out.println("   on " +  new Date());
      out.println("   for '" + target + "'");
      out.println("-->");
      
      out.println("<mbean>");
      
      // mbean
      out.println("   <description>" + mbeanInfo.getDescription() + "</description>");
      out.println("   <class>" + mbeanInfo.getClassName() + "</class>");
      out.println();
      
      // constructors
      if (ctors.length > 0)
      {
         for (int i = 0; i < ctors.length; i++)
         {
            MBeanConstructorInfo ctorInfo = ctors[i];
            out.println("   <constructor>");
            out.println("      <description>" + ctorInfo.getDescription() + "</description>");
            out.println("      <name>" + ctorInfo.getName() + "</name>");
            outputParameters(out, ctorInfo.getSignature());
            out.println("   </constructor>");
         }
         out.println();
      }
      
      // attributes
      if (attrs.length > 0)
      {
         for (int i = 0; i < attrs.length; i++)
         {
            MBeanAttributeInfo attrInfo = attrs[i];
            
            // determine access, rw by default
            String access = "read-write";
            access = attrInfo.isReadable() ? access : "write-only";
            access = attrInfo.isWritable() ? access : "read-only";
            String accessString = " access='" + access + "'";

            // determine get method, if any
            String getMethodString = "";
            if (attrInfo.isReadable())
            {
               getMethodString = " getMethod='" +
                  (attrInfo.isIs() ? "is" : "get") + attrInfo.getName() + "'";  
            }
            
            // determine set method, if any
            String setMethodString = "";
            if (attrInfo.isWritable())
            {
               setMethodString = " setMethod='set" + attrInfo.getName() + "'";
               
            }
            
            out.println("   <attribute" + accessString + getMethodString + setMethodString + ">");
            out.println("      <description>" + attrInfo.getDescription() + "</description>");
            out.println("      <name>" + attrInfo.getName() + "</name>");
            out.println("      <type>" + attrInfo.getType() + "</type>");
            out.println("   </attribute>");
         }
         out.println();
      }
      
      // operations
      if (ops.length > 0)
      {
         for (int i = 0; i < ops.length; i++)
         {
            MBeanOperationInfo opInfo = ops[i];
            // nobody uses opInfo.getImpact()
            out.println("   <operation>");
            out.println("      <description>" + opInfo.getDescription() + "</description>");
            out.println("      <name>" + opInfo.getName() + "</name>");
            outputParameters(out, opInfo.getSignature());            
            out.println("      <return-type>" + opInfo.getReturnType() + "</return-type>");
            out.println("   </operation>");
         }
         out.println();
      }
      
      // notifications
      if (notifs.length > 0)
      {
         for (int i = 0; i < notifs.length; i++)
         {
            MBeanNotificationInfo notifInfo = notifs[i];
            String[] types = notifInfo.getNotifTypes();
            out.println("   <notification>");
            out.println("      <description>" + notifInfo.getDescription() + "</description>");
            out.println("      <name>" + notifInfo.getName() + "</name>");
            for (int j = 0; j < types.length; j++)
            {
               out.println("      <notification-type>" + types[j] + "</notification-type>");
            }
            out.println("   </notification>");
         }
         out.println();
      }
      
      out.println("</mbean>");
      
      out.flush();
   
private voidoutputParameters(java.io.PrintWriter out, javax.management.MBeanParameterInfo[] params)

      for (int i = 0; i < params.length; i++)
      {
         MBeanParameterInfo paramInfo = params[i];
         out.println("      <parameter>");
         out.println("         <description>" + paramInfo.getDescription() + "</description>");
         out.println("         <name>" + paramInfo.getName() + "</name>");
         out.println("         <type>" + paramInfo.getType() + "</type>");
         out.println("      </parameter>");
      }