KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > management > browser > ui > ManagedObjectStatsViewer


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

7 package org.ejtools.management.browser.ui;
8
9 import java.awt.GridBagConstraints JavaDoc;
10 import java.awt.Insets JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import javax.management.MBeanAttributeInfo JavaDoc;
18 import javax.management.j2ee.statistics.Statistic JavaDoc;
19 import javax.management.j2ee.statistics.Stats JavaDoc;
20 import javax.swing.JButton JavaDoc;
21
22 import org.apache.log4j.Logger;
23 import org.ejtools.graph.service.GraphConsumer;
24 import org.ejtools.graph.service.GraphConsumerMediator;
25 import org.ejtools.graph.service.GraphConsumerSelector;
26 import org.ejtools.management.browser.model.ManagedObject;
27 import org.ejtools.management.browser.ui.statistics.StatisticCustomizer;
28 import org.ejtools.management.browser.ui.statistics.StatisticCustomizerFactory;
29
30 /**
31  * Description of the Class
32  *
33  * @author letiemble
34  * @version $Revision: 1.6 $
35  */

36 public class ManagedObjectStatsViewer extends ManagedObjectViewer implements GraphConsumerMediator
37 {
38    /** Description of the Field */
39    private Hashtable JavaDoc customizers;
40    /** Description of the Field */
41    private static Logger logger = Logger.getLogger(ManagedObjectStatsViewer.class);
42
43
44    /**Constructor for the ManagedObjectStatsViewer object */
45    public ManagedObjectStatsViewer()
46    {
47       super();
48    }
49
50
51
52    /**
53     *Constructor for the ManagedObjectStatsViewer object
54     *
55     * @param bean Description of the Parameter
56     */

57    public ManagedObjectStatsViewer(Object JavaDoc bean)
58    {
59       super(bean);
60    }
61
62
63    /**
64     * @param statisticName The feature to be added to the StatisticToGraph attribute
65     * @param statisticProperty The feature to be added to the StatisticToGraph attribute
66     */

67    public void addStatisticToGraph(String JavaDoc statisticName, String JavaDoc statisticProperty)
68    {
69       GraphConsumer consumer = GraphConsumerSelector.selectWithDialog(this);
70       if (consumer != null)
71       {
72          this.object.registerForGraph(consumer, statisticName, statisticProperty);
73       }
74    }
75
76
77    /**
78     * @param infos Description of the Parameter
79     * @return Description of the Return Value
80     */

81    public MBeanAttributeInfo JavaDoc[] filter(MBeanAttributeInfo JavaDoc[] infos)
82    {
83       ArrayList JavaDoc filteredInfos = new ArrayList JavaDoc();
84       ArrayList JavaDoc includes = new ArrayList JavaDoc();
85       includes.add(MO_ATTRIBUTE_STATS);
86
87       for (int i = 0; i < infos.length; i++)
88       {
89          String JavaDoc name = infos[i].getName();
90          if (includes.contains(name.toLowerCase()))
91          {
92             filteredInfos.add(infos[i]);
93          }
94       }
95
96       return (MBeanAttributeInfo JavaDoc[]) filteredInfos.toArray(new MBeanAttributeInfo JavaDoc[0]);
97    }
98
99
100    /**
101     * Gets the graphConsumers attribute of the ManagedObject object
102     *
103     * @return The graphConsumers value
104     */

105    public GraphConsumer[] getGraphConsumers()
106    {
107       return this.object.getGraphConsumers();
108    }
109
110
111    /**
112     * Sets the object attribute of the ManagedObjectStatsViewer object
113     *
114     * @param bean The new object value
115     */

116    public void setObject(Object JavaDoc bean)
117    {
118       try
119       {
120          this.panel.removeAll();
121
122          if (bean == null)
123          {
124             this.validate();
125             this.repaint();
126             return;
127          }
128
129          this.object = (ManagedObject) bean;
130 // MBeanInfo beaninfo = this.object.getMBeanInfo();
131

132          GridBagConstraints JavaDoc gridbagconstraints = new GridBagConstraints JavaDoc();
133          gridbagconstraints.insets = new Insets JavaDoc(1, 1, 1, 1);
134          gridbagconstraints.anchor = GridBagConstraints.NORTH;
135          gridbagconstraints.weightx = 1.0D;
136          gridbagconstraints.weighty = 0.0D;
137          gridbagconstraints.fill = GridBagConstraints.HORIZONTAL;
138          gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER;
139
140          Stats JavaDoc stats = this.getStats();
141          String JavaDoc[] names = stats.getStatisticNames();
142          for (int i = 0; i < names.length; i++)
143          {
144             String JavaDoc name = names[i];
145
146             Statistic JavaDoc statistic = stats.getStatistic(name);
147             StatisticCustomizer sc = StatisticCustomizerFactory.create(statistic);
148
149             if (sc != null)
150             {
151                this.panel.add(sc, gridbagconstraints);
152                this.getCustomizers().put(name, sc);
153                sc.setMediator(this);
154                sc.refresh();
155             }
156          }
157
158          JButton JavaDoc button = new JButton JavaDoc(resources.getString("customizer.tab.statistics.button.refresh"));
159          button.addActionListener(
160             new ActionListener JavaDoc()
161             {
162                /**
163                 * @param e Description of the Parameter
164                 */

165                public void actionPerformed(ActionEvent JavaDoc e)
166                {
167                   try
168                   {
169                      ManagedObjectStatsViewer.this.update();
170                   }
171                   catch (Exception JavaDoc ex)
172                   {
173                      logger.error("Error while updating" + ex);
174                   }
175                }
176             }
177             );
178          gridbagconstraints.weighty = 1.0D;
179          this.panel.add(button, gridbagconstraints);
180
181          this.validate();
182          this.repaint();
183       }
184       catch (Exception JavaDoc e)
185       {
186          logger.error("Error while setting object" + e);
187       }
188    }
189
190
191    /**
192     * Gets the customizers attribute of the ManagedObjectStatsViewer object
193     *
194     * @return The customizers value
195     */

196    protected Hashtable JavaDoc getCustomizers()
197    {
198       if (this.customizers == null)
199       {
200          this.customizers = new Hashtable JavaDoc();
201       }
202       return this.customizers;
203    }
204
205
206    /**
207     * Gets the stats attribute of the ManagedObjectStatsViewer object
208     *
209     * @return The stats value
210     * @exception Exception Description of the Exception
211     */

212    protected Stats JavaDoc getStats()
213       throws Exception JavaDoc
214    {
215       return this.object.getStats();
216    }
217
218
219    /**
220     * Description of the Method
221     *
222     * @exception Exception Description of the Exception
223     */

224    protected void update()
225       throws Exception JavaDoc
226    {
227       Stats JavaDoc stats = this.getStats();
228       Iterator JavaDoc it = this.getCustomizers().keySet().iterator();
229       while (it.hasNext())
230       {
231          String JavaDoc name = (String JavaDoc) it.next();
232          StatisticCustomizer sc = (StatisticCustomizer) this.getCustomizers().get(name);
233          Statistic JavaDoc statistic = stats.getStatistic(name);
234          sc.setStatistic(statistic);
235          sc.refresh();
236       }
237    }
238 }
239
Popular Tags