1 25 26 package org.objectweb.jonas.webapp.jonasadmin.mbean; 27 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.Comparator ; 33 import java.util.Iterator ; 34 35 import javax.management.MBeanAttributeInfo ; 36 import javax.management.MBeanInfo ; 37 import javax.management.ObjectName ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 42 import org.apache.struts.action.ActionForm; 43 import org.apache.struts.action.ActionForward; 44 import org.apache.struts.action.ActionMapping; 45 import org.objectweb.jonas.jmx.JonasManagementRepr; 46 import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou; 47 48 53 54 public final class ListMBeanAttributesAction extends ListMBeanDetailsAction { 55 56 58 public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form 59 , HttpServletRequest p_Request, HttpServletResponse p_Response) 60 throws IOException , ServletException { 61 62 try { 63 setAction(ACTION_ATTRIBUTES); 65 String sSelect = p_Request.getParameter("select"); 67 68 ArrayList list = new ArrayList (); 70 ObjectName on = new ObjectName (sSelect); 72 MbeanItem oItem = MbeanItem.build(on); 73 MBeanInfo oMBeanInfo = JonasManagementRepr.getMBeanInfo(on); 74 MBeanAttributeInfo [] aoAttributes = oMBeanInfo.getAttributes(); 76 if (aoAttributes.length > 0) { 77 Object oValue; 78 String sError; 79 for (int i = 0; i < aoAttributes.length; i++) { 81 sError = null; 82 oValue = null; 83 try { 84 oValue = JonasManagementRepr.getAttribute(on, aoAttributes[i].getName()); 85 } 86 catch (Exception ex) { 87 sError = ex.getMessage(); 88 } 89 list.add(new ViewMBeanAttributes(aoAttributes[i], oValue, sError)); 90 } 91 Collections.sort(list, new MBeanAttributesByName()); 93 } 94 p_Request.setAttribute("MBean", oItem); 96 p_Request.setAttribute("MBeanAttributes", list); 97 MbeanFilteringForm oForm = (MbeanFilteringForm) m_Session.getAttribute( 99 "mbeanFilteringForm"); 100 if (oForm == null) { 101 oForm = new MbeanFilteringForm(); 102 oForm.reset(p_Mapping, p_Request); 103 m_Session.setAttribute("mbeanFilteringForm", oForm); 104 } 105 oForm.setSelectedName(sSelect); 106 StringBuffer sbBranch = new StringBuffer ("domain*mbeans"); 108 sbBranch.append(WhereAreYou.NODE_SEPARATOR); 109 sbBranch.append(sSelect); 110 m_WhereAreYou.selectNameNode(sbBranch.toString(), true); 111 } 112 catch (Throwable t) { 113 addGlobalError(t); 114 saveErrors(p_Request, m_Errors); 115 return (p_Mapping.findForward("Global Error")); 116 } 117 return p_Mapping.findForward("List MBean Attributes"); 119 } 120 121 123 public class ViewMBeanAttributes { 124 private String name; 125 private String type; 126 private String is; 127 private String read; 128 private String write; 129 private String description; 130 private String value; 131 private String errorMessage; 132 private boolean error; 133 134 public ViewMBeanAttributes(MBeanAttributeInfo po_Attr, Object po_Value, String ps_Error) { 135 setName(po_Attr.getName()); 136 setType(po_Attr.getType()); 137 setDescription(po_Attr.getDescription()); 138 setObjectValue(po_Value); 139 setErrorMessage(ps_Error); 140 if (ps_Error != null) { 141 setError(true); 142 } 143 if (po_Attr.isIs() == true) { 144 setIs("is"); 145 } 146 if (po_Attr.isReadable() == true) { 147 setRead("read"); 148 } 149 if (po_Attr.isWritable() == true) { 150 setWrite("write"); 151 } 152 } 153 154 public void setObjectValue(Object objectValue) { 155 if (objectValue == null) { 156 value = "null"; 157 } 158 else { 159 if (objectValue.getClass().isArray() == true) { 161 value = arrayToString((Object []) objectValue); 163 } 164 else { 165 try { 166 value = collectionToString((Collection ) objectValue); 168 } 169 catch (Exception e) { 170 value = objectValue.toString(); 172 } 173 } 174 } 175 } 176 177 public String arrayToString(Object [] p_Array) { 178 StringBuffer sb = new StringBuffer (); 179 sb.append("[ "); 180 for (int i = 0; i < p_Array.length; i++) { 181 if (p_Array[i] == null) { 182 sb.append("null"); 183 } 184 else { 185 sb.append(p_Array[i].toString()); 186 } 187 if ((i + 1) < p_Array.length) { 188 sb.append(" - "); 189 } 190 } 191 sb.append(" ]"); 192 return sb.toString(); 193 } 194 195 public String collectionToString(Collection p_Collection) { 196 StringBuffer sb = new StringBuffer (); 197 sb.append("[ "); 198 Iterator it = p_Collection.iterator(); 199 while (it.hasNext()) { 200 sb.append(it.next().toString()); 201 if (it.hasNext()) { 202 sb.append(" - "); 203 } 204 } 205 sb.append(" ]"); 206 return sb.toString(); 207 } 208 209 public String getName() { 210 return name; 211 } 212 213 public void setName(String name) { 214 this.name = name; 215 } 216 217 public String getType() { 218 return type; 219 } 220 221 public void setType(String type) { 222 this.type = type; 223 } 224 225 public String getIs() { 226 return is; 227 } 228 229 public void setIs(String is) { 230 this.is = is; 231 } 232 233 public String getRead() { 234 return read; 235 } 236 237 public void setRead(String read) { 238 this.read = read; 239 } 240 241 public String getWrite() { 242 return write; 243 } 244 245 public void setWrite(String write) { 246 this.write = write; 247 } 248 249 public String getDescription() { 250 return description; 251 } 252 253 public void setDescription(String description) { 254 this.description = description; 255 } 256 257 public String getValue() { 258 return value; 259 } 260 261 public void setValue(String value) { 262 this.value = value; 263 } 264 265 public String getErrorMessage() { 266 return errorMessage; 267 } 268 269 public void setErrorMessage(String errorMessage) { 270 this.errorMessage = errorMessage; 271 } 272 273 public boolean isError() { 274 return error; 275 } 276 277 public void setError(boolean error) { 278 this.error = error; 279 } 280 } 281 282 public class MBeanAttributesByName implements Comparator { 283 284 public int compare(Object p_O1, Object p_O2) { 285 ViewMBeanAttributes o1 = (ViewMBeanAttributes) p_O1; 286 ViewMBeanAttributes o2 = (ViewMBeanAttributes) p_O2; 287 return o1.getName().compareToIgnoreCase(o2.getName()); 288 } 289 290 public boolean equals(Object p_Obj) { 291 if (p_Obj instanceof ViewMBeanAttributes) { 292 return true; 293 } 294 return false; 295 } 296 } 297 298 } 299 | Popular Tags |