KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > monitoring > MonitoringGraph


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s):
23  */

24
25 package org.objectweb.cjdbc.console.monitoring;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import javax.swing.JFrame JavaDoc;
33
34 import org.jfree.chart.ChartFactory;
35 import org.jfree.chart.ChartPanel;
36 import org.jfree.chart.ChartUtilities;
37 import org.jfree.chart.JFreeChart;
38 import org.jfree.chart.plot.PlotOrientation;
39 import org.jfree.data.XYSeries;
40 import org.jfree.data.XYSeriesCollection;
41 import org.jfree.ui.RefineryUtilities;
42 import org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean;
43 import org.objectweb.cjdbc.common.monitor.AbstractDataCollector;
44
45 /**
46  * Encapsulate the JFreeChart classes and methods to provide easiness of
47  * configuration for a monitoring graph. This <code>MonitoringGraph</code>
48  * also contains the thread that retrieves the information at the moment.
49  * <p>
50  * TODO: Should take out this <code>Thread</code> so that it connects to a
51  * monitoring repository.
52  *
53  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
54  */

55 public class MonitoringGraph extends Thread JavaDoc
56 {
57   /*
58    * Graph Components
59    */

60   private AbstractDataCollector collector;
61   private JFreeChart chart;
62   private ChartPanel panel;
63   private XYSeries series;
64   private XYSeriesCollection dataset;
65   private DataCollectorMBean jmxClient;
66   private JFrame JavaDoc frame;
67
68   /*
69    * Graph settings
70    */

71   private int frameHeight = 250;
72   private int frameWidth = 600;
73   private long frequency = 1000;
74   private long displayFrequency = 10;
75   private long poolingSpeed = 1;
76   private long timeStarted;
77   private int timeFrame = 10;
78   private long repeat = -1;
79   private boolean stop = false;
80   private boolean framed = true;
81   private boolean saveOnFinish = false;
82   private boolean display = true;
83   private String JavaDoc text = "";
84
85   // Buffer values
86
private long displayFrequencyCount = 0;
87   private XYSeries buffer;
88
89   /**
90    * Creates a new <code>MonitoringGraph</code> object
91    *
92    * @param collector An <code>AbstractDataCollector</code> object
93    * @param jmxClient a <code>DataCollectorJmxClient</code> object
94    */

95   public MonitoringGraph(AbstractDataCollector collector,
96       DataCollectorMBean jmxClient)
97   {
98     this.collector = collector;
99     this.jmxClient = jmxClient;
100
101     buffer = new XYSeries("Buffer");
102
103     series = new XYSeries(collector.getTargetName());
104     series.setMaximumItemCount(timeFrame);
105     dataset = new XYSeriesCollection(series);
106     chart = ChartFactory.createXYLineChart(collector.getDescription(),
107         "Time (Started at:"
108             + new SimpleDateFormat JavaDoc("HH:mm:ss").format(new Date JavaDoc()) + ")", "",
109         dataset, PlotOrientation.VERTICAL, true, false, false);
110     panel = new ChartPanel(chart);
111     panel.setSize(frameWidth, frameHeight);
112
113     if (display)
114       display();
115   }
116
117   /**
118    * Display the graph on screen
119    */

120   public void display()
121   {
122     chart.setBorderVisible(false);
123     panel.setVisible(true);
124     if (framed)
125     {
126       frame = new JFrame JavaDoc(collector.getDescription());
127       frame.getContentPane().add(panel);
128       frame.setSize(new java.awt.Dimension JavaDoc(frameWidth, frameHeight));
129       RefineryUtilities.centerFrameOnScreen(frame);
130       frame.setVisible(true);
131       frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
132     }
133   }
134
135   /**
136    * Add a data entry to the current serie. Bufferize given the buffer series
137    * size
138    *
139    * @param newValue new data to add
140    */

141   public void addData(long newValue)
142   {
143     long now = (System.currentTimeMillis() / 1000) - timeStarted;
144     if (displayFrequency == 0)
145     {
146       series.add(now, newValue);
147     }
148     else if (displayFrequencyCount < displayFrequency)
149     {
150       displayFrequencyCount++;
151       buffer.add(now, newValue);
152     }
153     else
154     {
155       int count = buffer.getItemCount();
156       for (int i = 0; i < count; i++)
157       {
158         series.add(buffer.getDataItem(i));
159       }
160       buffer = new XYSeries("buffer");
161       displayFrequencyCount = 0;
162     }
163   }
164
165   /**
166    * Save the graph into a file
167    *
168    * @throws IOException if an error occurs
169    */

170   public void saveAs() throws IOException JavaDoc
171   {
172     String JavaDoc fileName = collector.getTargetName() + "-"
173         + new SimpleDateFormat JavaDoc().format(new Date JavaDoc());
174     ChartUtilities.saveChartAsJPEG(new File JavaDoc(fileName), this.chart, frameWidth,
175         frameHeight);
176   }
177
178   /**
179    * @see java.lang.Runnable#run()
180    */

181   public void run()
182   {
183     timeStarted = System.currentTimeMillis() / 1000;
184     int count = 0;
185     while (repeat == -1 || count < repeat)
186     {
187       count++;
188       synchronized (this)
189       {
190         try
191         {
192           addData(jmxClient.retrieveData(collector));
193           wait(frequency);
194           if (stop)
195             break;
196
197           if (display == true && panel.isShowing() == false)
198           {
199             if (panel.getParent().isShowing() == false)
200               stop = true;
201           }
202
203         }
204         catch (Exception JavaDoc e)
205         {
206           stop = true;
207           throw new RuntimeException JavaDoc(e.getMessage());
208         }
209       }
210     }
211     if (saveOnFinish)
212     {
213       try
214       {
215         saveAs();
216       }
217       catch (Exception JavaDoc e)
218       {
219         //ignore
220
}
221     }
222   }
223
224   /**
225    * @return Returns the collector.
226    */

227   public AbstractDataCollector getCollector()
228   {
229     return collector;
230   }
231
232   /**
233    * @param collector The collector to set.
234    */

235   public void setCollector(AbstractDataCollector collector)
236   {
237     this.collector = collector;
238   }
239
240   /**
241    * @return Returns the frame.
242    */

243   public JFrame JavaDoc getFrame()
244   {
245     return frame;
246   }
247
248   /**
249    * @param frame The frame to set.
250    */

251   public void setFrame(JFrame JavaDoc frame)
252   {
253     this.frame = frame;
254   }
255
256   /**
257    * @return Returns the framed.
258    */

259   public boolean getFramed()
260   {
261     return framed;
262   }
263
264   /**
265    * @param framed The framed to set.
266    */

267   public void setFramed(boolean framed)
268   {
269     this.framed = framed;
270   }
271
272   /**
273    * @return Returns the frequency.
274    */

275   public long getFrequency()
276   {
277     return frequency;
278   }
279
280   /**
281    * @param frequency The frequency to set.
282    */

283   public void setFrequency(long frequency)
284   {
285     this.frequency = frequency;
286   }
287
288   /**
289    * @return Returns the repeat.
290    */

291   public long getRepeat()
292   {
293     return repeat;
294   }
295
296   /**
297    * @param repeat The repeat to set.
298    */

299   public void setRepeat(long repeat)
300   {
301     this.repeat = repeat;
302   }
303
304   /**
305    * @return Returns the stop.
306    */

307   public boolean getStop()
308   {
309     return stop;
310   }
311
312   /**
313    * @param stop The stop to set.
314    */

315   public void setStop(boolean stop)
316   {
317     this.stop = stop;
318   }
319
320   /**
321    * @return Returns the timeFrame.
322    */

323   public int getTimeFrame()
324   {
325     return timeFrame;
326   }
327
328   /**
329    * @param timeFrame The timeFrame to set.
330    */

331   public void setTimeFrame(int timeFrame)
332   {
333     this.timeFrame = timeFrame;
334     series.setMaximumItemCount(timeFrame);
335   }
336
337   /**
338    * @return Returns the chart.
339    */

340   public JFreeChart getChart()
341   {
342     return chart;
343   }
344
345   /**
346    * @return Returns the panel.
347    */

348   public ChartPanel getPanel()
349   {
350     return panel;
351   }
352
353   /**
354    * @return Returns the timeStarted.
355    */

356   public long getTimeStarted()
357   {
358     return timeStarted;
359   }
360
361   /**
362    * @return Returns the display.
363    */

364   public boolean getDisplay()
365   {
366     return display;
367   }
368
369   /**
370    * @param display The display to set.
371    */

372   public void setDisplay(boolean display)
373   {
374     this.display = display;
375   }
376
377   /**
378    * @return Returns the frameHeight.
379    */

380   public int getFrameHeight()
381   {
382     return frameHeight;
383   }
384
385   /**
386    * @param frameHeight The frameHeight to set.
387    */

388   public void setFrameHeight(int frameHeight)
389   {
390     this.frameHeight = frameHeight;
391   }
392
393   /**
394    * @return Returns the frameWidth.
395    */

396   public int getFrameWidth()
397   {
398     return frameWidth;
399   }
400
401   /**
402    * @param frameWidth The frameWidth to set.
403    */

404   public void setFrameWidth(int frameWidth)
405   {
406     this.frameWidth = frameWidth;
407   }
408
409   /**
410    * @return Returns the saveOnFinish.
411    */

412   public boolean getSaveOnFinish()
413   {
414     return saveOnFinish;
415   }
416
417   /**
418    * @param saveOnFinish The saveOnFinish to set.
419    */

420   public void setSaveOnFinish(boolean saveOnFinish)
421   {
422     this.saveOnFinish = saveOnFinish;
423   }
424
425   /**
426    * @return Returns the text.
427    */

428   public String JavaDoc getText()
429   {
430     return text;
431   }
432
433   /**
434    * @param text The text to set.
435    */

436   public void setText(String JavaDoc text)
437   {
438     this.text = text;
439   }
440
441   /**
442    * @return Returns the poolingSpeed.
443    */

444   public long getPoolingSpeed()
445   {
446     return poolingSpeed;
447   }
448
449   /**
450    * @param poolingSpeed The poolingSpeed to set.
451    */

452   public void setPoolingSpeed(long poolingSpeed)
453   {
454     this.poolingSpeed = poolingSpeed;
455   }
456
457   /**
458    * @return Returns the displayFrequency.
459    */

460   public long getDisplayFrequency()
461   {
462     return displayFrequency;
463   }
464
465   /**
466    * @param displayFrequency The displayFrequency to set.
467    */

468   public void setDisplayFrequency(long displayFrequency)
469   {
470     this.displayFrequency = displayFrequency;
471   }
472 }
Popular Tags