1 22 package org.jboss.console.twiddle.command; 23 24 import java.io.PrintWriter ; 25 import java.util.Set ; 26 import java.util.Iterator ; 27 28 import javax.management.ObjectName ; 29 import javax.management.MBeanServerConnection ; 30 31 import gnu.getopt.Getopt; 32 import gnu.getopt.LongOpt; 33 34 import org.jboss.util.Strings; 35 36 43 public class ServerInfoCommand 44 extends MBeanServerCommand 45 { 46 public static final int UNKNOWN = 0; 47 public static final int DEFAULT_DOMAIN = 1; 48 public static final int MBEAN_COUNT = 2; 49 public static final int LIST_NAMES = 3; 50 51 private int mode = UNKNOWN; 52 53 public ServerInfoCommand() 54 { 55 super("serverinfo", "Get information about the MBean server"); 56 } 57 58 public void displayHelp() 59 { 60 PrintWriter out = context.getWriter(); 61 62 out.println(desc); 63 out.println(); 64 out.println("usage: " + name + " [options]"); 65 out.println(); 66 out.println("options:"); 67 out.println(" -d, --domain Get the default domain"); 68 out.println(" -c, --count Get the MBean count"); 69 out.println(" -l, --list List the MBeans"); 70 out.println(" -- Stop processing options"); 71 } 72 73 private void processArguments(final String [] args) 74 throws CommandException 75 { 76 log.debug("processing arguments: " + Strings.join(args, ",")); 77 78 if (args.length == 0) 79 { 80 throw new CommandException("Command requires arguments"); 81 } 82 83 String sopts = "-:dcl"; 84 LongOpt[] lopts = 85 { 86 new LongOpt("domain", LongOpt.NO_ARGUMENT, null, 'd'), 87 new LongOpt("count", LongOpt.NO_ARGUMENT, null, 'c'), 88 new LongOpt("list", LongOpt.NO_ARGUMENT, null, 'l'), 89 }; 90 91 Getopt getopt = new Getopt(null, args, sopts, lopts); 92 getopt.setOpterr(false); 93 94 int code; 95 while ((code = getopt.getopt()) != -1) 96 { 97 switch (code) 98 { 99 case ':': 100 throw new CommandException 101 ("Option requires an argument: "+ args[getopt.getOptind() - 1]); 102 103 case '?': 104 throw new CommandException 105 ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]); 106 107 case 1: 109 throw new CommandException("Unused argument: " + getopt.getOptarg()); 110 111 case 'd': 112 mode = DEFAULT_DOMAIN; 113 break; 114 115 case 'c': 116 mode = MBEAN_COUNT; 117 break; 118 case 'l': 119 mode = LIST_NAMES; 120 break; 121 } 122 } 123 } 124 125 public void execute(String [] args) throws Exception 126 { 127 processArguments(args); 128 129 PrintWriter out = context.getWriter(); 130 MBeanServerConnection server = getMBeanServer(); 131 132 134 switch (mode) 135 { 136 case DEFAULT_DOMAIN: 137 out.println(server.getDefaultDomain()); 138 break; 139 140 case MBEAN_COUNT: 141 out.println(server.getMBeanCount()); 142 break; 143 144 case LIST_NAMES: 145 ObjectName all = new ObjectName ("*:*"); 146 Set names = server.queryNames(all, null); 147 Iterator iter = names.iterator(); 148 while( iter.hasNext() ) 149 out.println(iter.next()); 150 break; 151 152 default: 153 throw new IllegalStateException ("invalid mode: " + mode); 154 } 155 156 out.flush(); 157 } 158 } 159 | Popular Tags |