Package org.jboss.fresh.shell.commands

Source Code of org.jboss.fresh.shell.commands.MBeanQueryExe

package org.jboss.fresh.shell.commands;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.PrintWriter2;
import org.jboss.fresh.shell.AbstractExecutable;

public class MBeanQueryExe extends AbstractExecutable {

  private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(MBeanQueryExe.class);

  private static final char LIST = 'L';
  private static final char QUERY = 'Q';

  public void process(String path, String[] params) throws Exception {

    log.debug("entered");
    // read first parameter which is filename
    if (helpRequested()) {
      usage();
      return;
    }

    PrintWriter2 pout = new PrintWriter2(new BufferWriter(getStdOut()));

    boolean alpha = false;
    int srv = 1;
    char action = QUERY;
    boolean info = false;

    String pattern = null;
    String mbname = null;

    for (int i = 0; i < params.length; i++) {
      String tmp = params[i];

      if (tmp.equals("-a")) {
        alpha = true;
      } else if (tmp.equals("-s")) {
        try {
          srv = Integer.parseInt(params[i + 1]);
               i++; // move past server ordinal
        } catch (Exception ex) {
          pout.println("-s should be followed by an ordinal number");
          return;
        }
      } else if (tmp.equals("-l")) {
        action = LIST;
      } else if (tmp.equals("-i")) {
        info = true;
      } else {
        pattern = tmp;
        break; // this conditions order :-)
      }
    }


    if (action == LIST) {
      ArrayList ls = MBeanServerFactory.findMBeanServer(null);
      pout.println(ls.size() + " server" + (ls.size() == 1 ? "" : "s") + " available:");
      Iterator it = ls.iterator();
      for (int i = 1; it.hasNext(); i++) {
        MBeanServer mbsrv = (MBeanServer) it.next();
        pout.println(i + ")  has " + mbsrv.getMBeanCount() + " mbeans registered.");
      }
      return;
    }

    if (pattern == null) {
      pout.println("No query specified.");
      usage();
      return;
    }

    MBeanServer mbsrv = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(srv - 1);

/*
    int idx = pattern.indexOf(",");
    if(idx==-1) {
      mbname = pattern;
      pattern = null;
    } else {
      mbname = pattern.substring(0, idx);
      if(mbname.trim().length()==0) mbname = null;

      pattern = pattern.substring(idx+1);
      if(pattern.trim().length()==0) pattern = null;
*/
    Set res = mbsrv.queryNames(new ObjectName(pattern), null);
    Iterator it = res.iterator();

    while (it.hasNext()) {
      ObjectName nm = (ObjectName) it.next();
      pout.println(nm);
      if(info) {
        //Iterator it2 = mbsrv.queryMBeans(nm, null).iterator();
        //while(it2.hasNext()) {
          //ObjectInstance inst = (ObjectInstance) it2.next();
          //pout.println();
        //}
       
         MBeanInfo mbinf = mbsrv.getMBeanInfo(nm);
         pout.println("  Description: " + mbinf.getDescription());
         pout.println("  Class: " + mbinf.getClassName());
         pout.println("  Attributes:");
         MBeanAttributeInfo [] ainf = mbinf.getAttributes();
         for(int i=0; i<ainf.length; i++) {
           pout.println("    " + ainf[i].getType() + "   " + ainf[i].getName() + "   (" + (ainf[i].getDescription() == null ? "no description"  : ainf[i].getDescription()) + ")");
         }
        
         pout.println("  Operations:");
         MBeanOperationInfo [] oinf = mbinf.getOperations();
         for(int i=0; i<oinf.length; i++) {
           MBeanParameterInfo [] pinf = oinf[i].getSignature();
           StringBuffer sb = new StringBuffer();
           for(int j=0; j<pinf.length; j++) {
             if(j > 0)
               sb.append(", ");
             sb.append(pinf[j].getType() + " " + pinf[j].getName());
           }
           pout.println("    " + oinf[i].getReturnType() + "   " + oinf[i].getName() + "   (" + sb + ")");
         }


         //mbsrv.add
         pout.println("  Notifications:");
         MBeanNotificationInfo [] ninf = mbinf.getNotifications();
         for(int i=0; i<ninf.length; i++) {
           pout.println("    " + ninf[i].getName() + " :: " + Arrays.asList(ninf[i].getNotifTypes()));
         }        
      }
    }
  }


  void usage() throws Exception {
    PrintWriter2 pout = new PrintWriter2(new BufferWriter(getStdOut()));
    pout.println("Usage:  mbquery [-a] <query pattern>");
    pout.println();
    pout.println("          -a : list results in alphabetical order.");
    pout.println("          -s : server expressed as ordinal");
    pout.println("          -l : list available servers");
    pout.println("         --help : this help");
    pout.println();
    pout.println("  Example:  mbquery -a *:*");
    pout.close();
    log.debug("done");
  }

}
TOP

Related Classes of org.jboss.fresh.shell.commands.MBeanQueryExe

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.