1 22 package org.jboss.console.twiddle.command; 23 24 import java.io.PrintWriter ; 25 26 import javax.management.ObjectName ; 27 import javax.management.ObjectInstance ; 28 import javax.management.MBeanServerConnection ; 29 import javax.management.MalformedObjectNameException ; 30 31 import gnu.getopt.Getopt; 32 import gnu.getopt.LongOpt; 33 34 import org.jboss.util.Strings; 35 36 42 public class CreateCommand 43 extends MBeanServerCommand 44 { 45 private String className; 46 47 private ObjectName objectName; 48 49 private ObjectName loaderName; 50 51 public CreateCommand() 52 { 53 super("create", "Create an MBean"); 54 } 55 56 public void displayHelp() 57 { 58 PrintWriter out = context.getWriter(); 59 60 out.println(desc); 61 out.println(); 62 out.println("usage: " + name + " [options] <class> [<name>]"); 63 out.println(); 64 out.println("options:"); 65 out.println(" -l, --loader=<name> Treat object name as a query"); 66 out.println(" -- Stop processing options"); 67 68 out.flush(); 69 } 70 71 private boolean processArguments(final String [] args) 72 throws CommandException 73 { 74 log.debug("processing arguments: " + Strings.join(args, ",")); 75 76 if (args.length == 0) { 77 throw new CommandException("Command requires arguments"); 78 } 79 80 String sopts = "-:l:"; 81 LongOpt[] lopts = 82 { 83 new LongOpt("loader", LongOpt.OPTIONAL_ARGUMENT, null, 'l'), 84 }; 85 86 Getopt getopt = new Getopt(null, args, sopts, lopts); 87 getopt.setOpterr(false); 88 89 int code; 90 int argidx = 0; 91 92 while ((code = getopt.getopt()) != -1) 93 { 94 switch (code) 95 { 96 case ':': 97 throw new CommandException 98 ("Option requires an argument: "+ args[getopt.getOptind() - 1]); 99 100 case '?': 101 throw new CommandException 102 ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]); 103 104 case 1: 106 { 107 String arg = getopt.getOptarg(); 108 109 switch (argidx++) { 110 case 0: 111 className = arg; 112 log.debug("class name: " + className); 113 break; 114 115 case 1: 116 { 117 try { 118 objectName = new ObjectName (arg); 119 log.debug("mbean name: " + objectName); 120 } 121 catch (MalformedObjectNameException e) { 122 throw new CommandException("Invalid object name: " + arg); 123 } 124 break; 125 } 126 127 default: 128 throw new CommandException("Unused argument: " + arg); 129 } 130 break; 131 } 132 133 case 'l': 135 { 136 String arg = getopt.getOptarg(); 137 138 try { 139 loaderName = new ObjectName (arg); 140 log.debug("loader name: " + loaderName); 141 } 142 catch (MalformedObjectNameException e) { 143 throw new CommandException("Invalid loader object name: " + arg); 144 } 145 break; 146 } 147 } 148 } 149 150 return true; 151 } 152 153 public void execute(String [] args) throws Exception 154 { 155 processArguments(args); 156 157 if (className == null) 158 throw new CommandException("Missing class name"); 159 160 MBeanServerConnection server = getMBeanServer(); 161 162 ObjectInstance obj; 163 164 if (loaderName == null) { 165 obj = server.createMBean(className, objectName); 166 } 167 else { 168 obj = server.createMBean(className, objectName, loaderName); 169 } 170 171 if (!context.isQuiet()) { 172 PrintWriter out = context.getWriter(); 173 out.println(obj.getObjectName()); 174 out.flush(); 175 } 176 } 177 } 178 | Popular Tags |