00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00064 setAction(ACTION_ATTRIBUTES);
00065
00066 String sSelect = p_Request.getParameter("select");
00067
00068
00069 ArrayList list = new ArrayList();
00070
00071 ObjectName on = new ObjectName(sSelect);
00072 MbeanItem oItem = MbeanItem.build(on);
00073 MBeanInfo oMBeanInfo = JonasManagementRepr.getMBeanInfo(on);
00074
00075 MBeanAttributeInfo[] aoAttributes = oMBeanInfo.getAttributes();
00076 if (aoAttributes.length > 0) {
00077 Object oValue;
00078 String sError;
00079
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
00092 Collections.sort(list, new MBeanAttributesByName());
00093 }
00094
00095 p_Request.setAttribute("MBean", oItem);
00096 p_Request.setAttribute("MBeanAttributes", list);
00097
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
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
00118 return p_Mapping.findForward("List MBean Attributes");
00119 }
00120
00121
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
00160 if (objectValue.getClass().isArray() == true) {
00161
00162 value = arrayToString((Object[]) objectValue);
00163 }
00164 else {
00165 try {
00166
00167 value = collectionToString((Collection) objectValue);
00168 }
00169 catch (Exception e) {
00170
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 }