KickJava   Java API By Example, From Geeks To Geeks.

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


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.management.MBeanAttributeInfo JavaDoc;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
28
29
30
31 import org.apache.log4j.Logger;
32
33 import org.apache.struts.util.MessageResources;
34
35 import org.apache.struts.util.RequestUtils;
36
37 import org.apache.struts.util.ResponseUtils;
38
39 import org.ejtools.beans.CustomPropertyEditorManager;
40
41 import org.ejtools.jmx.MBeanAccessor;
42
43 import org.ejtools.util.ClassTools;
44
45
46
47 /**
48
49  * Description of the Class
50
51  *
52
53  * @author letiemble
54
55  * @created 25 avril 2002
56
57  * @version $Revision: 1.6 $
58
59  * @todo Javadoc to complete
60
61  * @jsp:tag name="mbeanAttributeEditor" body-content="empty"
62
63  */

64
65 public class MBeanAttributeEditorTag extends TagSupport JavaDoc
66
67 {
68
69    /** Filter the rendered output for characters that are sensitive in HTML? */
70
71    protected boolean filter = true;
72
73    /** Should we ignore missing beans and simply output nothing? */
74
75    protected boolean ignore = false;
76
77    /** Name of the bean that contains the data we will be rendering. */
78
79    protected String JavaDoc name = null;
80
81    /** Description of the Field */
82
83    protected String JavaDoc page = "/detail.do";
84
85    /** Name of the property to be accessed on the specified bean. */
86
87    protected String JavaDoc property = null;
88
89    /** The scope to be searched to retrieve the specified bean. */
90
91    protected String JavaDoc scope = null;
92
93    /** Description of the Field */
94
95    private static Logger logger = Logger.getLogger(MBeanAttributeEditorTag.class);
96
97    /** Description of the Field */
98
99    private static MessageResources messages = MessageResources.getMessageResources("org.ejtools.jmx.browser.web.taglib.MBeanAttributeInfoEditor");
100
101
102
103
104
105
106
107    /**
108
109     * Description of the Method
110
111     *
112
113     * @return Description of the Returned Value
114
115     * @exception JspException Description of the Exception
116
117     */

118
119    public int doStartTag()
120
121       throws JspException JavaDoc
122
123    {
124
125       // Look up the requested bean (if necessary)
126

127       MBeanAccessor bean = (MBeanAccessor) RequestUtils.lookup(pageContext, name, scope);
128
129       if (ignore)
130
131       {
132
133          if (bean == null)
134
135          {
136
137             return (SKIP_BODY);
138
139          }
140
141          // Nothing to output
142

143       }
144
145
146
147       MBeanAttributeInfo JavaDoc info = (MBeanAttributeInfo JavaDoc) RequestUtils.lookup(pageContext, property, scope);
148
149
150
151       PropertyEditor JavaDoc propertyeditor = null;
152
153       Class JavaDoc c = ClassTools.getClass(info.getType());
154
155
156
157       logger.debug("Class is " + c);
158
159
160
161       if (c == null)
162
163       {
164
165          this.addUnsupportedProperty(info);
166
167       }
168
169       else
170
171       {
172
173          if (c.isArray())
174
175          {
176
177             Class JavaDoc componentType = c.getComponentType();
178
179             propertyeditor = CustomPropertyEditorManager.findEditor(componentType);
180
181
182
183             if (propertyeditor == null)
184
185             {
186
187                propertyeditor = CustomPropertyEditorManager.findEditor(String JavaDoc.class);
188
189             }
190
191             this.addArrayProperty(bean, propertyeditor, info, componentType);
192
193          }
194
195          else
196
197          {
198
199             propertyeditor = CustomPropertyEditorManager.findEditor(c);
200
201
202
203             if (propertyeditor == null)
204
205             {
206
207                propertyeditor = CustomPropertyEditorManager.findEditor(String JavaDoc.class);
208
209             }
210
211             this.addProperty(bean, propertyeditor, info);
212
213          }
214
215       }
216
217
218
219       // Continue processing this page
220

221       return (SKIP_BODY);
222
223    }
224
225
226
227
228
229    /**
230
231     * Getter for the filter attribute
232
233     *
234
235     * @return The value of filter attribute
236
237     * @jsp:attribute name="filter" required="false" rtexprvalue="true"
238
239     */

240
241    public boolean getFilter()
242
243    {
244
245       return (this.filter);
246
247    }
248
249
250
251
252
253    /**
254
255     * Getter for the ignore attribute
256
257     *
258
259     * @return The value of ignore attribute
260
261     * @jsp:attribute name="ignore" required="false" rtexprvalue="true"
262
263     */

264
265    public boolean getIgnore()
266
267    {
268
269       return (this.ignore);
270
271    }
272
273
274
275
276
277    /**
278
279     * Getter for the name attribute
280
281     *
282
283     * @return The value of name attribute
284
285     * @jsp:attribute name="name" required="true" rtexprvalue="true"
286
287     */

288
289    public String JavaDoc getName()
290
291    {
292
293       return (this.name);
294
295    }
296
297
298
299
300
301    /**
302
303     * Getter for the property attribute
304
305     *
306
307     * @return The value of property attribute
308
309     * @jsp:attribute name="property" required="true" rtexprvalue="true"
310
311     */

312
313    public String JavaDoc getProperty()
314
315    {
316
317       return (this.property);
318
319    }
320
321
322
323
324
325    /**
326
327     * Getter for the scope attribute
328
329     *
330
331     * @return The value of scope attribute
332
333     * @jsp:attribute name="scope" required="false" rtexprvalue="true"
334
335     */

336
337    public String JavaDoc getScope()
338
339    {
340
341       return (this.scope);
342
343    }
344
345
346
347
348
349    /** Release all allocated resources. */
350
351    public void release()
352
353    {
354
355       super.release();
356
357       filter = true;
358
359       ignore = false;
360
361       name = null;
362
363       scope = null;
364
365       property = null;
366
367    }
368
369
370
371
372
373    /**
374
375     * Setter for the filter attribute
376
377     *
378
379     * @param filter The new filter value
380
381     */

382
383    public void setFilter(boolean filter)
384
385    {
386
387       this.filter = filter;
388
389    }
390
391
392
393
394
395    /**
396
397     * Setter for the ignore attribute
398
399     *
400
401     * @param ignore The new ignore value
402
403     */

404
405    public void setIgnore(boolean ignore)
406
407    {
408
409       this.ignore = ignore;
410
411    }
412
413
414
415
416
417    /**
418
419     * Setter for the name attribute
420
421     *
422
423     * @param name The new name value
424
425     */

426
427    public void setName(String JavaDoc name)
428
429    {
430
431       this.name = name;
432
433    }
434
435
436
437
438
439    /**
440
441     * Setter for the property attribute
442
443     *
444
445     * @param property The new property value
446
447     */

448
449    public void setProperty(String JavaDoc property)
450
451    {
452
453       this.property = property;
454
455    }
456
457
458
459
460
461    /**
462
463     * Setter for the scope attribute
464
465     *
466
467     * @param scope The new scope value
468
469     */

470
471    public void setScope(String JavaDoc scope)
472
473    {
474
475       this.scope = scope;
476
477    }
478
479
480
481
482
483    /**
484
485     * Adds a feature to the ArrayProperty attribute of the MBeanAttributeEditorTag object
486
487     *
488
489     * @param object The feature to be added to the ArrayProperty attribute
490
491     * @param propertyeditor The feature to be added to the ArrayProperty attribute
492
493     * @param info The feature to be added to the ArrayProperty attribute
494
495     * @param componentType The feature to be added to the ArrayProperty attribute
496
497     * @exception JspException Description of the Exception
498
499     */

500
501    protected void addArrayProperty(MBeanAccessor object, PropertyEditor JavaDoc propertyeditor, MBeanAttributeInfo JavaDoc info, Class JavaDoc componentType)
502
503       throws JspException JavaDoc
504
505    {
506
507       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
508
509       Object JavaDoc obj = null;
510
511       Object JavaDoc[] array = null;
512
513
514
515       logger.debug("addArrayProperty " + propertyeditor + " " + info.getName());
516
517
518
519       try
520
521       {
522
523          if (info.isReadable())
524
525          {
526
527             obj = object.getAttribute(info.getName());
528
529          }
530
531          if (obj != null)
532
533          {
534
535             array = (Object JavaDoc[]) obj;
536
537          }
538
539       }
540
541       catch (Throwable JavaDoc t)
542
543       {
544
545          logger.warn("Exception during array reading " + t.getMessage());
546
547       }
548
549
550
551       if (array != null)
552
553       {
554
555          String JavaDoc output = "";
556
557
558
559          for (int i = 0; i < array.length; i++)
560
561          {
562
563             try
564
565             {
566
567                PropertyEditor JavaDoc propertyeditor1 = (PropertyEditor JavaDoc) propertyeditor.getClass().newInstance();
568
569                propertyeditor1.setValue(array[i]);
570
571
572
573                output = propertyeditor1.getAsText();
574
575
576
577                if (filter)
578
579                {
580
581                   output = ResponseUtils.filter(output);
582
583                }
584
585
586
587                output = computeReadableEditor(componentType.getName(), output);
588
589
590
591                buffer.append(output);
592
593                buffer.append("<br/>");
594
595             }
596
597             catch (Exception JavaDoc e)
598
599             {
600
601                logger.warn("Exception during array output " + e.getMessage());
602
603             }
604
605          }
606
607          ResponseUtils.write(pageContext, buffer.toString());
608
609       }
610
611    }
612
613
614
615
616
617    /**
618
619     * Adds a feature to the UnsupportedProperty attribute of the MBeanAttributeEditorTag object
620
621     *
622
623     * @param object The feature to be added to the Property attribute
624
625     * @param propertyeditor The feature to be added to the Property attribute
626
627     * @param info The feature to be added to the Property attribute
628
629     * @exception JspException Description of the Exception
630
631     */

632
633    protected void addProperty(MBeanAccessor object, PropertyEditor JavaDoc propertyeditor, MBeanAttributeInfo JavaDoc info)
634
635       throws JspException JavaDoc
636
637    {
638
639       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
640
641       Object JavaDoc value = null;
642
643       String JavaDoc output = "";
644
645
646
647       logger.debug("addProperty " + propertyeditor + " " + info.getName());
648
649
650
651       try
652
653       {
654
655          if (info.isReadable())
656
657          {
658
659             value = object.getAttribute(info.getName());
660
661             propertyeditor.setValue(value);
662
663          }
664
665       }
666
667       catch (Throwable JavaDoc t)
668
669       {
670
671          logger.warn("Exception during property reading " + t.getMessage());
672
673       }
674
675
676
677       if (info.isWritable())
678
679       {
680
681          buffer.append("<input type=\"hidden\" name=\":attribute:");
682
683          buffer.append(info.getName());
684
685          buffer.append("\" value=\"");
686
687          buffer.append(info.getType());
688
689          buffer.append("\"/>");
690
691
692
693          buffer.append("<input type=\"hidden\" name=\":editor:");
694
695          buffer.append(info.getName());
696
697          buffer.append("\" value=\"");
698
699          buffer.append(propertyeditor.getClass().getName());
700
701          buffer.append("\"/>");
702
703
704
705          buffer.append("<input type=\"hidden\" name=\":previous:");
706
707          buffer.append(info.getName());
708
709          buffer.append("\" value=\"");
710
711
712
713          if (value != null)
714
715          {
716
717             buffer.append(value);
718
719          }
720
721          buffer.append("\"/>");
722
723
724
725          // Boolean types
726

727          if (("java.lang.Boolean".equals(info.getType())) || ("boolean".equals(info.getType())))
728
729          {
730
731             buffer.append("<select name=\":value:");
732
733             buffer.append(info.getName());
734
735             buffer.append("\">");
736
737
738
739             buffer.append("<option value=\"true\" ");
740
741             if (Boolean.TRUE.equals(value))
742
743             {
744
745                buffer.append("selected");
746
747             }
748
749             buffer.append(">");
750
751             buffer.append(messages.getMessage("boolean.true"));
752
753             buffer.append("</option>");
754
755
756
757             buffer.append("<option value=\"false\" ");
758
759             if (Boolean.FALSE.equals(value))
760
761             {
762
763                buffer.append("selected");
764
765             }
766
767             buffer.append(">");
768
769             buffer.append(messages.getMessage("boolean.false"));
770
771             buffer.append("</option>");
772
773
774
775             buffer.append("</select>");
776
777          }
778
779          // Others types
780

781          else
782
783          {
784
785             String JavaDoc content = "";
786
787             String JavaDoc link = "";
788
789
790
791             buffer.append("<input type=\"text\" name=\":value:");
792
793             buffer.append(info.getName());
794
795             buffer.append("\" value=\"");
796
797
798
799             if (value != null)
800
801             {
802
803                content = propertyeditor.getAsText();
804
805
806
807                if (filter)
808
809                {
810
811                   content = ResponseUtils.filter(content);
812
813                }
814
815             }
816
817
818
819             buffer.append(content);
820
821             buffer.append("\"/>");
822
823
824
825             link = computeWritableEditor(info.getType(), content);
826
827
828
829             buffer.append(link);
830
831             ;
832
833          }
834
835       }
836
837       else
838
839       {
840
841          output = propertyeditor.getAsText();
842
843
844
845          if (filter)
846
847          {
848
849             output = ResponseUtils.filter(output);
850
851          }
852
853
854
855          buffer.append(computeReadableEditor(info.getType(), output));
856
857       }
858
859
860
861       ResponseUtils.write(pageContext, buffer.toString());
862
863    }
864
865
866
867
868
869    /**
870
871     * Adds a feature to the UnsupportedProperty attribute of the MBeanAttributeEditorTag object
872
873     *
874
875     * @param info The feature to be added to the UnsupportedProperty attribute
876
877     * @exception JspException Description of the Exception
878
879     * @todo I18N
880
881     */

882
883    protected void addUnsupportedProperty(MBeanAttributeInfo JavaDoc info)
884
885       throws JspException JavaDoc
886
887    {
888
889       String JavaDoc output = "Unsupported Class";
890
891
892
893       if (filter)
894
895       {
896
897          output = "<i>" + ResponseUtils.filter(output) + "</i>";
898
899       }
900
901       else
902
903       {
904
905          output = "<i>" + output + "</i>";
906
907       }
908
909
910
911       ResponseUtils.write(pageContext, output);
912
913    }
914
915
916
917
918
919    /**
920
921     * Description of the Method
922
923     *
924
925     * @param type Description of the Parameter
926
927     * @param value Description of the Parameter
928
929     * @return Description of the Return Value
930
931     */

932
933    protected String JavaDoc computeReadableEditor(String JavaDoc type, String JavaDoc value)
934
935    {
936
937       if ((value == null) || ("".equals(value)) || ("null".equals(value)))
938
939       {
940
941          return value;
942
943       }
944
945
946
947       if ("javax.management.ObjectName".equals(type))
948
949       {
950
951          try
952
953          {
954
955             Hashtable JavaDoc params = new Hashtable JavaDoc();
956
957             params.put("reference", value);
958
959
960
961             String JavaDoc url = RequestUtils.computeURL(pageContext, null, null, page, params, null, false);
962
963
964
965             return ("<a HREF=\"" + url + "\">" + value + "</a>");
966
967          }
968
969          catch (Exception JavaDoc e)
970
971          {
972
973             logger.warn("Exception during computation of readable editor " + e.getMessage());
974
975          }
976
977       }
978
979
980
981       return value;
982
983    }
984
985
986
987
988
989    /**
990
991     * Description of the Method
992
993     *
994
995     * @param type Description of the Parameter
996
997     * @param value Description of the Parameter
998
999     * @return Description of the Return Value
1000
1001    */

1002
1003   protected String JavaDoc computeWritableEditor(String JavaDoc type, String JavaDoc value)
1004
1005   {
1006
1007      if ((value == null) || ("".equals(value)) || ("null".equals(value)))
1008
1009      {
1010
1011         return "";
1012
1013      }
1014
1015
1016
1017      if ("javax.management.ObjectName".equals(type))
1018
1019      {
1020
1021         try
1022
1023         {
1024
1025            Hashtable JavaDoc params = new Hashtable JavaDoc();
1026
1027            params.put("reference", value);
1028
1029
1030
1031            String JavaDoc url = RequestUtils.computeURL(pageContext, null, null, page, params, null, false);
1032
1033
1034
1035            return (" <a HREF=\"" + url + "\">" + messages.getMessage("hyperlink.go") + "</a>");
1036
1037         }
1038
1039         catch (Exception JavaDoc e)
1040
1041         {
1042
1043            logger.warn("Exception during computation of writable editor " + e.getMessage());
1044
1045         }
1046
1047      }
1048
1049
1050
1051      return "";
1052
1053   }
1054
1055}
1056
1057
Popular Tags