KickJava   Java API By Example, From Geeks To Geeks.

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


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.Locale;
28
29 import java.util.Vector;
30
31
32
33 import javax.servlet.ServletContext;
34
35 import javax.servlet.ServletException;
36
37 import javax.servlet.http.HttpServletRequest;
38
39 import javax.servlet.http.HttpServletResponse;
40
41
42
43 import org.apache.log4j.Logger;
44
45 import org.apache.struts.action.Action;
46
47 import org.apache.struts.action.ActionError;
48
49 import org.apache.struts.action.ActionErrors;
50
51 import org.apache.struts.action.ActionForm;
52
53 import org.apache.struts.action.ActionForward;
54
55 import org.apache.struts.action.ActionMapping;
56
57 import org.apache.struts.util.MessageResources;
58
59 import org.ejtools.jmx.browser.model.Resource;
60
61 import org.ejtools.jmx.browser.web.Constants;
62
63 import org.ejtools.jmx.browser.web.JMXContainer;
64
65
66
67 /**
68
69  * Description of the Class
70
71  *
72
73  * @author letiemble
74
75  * @created 12 novembre 2001
76
77  * @version $Revision: 1.6 $
78
79  * @todo Javadoc to complete
80
81  */

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

126
127    public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
128
129       throws IOException, ServletException
130
131    {
132
133       String reference = null;
134
135       String operation = null;
136
137       String type = null;
138
139       Vector parameters = new Vector();
140
141       String[] parameterTypes;
142
143       Object[] parameterValues;
144
145       Object result = null;
146
147
148
149       // Extract attributes we will need
150

151       Locale locale = getLocale(request);
152
153       MessageResources messages = getResources();
154
155
156
157       // Validate the request parameters specified by the user
158

159       ActionErrors errors = new ActionErrors();
160
161
162
163       reference = request.getParameter("reference");
164
165       logger.debug("ObjectName requested " + reference);
166
167       operation = request.getParameter("operation");
168
169       logger.debug("Operation requested " + operation);
170
171       type = request.getParameter("type");
172
173       logger.debug("Operation of type " + type);
174
175
176
177       ServletContext context = this.getServlet().getServletContext();
178
179       JMXContainer tree = (JMXContainer) context.getAttribute(Constants.TREE);
180
181
182
183       context.setAttribute("operation.name", "" + operation);
184
185       context.setAttribute("operation.type", "" + type);
186
187
188
189       if (tree != null)
190
191       {
192
193          logger.debug("Tree root found => " + tree);
194
195          Resource res = (Resource) tree.searchObjectName(reference);
196
197
198
199          if (res != null)
200
201          {
202
203             context.setAttribute(Constants.DETAIL, res);
204
205             logger.debug("MBean found => " + res);
206
207             context.setAttribute(Constants.DETAIL_INFO, res.getMBeanInfo());
208
209             logger.debug("MBeanInfo found => " + res.getMBeanInfo());
210
211          }
212
213          else
214
215          {
216
217             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.no.mbean"));
218
219          }
220
221
222
223          if (res != null)
224
225          {
226
227             try
228
229             {
230
231                Enumeration enum = request.getParameterNames();
232
233                while (enum.hasMoreElements())
234
235                {
236
237                   String name = (String) enum.nextElement();
238
239
240
241                   if (name.startsWith(":parameter:"))
242
243                   {
244
245                      name = name.substring(":parameter:".length());
246
247                      parameters.add(name);
248
249                   }
250
251                }
252
253
254
255                parameterTypes = new String[parameters.size()];
256
257                parameterValues = new Object[parameters.size()];
258
259
260
261                for (int i = 0; i < parameters.size(); i++)
262
263                {
264
265                   String name = (String) parameters.elementAt(i);
266
267                   String clazz = null;
268
269                   String editor = null;
270
271                   String value = null;
272
273
274
275                   int position = Integer.parseInt(name);
276
277
278
279                   clazz = request.getParameter(":parameter:" + name);
280
281                   editor = request.getParameter(":editor:" + name);
282
283                   value = request.getParameter(":value:" + name);
284
285
286
287                   logger.debug("Processing parameter " + name);
288
289                   logger.debug("Editor for parameter " + name + " " + editor);
290
291                   logger.debug("Value for parameter " + name + " " + value);
292
293
294
295                   PropertyEditor pe = (PropertyEditor) Thread.currentThread().getContextClassLoader().loadClass(editor).newInstance();
296
297                   pe.setAsText(value);
298
299                   Object o = pe.getValue();
300
301
302
303                   logger.debug("Value for parameter " + o);
304
305
306
307                   parameterTypes[position] = clazz;
308
309                   parameterValues[position] = o;
310
311                }
312
313
314
315                logger.debug("Parameters :");
316
317                for (int i = 0; i < parameterTypes.length; i++)
318
319                {
320
321                   logger.debug("> Parameter " + i + " (" + parameterTypes[i] + ") = <" + parameterValues[i] + ">");
322
323                }
324
325
326
327                result = res.invoke(operation, parameterValues, parameterTypes);
328
329             }
330
331             catch (Exception e)
332
333             {
334
335                logger.error("Exception occured " + e.getMessage());
336
337
338
339                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.message", e.getMessage()));
340
341
342
343                StringWriter w = new StringWriter();
344
345                PrintWriter pw = new PrintWriter(w);
346
347                e.printStackTrace(pw);
348
349                pw.close();
350
351
352
353                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.stack", w.toString()));
354
355             }
356
357          }
358
359          else
360
361          {
362
363             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.no.mbean"));
364
365          }
366
367       }
368
369       else
370
371       {
372
373          errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.connect"));
374
375       }
376
377
378
379       // Report any errors we have discovered back to the original form
380

381       if (!errors.empty())
382
383       {
384
385          saveErrors(request, errors);
386
387          context.removeAttribute(Constants.INVOCATION_RESULT);
388
389
390
391          return (mapping.findForward("error"));
392
393       }
394
395
396
397       logger.debug("Result from invocation :");
398
399       logger.debug("----------------------------------------");
400
401       logger.debug("" + result);
402
403       logger.debug("----------------------------------------");
404
405
406
407       if ("void".equals(type))
408
409       {
410
411          context.setAttribute(Constants.INVOCATION_RESULT, "Void");
412
413       }
414
415       else
416
417       {
418
419          context.setAttribute(Constants.INVOCATION_RESULT, "" + result);
420
421       }
422
423
424
425       return (mapping.findForward("result"));
426
427    }
428
429 }
430
431
Popular Tags