KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date JavaDoc;
26
27 import javax.management.MBeanAttributeInfo JavaDoc;
28 import javax.management.MBeanConstructorInfo JavaDoc;
29 import javax.management.MBeanInfo JavaDoc;
30 import javax.management.MBeanNotificationInfo JavaDoc;
31 import javax.management.MBeanOperationInfo JavaDoc;
32 import javax.management.MBeanParameterInfo JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34
35 /**
36  * Command to print out mbean metadata as an xmbean descriptor.
37  *
38  * The idea is to redirect and use the output as the skeleton
39  * for writing an xmbean descriptor for an existing mbean,
40  * since this can be very tedious to write by hand.
41  *
42  * @author <a HREF="dimitris@jboss.org">Dimitris Andreadis</a>
43  * @version $Revision: 40342 $
44  */

45 public class XMBeanCommand extends MBeanServerCommand
46 {
47    /**
48     * Default CTOR
49     */

50    public XMBeanCommand()
51    {
52       super("xmbean", "Print out mbean metadata as an xmbean descriptor");
53    }
54
55    public void displayHelp()
56    {
57       PrintWriter JavaDoc out = context.getWriter();
58
59       out.println(desc);
60       out.println();
61       out.println("Redirect the output and use it as a skeleton for");
62       out.println("writing an xmbean descriptor for an existing mbean.");
63       out.println();
64       out.println("Usage: " + name + " <object-name>");
65       out.println();
66
67       out.flush();
68    }
69
70    public void execute(String JavaDoc[] args) throws Exception JavaDoc
71    {
72       if (args.length != 1)
73       {
74          throw new CommandException("Missing object name");
75       }
76       ObjectName JavaDoc target = super.createObjectName(args[0]);
77
78       MBeanInfo JavaDoc mbeanInfo = getMBeanServer().getMBeanInfo(target);
79       MBeanConstructorInfo JavaDoc[] ctors = mbeanInfo.getConstructors();
80       MBeanAttributeInfo JavaDoc[] attrs = mbeanInfo.getAttributes();
81       MBeanOperationInfo JavaDoc[] ops = mbeanInfo.getOperations();
82       MBeanNotificationInfo JavaDoc[] notifs = mbeanInfo.getNotifications();
83       
84       PrintWriter JavaDoc out = context.getWriter();
85
86       // header
87
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
88       out.println("<!DOCTYPE mbean PUBLIC");
89       out.println(" \"-//JBoss//DTD JBOSS XMBEAN 1.2//EN\"");
90       out.println(" \"http://www.jboss.org/j2ee/dtd/jboss_xmbean_1_2.dtd\">");
91       out.println("<!--");
92       out.println(" xmbean descriptor generated by 'twiddle'");
93       out.println(" on " + new Date JavaDoc());
94       out.println(" for '" + target + "'");
95       out.println("-->");
96       
97       out.println("<mbean>");
98       
99       // mbean
100
out.println(" <description>" + mbeanInfo.getDescription() + "</description>");
101       out.println(" <class>" + mbeanInfo.getClassName() + "</class>");
102       out.println();
103       
104       // constructors
105
if (ctors.length > 0)
106       {
107          for (int i = 0; i < ctors.length; i++)
108          {
109             MBeanConstructorInfo JavaDoc ctorInfo = ctors[i];
110             out.println(" <constructor>");
111             out.println(" <description>" + ctorInfo.getDescription() + "</description>");
112             out.println(" <name>" + ctorInfo.getName() + "</name>");
113             outputParameters(out, ctorInfo.getSignature());
114             out.println(" </constructor>");
115          }
116          out.println();
117       }
118       
119       // attributes
120
if (attrs.length > 0)
121       {
122          for (int i = 0; i < attrs.length; i++)
123          {
124             MBeanAttributeInfo JavaDoc attrInfo = attrs[i];
125             
126             // determine access, rw by default
127
String JavaDoc access = "read-write";
128             access = attrInfo.isReadable() ? access : "write-only";
129             access = attrInfo.isWritable() ? access : "read-only";
130             String JavaDoc accessString = " access='" + access + "'";
131
132             // determine get method, if any
133
String JavaDoc getMethodString = "";
134             if (attrInfo.isReadable())
135             {
136                getMethodString = " getMethod='" +
137                   (attrInfo.isIs() ? "is" : "get") + attrInfo.getName() + "'";
138             }
139             
140             // determine set method, if any
141
String JavaDoc setMethodString = "";
142             if (attrInfo.isWritable())
143             {
144                setMethodString = " setMethod='set" + attrInfo.getName() + "'";
145                
146             }
147             
148             out.println(" <attribute" + accessString + getMethodString + setMethodString + ">");
149             out.println(" <description>" + attrInfo.getDescription() + "</description>");
150             out.println(" <name>" + attrInfo.getName() + "</name>");
151             out.println(" <type>" + attrInfo.getType() + "</type>");
152             out.println(" </attribute>");
153          }
154          out.println();
155       }
156       
157       // operations
158
if (ops.length > 0)
159       {
160          for (int i = 0; i < ops.length; i++)
161          {
162             MBeanOperationInfo JavaDoc opInfo = ops[i];
163             // nobody uses opInfo.getImpact()
164
out.println(" <operation>");
165             out.println(" <description>" + opInfo.getDescription() + "</description>");
166             out.println(" <name>" + opInfo.getName() + "</name>");
167             outputParameters(out, opInfo.getSignature());
168             out.println(" <return-type>" + opInfo.getReturnType() + "</return-type>");
169             out.println(" </operation>");
170          }
171          out.println();
172       }
173       
174       // notifications
175
if (notifs.length > 0)
176       {
177          for (int i = 0; i < notifs.length; i++)
178          {
179             MBeanNotificationInfo JavaDoc notifInfo = notifs[i];
180             String JavaDoc[] types = notifInfo.getNotifTypes();
181             out.println(" <notification>");
182             out.println(" <description>" + notifInfo.getDescription() + "</description>");
183             out.println(" <name>" + notifInfo.getName() + "</name>");
184             for (int j = 0; j < types.length; j++)
185             {
186                out.println(" <notification-type>" + types[j] + "</notification-type>");
187             }
188             out.println(" </notification>");
189          }
190          out.println();
191       }
192       
193       out.println("</mbean>");
194       
195       out.flush();
196    }
197    
198    private void outputParameters(PrintWriter JavaDoc out, MBeanParameterInfo JavaDoc[] params)
199    {
200       for (int i = 0; i < params.length; i++)
201       {
202          MBeanParameterInfo JavaDoc paramInfo = params[i];
203          out.println(" <parameter>");
204          out.println(" <description>" + paramInfo.getDescription() + "</description>");
205          out.println(" <name>" + paramInfo.getName() + "</name>");
206          out.println(" <type>" + paramInfo.getType() + "</type>");
207          out.println(" </parameter>");
208       }
209    }
210 }
211
Popular Tags