KickJava   Java API By Example, From Geeks To Geeks.

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


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.MBeanServerConnection JavaDoc;
28 import javax.management.MBeanInfo JavaDoc;
29 import javax.management.MBeanAttributeInfo JavaDoc;
30 import javax.management.MBeanOperationInfo JavaDoc;
31 import javax.management.MBeanParameterInfo JavaDoc;
32
33 import gnu.getopt.Getopt;
34 import gnu.getopt.LongOpt;
35
36 import org.jboss.util.Strings;
37
38 /** Query the MBeanInfo for an MBean
39  *
40  * @author Scott.Stark@jboss.org
41  * @version $Revision: 37459 $
42  */

43 public class InfoCommand
44    extends MBeanServerCommand
45 {
46    private ObjectName JavaDoc objectName;
47
48    public InfoCommand()
49    {
50       super("info", "Get the metadata for an MBean");
51    }
52
53    public void displayHelp()
54    {
55       PrintWriter JavaDoc out = context.getWriter();
56
57       out.println(desc);
58       out.println();
59       out.println("usage: " + name + " <mbean-name>");
60       out.println(" Use '*' to query for all attributes");
61       out.flush();
62    }
63
64    private boolean processArguments(final String JavaDoc[] args)
65       throws CommandException
66    {
67       log.debug("processing arguments: " + Strings.join(args, ","));
68
69       if (args.length == 0)
70       {
71          throw new CommandException("Command requires arguments");
72       }
73
74       String JavaDoc sopts = "-:";
75       LongOpt[] lopts =
76          {
77          };
78
79       Getopt getopt = new Getopt(null, args, sopts, lopts);
80       getopt.setOpterr(false);
81
82       int code;
83       int argidx = 0;
84
85       while ((code = getopt.getopt()) != -1)
86       {
87          switch (code)
88          {
89             case ':':
90                throw new CommandException
91                   ("Option requires an argument: " + args[getopt.getOptind() - 1]);
92
93             case '?':
94                throw new CommandException
95                   ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);
96
97                // non-option arguments
98
case 1:
99                {
100                   String JavaDoc arg = getopt.getOptarg();
101
102                   switch (argidx++)
103                   {
104                      case 0:
105                         objectName = createObjectName(arg);
106                         log.debug("mbean name: " + objectName);
107                         break;
108                   }
109                   break;
110                }
111          }
112       }
113
114       return true;
115    }
116
117    public void execute(String JavaDoc[] args) throws Exception JavaDoc
118    {
119       processArguments(args);
120
121       if (objectName == null)
122          throw new CommandException("Missing object name");
123
124       MBeanServerConnection JavaDoc server = getMBeanServer();
125       MBeanInfo JavaDoc mbeanInfo = server.getMBeanInfo(objectName);
126       MBeanAttributeInfo JavaDoc[] attrInfo = mbeanInfo.getAttributes();
127       MBeanOperationInfo JavaDoc[] opInfo = mbeanInfo.getOperations();
128
129       PrintWriter JavaDoc out = context.getWriter();
130       out.println("Description: "+mbeanInfo.getDescription());
131       out.println("+++ Attributes:");
132       int length = attrInfo != null ? attrInfo.length : 0;
133       for(int n = 0; n < length; n ++)
134       {
135          MBeanAttributeInfo JavaDoc info = attrInfo[n];
136          out.print(" Name: ");
137          out.println(info.getName());
138          out.print(" Type: ");
139          out.println(info.getType());
140          String JavaDoc rw = "";
141          if( info.isReadable() )
142             rw = "r";
143          else
144             rw = "-";
145          if( info.isWritable() )
146             rw += "w";
147          else
148             rw += "-";
149          out.print(" Access: ");
150          out.println(rw);
151       }
152
153       out.println("+++ Operations:");
154       length = opInfo != null ? opInfo.length : 0;
155       for(int n = 0; n < length; n ++)
156       {
157          MBeanOperationInfo JavaDoc info = opInfo[n];
158          out.print(' ');
159          out.print(info.getReturnType());
160          out.print(' ');
161          out.print(info.getName());
162          out.print('(');
163          MBeanParameterInfo JavaDoc[] sig = info.getSignature();
164          for(int s = 0; s < sig.length; s ++)
165          {
166             out.print(sig[s].getType());
167             out.print(' ');
168             out.print(sig[s].getName());
169             if( s < sig.length-1 )
170                out.print(',');
171          }
172          out.println(')');
173       }
174    }
175 }
176
Popular Tags