KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > twiddle > command > CreateCommand


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.twiddle.command;
23
24 import java.io.PrintWriter JavaDoc;
25
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.ObjectInstance JavaDoc;
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30
31 import gnu.getopt.Getopt;
32 import gnu.getopt.LongOpt;
33
34 import org.jboss.util.Strings;
35
36 /**
37  * Create an MBean.
38  *
39  * @version <tt>$Revision: 37459 $</tt>
40  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
41  */

42 public class CreateCommand
43    extends MBeanServerCommand
44 {
45    private String JavaDoc className;
46
47    private ObjectName JavaDoc objectName;
48
49    private ObjectName JavaDoc loaderName;
50
51    public CreateCommand()
52    {
53       super("create", "Create an MBean");
54    }
55    
56    public void displayHelp()
57    {
58       PrintWriter JavaDoc 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 JavaDoc[] 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 JavaDoc 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             // non-option arguments
105
case 1:
106             {
107                String JavaDoc 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 JavaDoc(arg);
119                         log.debug("mbean name: " + objectName);
120                      }
121                      catch (MalformedObjectNameException JavaDoc 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             // Set the loader name
134
case 'l':
135             {
136                String JavaDoc arg = getopt.getOptarg();
137                
138                try {
139                   loaderName = new ObjectName JavaDoc(arg);
140                   log.debug("loader name: " + loaderName);
141                }
142                catch (MalformedObjectNameException JavaDoc 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 JavaDoc[] args) throws Exception JavaDoc
154    {
155       processArguments(args);
156       
157       if (className == null)
158          throw new CommandException("Missing class name");
159
160       MBeanServerConnection JavaDoc server = getMBeanServer();
161
162       ObjectInstance JavaDoc 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 JavaDoc out = context.getWriter();
173          out.println(obj.getObjectName());
174          out.flush();
175       }
176    }
177 }
178
Popular Tags