1 22 package org.jboss.console.twiddle.command; 23 24 import java.io.PrintWriter ; 25 26 import javax.management.ObjectName ; 27 28 import gnu.getopt.Getopt; 29 import gnu.getopt.LongOpt; 30 31 import org.jboss.util.Strings; 32 33 40 public class QueryCommand 41 extends MBeanServerCommand 42 { 43 private String query; 44 45 private boolean displayCount; 46 47 public QueryCommand() 48 { 49 super("query", "Query the server for a list of matching MBeans"); 50 } 51 52 public void displayHelp() 53 { 54 PrintWriter out = context.getWriter(); 55 56 out.println(desc); 57 out.println(); 58 out.println("usage: " + name + " [options] <query>"); 59 out.println("options:"); 60 out.println(" -c, --count Display the matching MBean count"); 61 out.println(" -- Stop processing options"); 62 out.println("Examples:"); 63 out.println(" query all mbeans: "+name+" '*:*'"); 64 out.println(" query all mbeans in the jboss.j2ee domain: "+name+" 'jboss.j2ee:*'"); 65 66 out.flush(); 67 } 68 69 private void processArguments(final String [] args) 70 throws CommandException 71 { 72 log.debug("processing arguments: " + Strings.join(args, ",")); 73 74 if (args.length == 0) 75 { 76 throw new CommandException("Command requires arguments"); 77 } 78 79 String sopts = "-:c"; 80 LongOpt[] lopts = 81 { 82 new LongOpt("count", LongOpt.NO_ARGUMENT, null, 'c'), 83 }; 84 85 Getopt getopt = new Getopt(null, args, sopts, lopts); 86 getopt.setOpterr(false); 87 88 int code; 89 int argidx = 0; 90 91 while ((code = getopt.getopt()) != -1) 92 { 93 switch (code) 94 { 95 case ':': 96 throw new CommandException 97 ("Option requires an argument: " + args[getopt.getOptind() - 1]); 98 99 case '?': 100 throw new CommandException 101 ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]); 102 103 case 1: 105 { 106 String arg = getopt.getOptarg(); 107 108 switch (argidx++) 109 { 110 case 0: 111 query = arg; 112 log.debug("query: " + query); 113 break; 114 115 default: 116 throw new CommandException("Unused argument: " + arg); 117 } 118 break; 119 } 120 121 case 'c': 123 displayCount = true; 124 break; 125 } 126 } 127 } 128 129 public void execute(String [] args) throws Exception 130 { 131 processArguments(args); 132 133 if (query == null) 134 throw new CommandException("Missing MBean query"); 135 136 137 ObjectName [] names = queryMBeans(query); 139 140 PrintWriter out = context.getWriter(); 141 142 if (displayCount) 143 { 144 out.println(names.length); 145 } 146 else 147 { 148 for (int i = 0; i < names.length; i++) 149 { 150 out.println(names[i]); 151 } 152 } 153 154 out.flush(); 155 } 156 } 157 | Popular Tags |