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

CreateCommand

public class CreateCommand extends MBeanServerCommand
Create an MBean.
version
$Revision: 57191 $
author
Jason Dillon

Fields Summary
private String
className
private ObjectName
objectName
private ObjectName
loaderName
Constructors Summary
public CreateCommand()

      super("create", "Create an MBean");
   
Methods Summary
public voiddisplayHelp()

      PrintWriter out = context.getWriter();
      
      out.println(desc);
      out.println();
      out.println("usage: " + name + " [options] <class> [<name>]");
      out.println();
      out.println("options:");
      out.println("    -l, --loader=<name>    Treat object name as a query");
      out.println("    --                     Stop processing options");

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

      processArguments(args);
      
      if (className == null)
         throw new CommandException("Missing class name");

      MBeanServerConnection server = getMBeanServer();

      ObjectInstance obj;

      if (loaderName == null) {
         obj = server.createMBean(className, objectName);
      }
      else {
         obj = server.createMBean(className, objectName, loaderName);
      }

      if (!context.isQuiet()) {
         PrintWriter out = context.getWriter();
         out.println(obj.getObjectName());
         out.flush();
      }
   
private booleanprocessArguments(java.lang.String[] args)

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

      if (args.length == 0) {
         throw new CommandException("Command requires arguments");
      }
      
      String sopts = "-:l:";
      LongOpt[] lopts =
      {
         new LongOpt("loader", LongOpt.OPTIONAL_ARGUMENT, null, 'l"),
      };

      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:
                     className = arg;
                     log.debug("class name: " + className);
                     break;

                  case 1:
                  {
                     try {
                        objectName = new ObjectName(arg);
                        log.debug("mbean name: " + objectName);
                     }
                     catch (MalformedObjectNameException e) {
                        throw new CommandException("Invalid object name: " + arg);
                     }
                     break;
                  }
                  
                  default:
                     throw new CommandException("Unused argument: " + arg);
               }
               break;
            }

            // Set the loader name
            case 'l":
            {
               String arg = getopt.getOptarg();
               
               try {
                  loaderName = new ObjectName(arg);
                  log.debug("loader name: " + loaderName);
               }
               catch (MalformedObjectNameException e) {
                  throw new CommandException("Invalid loader object name: " + arg);
               }
               break;
            }
         }
      }

      return true;