KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import javax.management.Attribute JavaDoc;
31 import javax.management.AttributeList JavaDoc;
32 import javax.management.MBeanServerConnection JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34
35 import org.jboss.management.j2ee.J2EETypeConstants;
36
37 /**
38  * Command to print out jsr77 related information.
39  *
40  * @author <a HREF="dimitris@jboss.org">Dimitris Andreadis</a>
41  * @version $Revision: 44319 $
42  */

43 public class JSR77Command extends MBeanServerCommand
44 {
45    private static final String JavaDoc INDENT = " ";
46    
47    private static final Map JavaDoc j2eeTypeMap = new HashMap JavaDoc();
48    static
49    {
50       j2eeTypeMap.put(J2EETypeConstants.J2EEDomain,
51          new J2EEType(J2EETypeConstants.J2EEDomain, null, new String JavaDoc[] { "servers" }, null));
52       j2eeTypeMap.put(J2EETypeConstants.J2EEServer,
53          new J2EEType(J2EETypeConstants.J2EEServer, new String JavaDoc[] { "serverVendor", "serverVersion" }, new String JavaDoc[] { "deployedObjects", "resources", "javaVMs" }, null));
54       j2eeTypeMap.put(J2EETypeConstants.JVM,
55          new J2EEType(J2EETypeConstants.JVM, new String JavaDoc[] { "javaVersion", "javaVendor", "node" }, null, null));
56       j2eeTypeMap.put(J2EETypeConstants.J2EEApplication,
57          new J2EEType(J2EETypeConstants.J2EEApplication, new String JavaDoc[] { /*"deploymentDescriptor"*/ }, null, new String JavaDoc[] { "modules" }));
58       j2eeTypeMap.put(J2EETypeConstants.AppClientModule,
59          new J2EEType(J2EETypeConstants.AppClientModule, null, null, null));
60       j2eeTypeMap.put(J2EETypeConstants.EJBModule,
61          new J2EEType(J2EETypeConstants.EJBModule, null, new String JavaDoc[] { "ejbs" }, null));
62       j2eeTypeMap.put(J2EETypeConstants.EntityBean,
63          new J2EEType(J2EETypeConstants.EntityBean, new String JavaDoc[] { "JndiName" }, null, null));
64       j2eeTypeMap.put(J2EETypeConstants.MessageDrivenBean,
65          new J2EEType(J2EETypeConstants.MessageDrivenBean, new String JavaDoc[] { "JndiName" }, null, null));
66       j2eeTypeMap.put(J2EETypeConstants.StatelessSessionBean,
67          new J2EEType(J2EETypeConstants.StatelessSessionBean, new String JavaDoc[] { "JndiName" }, null, null));
68       j2eeTypeMap.put(J2EETypeConstants.StatefulSessionBean,
69          new J2EEType(J2EETypeConstants.StatefulSessionBean, new String JavaDoc[] { "JndiName" }, null, null));
70       j2eeTypeMap.put(J2EETypeConstants.WebModule,
71          new J2EEType(J2EETypeConstants.WebModule, new String JavaDoc[] { /*"deploymentDescriptor"*/ }, new String JavaDoc[] { "servlets" }, null));
72       j2eeTypeMap.put(J2EETypeConstants.Servlet,
73          new J2EEType(J2EETypeConstants.Servlet, null, null, null));
74       j2eeTypeMap.put(J2EETypeConstants.ServiceModule,
75          new J2EEType(J2EETypeConstants.ServiceModule, new String JavaDoc[] { /*"deploymentDescriptor"*/ }, new String JavaDoc[] { "MBeans" }, null));
76       j2eeTypeMap.put(J2EETypeConstants.MBean,
77          new J2EEType(J2EETypeConstants.MBean, new String JavaDoc[] { "stateMonitored", "StateString" }, null, null));
78       j2eeTypeMap.put(J2EETypeConstants.ResourceAdapterModule,
79          new J2EEType(J2EETypeConstants.ResourceAdapterModule, new String JavaDoc[] { /*"deploymentDescriptor"*/ }, new String JavaDoc[] { "resourceAdapters" }, null));
80       j2eeTypeMap.put(J2EETypeConstants.ResourceAdapter,
81          new J2EEType(J2EETypeConstants.ResourceAdapter, null, new String JavaDoc[] { "jcaResource" }, null));
82       j2eeTypeMap.put(J2EETypeConstants.JCAResource,
83          new J2EEType(J2EETypeConstants.JCAResource, null, new String JavaDoc[] { "connectionFactories" }, null));
84       j2eeTypeMap.put(J2EETypeConstants.JCAConnectionFactory,
85          new J2EEType(J2EETypeConstants.JCAConnectionFactory, null, new String JavaDoc[] { "managedConnectionFactory" }, null));
86       j2eeTypeMap.put(J2EETypeConstants.JCAManagedConnectionFactory,
87          new J2EEType(J2EETypeConstants.JCAManagedConnectionFactory, null, null, null));
88       j2eeTypeMap.put(J2EETypeConstants.JNDIResource,
89          new J2EEType(J2EETypeConstants.JNDIResource, new String JavaDoc[] { "StateString" }, null, null));
90       j2eeTypeMap.put(J2EETypeConstants.JTAResource,
91          new J2EEType(J2EETypeConstants.JTAResource, null, null, null));
92       j2eeTypeMap.put(J2EETypeConstants.RMI_IIOPResource,
93          new J2EEType(J2EETypeConstants.RMI_IIOPResource, null, null, null));
94       j2eeTypeMap.put(J2EETypeConstants.JavaMailResource,
95          new J2EEType(J2EETypeConstants.JavaMailResource, new String JavaDoc[] { "StateString" }, null, null));
96       j2eeTypeMap.put(J2EETypeConstants.JMSResource,
97          new J2EEType(J2EETypeConstants.JMSResource, null, null, null));
98    }
99    
100    /**
101     * Default CTOR
102     */

103    public JSR77Command()
104    {
105       super("jsr77", "Print out JSR77 related information");
106    }
107
108    public void displayHelp()
109    {
110       PrintWriter JavaDoc out = context.getWriter();
111
112       out.println(desc);
113       out.println();
114       out.println("Print out JSR77 related information");
115       out.println();
116       out.println("Usage: " + name);
117       out.println();
118
119       out.flush();
120    }
121
122    public void execute(String JavaDoc[] args) throws Exception JavaDoc
123    {
124       //if (args.length != 1)
125
//{
126
// throw new CommandException("Missing object name");
127
//}
128
//ObjectName target = super.createObjectName(args[0]);
129

130       MBeanServerConnection JavaDoc server = super.getMBeanServer();
131       PrintWriter JavaDoc out = context.getWriter();
132       
133       Set JavaDoc jsr77Domains = locateJSR77Domains(server);
134       for (Iterator JavaDoc i = jsr77Domains.iterator(); i.hasNext(); )
135       {
136          printJsr77Node(server, (ObjectName JavaDoc)i.next(), out, 0);
137       }
138       out.flush();
139    }
140
141    /**
142     * Locate the ObjectNames of any JSR77 registered domains
143     *
144     * @param server the mbean server to query
145     * @return a set of JSR77 ObjectNames (can be empty)
146     * @throws Exception in case of error
147     */

148    private static Set JavaDoc locateJSR77Domains(MBeanServerConnection JavaDoc server) throws Exception JavaDoc
149    {
150       // The potential list of domain starting points. According to the
151
// spec we should be looking for *:j2eeType=J2EEDomain,* where
152
// the value of the "name" attribute matches the domain name.
153
ObjectName JavaDoc domainNameQuery = new ObjectName JavaDoc("*:j2eeType=J2EEDomain,*");
154       Set JavaDoc domainNames = server.queryNames(domainNameQuery, null);
155       
156       for (Iterator JavaDoc i = domainNames.iterator(); i.hasNext(); )
157       {
158          ObjectName JavaDoc objectName = (ObjectName JavaDoc)i.next();
159
160          if (objectName.getDomain().equals(objectName.getKeyProperty("name")) == false)
161          {
162             // value of "name" attribute doesn't match the domain name
163
// remove from the domain set
164
i.remove();
165          }
166       }
167       return domainNames;
168    }
169
170    /**
171     * Generic traversal of a JSR77 node
172     */

173    private static void printJsr77Node(MBeanServerConnection JavaDoc server, ObjectName JavaDoc node, PrintWriter JavaDoc out, int depth) throws Exception JavaDoc
174    {
175       String JavaDoc j2eeType = node.getKeyProperty("j2eeType");
176       String JavaDoc name = node.getKeyProperty("name");
177       
178       J2EEType type = (J2EEType)j2eeTypeMap.get(j2eeType);
179       if (type == null)
180       {
181          println(out, depth, "Unknown j2eeType=" + j2eeType);
182       }
183       else
184       {
185          // print the node's j2eeType
186
println(out, depth, j2eeType + "=" + name);
187          
188          // print attributes, if any
189
if (type.attributes != null)
190          {
191             AttributeList JavaDoc attrs = server.getAttributes(node, type.attributes);
192             for (int i = 0; i < attrs.size(); i++)
193             {
194                Attribute JavaDoc attr = (Attribute JavaDoc)attrs.get(i);
195                println(out, depth + 1, attr.getName() + "=" + attr.getValue());
196             }
197          }
198          
199          // print out associations, if any
200
if (type.associations != null)
201          {
202             for (int i = 0; i < type.associations.length; i++)
203             {
204                String JavaDoc association = type.associations[i];
205                String JavaDoc[] children = (String JavaDoc[])server.getAttribute(node, association);
206                for (int j = 0; j < children.length; j++)
207                {
208                   String JavaDoc child = children[j];
209                   ObjectName JavaDoc subnode = new ObjectName JavaDoc(child);
210                   String JavaDoc subnodeType = subnode.getKeyProperty("j2eeType");
211                   String JavaDoc subnodeName = subnode.getKeyProperty("name");
212                   
213                   println(out, depth + 1, subnodeType + "=" + subnodeName);
214                }
215             }
216          }
217          
218          // recurse into subnodes, if any
219
if (type.containment != null)
220          {
221             for (int i = 0; i < type.containment.length; i++)
222             {
223                String JavaDoc containment = type.containment[i];
224                Object JavaDoc result = server.getAttribute(node, containment);
225                // this is probably a bug, it should be String
226
if (result instanceof ObjectName JavaDoc)
227                {
228                   printJsr77Node(server, (ObjectName JavaDoc)result, out, depth + 1);
229                }
230                else if (result instanceof String JavaDoc)
231                {
232                   printJsr77Node(server, new ObjectName JavaDoc((String JavaDoc)result), out, depth + 1);
233                }
234                else if (result instanceof String JavaDoc[])
235                {
236                   String JavaDoc[] children = (String JavaDoc[])result;
237                   for (int j = 0; j < children.length; j++)
238                   {
239                      String JavaDoc child = children[j];
240                      ObjectName JavaDoc subnode = new ObjectName JavaDoc(child);
241                      if (depth < 2)
242                         out.println();
243                      printJsr77Node(server, subnode, out, depth + 1);
244                   }
245                }
246             }
247          }
248       }
249    }
250    
251    /**
252     * Println with indentation
253     */

254    private static void println(PrintWriter JavaDoc out, int depth, String JavaDoc msg)
255    {
256       for (int i = 0; i < depth; i++)
257       {
258          out.print(INDENT);
259       }
260       out.println(msg);
261    }
262    
263    /**
264     * Simple data holder class to record information
265     * for the various JSR77 j2eeType(s)
266     */

267    private static class J2EEType
268    {
269       public String JavaDoc type;
270       public String JavaDoc[] attributes;
271       public String JavaDoc[] containment;
272       public String JavaDoc[] associations;
273       
274       public J2EEType(String JavaDoc type, String JavaDoc[] attributes, String JavaDoc[] containment, String JavaDoc[] associations)
275       {
276          this.type = type;
277          this.attributes = attributes;
278          this.containment = containment;
279          this.associations = associations;
280       }
281    }
282 }
283
Popular Tags