KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

132
133    public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
134
135       throws IOException JavaDoc, ServletException JavaDoc
136
137    {
138
139       JMXContainer tree = null;
140
141       SearchForm searchForm = null;
142
143
144
145       // Extract attributes we will need
146

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

155       ActionErrors errors = new ActionErrors();
156
157
158
159       if (form instanceof SearchForm)
160
161       {
162
163          searchForm = (SearchForm) form;
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       logger.debug("Connecting to JMX Server");
178
179
180
181       ServletContext JavaDoc context = this.getServlet().getServletContext();
182
183       tree = (JMXContainer) context.getAttribute(Constants.TREE);
184
185
186
187       context.setAttribute(Constants.SEARCH_RESULT, new Vector JavaDoc());
188
189
190
191       if (tree != null)
192
193       {
194
195          String JavaDoc filter = searchForm.getFilter();
196
197          String JavaDoc attribute = searchForm.getAttribute();
198
199          String JavaDoc query = searchForm.getQuery();
200
201          String JavaDoc value = searchForm.getValue();
202
203          String JavaDoc type = searchForm.getType();
204
205
206
207          try
208
209          {
210
211             ObjectName JavaDoc name = null;
212
213             QueryExp JavaDoc qExp = null;
214
215
216
217             if (!("".equals(filter)))
218
219             {
220
221                name = new ObjectName JavaDoc(filter);
222
223             }
224
225
226
227             logger.debug("ObjectName " + name);
228
229
230
231             if (!("".equals(attribute)))
232
233             {
234
235                AttributeValueExp JavaDoc attrExp = Query.attr(attribute);
236
237                ValueExp JavaDoc valExp = computeValue(type, value);
238
239
240
241                qExp = computeQuery(query, attrExp, valExp);
242
243             }
244
245
246
247             logger.debug("QueryExp " + qExp);
248
249
250
251             Set JavaDoc result = tree.query(name, qExp);
252
253
254
255             if (result != null)
256
257             {
258
259                logger.debug("Result " + result);
260
261
262
263                context.setAttribute(Constants.SEARCH_RESULT, result);
264
265             }
266
267
268
269          }
270
271          catch (Exception JavaDoc e)
272
273          {
274
275             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.query.filter", filter));
276
277             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.message", e.getMessage()));
278
279
280
281             StringWriter JavaDoc w = new StringWriter JavaDoc();
282
283             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(w);
284
285             e.printStackTrace(pw);
286
287             pw.close();
288
289
290
291             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.stack", w.toString()));
292
293          }
294
295       }
296
297       else
298
299       {
300
301          errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.connect"));
302
303       }
304
305
306
307       // Report any errors we have discovered back to the original form
308

309       if (!errors.empty())
310
311       {
312
313          saveErrors(request, errors);
314
315          return (new ActionForward(mapping.getInput()));
316
317       }
318
319
320
321       return (mapping.findForward("view"));
322
323    }
324
325
326
327
328
329    /**
330
331     * Description of the Method
332
333     *
334
335     * @param op Description of the Parameter
336
337     * @param value1 Description of the Parameter
338
339     * @param value2 Description of the Parameter
340
341     * @return Description of the Return Value
342
343     */

344
345    protected QueryExp JavaDoc computeQuery(String JavaDoc op, ValueExp JavaDoc value1, ValueExp JavaDoc value2)
346
347    {
348
349       if ("=".equals(op))
350
351       {
352
353          return Query.eq(value1, value2);
354
355       }
356
357       if (">".equals(op))
358
359       {
360
361          return Query.gt(value1, value2);
362
363       }
364
365       if (">=".equals(op))
366
367       {
368
369          return Query.geq(value1, value2);
370
371       }
372
373       if ("<".equals(op))
374
375       {
376
377          return Query.lt(value1, value2);
378
379       }
380
381       if ("<=".equals(op))
382
383       {
384
385          return Query.leq(value1, value2);
386
387       }
388
389       return null;
390
391    }
392
393
394
395
396
397    /**
398
399     * Description of the Method
400
401     *
402
403     * @param type Description of the Parameter
404
405     * @param value Description of the Parameter
406
407     * @return Description of the Return Value
408
409     * @exception Exception Description of the Exception
410
411     */

412
413    protected ValueExp JavaDoc computeValue(String JavaDoc type, String JavaDoc value)
414
415       throws Exception JavaDoc
416
417    {
418
419       logger.debug("Converting " + value + " of type " + type);
420
421
422
423       if ("java.lang.String".equals(type))
424
425       {
426
427          return Query.value(value);
428
429       }
430
431
432
433       if ("java.lang.Integer".equals(type))
434
435       {
436
437          return Query.value(new Integer JavaDoc(value));
438
439       }
440
441       if ("java.lang.Long".equals(type))
442
443       {
444
445          return Query.value(new Long JavaDoc(value));
446
447       }
448
449       if ("java.lang.Float".equals(type))
450
451       {
452
453          return Query.value(new Float JavaDoc(value));
454
455       }
456
457       if ("java.lang.Double".equals(type))
458
459       {
460
461          return Query.value(new Double JavaDoc(value));
462
463       }
464
465       if ("java.lang.Boolean".equals(type))
466
467       {
468
469          return Query.value((new Boolean JavaDoc(value)).booleanValue());
470
471       }
472
473
474
475       if ("byte".equals(type))
476
477       {
478
479          return Query.value(Byte.parseByte(value));
480
481       }
482
483       if ("short".equals(type))
484
485       {
486
487          return Query.value(Short.parseShort(value));
488
489       }
490
491       if ("int".equals(type))
492
493       {
494
495          return Query.value(Integer.parseInt(value));
496
497       }
498
499       if ("long".equals(type))
500
501       {
502
503          return Query.value(Long.parseLong(value));
504
505       }
506
507       if ("float".equals(type))
508
509       {
510
511          return Query.value(Float.parseFloat(value));
512
513       }
514
515       if ("double".equals(type))
516
517       {
518
519          return Query.value(Double.parseDouble(value));
520
521       }
522
523       if ("boolean".equals(type))
524
525       {
526
527          return Query.value((new Boolean JavaDoc(value)).booleanValue());
528
529       }
530
531       return null;
532
533    }
534
535 }
536
537
Popular Tags