KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import gnu.getopt.Getopt;
29 import gnu.getopt.LongOpt;
30
31 import org.jboss.util.Strings;
32
33 /**
34  * Query the server for a list of matching MBeans.
35  *
36  * @version <tt>$Revision: 37459 $</tt>
37  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
38  * @author Scott.Stark@jboss.org
39  */

40 public class QueryCommand
41    extends MBeanServerCommand
42 {
43    private String JavaDoc query;
44
45    private boolean displayCount;
46
47    public QueryCommand()
48    {
49       super("query", "Query the server for a list of matching MBeans");
50    }
51
52    public void displayHelp()
53    {
54       PrintWriter JavaDoc out = context.getWriter();
55
56       out.println(desc);
57       out.println();
58       out.println("usage: " + name + " [options] <query>");
59       out.println("options:");
60       out.println(" -c, --count Display the matching MBean count");
61       out.println(" -- Stop processing options");
62       out.println("Examples:");
63       out.println(" query all mbeans: "+name+" '*:*'");
64       out.println(" query all mbeans in the jboss.j2ee domain: "+name+" 'jboss.j2ee:*'");
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       {
76          throw new CommandException("Command requires arguments");
77       }
78
79       String JavaDoc sopts = "-:c";
80       LongOpt[] lopts =
81          {
82             new LongOpt("count", LongOpt.NO_ARGUMENT, null, 'c'),
83          };
84
85       Getopt getopt = new Getopt(null, args, sopts, lopts);
86       getopt.setOpterr(false);
87
88       int code;
89       int argidx = 0;
90
91       while ((code = getopt.getopt()) != -1)
92       {
93          switch (code)
94          {
95             case ':':
96                throw new CommandException
97                   ("Option requires an argument: " + args[getopt.getOptind() - 1]);
98
99             case '?':
100                throw new CommandException
101                   ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);
102
103                // non-option arguments
104
case 1:
105                {
106                   String JavaDoc arg = getopt.getOptarg();
107
108                   switch (argidx++)
109                   {
110                      case 0:
111                         query = arg;
112                         log.debug("query: " + query);
113                         break;
114
115                      default:
116                         throw new CommandException("Unused argument: " + arg);
117                   }
118                   break;
119                }
120
121                // Show count
122
case 'c':
123                displayCount = true;
124                break;
125          }
126       }
127    }
128
129    public void execute(String JavaDoc[] args) throws Exception JavaDoc
130    {
131       processArguments(args);
132
133       if (query == null)
134          throw new CommandException("Missing MBean query");
135       
136       
137       // get the list of object names to work with
138
ObjectName JavaDoc[] names = queryMBeans(query);
139
140       PrintWriter JavaDoc out = context.getWriter();
141
142       if (displayCount)
143       {
144          out.println(names.length);
145       }
146       else
147       {
148          for (int i = 0; i < names.length; i++)
149          {
150             out.println(names[i]);
151          }
152       }
153
154       out.flush();
155    }
156 }
157
Popular Tags