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

QueryCommand

public class QueryCommand extends MBeanServerCommand
Query the server for a list of matching MBeans.
version
$Revision: 57191 $
author
Jason Dillon
author
Scott.Stark@jboss.org

Fields Summary
private String
query
private boolean
displayCount
Constructors Summary
public QueryCommand()

      super("query", "Query the server for a list of matching MBeans");
   
Methods Summary
public voiddisplayHelp()

      PrintWriter out = context.getWriter();

      out.println(desc);
      out.println();
      out.println("usage: " + name + " [options] <query>");
      out.println("options:");
      out.println("    -c, --count    Display the matching MBean count");
      out.println("    --             Stop processing options");
      out.println("Examples:");
      out.println(" query all mbeans: "+name+" '*:*'");
      out.println(" query all mbeans in the jboss.j2ee domain: "+name+" 'jboss.j2ee:*'");

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

      processArguments(args);

      if (query == null)
         throw new CommandException("Missing MBean query");
      
      
      // get the list of object names to work with
      ObjectName[] names = queryMBeans(query);

      PrintWriter out = context.getWriter();

      if (displayCount)
      {
         out.println(names.length);
      }
      else
      {
         for (int i = 0; i < names.length; i++)
         {
            out.println(names[i]);
         }
      }

      out.flush();
   
private voidprocessArguments(java.lang.String[] args)

      log.debug("processing arguments: " + Strings.join(args, ","));

      if (args.length == 0)
      {
         throw new CommandException("Command requires arguments");
      }

      String sopts = "-:c";
      LongOpt[] lopts =
         {
            new LongOpt("count", LongOpt.NO_ARGUMENT, null, 'c"),
         };

      Getopt getopt = new Getopt(null, args, sopts, lopts);
      getopt.setOpterr(false);

      int code;
      int argidx = 0;

      while ((code = getopt.getopt()) != -1)
      {
         switch (code)
         {
            case ':":
               throw new CommandException
                  ("Option requires an argument: " + args[getopt.getOptind() - 1]);

            case '?":
               throw new CommandException
                  ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);

               // non-option arguments
            case 1:
               {
                  String arg = getopt.getOptarg();

                  switch (argidx++)
                  {
                     case 0:
                        query = arg;
                        log.debug("query: " + query);
                        break;

                     default:
                        throw new CommandException("Unused argument: " + arg);
                  }
                  break;
               }

               // Show count
            case 'c":
               displayCount = true;
               break;
         }
      }