KickJava   Java API By Example, From Geeks To Geeks.

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


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

48 public class GetCommand
49    extends MBeanServerCommand
50 {
51    private ObjectName JavaDoc objectName;
52
53    private List JavaDoc attributeNames = new ArrayList JavaDoc(5);
54
55    private boolean prefix = true;
56
57    public GetCommand()
58    {
59       super("get", "Get the values of one or more MBean attributes");
60    }
61
62    public void displayHelp()
63    {
64       PrintWriter JavaDoc out = context.getWriter();
65
66       out.println(desc);
67       out.println();
68       out.println("usage: " + name + " [options] <name> [<attr>+]");
69       out.println(" If no attribute names are given all readable attributes are retrieved");
70       out.println("options:");
71       out.println(" --noprefix Do not display attribute name prefixes");
72       out.println(" -- Stop processing options");
73
74       out.flush();
75    }
76
77    private boolean processArguments(final String JavaDoc[] args)
78       throws CommandException
79    {
80       log.debug("processing arguments: " + Strings.join(args, ","));
81
82       if (args.length == 0)
83       {
84          throw new CommandException("Command requires arguments");
85       }
86
87       String JavaDoc sopts = "-:";
88       LongOpt[] lopts =
89          {
90             new LongOpt("noprefix", LongOpt.NO_ARGUMENT, null, 0x1000),
91          };
92
93       Getopt getopt = new Getopt(null, args, sopts, lopts);
94       getopt.setOpterr(false);
95
96       int code;
97       int argidx = 0;
98
99       while ((code = getopt.getopt()) != -1)
100       {
101          switch (code)
102          {
103             case ':':
104                throw new CommandException
105                   ("Option requires an argument: " + args[getopt.getOptind() - 1]);
106
107             case '?':
108                throw new CommandException
109                   ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);
110
111             case 0x1000:
112                prefix = false;
113                break;
114                   
115                // non-option arguments
116
case 1:
117                {
118                   String JavaDoc arg = getopt.getOptarg();
119
120                   switch (argidx++)
121                   {
122                      case 0:
123                         objectName = createObjectName(arg);
124                         log.debug("mbean name: " + objectName);
125                         break;
126
127                      default:
128                         log.debug("adding attribute name: " + arg);
129                         attributeNames.add(arg);
130                         break;
131                   }
132                   break;
133                }
134          }
135       }
136
137       return true;
138    }
139
140    public void execute(String JavaDoc[] args) throws Exception JavaDoc
141    {
142       processArguments(args);
143
144       if (objectName == null)
145          throw new CommandException("Missing object name");
146
147       log.debug("attribute names: " + attributeNames);
148
149       MBeanServerConnection JavaDoc server = getMBeanServer();
150       if (attributeNames.size() == 0)
151       {
152          // Display all readable attributes
153
attributeNames.clear();
154          MBeanInfo JavaDoc info = server.getMBeanInfo(objectName);
155          MBeanAttributeInfo JavaDoc[] attrInfos = info.getAttributes();
156          for (int a = 0; a < attrInfos.length; a++)
157          {
158             MBeanAttributeInfo JavaDoc attrInfo = attrInfos[a];
159             if (attrInfo.isReadable())
160                attributeNames.add(attrInfo.getName());
161          }
162       }
163
164       String JavaDoc[] names = new String JavaDoc[attributeNames.size()];
165       attributeNames.toArray(names);
166       log.debug("as string[]: " + Strings.join(names, ","));
167
168       AttributeList JavaDoc attrList = server.getAttributes(objectName, names);
169       log.debug("attribute list: " + attrList);
170
171       if (attrList.size() == 0)
172       {
173          throw new CommandException("No matching attributes");
174       }
175       else if (attrList.size() != names.length)
176       {
177          log.warn("Not all specified attributes were found");
178       }
179
180       PrintWriter JavaDoc out = context.getWriter();
181
182       Iterator JavaDoc iter = attrList.iterator();
183       while (iter.hasNext())
184       {
185          Attribute JavaDoc attr = (Attribute JavaDoc) iter.next();
186          if (prefix)
187          {
188             out.print(attr.getName());
189             out.print("=");
190          }
191          out.println(attr.getValue());
192       }
193    }
194 }
195
Popular Tags