KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jmx > browser > web > action > UpdateAction


1 /*
2
3  * EJTools, the Enterprise Java Tools
4
5  *
6
7  * Distributable under LGPL license.
8
9  * See terms of license at www.gnu.org.
10
11  */

12
13 package org.ejtools.jmx.browser.web.action;
14
15
16
17 import java.beans.PropertyEditor;
18
19 import java.io.IOException;
20
21 import java.io.PrintWriter;
22
23 import java.io.StringWriter;
24
25 import java.util.Enumeration;
26
27 import java.util.Iterator;
28
29 import java.util.Locale;
30
31 import java.util.Vector;
32
33
34
35 import javax.management.Attribute;
36
37 import javax.servlet.ServletContext;
38
39 import javax.servlet.ServletException;
40
41 import javax.servlet.http.HttpServletRequest;
42
43 import javax.servlet.http.HttpServletResponse;
44
45
46
47 import org.apache.log4j.Logger;
48
49 import org.apache.struts.action.Action;
50
51 import org.apache.struts.action.ActionError;
52
53 import org.apache.struts.action.ActionErrors;
54
55 import org.apache.struts.action.ActionForm;
56
57 import org.apache.struts.action.ActionForward;
58
59 import org.apache.struts.action.ActionMapping;
60
61 import org.apache.struts.util.MessageResources;
62
63 import org.ejtools.jmx.browser.model.Resource;
64
65 import org.ejtools.jmx.browser.web.Constants;
66
67 import org.ejtools.jmx.browser.web.JMXContainer;
68
69 import org.ejtools.jmx.browser.web.form.DetailForm;
70
71
72
73 /**
74
75  * Description of the Class
76
77  *
78
79  * @author letiemble
80
81  * @created 12 novembre 2001
82
83  * @version $Revision: 1.6 $
84
85  * @todo Javadoc to complete
86
87  */

88
89 public class UpdateAction extends Action
90
91 {
92
93    /** Description of the Field */
94
95    private static Logger logger = Logger.getLogger(UpdateAction.class);
96
97
98
99
100
101    /** Constructor for the SearchLoginAction object */
102
103    public UpdateAction() { }
104
105
106
107
108
109    /**
110
111     * Description of the Method
112
113     *
114
115     * @param mapping Description of the Parameter
116
117     * @param form Description of the Parameter
118
119     * @param request Description of the Parameter
120
121     * @param response Description of the Parameter
122
123     * @return Description of the Return Value
124
125     * @exception IOException Description of the Exception
126
127     * @exception ServletException Description of the Exception
128
129     */

130
131    public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
132
133       throws IOException, ServletException
134
135    {
136
137       String reference = null;
138
139       Vector names = new Vector();
140
141
142
143       // Extract attributes we will need
144

145       Locale locale = getLocale(request);
146
147       MessageResources messages = getResources();
148
149
150
151       // Validate the request parameters specified by the user
152

153       ActionErrors errors = new ActionErrors();
154
155
156
157       if (form instanceof DetailForm)
158
159       {
160
161          reference = ((DetailForm) form).getReference();
162
163          logger.debug("ObjectName requested " + reference);
164
165       }
166
167       else
168
169       {
170
171          errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.no.form"));
172
173       }
174
175
176
177       ServletContext context = this.getServlet().getServletContext();
178
179       JMXContainer tree = (JMXContainer) context.getAttribute(Constants.TREE);
180
181
182
183       if (tree != null)
184
185       {
186
187          logger.debug("Tree root found => " + tree);
188
189          Resource res = (Resource) tree.searchObjectName(reference);
190
191
192
193          if (res != null)
194
195          {
196
197             Enumeration enum = request.getParameterNames();
198
199             while (enum.hasMoreElements())
200
201             {
202
203                String name = (String) enum.nextElement();
204
205                if (name.startsWith(":attribute:"))
206
207                {
208
209                   name = name.substring(":attribute:".length());
210
211                   names.add(name);
212
213                }
214
215             }
216
217
218
219             String name = null;
220
221             String editor = null;
222
223             String previous = null;
224
225             String value = null;
226
227
228
229             try
230
231             {
232
233                // names contains the list of attribute to set
234

235                Iterator it = names.iterator();
236
237                while (it.hasNext())
238
239                {
240
241                   name = (String) it.next();
242
243
244
245                   logger.debug("Processing attribute " + name);
246
247
248
249                   editor = request.getParameter(":editor:" + name);
250
251                   previous = request.getParameter(":previous:" + name);
252
253                   value = request.getParameter(":value:" + name);
254
255
256
257                   if (!value.equals(previous))
258
259                   {
260
261                      logger.debug("Attribute " + name + " has changed");
262
263
264
265                      PropertyEditor pe = (PropertyEditor) Thread.currentThread().getContextClassLoader().loadClass(editor).newInstance();
266
267
268
269                      pe.setAsText(value);
270
271                      Object o = pe.getValue();
272
273
274
275                      logger.debug("Setting attribute " + name);
276
277
278
279                      res.setAttribute(new Attribute(name, o));
280
281                   }
282
283                }
284
285             }
286
287             catch (Exception e)
288
289             {
290
291                logger.error("Exception occured " + e.getMessage());
292
293
294
295                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.mbean.attribute", name));
296
297                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.message", e.getMessage()));
298
299
300
301                StringWriter w = new StringWriter();
302
303                PrintWriter pw = new PrintWriter(w);
304
305                e.printStackTrace(pw);
306
307                pw.close();
308
309
310
311                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.stack", w.toString()));
312
313             }
314
315          }
316
317          else
318
319          {
320
321             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.no.mbean"));
322
323          }
324
325       }
326
327       else
328
329       {
330
331          errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.connect"));
332
333       }
334
335
336
337       // Report any errors we have discovered back to the original form
338

339       if (!errors.empty())
340
341       {
342
343          saveErrors(request, errors);
344
345          return (mapping.findForward("error"));
346
347       }
348
349
350
351       return (mapping.findForward("detail"));
352
353    }
354
355 }
356
357
358
359
Popular Tags