KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > manager > interfaces > impl > GraphMBeanAttributeAction


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.manager.interfaces.impl;
23
24 import org.jboss.console.navtree.AppletBrowser;
25 import org.jboss.console.navtree.AppletTreeAction;
26 import org.jboss.console.navtree.TreeContext;
27 import org.jfree.chart.ChartFactory;
28 import org.jfree.chart.ChartFrame;
29 import org.jfree.chart.JFreeChart;
30 import org.jfree.chart.plot.PlotOrientation;
31 import org.jfree.data.AbstractXYDataset;
32 import org.jfree.data.DatasetChangeEvent;
33
34 import javax.management.ObjectName JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 /**
38  * <description>
39  *
40  * @see <related>
41  *
42  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
43  * @version $Revision: 37459 $
44  *
45  * <p><b>Revisions:</b>
46  *
47  * <p><b>3 janv. 2003 Sacha Labourey:</b>
48  * <ul>
49  * <li> First implementation </li>
50  * </ul>
51  */

52 public class GraphMBeanAttributeAction
53         implements AppletTreeAction
54 {
55    public class MBeanXYDataset extends AbstractXYDataset
56    {
57
58       private ArrayList JavaDoc data = new ArrayList JavaDoc();
59
60       /**
61        * Default constructor.
62        */

63       public MBeanXYDataset()
64       {
65       }
66
67       public void clear()
68       {
69          data.clear();
70          notifyListeners(new DatasetChangeEvent(this, this));
71       }
72
73       public void add(Object JavaDoc num)
74       {
75          data.add(num);
76          notifyListeners(new DatasetChangeEvent(this, this));
77       }
78
79       /**
80        * Returns the x-value for the specified series and item. Series are numbered 0, 1, ...
81        *
82        * @param series the index (zero-based) of the series.
83        * @param item the index (zero-based) of the required item.
84        *
85        * @return the x-value for the specified series and item.
86        */

87       public Number JavaDoc getXValue(int series, int item)
88       {
89          return new Integer JavaDoc(item);
90       }
91
92       /**
93        * Returns the y-value for the specified series and item. Series are numbered 0, 1, ...
94        *
95        * @param series the index (zero-based) of the series.
96        * @param item the index (zero-based) of the required item.
97        *
98        * @return the y-value for the specified series and item.
99        */

100       public Number JavaDoc getYValue(int series, int item)
101       {
102          return (Number JavaDoc) data.get(item);
103       }
104
105       /**
106        * Returns the number of series in the dataset.
107        *
108        * @return the number of series in the dataset.
109        */

110       public int getSeriesCount()
111       {
112          return 1;
113       }
114
115       /**
116        * Returns the name of the series.
117        *
118        * @param series the index (zero-based) of the series.
119        *
120        * @return the name of the series.
121        */

122       public String JavaDoc getSeriesName(int series)
123       {
124          return "y = " + attr;
125       }
126
127       /**
128        * Returns the number of items in the specified series.
129        *
130        * @param series the index (zero-based) of the series.
131        * @return the number of items in the specified series.
132        *
133        */

134       public int getItemCount(int series)
135       {
136          return data.size();
137       }
138    }
139
140    public class UpdateThread implements Runnable JavaDoc
141    {
142       MBeanXYDataset data;
143       TreeContext tc;
144
145       public UpdateThread(MBeanXYDataset data, TreeContext tc)
146       {
147          this.data = data;
148          this.tc = tc;
149       }
150
151       public void run()
152       {
153          while (true)
154          {
155             try
156             {
157                if (frame.isShowing())
158                {
159                   Object JavaDoc val = tc.getRemoteMBeanInvoker().getAttribute(targetObjectName, attr);
160                   System.out.println("added value: " + val);
161                   data.add(val);
162                }
163                Thread.sleep(1000);
164             }
165             catch (Exception JavaDoc ex)
166             {
167                ex.printStackTrace();
168             }
169          }
170       }
171    }
172
173
174    protected ObjectName JavaDoc targetObjectName = null;
175    protected String JavaDoc attr = null;
176    protected transient ChartFrame frame = null;
177    protected transient MBeanXYDataset dataset = null;
178
179    public GraphMBeanAttributeAction()
180    {
181    }
182
183    public GraphMBeanAttributeAction(ObjectName JavaDoc pName,
184                                     String JavaDoc attr)
185    {
186       this.targetObjectName = pName;
187       this.attr = attr;
188    }
189
190    public void doAction(TreeContext tc, AppletBrowser applet)
191    {
192       try
193       {
194          if (frame == null)
195          {
196             //tc.getRemoteMBeanInvoker ().invoke(targetObjectName, actionName, params, signature);
197
dataset = new MBeanXYDataset();
198             JFreeChart chart = ChartFactory.createXYLineChart(
199                     "JMX Attribute: " + attr, "count", attr, dataset,
200                     PlotOrientation.VERTICAL,
201                     true,
202                     true,
203                     false
204             );
205             UpdateThread update = new UpdateThread(dataset, tc);
206
207             Thread JavaDoc thread = new Thread JavaDoc(update);
208             thread.start();
209             frame = new ChartFrame("JMX Attribute: " + attr, chart);
210             frame.getChartPanel().setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
211             frame.pack();
212          }
213          else
214          {
215             dataset.clear();
216          }
217          frame.show();
218          frame.requestFocus();
219       }
220       catch (Exception JavaDoc displayed)
221       {
222          displayed.printStackTrace();
223       }
224    }
225
226 }
227
Popular Tags