KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27
28 import javax.management.AttributeNotFoundException JavaDoc;
29 import javax.management.ObjectInstance JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.jboss.console.manager.interfaces.ManageableResource;
33 import org.jboss.console.manager.interfaces.ResourceTreeNode;
34 import org.jboss.console.manager.interfaces.TreeNode;
35 import org.jboss.console.plugins.helpers.AbstractPluginWrapper;
36 import org.jboss.management.j2ee.J2EEServerMBean;
37 import org.jboss.mx.util.MBeanProxyExt;
38
39 /**
40  * As the number of MBeans is very big, we use a real Java class which is far
41  * faster than beanshell
42  *
43  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>
44  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
45  * @version $Revision: 43303 $
46  */

47 public class JSR77Lister extends AbstractPluginWrapper
48 {
49    private static final long serialVersionUID = -5466799611043095874L;
50
51    /**
52     * Default CTOR
53     */

54    public JSR77Lister()
55    {
56       super();
57    }
58
59    /*
60    ResourceTreeNode[] createEARSubModules (ObjectName parent) throws Exception
61    {
62       // there is a bug in the current jsr77 implementation with regard to naming
63       // of EJBModule that are part of EARs => I've used a workaround
64       //
65       ObjectInstance[] insts = getMBeansForQuery(JMX_JSR77_DOMAIN +
66          ":j2eeType=EJBModule,J2EEServer="+parent.getKeyProperty("name")+",*", null);
67
68       ResourceTreeNode[] jars = new ResourceTreeNode[insts.length];
69       for (int i=0; i<insts.length; i++)
70       {
71          ObjectName objName = insts[i].getObjectName();
72          //EJBModule jarProxy = (EJBModule)
73          // MBeanProxy.create(EJBModule.class, objName, getMBeanServer());
74             
75          jars[i] = createResourceNode(
76                objName.getKeyProperty("name"), // name
77                "", // description
78                null, // Icon URL
79                null, // Default URL
80                null,
81                null, // sub nodes
82                null, // Sub-Resources
83                objName.toString(),
84                org.jboss.management.j2ee.EJBModule.class.getName()
85             ).setVisibility(ResourceTreeNode.INVISIBLE_IF_SUBNODE_EXISTS);
86          
87       }
88           
89       return jars;
90    }
91    
92    ResourceTreeNode[] createEARs (ObjectName parent) throws Exception
93    {
94       ObjectInstance[] insts = getMBeansForQuery(JMX_JSR77_DOMAIN +
95          ":j2eeType=J2EEApplication,J2EEServer="+parent.getKeyProperty("name")+",*", null);
96
97       ResourceTreeNode[] ears = new ResourceTreeNode[insts.length];
98       for (int i=0; i<insts.length; i++)
99       {
100          ObjectName objName = insts[i].getObjectName();
101          //J2EEApplication earProxy = (J2EEApplication)
102          // MBeanProxy.create(J2EEApplication.class, objName, getMBeanServer());
103
104          ears[i] = createResourceNode (
105                objName.getKeyProperty("name"), // name
106                "", // description
107                "images/EspressoMaker.gif", // Icon URL
108                "J2EEApp.jsp?ObjectName=" + encode(objName.toString()), // Default URL
109                null,
110                null, // sub nodes
111                createEARSubModules (objName), // Sub-Resources
112                parent.toString(),
113                J2EEApplication.class.getName()
114             );
115
116       }
117           
118       return ears;
119    }
120    
121    ResourceTreeNode[] singleEJBs (ObjectName parent) throws Exception
122    {
123       ObjectInstance[] insts = getMBeansForQuery(JMX_JSR77_DOMAIN +
124          ":j2eeType=EJBModule,J2EEServer="+parent.getKeyProperty("name")+",*", null);
125
126       ResourceTreeNode[] jars = new ResourceTreeNode[insts.length];
127       for (int i=0; i<insts.length; i++)
128       {
129          ObjectName objName = insts[i].getObjectName();
130          //EJBModule jarProxy = (EJBModule)
131          // MBeanProxy.create(EJBModule.class, objName, getMBeanServer());
132             
133          jars[i] = createResourceNode(
134                objName.getKeyProperty("name"), // name
135                "", // description
136                null, // Icon URL
137                null, // Default URL
138                null,
139                null, // sub nodes
140                null, // Sub-Resources
141                objName.toString(),
142                org.jboss.management.j2ee.EJBModule.class.getName()
143             ).setVisibility(ResourceTreeNode.INVISIBLE_IF_SUBNODE_EXISTS);
144          
145       }
146           
147       return jars;
148    }
149 */

150    TreeNode createSubResources (String JavaDoc[] resources) throws Exception JavaDoc
151    {
152       ResourceTreeNode[] deployed = new ResourceTreeNode[resources.length];
153       for (int i = 0; i < resources.length; i++)
154       {
155          ObjectName JavaDoc objectName = new ObjectName JavaDoc(resources[i]);
156          deployed[i] = createResourceNode (
157             objectName.getKeyProperty ("name"), // name
158
"J2EE Resource", // description
159
null, //"images/EspressoMaker.gif", // Icon URL
160
null, // "J2EEApp.jsp?ObjectName=" + encode (objName.toString ()), // Default URL
161
null,
162             null, // sub nodes
163
null, //createEARSubModules (objName), // Sub-Resources
164
resources[i].toString (),
165             this.mbeanServer.getMBeanInfo (objectName).getClassName ()
166          ).setVisibility (ResourceTreeNode.INVISIBLE_IF_SUBNODE_EXISTS);
167       }
168
169       return createTreeNode (
170          "J2EE Resources", // name
171
"J2EE Resources", // description
172
"images/spirale.gif", // Icon URL
173
null, //"J2EEDomain.jsp&objectName=" + encode(objName.toString()), // Default URL
174
null,
175          null, // sub nodes
176
deployed // Sub-Resources
177
);
178    }
179
180    ResourceTreeNode[] createDeployedObjects (String JavaDoc[] resources) throws Exception JavaDoc
181    {
182       ArrayList JavaDoc deployed = new ArrayList JavaDoc ();
183       for (int i = 0; i < resources.length; i++)
184       {
185          ObjectName JavaDoc objectName = new ObjectName JavaDoc(resources[i]);
186          //if (resources[i].getKeyProperty ("J2EEApplication") == null)
187
{
188             deployed.add(createResourceNode (
189                objectName.getKeyProperty("name"), // name
190
"", // description
191
"images/EspressoMaker.gif", // Icon URL
192
null, // "J2EEApp.jsp?ObjectName=" + encode (objName.toString ()), // Default URL
193
null,
194                null, // sub nodes
195
null, //createEARSubModules (objName), // Sub-Resources
196
resources[i].toString (),
197                this.mbeanServer.getMBeanInfo (objectName).getClassName ()
198             ).setVisibility (ResourceTreeNode.INVISIBLE_IF_SUBNODE_EXISTS));
199          }
200
201       }
202       Collections.sort(deployed, new ListerSorter());
203
204       return (ResourceTreeNode[])deployed.toArray(new ResourceTreeNode[deployed.size()]);
205    }
206
207    ResourceTreeNode createServer (String JavaDoc serverName) throws Exception JavaDoc
208    {
209
210       ObjectName JavaDoc objectName = new ObjectName JavaDoc(serverName);
211       J2EEServerMBean serv = (J2EEServerMBean)
212          MBeanProxyExt.create(J2EEServerMBean.class, objectName, getMBeanServer ());
213
214       String JavaDoc[] deployedON = serv.getdeployedObjects();
215       ResourceTreeNode[] subResArray = createDeployedObjects (deployedON);
216
217       return createResourceNode (
218          serv.getserverVendor () + " - " + serv.getserverVersion (), // name
219
objectName.getKeyProperty ("name"), // description
220
"images/database.gif", // Icon URL
221
null, //"J2EEDomain.jsp?objectName=" + encode(objName.toString()), // Default URL
222
null,
223          new TreeNode[] {createSubResources (serv.getresources())}, // sub nodes
224
subResArray, // Sub-Resources
225
serverName.toString (),
226          org.jboss.management.j2ee.J2EEServer.class.getName ()
227       );
228
229    }
230
231    ResourceTreeNode[] createServers(ObjectName JavaDoc domain) throws Exception JavaDoc
232    {
233       // will throw an exception if there is no such attribute
234
String JavaDoc[] serversObjectNames = (String JavaDoc[])getMBeanServer().getAttribute(domain, "servers");
235       
236       ArrayList JavaDoc servers = new ArrayList JavaDoc();
237       for (int i = 0; i < serversObjectNames.length; i++)
238       {
239          servers.add(createServer(serversObjectNames[i]));
240       }
241
242       return (ResourceTreeNode[])servers.toArray(new ResourceTreeNode[servers.size()]);
243    }
244    
245    /*
246    TreeNode createGenericNode (String name, ObjectName on, Class clazz) throws Exception
247    {
248       return createResourceNode(name, name, null, null, null, null, null, on.toString(), clazz.toString());
249    }
250    */

251
252    TreeNode createDomain(ObjectName JavaDoc domain) throws Exception JavaDoc
253    {
254       return createTreeNode(
255          domain.getKeyProperty("name"), // name
256
"", // description
257
"images/spirale.gif", // Icon URL
258
null, //"J2EEDomain.jsp&objectName=" + encode(objName.toString()), // Default URL
259
null,
260          null, // sub nodes
261
createServers(domain) // Sub-Resources
262
);
263    }
264
265    /**
266     * Find the root JSR-77 domains
267     */

268    TreeNode[] createDomains()
269    {
270       // The potential list of domain starting points. According to the
271
// spec we should be looking for *:j2eeType=J2EEDomain,* where
272
// the value of the "name" attribute matches the domain name.
273
ObjectInstance JavaDoc[] insts = getMBeansForQuery("*:j2eeType=J2EEDomain,*", null);
274       
275       // Holds the domains that are succesfully created
276
ArrayList JavaDoc domainsCreated = new ArrayList JavaDoc();
277       
278       for (int i=0; i < insts.length; i++)
279       {
280          ObjectName JavaDoc objectName = insts[i].getObjectName();
281          try
282          {
283             if (objectName.getDomain().equals(objectName.getKeyProperty("name")))
284             {
285                // value of "name" attribute matches the domain name, assume is ok
286
domainsCreated.add(createDomain(objectName));
287             }
288          }
289          catch (AttributeNotFoundException JavaDoc e)
290          {
291             // we are looking at a bad starting point
292
System.err.println(e.getClass().getName() + ": " + e.getMessage()
293                   + "; mbean '" + objectName + "' not a proper j2eeType=J2EEDomain");
294          }
295          catch (Exception JavaDoc e)
296          {
297             // show error and continue
298
e.printStackTrace();
299          }
300       }
301       // Transform the list to TreeNode[]
302
TreeNode[] domains = (TreeNode[])domainsCreated.toArray(new TreeNode[domainsCreated.size()]);
303       
304       // Done
305
return domains;
306    }
307    
308    protected TreeNode getTreeForResource(String JavaDoc profile, ManageableResource resource)
309    {
310       try
311       {
312          return createTreeNode (
313                "J2EE Domains", // name
314
"Display JSR-77 Managed Objects", // description
315
"images/elements32.gif", // Icon URL
316
null, // Default URL
317
null,
318                createDomains (), // sub nodes
319
null // Sub-Resources
320
);
321       }
322       catch (Exception JavaDoc e)
323       {
324          e.printStackTrace ();
325          return null;
326       }
327    }
328
329    public final static String JavaDoc[] DEFAULT_SUFFIX_ORDER = {
330       "ear", "jar", "war", "sar", "rar", "ds.xml", "service.xml", "wsr", "zip"
331    };
332
333    public class ListerSorter implements Comparator JavaDoc
334    {
335
336       protected String JavaDoc[] suffixOrder;
337
338       public ListerSorter (String JavaDoc[] suffixOrder)
339       {
340          this.suffixOrder = suffixOrder;
341       }
342
343       public ListerSorter ()
344       {
345          this (DEFAULT_SUFFIX_ORDER);
346       }
347
348       public int compare (Object JavaDoc o1, Object JavaDoc o2)
349       {
350          return getExtensionIndex ((ResourceTreeNode) o1) - getExtensionIndex ((ResourceTreeNode) o2);
351       }
352
353       public int getExtensionIndex (ResourceTreeNode node)
354       {
355          String JavaDoc name = node.getName();
356          if (name == null) name = "";
357
358          int i = 0;
359          for (; i < suffixOrder.length; i++)
360          {
361             if (name.endsWith (suffixOrder[i]))
362                break;
363          }
364          return i;
365       }
366    }
367
368 }
369
Popular Tags