ListMBeanAttributesAction.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * --------------------------------------------------------------------------
00022  * $Id: ListMBeanAttributesAction.java,v 1.8 2004/03/19 14:31:47 sauthieg Exp $
00023  * --------------------------------------------------------------------------
00024  */
00025 
00026 package org.objectweb.jonas.webapp.jonasadmin.mbean;
00027 
00028 import java.io.IOException;
00029 import java.util.ArrayList;
00030 import java.util.Collection;
00031 import java.util.Collections;
00032 import java.util.Comparator;
00033 import java.util.Iterator;
00034 
00035 import javax.management.MBeanAttributeInfo;
00036 import javax.management.MBeanInfo;
00037 import javax.management.ObjectName;
00038 import javax.servlet.ServletException;
00039 import javax.servlet.http.HttpServletRequest;
00040 import javax.servlet.http.HttpServletResponse;
00041 
00042 import org.apache.struts.action.ActionForm;
00043 import org.apache.struts.action.ActionForward;
00044 import org.apache.struts.action.ActionMapping;
00045 import org.objectweb.jonas.jmx.JonasManagementRepr;
00046 import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
00047 
00054 public final class ListMBeanAttributesAction extends ListMBeanDetailsAction {
00055 
00056 // --------------------------------------------------------- Public Methods
00057 
00058     public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
00059         , HttpServletRequest p_Request, HttpServletResponse p_Response)
00060         throws IOException, ServletException {
00061 
00062         try {
00063             // Save current action
00064             setAction(ACTION_ATTRIBUTES);
00065             // Parameter
00066             String sSelect = p_Request.getParameter("select");
00067 
00068             // Create a request attribute with our collection of MBeans
00069             ArrayList list = new ArrayList();
00070             // Get all infos of a MBean
00071             ObjectName on = new ObjectName(sSelect);
00072             MbeanItem oItem = MbeanItem.build(on);
00073             MBeanInfo oMBeanInfo = JonasManagementRepr.getMBeanInfo(on);
00074             // Get attributes infos
00075             MBeanAttributeInfo[] aoAttributes = oMBeanInfo.getAttributes();
00076             if (aoAttributes.length > 0) {
00077                 Object oValue;
00078                 String sError;
00079                 // Loop to append each attribute node
00080                 for (int i = 0; i < aoAttributes.length; i++) {
00081                     sError = null;
00082                     oValue = null;
00083                     try {
00084                         oValue = JonasManagementRepr.getAttribute(on, aoAttributes[i].getName());
00085                     }
00086                     catch (Exception ex) {
00087                         sError = ex.getMessage();
00088                     }
00089                     list.add(new ViewMBeanAttributes(aoAttributes[i], oValue, sError));
00090                 }
00091                 // Sort
00092                 Collections.sort(list, new MBeanAttributesByName());
00093             }
00094             // Infos for displaying
00095             p_Request.setAttribute("MBean", oItem);
00096             p_Request.setAttribute("MBeanAttributes", list);
00097             // Active and save filtering display if not exists
00098             MbeanFilteringForm oForm = (MbeanFilteringForm) m_Session.getAttribute(
00099                 "mbeanFilteringForm");
00100             if (oForm == null) {
00101                 oForm = new MbeanFilteringForm();
00102                 oForm.reset(p_Mapping, p_Request);
00103                 m_Session.setAttribute("mbeanFilteringForm", oForm);
00104             }
00105             oForm.setSelectedName(sSelect);
00106             // Force the node selected in tree when the direct is used (MBeans list)
00107             StringBuffer sbBranch = new StringBuffer("mbeans");
00108             sbBranch.append(WhereAreYou.NODE_SEPARATOR);
00109             sbBranch.append(sSelect);
00110             m_WhereAreYou.selectNameNode(sbBranch.toString(), true);
00111         }
00112         catch (Throwable t) {
00113             addGlobalError(t);
00114             saveErrors(p_Request, m_Errors);
00115             return (p_Mapping.findForward("Global Error"));
00116         }
00117         // Forward to the corresponding display page
00118         return p_Mapping.findForward("List MBean Attributes");
00119     }
00120 
00121 // --------------------------------------------------------- Inner Classes
00122 
00123     public class ViewMBeanAttributes {
00124         private String name;
00125         private String type;
00126         private String is;
00127         private String read;
00128         private String write;
00129         private String description;
00130         private String value;
00131         private String errorMessage;
00132         private boolean error;
00133 
00134         public ViewMBeanAttributes(MBeanAttributeInfo po_Attr, Object po_Value, String ps_Error) {
00135             setName(po_Attr.getName());
00136             setType(po_Attr.getType());
00137             setDescription(po_Attr.getDescription());
00138             setObjectValue(po_Value);
00139             setErrorMessage(ps_Error);
00140             if (ps_Error != null) {
00141                 setError(true);
00142             }
00143             if (po_Attr.isIs() == true) {
00144                 setIs("is");
00145             }
00146             if (po_Attr.isReadable() == true) {
00147                 setRead("read");
00148             }
00149             if (po_Attr.isWritable() == true) {
00150                 setWrite("write");
00151             }
00152         }
00153 
00154         public void setObjectValue(Object objectValue) {
00155             if (objectValue == null) {
00156                 value = "null";
00157             }
00158             else {
00159                 // Detect Array or Collection
00160                 if (objectValue.getClass().isArray() == true) {
00161                     // Array
00162                     value = arrayToString((Object[]) objectValue);
00163                 }
00164                 else {
00165                     try {
00166                         // Collection
00167                         value = collectionToString((Collection) objectValue);
00168                     }
00169                     catch (Exception e) {
00170                         // Default
00171                         value = objectValue.toString();
00172                     }
00173                 }
00174             }
00175         }
00176 
00177         public String arrayToString(Object[] p_Array) {
00178             StringBuffer sb = new StringBuffer();
00179             sb.append("[ ");
00180             for (int i = 0; i < p_Array.length; i++) {
00181                 if (p_Array[i] == null) {
00182                     sb.append("null");
00183                 }
00184                 else {
00185                     sb.append(p_Array[i].toString());
00186                 }
00187                 if ((i + 1) < p_Array.length) {
00188                     sb.append(" - ");
00189                 }
00190             }
00191             sb.append(" ]");
00192             return sb.toString();
00193         }
00194 
00195         public String collectionToString(Collection p_Collection) {
00196             StringBuffer sb = new StringBuffer();
00197             sb.append("[ ");
00198             Iterator it = p_Collection.iterator();
00199             while (it.hasNext()) {
00200                 sb.append(it.next().toString());
00201                 if (it.hasNext()) {
00202                     sb.append(" - ");
00203                 }
00204             }
00205             sb.append(" ]");
00206             return sb.toString();
00207         }
00208 
00209         public String getName() {
00210             return name;
00211         }
00212 
00213         public void setName(String name) {
00214             this.name = name;
00215         }
00216 
00217         public String getType() {
00218             return type;
00219         }
00220 
00221         public void setType(String type) {
00222             this.type = type;
00223         }
00224 
00225         public String getIs() {
00226             return is;
00227         }
00228 
00229         public void setIs(String is) {
00230             this.is = is;
00231         }
00232 
00233         public String getRead() {
00234             return read;
00235         }
00236 
00237         public void setRead(String read) {
00238             this.read = read;
00239         }
00240 
00241         public String getWrite() {
00242             return write;
00243         }
00244 
00245         public void setWrite(String write) {
00246             this.write = write;
00247         }
00248 
00249         public String getDescription() {
00250             return description;
00251         }
00252 
00253         public void setDescription(String description) {
00254             this.description = description;
00255         }
00256 
00257         public String getValue() {
00258             return value;
00259         }
00260 
00261         public void setValue(String value) {
00262             this.value = value;
00263         }
00264 
00265         public String getErrorMessage() {
00266             return errorMessage;
00267         }
00268 
00269         public void setErrorMessage(String errorMessage) {
00270             this.errorMessage = errorMessage;
00271         }
00272 
00273         public boolean isError() {
00274             return error;
00275         }
00276 
00277         public void setError(boolean error) {
00278             this.error = error;
00279         }
00280     }
00281 
00282     public class MBeanAttributesByName implements Comparator {
00283 
00284         public int compare(Object p_O1, Object p_O2) {
00285             ViewMBeanAttributes o1 = (ViewMBeanAttributes) p_O1;
00286             ViewMBeanAttributes o2 = (ViewMBeanAttributes) p_O2;
00287             return o1.getName().compareToIgnoreCase(o2.getName());
00288         }
00289 
00290         public boolean equals(Object p_Obj) {
00291             if (p_Obj instanceof ViewMBeanAttributes) {
00292                 return true;
00293             }
00294             return false;
00295         }
00296     }
00297 
00298 }

Generated on Tue Feb 15 15:05:28 2005 for JOnAS by  doxygen 1.3.9.1