00001
00025
package org.objectweb.cjdbc.console.monitoring;
00026
00027
import java.io.File;
00028
import java.io.IOException;
00029
import java.text.SimpleDateFormat;
00030
import java.util.Date;
00031
00032
import javax.swing.JFrame;
00033
00034
import org.jfree.chart.ChartFactory;
00035
import org.jfree.chart.ChartPanel;
00036
import org.jfree.chart.ChartUtilities;
00037
import org.jfree.chart.JFreeChart;
00038
import org.jfree.chart.plot.PlotOrientation;
00039
import org.jfree.data.XYSeries;
00040
import org.jfree.data.XYSeriesCollection;
00041
import org.jfree.ui.RefineryUtilities;
00042
import org.objectweb.cjdbc.common.monitor.AbstractDataCollector;
00043
import org.objectweb.cjdbc.console.jmx.DataCollectorJmxClient;
00044
00054 public class MonitoringGraph extends Thread
00055 {
00056
00057
00058
00059 private AbstractDataCollector
collector;
00060 private JFreeChart
chart;
00061 private ChartPanel
panel;
00062 private XYSeries
series;
00063 private XYSeriesCollection
dataset;
00064 private DataCollectorJmxClient
jmxClient;
00065 private JFrame
frame;
00066
00067
00068
00069
00070 private int frameHeight = 250;
00071 private int frameWidth = 600;
00072 private long frequency = 1000;
00073 private long displayFrequency = 10;
00074 private long poolingSpeed = 1;
00075 private long timeStarted;
00076 private int timeFrame = 10;
00077 private long repeat = -1;
00078 private boolean stop =
false;
00079 private boolean framed =
true;
00080 private boolean saveOnFinish =
false;
00081 private boolean display =
true;
00082 private String
text =
"";
00083
00084
00085 private long displayFrequencyCount = 0;
00086 private XYSeries
buffer;
00087
00094 public MonitoringGraph(AbstractDataCollector collector,
00095 DataCollectorJmxClient jmxClient)
00096 {
00097
this.collector = collector;
00098
this.jmxClient = jmxClient;
00099
00100
buffer =
new XYSeries(
"Buffer");
00101
00102
series =
new XYSeries(collector.getTargetName());
00103
series.setMaximumItemCount(
timeFrame);
00104
dataset =
new XYSeriesCollection(
series);
00105
chart = ChartFactory.createXYLineChart(collector.getDescription(),
"Time",
00106
"",
dataset, PlotOrientation.VERTICAL,
true,
false,
false);
00107
panel =
new ChartPanel(
chart);
00108
panel.setSize(
frameWidth,
frameHeight);
00109
00110
if (
display)
00111
display();
00112 }
00113
00117 public void display()
00118 {
00119
chart.setBorderVisible(
false);
00120
panel.setVisible(
true);
00121
if (
framed)
00122 {
00123
frame =
new JFrame(
collector.getDescription());
00124
frame.getContentPane().add(
panel);
00125
frame.setSize(
new java.awt.Dimension(
frameWidth,
frameHeight));
00126 RefineryUtilities.centerFrameOnScreen(
frame);
00127
frame.setVisible(
true);
00128
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
00129 }
00130 }
00131
00138 public void addData(
long newValue)
00139 {
00140
long now = (System.currentTimeMillis() / 1000) -
timeStarted;
00141
if (
displayFrequency == 0)
00142 {
00143
series.add(now, newValue);
00144 }
00145
else if (
displayFrequencyCount <
displayFrequency)
00146 {
00147
displayFrequencyCount++;
00148
buffer.add(now, newValue);
00149 }
00150
else
00151 {
00152
int count =
buffer.getItemCount();
00153
for (
int i = 0; i < count; i++)
00154 {
00155
series.add(
buffer.getDataItem(i));
00156 }
00157
buffer =
new XYSeries(
"buffer");
00158
displayFrequencyCount = 0;
00159 }
00160 }
00161
00167 public void saveAs() throws IOException
00168 {
00169 String fileName =
collector.getTargetName() +
"-"
00170 +
new SimpleDateFormat().format(
new Date());
00171 ChartUtilities.saveChartAsJPEG(
new File(fileName),
this.
chart,
frameWidth,
00172
frameHeight);
00173 }
00174
00178 public void run()
00179 {
00180
timeStarted = System.currentTimeMillis() / 1000;
00181
int count = 0;
00182
while (
repeat == -1 || count <
repeat)
00183 {
00184 count++;
00185
synchronized (
this)
00186 {
00187
try
00188 {
00189
addData(
jmxClient.retrieveData(
collector));
00190 wait(
frequency);
00191
if (
stop)
00192
break;
00193
00194
if (
display ==
true &&
panel.isShowing() ==
false)
00195 {
00196
if (
panel.getParent().isShowing() ==
false)
00197
stop =
true;
00198 }
00199
00200 }
00201
catch (Exception e)
00202 {
00203
stop =
true;
00204
throw new RuntimeException(e.getMessage());
00205 }
00206 }
00207 }
00208
if (
saveOnFinish)
00209 {
00210
try
00211 {
00212
saveAs();
00213 }
00214
catch (Exception e)
00215 {
00216
00217 }
00218 }
00219 }
00220
00224 public AbstractDataCollector
getCollector()
00225 {
00226
return collector;
00227 }
00228
00232 public void setCollector(AbstractDataCollector collector)
00233 {
00234
this.collector = collector;
00235 }
00236
00240 public JFrame
getFrame()
00241 {
00242
return frame;
00243 }
00244
00248 public void setFrame(JFrame frame)
00249 {
00250
this.frame = frame;
00251 }
00252
00256 public boolean getFramed()
00257 {
00258
return framed;
00259 }
00260
00264 public void setFramed(
boolean framed)
00265 {
00266
this.framed = framed;
00267 }
00268
00272 public long getFrequency()
00273 {
00274
return frequency;
00275 }
00276
00280 public void setFrequency(
long frequency)
00281 {
00282
this.frequency = frequency;
00283 }
00284
00288 public long getRepeat()
00289 {
00290
return repeat;
00291 }
00292
00296 public void setRepeat(
long repeat)
00297 {
00298
this.repeat = repeat;
00299 }
00300
00304 public boolean getStop()
00305 {
00306
return stop;
00307 }
00308
00312 public void setStop(
boolean stop)
00313 {
00314
this.stop = stop;
00315 }
00316
00320 public int getTimeFrame()
00321 {
00322
return timeFrame;
00323 }
00324
00328 public void setTimeFrame(
int timeFrame)
00329 {
00330
this.timeFrame = timeFrame;
00331
series.setMaximumItemCount(timeFrame);
00332 }
00333
00337 public JFreeChart
getChart()
00338 {
00339
return chart;
00340 }
00341
00345 public ChartPanel
getPanel()
00346 {
00347
return panel;
00348 }
00349
00353 public long getTimeStarted()
00354 {
00355
return timeStarted;
00356 }
00357
00361 public boolean getDisplay()
00362 {
00363
return display;
00364 }
00365
00369 public void setDisplay(
boolean display)
00370 {
00371
this.display = display;
00372 }
00373
00377 public int getFrameHeight()
00378 {
00379
return frameHeight;
00380 }
00381
00385 public void setFrameHeight(
int frameHeight)
00386 {
00387
this.frameHeight = frameHeight;
00388 }
00389
00393 public int getFrameWidth()
00394 {
00395
return frameWidth;
00396 }
00397
00401 public void setFrameWidth(
int frameWidth)
00402 {
00403
this.frameWidth = frameWidth;
00404 }
00405
00409 public boolean getSaveOnFinish()
00410 {
00411
return saveOnFinish;
00412 }
00413
00417 public void setSaveOnFinish(
boolean saveOnFinish)
00418 {
00419
this.saveOnFinish = saveOnFinish;
00420 }
00421
00425 public String
getText()
00426 {
00427
return text;
00428 }
00429
00433 public void setText(String text)
00434 {
00435
this.text = text;
00436 }
00437
00441 public long getPoolingSpeed()
00442 {
00443
return poolingSpeed;
00444 }
00445
00449 public void setPoolingSpeed(
long poolingSpeed)
00450 {
00451
this.poolingSpeed = poolingSpeed;
00452 }
00453
00457 public long getDisplayFrequency()
00458 {
00459
return displayFrequency;
00460 }
00461
00465 public void setDisplayFrequency(
long displayFrequency)
00466 {
00467
this.displayFrequency = displayFrequency;
00468 }
00469 }