KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > mbean > ListMBeanAttributesAction


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ListMBeanAttributesAction.java,v 1.9 2005/03/15 14:45:29 danesa Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.mbean;
27
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.Comparator JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import javax.management.MBeanAttributeInfo JavaDoc;
36 import javax.management.MBeanInfo JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.servlet.ServletException JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
49  * List of all Attributes for a MBean.
50  *
51  * @author Michel-Ange ANTON
52  */

53
54 public final class ListMBeanAttributesAction extends ListMBeanDetailsAction {
55
56 // --------------------------------------------------------- Public Methods
57

58     public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
59         , HttpServletRequest JavaDoc p_Request, HttpServletResponse JavaDoc p_Response)
60         throws IOException JavaDoc, ServletException JavaDoc {
61
62         try {
63             // Save current action
64
setAction(ACTION_ATTRIBUTES);
65             // Parameter
66
String JavaDoc sSelect = p_Request.getParameter("select");
67
68             // Create a request attribute with our collection of MBeans
69
ArrayList JavaDoc list = new ArrayList JavaDoc();
70             // Get all infos of a MBean
71
ObjectName JavaDoc on = new ObjectName JavaDoc(sSelect);
72             MbeanItem oItem = MbeanItem.build(on);
73             MBeanInfo JavaDoc oMBeanInfo = JonasManagementRepr.getMBeanInfo(on);
74             // Get attributes infos
75
MBeanAttributeInfo JavaDoc[] aoAttributes = oMBeanInfo.getAttributes();
76             if (aoAttributes.length > 0) {
77                 Object JavaDoc oValue;
78                 String JavaDoc sError;
79                 // Loop to append each attribute node
80
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 JavaDoc ex) {
87                         sError = ex.getMessage();
88                     }
89                     list.add(new ViewMBeanAttributes(aoAttributes[i], oValue, sError));
90                 }
91                 // Sort
92
Collections.sort(list, new MBeanAttributesByName());
93             }
94             // Infos for displaying
95
p_Request.setAttribute("MBean", oItem);
96             p_Request.setAttribute("MBeanAttributes", list);
97             // Active and save filtering display if not exists
98
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             // Force the node selected in tree when the direct is used (MBeans list)
107
StringBuffer JavaDoc sbBranch = new StringBuffer JavaDoc("domain*mbeans");
108             sbBranch.append(WhereAreYou.NODE_SEPARATOR);
109             sbBranch.append(sSelect);
110             m_WhereAreYou.selectNameNode(sbBranch.toString(), true);
111         }
112         catch (Throwable JavaDoc t) {
113             addGlobalError(t);
114             saveErrors(p_Request, m_Errors);
115             return (p_Mapping.findForward("Global Error"));
116         }
117         // Forward to the corresponding display page
118
return p_Mapping.findForward("List MBean Attributes");
119     }
120
121 // --------------------------------------------------------- Inner Classes
122

123     public class ViewMBeanAttributes {
124         private String JavaDoc name;
125         private String JavaDoc type;
126         private String JavaDoc is;
127         private String JavaDoc read;
128         private String JavaDoc write;
129         private String JavaDoc description;
130         private String JavaDoc value;
131         private String JavaDoc errorMessage;
132         private boolean error;
133
134         public ViewMBeanAttributes(MBeanAttributeInfo JavaDoc po_Attr, Object JavaDoc po_Value, String JavaDoc 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 JavaDoc objectValue) {
155             if (objectValue == null) {
156                 value = "null";
157             }
158             else {
159                 // Detect Array or Collection
160
if (objectValue.getClass().isArray() == true) {
161                     // Array
162
value = arrayToString((Object JavaDoc[]) objectValue);
163                 }
164                 else {
165                     try {
166                         // Collection
167
value = collectionToString((Collection JavaDoc) objectValue);
168                     }
169                     catch (Exception JavaDoc e) {
170                         // Default
171
value = objectValue.toString();
172                     }
173                 }
174             }
175         }
176
177         public String JavaDoc arrayToString(Object JavaDoc[] p_Array) {
178             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc collectionToString(Collection JavaDoc p_Collection) {
196             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
197             sb.append("[ ");
198             Iterator JavaDoc 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 JavaDoc getName() {
210             return name;
211         }
212
213         public void setName(String JavaDoc name) {
214             this.name = name;
215         }
216
217         public String JavaDoc getType() {
218             return type;
219         }
220
221         public void setType(String JavaDoc type) {
222             this.type = type;
223         }
224
225         public String JavaDoc getIs() {
226             return is;
227         }
228
229         public void setIs(String JavaDoc is) {
230             this.is = is;
231         }
232
233         public String JavaDoc getRead() {
234             return read;
235         }
236
237         public void setRead(String JavaDoc read) {
238             this.read = read;
239         }
240
241         public String JavaDoc getWrite() {
242             return write;
243         }
244
245         public void setWrite(String JavaDoc write) {
246             this.write = write;
247         }
248
249         public String JavaDoc getDescription() {
250             return description;
251         }
252
253         public void setDescription(String JavaDoc description) {
254             this.description = description;
255         }
256
257         public String JavaDoc getValue() {
258             return value;
259         }
260
261         public void setValue(String JavaDoc value) {
262             this.value = value;
263         }
264
265         public String JavaDoc getErrorMessage() {
266             return errorMessage;
267         }
268
269         public void setErrorMessage(String JavaDoc 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 JavaDoc {
283
284         public int compare(Object JavaDoc p_O1, Object JavaDoc 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 JavaDoc p_Obj) {
291             if (p_Obj instanceof ViewMBeanAttributes) {
292                 return true;
293             }
294             return false;
295         }
296     }
297
298 }
299
Popular Tags