KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.MBeanServerConnection JavaDoc;
32
33 import gnu.getopt.Getopt;
34 import gnu.getopt.LongOpt;
35
36 import org.jboss.util.Strings;
37
38 /**
39  * Unregister one or more MBeans.
40  *
41  * @version <tt>$Revision: 37459 $</tt>
42  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
43  * @author Scott.Stark@jboss.com
44  */

45 public class UnregisterCommand
46    extends MBeanServerCommand
47 {
48    private List JavaDoc names = new ArrayList JavaDoc();
49    
50    public UnregisterCommand()
51    {
52       super("unregister", "Unregister one or more MBeans");
53    }
54    
55    public void displayHelp()
56    {
57       PrintWriter JavaDoc out = context.getWriter();
58       
59       out.println(desc);
60       out.println();
61       out.println("usage: " + name + " [options] (<name>)+");
62       out.println();
63       out.println("options:");
64       out.println(" -- Stop processing options");
65
66       out.flush();
67    }
68    
69    private void processArguments(final String JavaDoc[] args)
70       throws CommandException
71    {
72       log.debug("processing arguments: " + Strings.join(args, ","));
73
74       if (args.length == 0) {
75          throw new CommandException("Command requires arguments");
76       }
77       
78       String JavaDoc sopts = "-:";
79       LongOpt[] lopts =
80       {
81          // new LongOpt("count", LongOpt.NO_ARGUMENT, null, 'c'),
82
};
83
84       Getopt getopt = new Getopt(null, args, sopts, lopts);
85       getopt.setOpterr(false);
86       
87       int code;
88       int argidx = 0;
89       
90       while ((code = getopt.getopt()) != -1)
91       {
92          switch (code)
93          {
94             case ':':
95                throw new CommandException
96                   ("Option requires an argument: "+ args[getopt.getOptind() - 1]);
97
98             case '?':
99                throw new CommandException
100                   ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);
101
102             // non-option arguments
103
case 1:
104             {
105                String JavaDoc arg = getopt.getOptarg();
106                
107                switch (argidx++) {
108                   default:
109                      names.add(createObjectName(arg));
110                      break;
111                }
112                break;
113             }
114          }
115       }
116    }
117
118    public void execute(String JavaDoc[] args) throws Exception JavaDoc
119    {
120       processArguments(args);
121
122       log.debug("object names: " + names);
123
124       if (names.size() == 0)
125          throw new CommandException("At least one object name is required");
126
127       MBeanServerConnection JavaDoc server = getMBeanServer();
128
129       Iterator JavaDoc iter = names.iterator();
130       while (iter.hasNext())
131       {
132          ObjectName JavaDoc name = (ObjectName JavaDoc)iter.next();
133          server.unregisterMBean(name);
134       }
135    }
136 }
137
Popular Tags