KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > plugins > MBeansLister


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.plugins;
23
24 import org.jboss.console.manager.interfaces.ManageableResource;
25 import org.jboss.console.manager.interfaces.TreeNode;
26 import org.jboss.console.manager.interfaces.TreeNodeMenuEntry;
27 import org.jboss.console.manager.interfaces.impl.GraphMBeanAttributeAction;
28 import org.jboss.console.manager.interfaces.impl.SimpleTreeNodeMenuEntryImpl;
29 import org.jboss.console.manager.interfaces.impl.HttpLinkTreeAction;
30 import org.jboss.console.plugins.helpers.AbstractPluginWrapper;
31 import org.jboss.console.plugins.helpers.jmx.DomainData;
32 import org.jboss.console.plugins.helpers.jmx.MBeanData;
33 import org.jboss.console.plugins.helpers.jmx.Server;
34
35 import javax.management.MBeanAttributeInfo JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.HashSet JavaDoc;
40 /**
41  * As the number of MBeans is very big, we use a real Java class which is far
42  * faster than beanshell
43  *
44  * @see <related>
45  *
46  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
47  * @version $Revision: 37459 $
48  *
49  * <p><b>Revisions:</b>
50  *
51  * <p><b>2 janv. 2003 Sacha Labourey:</b>
52  * <ul>
53  * <li> First implementation </li>
54  * </ul>
55  */

56 public class MBeansLister
57    extends AbstractPluginWrapper
58 {
59    private static HashSet JavaDoc graphableClasses = new HashSet JavaDoc();
60
61    static
62    {
63       graphableClasses.add("java.lang.Integer");
64       graphableClasses.add("java.lang.Short");
65       graphableClasses.add("java.lang.Double");
66       graphableClasses.add("java.lang.Float");
67       graphableClasses.add("java.lang.Long");
68       graphableClasses.add("int");
69       graphableClasses.add("short");
70       graphableClasses.add("double");
71       graphableClasses.add("float");
72       graphableClasses.add("long");
73    }
74    public MBeansLister () { super(); }
75
76    TreeNode createJmxAttributeSubResource(MBeanAttributeInfo JavaDoc attr, ObjectName JavaDoc mbeanName) throws Exception JavaDoc
77    {
78       TreeNodeMenuEntry[] entries = null;
79       if (graphableClasses.contains(attr.getType()))
80       {
81          SimpleTreeNodeMenuEntryImpl entry = new SimpleTreeNodeMenuEntryImpl("graph", new GraphMBeanAttributeAction(mbeanName, attr.getName()));
82          SimpleTreeNodeMenuEntryImpl entry2 = new SimpleTreeNodeMenuEntryImpl("create monitor", new HttpLinkTreeAction(
83                  "/web-console/createThresholdMonitor.jsp?attribute=" + attr.getName() + "&objectName=" + encode(mbeanName.toString())));
84          SimpleTreeNodeMenuEntryImpl entry3 = new SimpleTreeNodeMenuEntryImpl("create snapshot", new HttpLinkTreeAction(
85                  "/web-console/createSnapshot.jsp?attribute=" + attr.getName() + "&objectName=" + encode(mbeanName.toString())));
86          entries = new TreeNodeMenuEntry[3];
87          entries[0] = entry;
88          entries[1] = entry2;
89          entries[2] = entry3;
90       }
91       else if (attr.getType().equals("String") || attr.getType().equals("java.lang.String"))
92       {
93          SimpleTreeNodeMenuEntryImpl entry = new SimpleTreeNodeMenuEntryImpl("create monitor", new HttpLinkTreeAction(
94                  "/web-console/createStringThresholdMonitor.jsp?attribute=" + attr.getName() + "&objectName=" + encode(mbeanName.toString())));
95          entries = new TreeNodeMenuEntry[1];
96          entries[0] = entry;
97       }
98
99       return createTreeNode(
100               attr.getName(),
101               attr.getDescription(),
102               "images/container.gif",
103               "/jmx-console/HtmlAdaptor?action=inspectMBean&name=" + encode("" + mbeanName), // Default URL
104
entries,
105               null,
106               null
107               //name,
108
//data.getClassName() TOO HEAVY TO GENERATE RESOURCE LOOKUP FOR EACH MBEAN!
109
);
110    }
111    TreeNode createJmxMBeanSubResources (MBeanData data) throws Exception JavaDoc
112    {
113       String JavaDoc name = "" + data.getObjectName();
114       String JavaDoc displayName = data.getName ();
115             
116       if (displayName == null)
117       {
118          // Get ride of the domain name because it is already is the header
119
int index = name.indexOf( ":" );
120          displayName = ( index >= 0 ) ? name.substring( index + 1 ) : name;
121       }
122
123       MBeanAttributeInfo JavaDoc[] attributes = data.getMetaData().getAttributes();
124       TreeNode[] attrNodes = new TreeNode[attributes.length];
125       for (int i = 0; i < attributes.length; i++)
126       {
127          attrNodes[i] = createJmxAttributeSubResource(attributes[i], data.getObjectName());
128       }
129
130       return createTreeNode (
131             displayName, // name
132
name, // description
133
"images/server.gif", // Icon URL
134
"/jmx-console/HtmlAdaptor?action=inspectMBean&name=" + encode(name), // Default URL
135
null,
136             attrNodes,
137             null
138             //name,
139
//data.getClassName() TOO HEAVY TO GENERATE RESOURCE LOOKUP FOR EACH MBEAN!
140
);
141    }
142    
143    TreeNode[] createJmxDomainsSubNodes () throws Exception JavaDoc
144    {
145       Iterator JavaDoc mbeans = Server.getDomainData(null);
146       
147       TreeNode[] result = null;
148       
149       ArrayList JavaDoc domains = new ArrayList JavaDoc ();
150             
151       while( mbeans.hasNext() )
152       {
153          DomainData domainData = (DomainData) mbeans.next();
154          String JavaDoc domainName = domainData.getDomainName();
155          MBeanData[] data = domainData.getData();
156          TreeNode[] subResources = new TreeNode[data.length];
157          
158          for(int d = 0; d < data.length; d ++)
159          {
160             subResources[d] = createJmxMBeanSubResources (data[d]);
161          }
162
163          TreeNodeMenuEntry[] menu = createMenus (new String JavaDoc[]
164             {
165                "Number of MBeans: " + data.length, null,
166             }
167          );
168          
169          domains.add(createTreeNode (
170                domainName, // name
171
"MBeans for domain " + domainName, // description
172
"images/serviceset.gif", // Icon URL
173
null, // Default URL
174
menu, // menu
175
subResources, // sub nodes
176
null // Sub-Resources
177
)
178          );
179          
180       }
181       
182       if (domains.size() == 0)
183       {
184          result = null;
185       }
186       else
187       {
188          result = (TreeNode[]) domains.toArray(new TreeNode[domains.size()]);
189       }
190       
191       return result;
192    }
193    
194    protected TreeNode getTreeForResource(String JavaDoc profile, ManageableResource resource)
195    {
196       try
197       {
198          return createTreeNode (
199                "JMX MBeans", // name
200
"Display all JMX MBeans", // description
201
"images/flash.gif", // Icon URL
202
"/jmx-console/HtmlAdaptor?action=displayMBeans", // Default URL
203
null,
204                createJmxDomainsSubNodes (), // sub nodes
205
null // Sub-Resources
206
);
207       }
208       catch (Exception JavaDoc e)
209       {
210          e.printStackTrace ();
211          return null;
212       }
213    }
214 }
215
Popular Tags