KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > MBeanTreeBuilder


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: MBeanTreeBuilder.java,v 1.12 2005/01/07 16:39:50 danesa Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin;
27
28 import java.net.URLEncoder JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.management.MBeanAttributeInfo JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.MBeanOperationInfo JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38
39 import org.apache.struts.Globals;
40 import org.apache.struts.action.ActionServlet;
41 import org.apache.struts.util.MessageResources;
42
43 import org.objectweb.jonas.jmx.JonasManagementRepr;
44 import org.objectweb.jonas.webapp.jonasadmin.common.BeanComparator;
45 import org.objectweb.jonas.webapp.jonasadmin.mbean.J2eeMbeanItem;
46 import org.objectweb.jonas.webapp.jonasadmin.mbean.MbeanItem;
47 import org.objectweb.jonas.webapp.jonasadmin.mbean.OwnerMbeanItem;
48 import org.objectweb.jonas.webapp.taglib.TreeBuilder;
49 import org.objectweb.jonas.webapp.taglib.TreeControl;
50 import org.objectweb.jonas.webapp.taglib.TreeControlNode;
51
52 /**
53  * Implementation of <code>TreeBuilder</code> that adds the nodes required
54  * for administering the Jonas server.
55  *
56  * @author Michel-Ange ANTON (initial developer)
57  * @author Florent Benoit (changes for struts 1.2.2)
58  */

59
60 public class MBeanTreeBuilder implements TreeBuilder {
61 // ----------------------------------------------------- Instance Variables
62

63 // ---------------------------------------------------- TreeBuilder Methods
64

65     /**
66      * Add the required nodes to the specified <code>treeControl</code>
67      * instance.
68      *
69      * @param treeControl The <code>TreeControl</code> to which we should
70      * add our nodes
71      * @param servlet The controller servlet for the admin application
72      * @param request The servlet request we are processing
73      */

74     public void buildTree(TreeControl treeControl, ActionServlet servlet
75         , HttpServletRequest JavaDoc request) {
76         try {
77             TreeControlNode rootNode = treeControl.getRoot();
78             MessageResources resources = (MessageResources) servlet.getServletContext().
79                 getAttribute(Globals.MESSAGES_KEY);
80             getMBeans(rootNode, resources);
81         }
82         catch (Throwable JavaDoc t) {
83             t.printStackTrace(System.out);
84         }
85     }
86
87 // ------------------------------------------------------ Protected Methods
88

89     /**
90      * Append nodes for all defined MBeans.
91      *
92      * @param rootNode Root node for the tree control
93      * @param resources The MessageResources for our localized messages
94      *
95      * @exception Exception if an exception occurs building the tree
96      */

97     public void getMBeans(TreeControlNode rootNode, MessageResources resources)
98         throws Exception JavaDoc {
99
100         TreeControlNode nodeMBeans = new TreeControlNode("mbeans", "icon/mbeans.gif"
101             , resources.getMessage("treenode.allmbeans"), "ListMBeans.do", "content", false);
102         rootNode.addChild(nodeMBeans);
103
104         ArrayList JavaDoc[] als = JonasAdminJmx.getFamiliesMbeansLists();
105         if (als[MbeanItem.FAMILY_J2EE].size() > 0) {
106             getJ2eeMBeans(nodeMBeans, resources, als[MbeanItem.FAMILY_J2EE]);
107         }
108         if (als[MbeanItem.FAMILY_OWNER].size() > 0) {
109             getOwnerMBeans(nodeMBeans, resources, als[MbeanItem.FAMILY_OWNER]);
110         }
111         if (als[MbeanItem.FAMILY_UNKNOWN].size() > 0) {
112             getUnknownMBeans(nodeMBeans, resources, als[MbeanItem.FAMILY_UNKNOWN]);
113         }
114     }
115
116     /**
117      * Append nodes for all defined Owner MBeans.
118      *
119      * @param p_ParentNode Parent node for the tree control
120      * @param p_Resources The MessageResources for our localized messages
121      * @param p_List The list of Mbeans
122      *
123      * @exception Exception if an exception occurs building the tree
124      */

125     public void getOwnerMBeans(TreeControlNode p_ParentNode, MessageResources p_Resources
126         , ArrayList JavaDoc p_List)
127         throws Exception JavaDoc {
128
129         TreeControlNode nodeMBeans = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
130             + "owner", "icon/mbeans.gif", p_Resources.getMessage("treenode.mbeans.owner")
131             , "ListOwnerMBeans.do", "content", false);
132         p_ParentNode.addChild(nodeMBeans);
133         // Sort list
134
Collections.sort(p_List, new BeanComparator(new String JavaDoc[] {
135             "domain", "type", "objectName"}));
136
137         // Detail MBean node
138
String JavaDoc sLastDomain = "";
139         String JavaDoc sLastType = "";
140         TreeControlNode nodeMBean = null;
141         TreeControlNode nodeDomain = null;
142         TreeControlNode nodeType = null;
143         TreeControlNode nodeParent = null;
144         OwnerMbeanItem oItem;
145
146         Iterator JavaDoc it = p_List.iterator();
147         while (it.hasNext()) {
148             oItem = (OwnerMbeanItem) it.next();
149             
150             nodeParent = nodeMBeans;
151             if (oItem.getDomain() != null) {
152                 // Domain node
153
if (oItem.getDomain().equals(sLastDomain) == false) {
154                     sLastDomain = oItem.getDomain();
155                     nodeDomain = new TreeControlNode(nodeParent.getName()
156                         + WhereAreYou.NODE_SEPARATOR + oItem.getDomain(), "icon/mbeandomain.gif"
157                         , oItem.getDomain(), null, "content", false);
158                     nodeMBeans.addChild(nodeDomain);
159                 }
160                 nodeParent = nodeDomain;
161
162                 if (oItem.getType() != null) {
163                     // Type node
164
if (oItem.getType().equals(sLastType) == false) {
165                         sLastType = oItem.getType();
166                         nodeType = new TreeControlNode(nodeParent.getName()
167                             + WhereAreYou.NODE_SEPARATOR + oItem.getType(), "icon/mbeantype.gif"
168                             , oItem.getType(), null, "content", false);
169                         nodeDomain.addChild(nodeType);
170                     }
171                     nodeParent = nodeType;
172                 }
173             }
174
175             // Mbean node
176
nodeMBean = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
177                 + oItem.getObjectName(), "icon/mbean.gif", oItem.getObjectName()
178                 , "ListMBeanDetails.do?select=" + URLEncoder.encode(oItem.getObjectName()
179                 , "UTF-8"), "content", false);
180             nodeParent.addChild(nodeMBean);
181
182         }
183     }
184
185     /**
186      * Append nodes for all defined J2EE MBeans.
187      *
188      * @param p_ParentNode Parent node for the tree control
189      * @param p_Resources The MessageResources for our localized messages
190      * @param p_List The list of Mbeans
191      *
192      * @exception Exception if an exception occurs building the tree
193      */

194     public void getJ2eeMBeans(TreeControlNode p_ParentNode, MessageResources p_Resources
195         , ArrayList JavaDoc p_List)
196         throws Exception JavaDoc {
197
198         TreeControlNode nodeMBeans = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
199             + "j2ee", "icon/mbeans.gif", p_Resources.getMessage("treenode.mbeans.j2ee")
200             , "ListJ2eeMBeans.do", "content", false);
201         p_ParentNode.addChild(nodeMBeans);
202         // Sort list
203
Collections.sort(p_List, new BeanComparator(new String JavaDoc[] {
204             "domain", "j2eeType", "name", "objectName"}));
205
206         // Detail MBean node
207
J2eeMbeanItem oItem;
208         String JavaDoc sLastDomain = "";
209         String JavaDoc sLastType = "";
210         TreeControlNode nodeMBean = null;
211         TreeControlNode nodeDomain = null;
212         TreeControlNode nodeType = null;
213         TreeControlNode nodeParent = null;
214
215         Iterator JavaDoc it = p_List.iterator();
216         while (it.hasNext()) {
217             oItem = (J2eeMbeanItem) it.next();
218
219             nodeParent = nodeMBeans;
220             if (oItem.getDomain() != null) {
221                 // Domain node
222
if (oItem.getDomain().equals(sLastDomain) == false) {
223                     sLastDomain = oItem.getDomain();
224                     nodeDomain = new TreeControlNode(nodeParent.getName()
225                         + WhereAreYou.NODE_SEPARATOR + oItem.getDomain(), "icon/mbeandomain.gif"
226                         , oItem.getDomain(), null, "content", false);
227                     nodeMBeans.addChild(nodeDomain);
228                 }
229                 nodeParent = nodeDomain;
230             }
231             if (oItem.getJ2eeType() != null) {
232                 // Type node
233
if (oItem.getJ2eeType().equals(sLastType) == false) {
234                     sLastType = oItem.getJ2eeType();
235                     nodeType = new TreeControlNode(nodeParent.getName()
236                         + WhereAreYou.NODE_SEPARATOR + oItem.getJ2eeType(), "icon/mbeantype.gif"
237                         , oItem.getJ2eeType(), null, "content", false);
238                     nodeDomain.addChild(nodeType);
239                 }
240                 nodeParent = nodeType;
241             }
242
243             // Mbean node
244
nodeMBean = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
245                 + oItem.getObjectName(), "icon/mbean.gif", oItem.getObjectName()
246                 , "ListMBeanDetails.do?select=" + URLEncoder.encode(oItem.getObjectName()
247                 , "UTF-8"), "content", false);
248             nodeParent.addChild(nodeMBean);
249
250         }
251     }
252
253     /**
254      * Append nodes for all defined Unknown MBeans.
255      *
256      * @param p_ParentNode Parent node for the tree control
257      * @param p_Resources The MessageResources for our localized messages
258      * @param p_List The list of Mbeans
259      *
260      * @exception Exception if an exception occurs building the tree
261      */

262     public void getUnknownMBeans(TreeControlNode p_ParentNode, MessageResources p_Resources
263         , ArrayList JavaDoc p_List)
264         throws Exception JavaDoc {
265
266         TreeControlNode nodeMBeans = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
267             + "unknown", "icon/mbeans.gif", p_Resources.getMessage("treenode.mbeans.unknown")
268             , "ListUnknownMBeans.do", "content", false);
269         p_ParentNode.addChild(nodeMBeans);
270         // Sort list
271
Collections.sort(p_List, new BeanComparator(new String JavaDoc[] {
272             "domain", "objectName"}));
273
274         // Detail MBean node
275
MbeanItem oItem;
276         String JavaDoc sLastDomain = "";
277         TreeControlNode nodeMBean = null;
278         TreeControlNode nodeDomain = null;
279         TreeControlNode nodeType = null;
280         TreeControlNode nodeParent = null;
281
282         Iterator JavaDoc it = p_List.iterator();
283         while (it.hasNext()) {
284             oItem = (MbeanItem) it.next();
285
286             nodeParent = nodeMBeans;
287             if (oItem.getDomain() != null) {
288                 // Domain node
289
if (oItem.getDomain().equals(sLastDomain) == false) {
290                     sLastDomain = oItem.getDomain();
291                     nodeDomain = new TreeControlNode(nodeParent.getName()
292                         + WhereAreYou.NODE_SEPARATOR + oItem.getDomain(), "icon/mbeandomain.gif"
293                         , oItem.getDomain(), null, "content", false);
294                     nodeMBeans.addChild(nodeDomain);
295                 }
296                 nodeParent = nodeDomain;
297
298             }
299
300             // Mbean node
301
nodeMBean = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
302                 + oItem.getObjectName(), "icon/mbean.gif", oItem.getObjectName()
303                 , "ListMBeanDetails.do?select=" + URLEncoder.encode(oItem.getObjectName()
304                 , "UTF-8"), "content", false);
305             nodeParent.addChild(nodeMBean);
306
307         }
308     }
309
310     /**
311      * Append nodes Attributes and Operations for a defined MBean.
312      *
313      * @param nodeMBean The MBean node
314      * @param onMBean The MBean Object name
315      * @param resources Resource
316      * @throws Exception
317      */

318     protected void getMBeanInfo(TreeControlNode nodeMBean, ObjectName JavaDoc onMBean
319         , MessageResources resources)
320         throws Exception JavaDoc {
321         // Get all infos of a MBean
322
MBeanInfo JavaDoc oMBeanInfo = JonasManagementRepr.getMBeanInfo(onMBean);
323         // Get attributes infos
324
MBeanAttributeInfo JavaDoc[] aoMBeanAttributeInfo = oMBeanInfo.getAttributes();
325         if (aoMBeanAttributeInfo.length > 0) {
326             // Append attributes node
327
TreeControlNode nodeAttributes = new TreeControlNode("Attributes"
328                 + WhereAreYou.NODE_SEPARATOR + onMBean.toString(), "icon/JonasQuestion.gif"
329                 , resources.getMessage("treenode.allmbeans.attributes"), null, "content", false);
330             nodeMBean.addChild(nodeAttributes);
331             // Loop to append each attribute node
332
for (int i = 0; i < aoMBeanAttributeInfo.length; i++) {
333                 TreeControlNode nodeAttr = new TreeControlNode(String.valueOf(i)
334                     + WhereAreYou.NODE_SEPARATOR + onMBean.toString() + WhereAreYou.NODE_SEPARATOR
335                     + aoMBeanAttributeInfo[i].getName(), "icon/property.gif"
336                     , aoMBeanAttributeInfo[i].getName(), null, "content", false);
337                 nodeAttributes.addChild(nodeAttr);
338             }
339         }
340         // Get operations infos
341
MBeanOperationInfo JavaDoc[] aoMBeanOperationInfo = oMBeanInfo.getOperations();
342         if (aoMBeanOperationInfo.length > 0) {
343             // Append operations node
344
TreeControlNode nodeOperations = new TreeControlNode("Operations"
345                 + WhereAreYou.NODE_SEPARATOR + onMBean.toString(), "icon/JonasQuestion.gif"
346                 , resources.getMessage("treenode.allmbeans.operations"), null, "content", false);
347             nodeMBean.addChild(nodeOperations);
348             // Loop to append each operation node
349
for (int i = 0; i < aoMBeanOperationInfo.length; i++) {
350                 TreeControlNode nodeOpe = new TreeControlNode(String.valueOf(i)
351                     + WhereAreYou.NODE_SEPARATOR + onMBean.toString() + WhereAreYou.NODE_SEPARATOR
352                     + aoMBeanOperationInfo[i].getName(), "icon/action.gif"
353                     , aoMBeanOperationInfo[i].getName(), null, "content", false);
354                 nodeOperations.addChild(nodeOpe);
355             }
356         }
357     }
358 }
359
Popular Tags