001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: MBeanTreeBuilder.java 9680 2006-10-06 12:08:33Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin;
025:
026: import java.net.URLEncoder;
027: import java.util.ArrayList;
028: import java.util.Collections;
029: import java.util.Iterator;
030:
031: import javax.management.MBeanAttributeInfo;
032: import javax.management.MBeanInfo;
033: import javax.management.MBeanOperationInfo;
034: import javax.management.ObjectName;
035: import javax.servlet.http.HttpServletRequest;
036:
037: import org.apache.struts.Globals;
038: import org.apache.struts.action.ActionServlet;
039: import org.apache.struts.util.MessageResources;
040:
041: import org.objectweb.jonas.jmx.JonasManagementRepr;
042: import org.objectweb.jonas.webapp.jonasadmin.common.BeanComparator;
043: import org.objectweb.jonas.webapp.jonasadmin.mbean.J2eeMbeanItem;
044: import org.objectweb.jonas.webapp.jonasadmin.mbean.MbeanItem;
045: import org.objectweb.jonas.webapp.jonasadmin.mbean.OwnerMbeanItem;
046: import org.objectweb.jonas.webapp.taglib.TreeBuilder;
047: import org.objectweb.jonas.webapp.taglib.TreeControl;
048: import org.objectweb.jonas.webapp.taglib.TreeControlNode;
049:
050: /**
051: * Implementation of <code>TreeBuilder</code> that adds the nodes required
052: * for administering the Jonas server.
053: *
054: * @author Michel-Ange ANTON (initial developer)
055: * @author Florent Benoit (changes for struts 1.2.2)
056: */
057:
058: public class MBeanTreeBuilder implements TreeBuilder {
059: // ----------------------------------------------------- Instance Variables
060:
061: // ---------------------------------------------------- TreeBuilder Methods
062:
063: /**
064: * Add the required nodes to the specified <code>treeControl</code>
065: * instance.
066: *
067: * @param treeControl The <code>TreeControl</code> to which we should
068: * add our nodes
069: * @param servlet The controller servlet for the admin application
070: * @param request The servlet request we are processing
071: */
072: public void buildTree(TreeControl treeControl,
073: ActionServlet servlet, HttpServletRequest request) {
074: try {
075: TreeControlNode rootNode = treeControl.getRoot();
076: MessageResources resources = (MessageResources) servlet
077: .getServletContext().getAttribute(
078: Globals.MESSAGES_KEY);
079: getMBeans(rootNode, resources, request);
080: } catch (Throwable t) {
081: t.printStackTrace(System.out);
082: }
083: }
084:
085: // ------------------------------------------------------ Protected Methods
086:
087: /**
088: * Append nodes for all defined MBeans.
089: *
090: * @param rootNode Root node for the tree control
091: * @param resources The MessageResources for our localized messages
092: *
093: * @exception Exception if an exception occurs building the tree
094: */
095: public void getMBeans(TreeControlNode rootNode,
096: MessageResources resources, HttpServletRequest request)
097: throws Exception {
098:
099: TreeControlNode nodeMBeans = new TreeControlNode("mbeans",
100: "icon/mbeans.gif", resources
101: .getMessage("treenode.allmbeans"),
102: "ListMBeans.do", "content", false);
103: rootNode.addChild(nodeMBeans);
104:
105: WhereAreYou oWhere = (WhereAreYou) request.getSession()
106: .getAttribute(WhereAreYou.SESSION_NAME);
107: String serverName = oWhere.getCurrentJonasServerName();
108:
109: ArrayList[] als = JonasAdminJmx
110: .getFamiliesMbeansLists(serverName);
111: if (als[MbeanItem.FAMILY_J2EE].size() > 0) {
112: getJ2eeMBeans(nodeMBeans, resources,
113: als[MbeanItem.FAMILY_J2EE]);
114: }
115: if (als[MbeanItem.FAMILY_OWNER].size() > 0) {
116: getOwnerMBeans(nodeMBeans, resources,
117: als[MbeanItem.FAMILY_OWNER]);
118: }
119: if (als[MbeanItem.FAMILY_UNKNOWN].size() > 0) {
120: getUnknownMBeans(nodeMBeans, resources,
121: als[MbeanItem.FAMILY_UNKNOWN]);
122: }
123: }
124:
125: /**
126: * Append nodes for all defined Owner MBeans.
127: *
128: * @param p_ParentNode Parent node for the tree control
129: * @param p_Resources The MessageResources for our localized messages
130: * @param p_List The list of Mbeans
131: *
132: * @exception Exception if an exception occurs building the tree
133: */
134: public void getOwnerMBeans(TreeControlNode p_ParentNode,
135: MessageResources p_Resources, ArrayList p_List)
136: throws Exception {
137:
138: TreeControlNode nodeMBeans = new TreeControlNode("mbeans"
139: + WhereAreYou.NODE_SEPARATOR + "owner",
140: "icon/mbeans.gif", p_Resources
141: .getMessage("treenode.mbeans.owner"),
142: "ListOwnerMBeans.do", "content", false);
143: p_ParentNode.addChild(nodeMBeans);
144: // Sort list
145: Collections.sort(p_List, new BeanComparator(new String[] {
146: "domain", "type", "objectName" }));
147:
148: // Detail MBean node
149: String sLastDomain = "";
150: String sLastType = "";
151: TreeControlNode nodeMBean = null;
152: TreeControlNode nodeDomain = null;
153: TreeControlNode nodeType = null;
154: TreeControlNode nodeParent = null;
155: OwnerMbeanItem oItem;
156:
157: Iterator it = p_List.iterator();
158: while (it.hasNext()) {
159: oItem = (OwnerMbeanItem) it.next();
160:
161: nodeParent = nodeMBeans;
162: if (oItem.getDomain() != null) {
163: // Domain node
164: if (oItem.getDomain().equals(sLastDomain) == false) {
165: sLastDomain = oItem.getDomain();
166: nodeDomain = new TreeControlNode(nodeParent
167: .getName()
168: + WhereAreYou.NODE_SEPARATOR
169: + oItem.getDomain(),
170: "icon/mbeandomain.gif", oItem.getDomain(),
171: null, "content", false);
172: nodeMBeans.addChild(nodeDomain);
173: }
174: nodeParent = nodeDomain;
175:
176: if (oItem.getType() != null) {
177: // Type node
178: if (oItem.getType().equals(sLastType) == false) {
179: sLastType = oItem.getType();
180: nodeType = new TreeControlNode(nodeParent
181: .getName()
182: + WhereAreYou.NODE_SEPARATOR
183: + oItem.getType(),
184: "icon/mbeantype.gif", oItem.getType(),
185: null, "content", false);
186: nodeDomain.addChild(nodeType);
187: }
188: nodeParent = nodeType;
189: }
190: }
191:
192: // Mbean node
193: nodeMBean = new TreeControlNode("mbeans"
194: + WhereAreYou.NODE_SEPARATOR
195: + oItem.getObjectName(), "icon/mbean.gif", oItem
196: .getObjectName(),
197: "ListMBeanDetails.do?select="
198: + URLEncoder.encode(oItem.getObjectName(),
199: "UTF-8"), "content", false);
200: nodeParent.addChild(nodeMBean);
201:
202: }
203: }
204:
205: /**
206: * Append nodes for all defined J2EE MBeans.
207: *
208: * @param p_ParentNode Parent node for the tree control
209: * @param p_Resources The MessageResources for our localized messages
210: * @param p_List The list of Mbeans
211: *
212: * @exception Exception if an exception occurs building the tree
213: */
214: public void getJ2eeMBeans(TreeControlNode p_ParentNode,
215: MessageResources p_Resources, ArrayList p_List)
216: throws Exception {
217:
218: TreeControlNode nodeMBeans = new TreeControlNode("mbeans"
219: + WhereAreYou.NODE_SEPARATOR + "j2ee",
220: "icon/mbeans.gif", p_Resources
221: .getMessage("treenode.mbeans.j2ee"),
222: "ListJ2eeMBeans.do", "content", false);
223: p_ParentNode.addChild(nodeMBeans);
224: // Sort list
225: Collections.sort(p_List, new BeanComparator(new String[] {
226: "domain", "j2eeType", "name", "objectName" }));
227:
228: // Detail MBean node
229: J2eeMbeanItem oItem;
230: String sLastDomain = "";
231: String sLastType = "";
232: TreeControlNode nodeMBean = null;
233: TreeControlNode nodeDomain = null;
234: TreeControlNode nodeType = null;
235: TreeControlNode nodeParent = null;
236:
237: Iterator it = p_List.iterator();
238: while (it.hasNext()) {
239: oItem = (J2eeMbeanItem) it.next();
240:
241: nodeParent = nodeMBeans;
242: if (oItem.getDomain() != null) {
243: // Domain node
244: if (oItem.getDomain().equals(sLastDomain) == false) {
245: sLastDomain = oItem.getDomain();
246: nodeDomain = new TreeControlNode(nodeParent
247: .getName()
248: + WhereAreYou.NODE_SEPARATOR
249: + oItem.getDomain(),
250: "icon/mbeandomain.gif", oItem.getDomain(),
251: null, "content", false);
252: nodeMBeans.addChild(nodeDomain);
253: }
254: nodeParent = nodeDomain;
255: }
256: if (oItem.getJ2eeType() != null) {
257: // Type node
258: if (oItem.getJ2eeType().equals(sLastType) == false) {
259: sLastType = oItem.getJ2eeType();
260: nodeType = new TreeControlNode(nodeParent.getName()
261: + WhereAreYou.NODE_SEPARATOR
262: + oItem.getJ2eeType(),
263: "icon/mbeantype.gif", oItem.getJ2eeType(),
264: null, "content", false);
265: nodeDomain.addChild(nodeType);
266: }
267: nodeParent = nodeType;
268: }
269:
270: // Mbean node
271: nodeMBean = new TreeControlNode("mbeans"
272: + WhereAreYou.NODE_SEPARATOR
273: + oItem.getObjectName(), "icon/mbean.gif", oItem
274: .getObjectName(),
275: "ListMBeanDetails.do?select="
276: + URLEncoder.encode(oItem.getObjectName(),
277: "UTF-8"), "content", false);
278: nodeParent.addChild(nodeMBean);
279:
280: }
281: }
282:
283: /**
284: * Append nodes for all defined Unknown MBeans.
285: *
286: * @param p_ParentNode Parent node for the tree control
287: * @param p_Resources The MessageResources for our localized messages
288: * @param p_List The list of Mbeans
289: *
290: * @exception Exception if an exception occurs building the tree
291: */
292: public void getUnknownMBeans(TreeControlNode p_ParentNode,
293: MessageResources p_Resources, ArrayList p_List)
294: throws Exception {
295:
296: TreeControlNode nodeMBeans = new TreeControlNode("mbeans"
297: + WhereAreYou.NODE_SEPARATOR + "unknown",
298: "icon/mbeans.gif", p_Resources
299: .getMessage("treenode.mbeans.unknown"),
300: "ListUnknownMBeans.do", "content", false);
301: p_ParentNode.addChild(nodeMBeans);
302: // Sort list
303: Collections.sort(p_List, new BeanComparator(new String[] {
304: "domain", "objectName" }));
305:
306: // Detail MBean node
307: MbeanItem oItem;
308: String sLastDomain = "";
309: TreeControlNode nodeMBean = null;
310: TreeControlNode nodeDomain = null;
311: TreeControlNode nodeType = null;
312: TreeControlNode nodeParent = null;
313:
314: Iterator it = p_List.iterator();
315: while (it.hasNext()) {
316: oItem = (MbeanItem) it.next();
317:
318: nodeParent = nodeMBeans;
319: if (oItem.getDomain() != null) {
320: // Domain node
321: if (oItem.getDomain().equals(sLastDomain) == false) {
322: sLastDomain = oItem.getDomain();
323: nodeDomain = new TreeControlNode(nodeParent
324: .getName()
325: + WhereAreYou.NODE_SEPARATOR
326: + oItem.getDomain(),
327: "icon/mbeandomain.gif", oItem.getDomain(),
328: null, "content", false);
329: nodeMBeans.addChild(nodeDomain);
330: }
331: nodeParent = nodeDomain;
332:
333: }
334:
335: // Mbean node
336: nodeMBean = new TreeControlNode("mbeans"
337: + WhereAreYou.NODE_SEPARATOR
338: + oItem.getObjectName(), "icon/mbean.gif", oItem
339: .getObjectName(),
340: "ListMBeanDetails.do?select="
341: + URLEncoder.encode(oItem.getObjectName(),
342: "UTF-8"), "content", false);
343: nodeParent.addChild(nodeMBean);
344:
345: }
346: }
347:
348: /**
349: * Append nodes Attributes and Operations for a defined MBean.
350: *
351: * @param nodeMBean The MBean node
352: * @param onMBean The MBean Object name
353: * @param resources Resource
354: * @throws Exception
355: */
356: protected void getMBeanInfo(TreeControlNode nodeMBean,
357: ObjectName onMBean, MessageResources resources,
358: String serverName) throws Exception {
359: // Get all infos of a MBean
360: MBeanInfo oMBeanInfo = JonasManagementRepr.getMBeanInfo(
361: onMBean, serverName);
362: // Get attributes infos
363: MBeanAttributeInfo[] aoMBeanAttributeInfo = oMBeanInfo
364: .getAttributes();
365: if (aoMBeanAttributeInfo.length > 0) {
366: // Append attributes node
367: TreeControlNode nodeAttributes = new TreeControlNode(
368: "Attributes" + WhereAreYou.NODE_SEPARATOR
369: + onMBean.toString(),
370: "icon/JonasQuestion.gif",
371: resources
372: .getMessage("treenode.allmbeans.attributes"),
373: null, "content", false);
374: nodeMBean.addChild(nodeAttributes);
375: // Loop to append each attribute node
376: for (int i = 0; i < aoMBeanAttributeInfo.length; i++) {
377: TreeControlNode nodeAttr = new TreeControlNode(String
378: .valueOf(i)
379: + WhereAreYou.NODE_SEPARATOR
380: + onMBean.toString()
381: + WhereAreYou.NODE_SEPARATOR
382: + aoMBeanAttributeInfo[i].getName(),
383: "icon/property.gif", aoMBeanAttributeInfo[i]
384: .getName(), null, "content", false);
385: nodeAttributes.addChild(nodeAttr);
386: }
387: }
388: // Get operations infos
389: MBeanOperationInfo[] aoMBeanOperationInfo = oMBeanInfo
390: .getOperations();
391: if (aoMBeanOperationInfo.length > 0) {
392: // Append operations node
393: TreeControlNode nodeOperations = new TreeControlNode(
394: "Operations" + WhereAreYou.NODE_SEPARATOR
395: + onMBean.toString(),
396: "icon/JonasQuestion.gif",
397: resources
398: .getMessage("treenode.allmbeans.operations"),
399: null, "content", false);
400: nodeMBean.addChild(nodeOperations);
401: // Loop to append each operation node
402: for (int i = 0; i < aoMBeanOperationInfo.length; i++) {
403: TreeControlNode nodeOpe = new TreeControlNode(String
404: .valueOf(i)
405: + WhereAreYou.NODE_SEPARATOR
406: + onMBean.toString()
407: + WhereAreYou.NODE_SEPARATOR
408: + aoMBeanOperationInfo[i].getName(),
409: "icon/action.gif", aoMBeanOperationInfo[i]
410: .getName(), null, "content", false);
411: nodeOperations.addChild(nodeOpe);
412: }
413: }
414: }
415: }
|