KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Set JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MBeanServerConnection JavaDoc;
30
31 import gnu.getopt.Getopt;
32 import gnu.getopt.LongOpt;
33
34 import org.jboss.util.Strings;
35
36 /**
37  * Get information about the server.
38  *
39  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
40  * @author Scott.Stark@jboss.org
41  * @version $Revision: 37459 $
42  */

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 JavaDoc 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 JavaDoc[] 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 JavaDoc 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             // non-option arguments
108
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 JavaDoc[] args) throws Exception JavaDoc
126    {
127       processArguments(args);
128       
129       PrintWriter JavaDoc out = context.getWriter();
130       MBeanServerConnection JavaDoc server = getMBeanServer();
131
132       // mode should be valid, either invalid arg or no arg
133

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 JavaDoc all = new ObjectName JavaDoc("*:*");
146             Set JavaDoc names = server.queryNames(all, null);
147             Iterator JavaDoc iter = names.iterator();
148             while( iter.hasNext() )
149                out.println(iter.next());
150             break;
151
152          default:
153             throw new IllegalStateException JavaDoc("invalid mode: " + mode);
154       }
155       
156       out.flush();
157    }
158 }
159
Popular Tags