KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jmx > browser > web > taglib > MBeanResultEditorTag


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.taglib;
14
15
16
17 import java.beans.PropertyEditor JavaDoc;
18
19 import java.util.Hashtable JavaDoc;
20
21
22
23 import javax.servlet.jsp.JspException JavaDoc;
24
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26
27
28
29 import org.apache.log4j.Logger;
30
31 import org.apache.struts.util.RequestUtils;
32
33 import org.apache.struts.util.ResponseUtils;
34
35 import org.ejtools.beans.CustomPropertyEditorManager;
36
37
38
39 /**
40
41  * Description of the Class
42
43  *
44
45  * @author letiemble
46
47  * @created 25 avril 2002
48
49  * @version $Revision: 1.6 $
50
51  * @todo Javadoc to complete
52
53  * @jsp:tag name="mbeanResultEditor" body-content="empty"
54
55  */

56
57 public class MBeanResultEditorTag extends TagSupport JavaDoc
58
59 {
60
61    /** Filter the rendered output for characters that are sensitive in HTML? */
62
63    protected boolean filter = true;
64
65    /** Should we ignore missing beans and simply output nothing? */
66
67    protected boolean ignore = false;
68
69    /** Name of the bean that contains the data we will be rendering. */
70
71    protected String JavaDoc name = null;
72
73    /** Description of the Field */
74
75    protected String JavaDoc page = "/detail.do";
76
77    /** The scope to be searched to retrieve the specified bean. */
78
79    protected String JavaDoc scope = null;
80
81    /** Description of the Field */
82
83    private static Logger logger = Logger.getLogger(MBeanAttributeEditorTag.class);
84
85
86
87
88
89    /**
90
91     * Description of the Method
92
93     *
94
95     * @return Description of the Returned Value
96
97     * @exception JspException Description of the Exception
98
99     */

100
101    public int doStartTag()
102
103       throws JspException JavaDoc
104
105    {
106
107       // Look up the requested bean (if necessary)
108

109       Object JavaDoc result = RequestUtils.lookup(pageContext, name, scope);
110
111
112
113       if (ignore)
114
115       {
116
117          if (result == null)
118
119          {
120
121             return (SKIP_BODY);
122
123          }
124
125          // Nothing to output
126

127       }
128
129
130
131       PropertyEditor JavaDoc propertyeditor = null;
132
133       Class JavaDoc c = result.getClass();
134
135
136
137       if (c.isArray())
138
139       {
140
141          Class JavaDoc componentType = c.getComponentType();
142
143          propertyeditor = CustomPropertyEditorManager.findEditor(componentType);
144
145
146
147          if (propertyeditor == null)
148
149          {
150
151             propertyeditor = CustomPropertyEditorManager.findEditor(String JavaDoc.class);
152
153          }
154
155          addArrayResult(result, propertyeditor, componentType);
156
157       }
158
159       else
160
161       {
162
163          propertyeditor = CustomPropertyEditorManager.findEditor(c);
164
165
166
167          if (propertyeditor == null)
168
169          {
170
171             propertyeditor = CustomPropertyEditorManager.findEditor(String JavaDoc.class);
172
173          }
174
175          addResult(result, propertyeditor);
176
177       }
178
179
180
181       // Continue processing this page
182

183       return (SKIP_BODY);
184
185    }
186
187
188
189
190
191    /**
192
193     * Getter for the filter attribute
194
195     *
196
197     * @return The value of filter attribute
198
199     * @jsp:attribute name="filter" required="false" rtexprvalue="true"
200
201     */

202
203    public boolean getFilter()
204
205    {
206
207       return (this.filter);
208
209    }
210
211
212
213
214
215    /**
216
217     * Getter for the ignore attribute
218
219     *
220
221     * @return The value of ignore attribute
222
223     * @jsp:attribute name="ignore" required="false" rtexprvalue="true"
224
225     */

226
227    public boolean getIgnore()
228
229    {
230
231       return (this.ignore);
232
233    }
234
235
236
237
238
239    /**
240
241     * Getter for the name attribute
242
243     *
244
245     * @return The value of name attribute
246
247     * @jsp:attribute name="name" required="true" rtexprvalue="true"
248
249     */

250
251    public String JavaDoc getName()
252
253    {
254
255       return (this.name);
256
257    }
258
259
260
261
262
263    /**
264
265     * Getter for the scope attribute
266
267     *
268
269     * @return The value of scope attribute
270
271     * @jsp:attribute name="scope" required="false" rtexprvalue="true"
272
273     */

274
275    public String JavaDoc getScope()
276
277    {
278
279       return (this.scope);
280
281    }
282
283
284
285
286
287    /** Release all allocated resources. */
288
289    public void release()
290
291    {
292
293       super.release();
294
295       filter = true;
296
297       ignore = false;
298
299       name = null;
300
301       scope = null;
302
303    }
304
305
306
307
308
309    /**
310
311     * Setter for the filter attribute
312
313     *
314
315     * @param filter The new filter value
316
317     */

318
319    public void setFilter(boolean filter)
320
321    {
322
323       this.filter = filter;
324
325    }
326
327
328
329
330
331    /**
332
333     * Setter for the ignore attribute
334
335     *
336
337     * @param ignore The new ignore value
338
339     */

340
341    public void setIgnore(boolean ignore)
342
343    {
344
345       this.ignore = ignore;
346
347    }
348
349
350
351
352
353    /**
354
355     * Setter for the name attribute
356
357     *
358
359     * @param name The new name value
360
361     */

362
363    public void setName(String JavaDoc name)
364
365    {
366
367       this.name = name;
368
369    }
370
371
372
373
374
375    /**
376
377     * Setter for the scope attribute
378
379     *
380
381     * @param scope The new scope value
382
383     */

384
385    public void setScope(String JavaDoc scope)
386
387    {
388
389       this.scope = scope;
390
391    }
392
393
394
395
396
397    /**
398
399     * Adds a feature to the ArrayProperty attribute of the MBeanAttributeEditorTag object
400
401     *
402
403     * @param result The feature to be added to the ArrayResult attribute
404
405     * @param propertyeditor The feature to be added to the ArrayResult attribute
406
407     * @param componentType The feature to be added to the ArrayResult attribute
408
409     * @exception JspException Description of the Exception
410
411     */

412
413    protected void addArrayResult(Object JavaDoc result, PropertyEditor JavaDoc propertyeditor, Class JavaDoc componentType)
414
415       throws JspException JavaDoc
416
417    {
418
419       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
420
421       Object JavaDoc[] array = null;
422
423
424
425       logger.debug("addArrayResult " + propertyeditor + " " + result.getClass().getName());
426
427
428
429       try
430
431       {
432
433          array = (Object JavaDoc[]) result;
434
435       }
436
437       catch (Throwable JavaDoc t)
438
439       {
440
441          logger.warn("Exception during result array reading " + t.getMessage());
442
443       }
444
445
446
447       if (array != null)
448
449       {
450
451          String JavaDoc output = "";
452
453
454
455          for (int i = 0; i < array.length; i++)
456
457          {
458
459             try
460
461             {
462
463                PropertyEditor JavaDoc propertyeditor1 = (PropertyEditor JavaDoc) propertyeditor.getClass().newInstance();
464
465                propertyeditor1.setValue(array[i]);
466
467
468
469                output = propertyeditor1.getAsText();
470
471
472
473                if (filter)
474
475                {
476
477                   output = ResponseUtils.filter(output);
478
479                }
480
481
482
483                output = computeReadableEditor(result.getClass().getName(), output);
484
485
486
487                buffer.append(output);
488
489                buffer.append("<br/>");
490
491             }
492
493             catch (Exception JavaDoc e)
494
495             {
496
497                logger.warn("Exception during array output " + e.getMessage());
498
499             }
500
501          }
502
503          ResponseUtils.write(pageContext, buffer.toString());
504
505       }
506
507    }
508
509
510
511
512
513    /**
514
515     * Adds a feature to the UnsupportedProperty attribute of the MBeanAttributeEditorTag object
516
517     *
518
519     * @param result The feature to be added to the Result attribute
520
521     * @param propertyeditor The feature to be added to the Result attribute
522
523     * @exception JspException Description of the Exception
524
525     */

526
527    protected void addResult(Object JavaDoc result, PropertyEditor JavaDoc propertyeditor)
528
529       throws JspException JavaDoc
530
531    {
532
533       String JavaDoc output = "";
534
535
536
537       logger.debug("addArrayResult " + propertyeditor + " " + result.getClass().getName());
538
539
540
541       try
542
543       {
544
545          propertyeditor.setValue(result);
546
547       }
548
549       catch (Throwable JavaDoc t)
550
551       {
552
553          logger.warn("Exception during result reading " + t.getMessage());
554
555       }
556
557
558
559       output = propertyeditor.getAsText();
560
561
562
563       if (filter)
564
565       {
566
567          output = ResponseUtils.filter(output);
568
569       }
570
571
572
573       output = computeReadableEditor(result.getClass().getName(), output);
574
575
576
577       ResponseUtils.write(pageContext, output);
578
579    }
580
581
582
583
584
585    /**
586
587     * Description of the Method
588
589     *
590
591     * @param type Description of the Parameter
592
593     * @param value Description of the Parameter
594
595     * @return Description of the Return Value
596
597     */

598
599    protected String JavaDoc computeReadableEditor(String JavaDoc type, String JavaDoc value)
600
601    {
602
603       if ((value == null) || ("".equals(value)) || ("null".equals(value)))
604
605       {
606
607          return value;
608
609       }
610
611
612
613       if ("javax.management.ObjectName".equals(type))
614
615       {
616
617          try
618
619          {
620
621             Hashtable JavaDoc params = new Hashtable JavaDoc();
622
623             params.put("reference", value);
624
625
626
627             String JavaDoc url = RequestUtils.computeURL(pageContext, null, null, page, params, null, false);
628
629
630
631             return ("<a HREF=\"" + url + "\">" + value + "</a>");
632
633          }
634
635          catch (Exception JavaDoc e)
636
637          {
638
639             logger.warn("Exception during computation of readable editor " + e.getMessage());
640
641          }
642
643       }
644
645
646
647       return value;
648
649    }
650
651 }
652
653
Popular Tags