001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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: ListMBeanAttributesAction.java 9680 2006-10-06 12:08:33Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.mbean;
025:
026: import java.io.IOException;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.Collections;
030: import java.util.Comparator;
031: import java.util.Iterator;
032:
033: import javax.management.MBeanAttributeInfo;
034: import javax.management.MBeanInfo;
035: import javax.management.ObjectName;
036: import javax.servlet.ServletException;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: import org.apache.struts.action.ActionForm;
041: import org.apache.struts.action.ActionForward;
042: import org.apache.struts.action.ActionMapping;
043: import org.objectweb.jonas.jmx.JonasManagementRepr;
044: import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
045:
046: /**
047: * List of all Attributes for a MBean.
048: *
049: * @author Michel-Ange ANTON
050: */
051:
052: public final class ListMBeanAttributesAction extends
053: ListMBeanDetailsAction {
054:
055: // --------------------------------------------------------- Public Methods
056:
057: public ActionForward executeAction(ActionMapping p_Mapping,
058: ActionForm p_Form, HttpServletRequest p_Request,
059: HttpServletResponse p_Response) throws IOException,
060: ServletException {
061:
062: WhereAreYou oWhere = (WhereAreYou) p_Request.getSession()
063: .getAttribute(WhereAreYou.SESSION_NAME);
064: String serverName = oWhere.getCurrentJonasServerName();
065:
066: try {
067: // Save current action
068: setAction(ACTION_ATTRIBUTES);
069: // Parameter
070: String sSelect = p_Request.getParameter("select");
071:
072: // Create a request attribute with our collection of MBeans
073: ArrayList list = new ArrayList();
074: // Get all infos of a MBean
075: ObjectName on = new ObjectName(sSelect);
076: MbeanItem oItem = MbeanItem.build(on);
077: MBeanInfo oMBeanInfo = JonasManagementRepr.getMBeanInfo(on,
078: serverName);
079: // Get attributes infos
080: MBeanAttributeInfo[] aoAttributes = oMBeanInfo
081: .getAttributes();
082: if (aoAttributes.length > 0) {
083: Object oValue;
084: String sError;
085: // Loop to append each attribute node
086: for (int i = 0; i < aoAttributes.length; i++) {
087: sError = null;
088: oValue = null;
089: try {
090: oValue = JonasManagementRepr.getAttribute(on,
091: aoAttributes[i].getName(), serverName);
092: } catch (Exception ex) {
093: sError = ex.getMessage();
094: }
095: list.add(new ViewMBeanAttributes(aoAttributes[i],
096: oValue, sError));
097: }
098: // Sort
099: Collections.sort(list, new MBeanAttributesByName());
100: }
101: // Infos for displaying
102: p_Request.setAttribute("MBean", oItem);
103: p_Request.setAttribute("MBeanAttributes", list);
104: // Active and save filtering display if not exists
105: MbeanFilteringForm oForm = (MbeanFilteringForm) m_Session
106: .getAttribute("mbeanFilteringForm");
107: if (oForm == null) {
108: oForm = new MbeanFilteringForm();
109: oForm.reset(p_Mapping, p_Request);
110: m_Session.setAttribute("mbeanFilteringForm", oForm);
111: }
112: oForm.setSelectedName(sSelect);
113: // Force the node selected in tree when the direct is used (MBeans list)
114: StringBuffer sbBranch = new StringBuffer("domain*mbeans");
115: sbBranch.append(WhereAreYou.NODE_SEPARATOR);
116: sbBranch.append(sSelect);
117: m_WhereAreYou.selectNameNode(sbBranch.toString(), true);
118: } catch (Throwable t) {
119: addGlobalError(t);
120: saveErrors(p_Request, m_Errors);
121: return (p_Mapping.findForward("Global Error"));
122: }
123: // Forward to the corresponding display page
124: return p_Mapping.findForward("List MBean Attributes");
125: }
126:
127: // --------------------------------------------------------- Inner Classes
128:
129: public class ViewMBeanAttributes {
130: private String name;
131: private String type;
132: private String is;
133: private String read;
134: private String write;
135: private String description;
136: private String value;
137: private String errorMessage;
138: private boolean error;
139:
140: public ViewMBeanAttributes(MBeanAttributeInfo po_Attr,
141: Object po_Value, String ps_Error) {
142: setName(po_Attr.getName());
143: setType(po_Attr.getType());
144: setDescription(po_Attr.getDescription());
145: setObjectValue(po_Value);
146: setErrorMessage(ps_Error);
147: if (ps_Error != null) {
148: setError(true);
149: }
150: if (po_Attr.isIs() == true) {
151: setIs("is");
152: }
153: if (po_Attr.isReadable() == true) {
154: setRead("read");
155: }
156: if (po_Attr.isWritable() == true) {
157: setWrite("write");
158: }
159: }
160:
161: public void setObjectValue(Object objectValue) {
162: if (objectValue == null) {
163: value = "null";
164: } else {
165: // Detect Array or Collection
166: if (objectValue.getClass().isArray() == true) {
167: // Array
168: value = arrayToString((Object[]) objectValue);
169: } else {
170: try {
171: // Collection
172: value = collectionToString((Collection) objectValue);
173: } catch (Exception e) {
174: // Default
175: value = objectValue.toString();
176: }
177: }
178: }
179: }
180:
181: public String arrayToString(Object[] p_Array) {
182: StringBuffer sb = new StringBuffer();
183: sb.append("[ ");
184: for (int i = 0; i < p_Array.length; i++) {
185: if (p_Array[i] == null) {
186: sb.append("null");
187: } else {
188: sb.append(p_Array[i].toString());
189: }
190: if ((i + 1) < p_Array.length) {
191: sb.append(" - ");
192: }
193: }
194: sb.append(" ]");
195: return sb.toString();
196: }
197:
198: public String collectionToString(Collection p_Collection) {
199: StringBuffer sb = new StringBuffer();
200: sb.append("[ ");
201: Iterator it = p_Collection.iterator();
202: while (it.hasNext()) {
203: sb.append(it.next().toString());
204: if (it.hasNext()) {
205: sb.append(" - ");
206: }
207: }
208: sb.append(" ]");
209: return sb.toString();
210: }
211:
212: public String getName() {
213: return name;
214: }
215:
216: public void setName(String name) {
217: this .name = name;
218: }
219:
220: public String getType() {
221: return type;
222: }
223:
224: public void setType(String type) {
225: this .type = type;
226: }
227:
228: public String getIs() {
229: return is;
230: }
231:
232: public void setIs(String is) {
233: this .is = is;
234: }
235:
236: public String getRead() {
237: return read;
238: }
239:
240: public void setRead(String read) {
241: this .read = read;
242: }
243:
244: public String getWrite() {
245: return write;
246: }
247:
248: public void setWrite(String write) {
249: this .write = write;
250: }
251:
252: public String getDescription() {
253: return description;
254: }
255:
256: public void setDescription(String description) {
257: this .description = description;
258: }
259:
260: public String getValue() {
261: return value;
262: }
263:
264: public void setValue(String value) {
265: this .value = value;
266: }
267:
268: public String getErrorMessage() {
269: return errorMessage;
270: }
271:
272: public void setErrorMessage(String errorMessage) {
273: this .errorMessage = errorMessage;
274: }
275:
276: public boolean isError() {
277: return error;
278: }
279:
280: public void setError(boolean error) {
281: this .error = error;
282: }
283: }
284:
285: public class MBeanAttributesByName implements Comparator {
286:
287: public int compare(Object p_O1, Object p_O2) {
288: ViewMBeanAttributes o1 = (ViewMBeanAttributes) p_O1;
289: ViewMBeanAttributes o2 = (ViewMBeanAttributes) p_O2;
290: return o1.getName().compareToIgnoreCase(o2.getName());
291: }
292:
293: public boolean equals(Object p_Obj) {
294: if (p_Obj instanceof ViewMBeanAttributes) {
295: return true;
296: }
297: return false;
298: }
299: }
300:
301: }
|