00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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.jmx.mbeans.DataCollectorMBean;
00043 import org.objectweb.cjdbc.common.monitor.AbstractDataCollector;
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
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 DataCollectorMBean 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
00088
00089
00090
00091
00092
00093
00094 public MonitoringGraph(AbstractDataCollector collector,
00095 DataCollectorMBean 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(),
00106 "Time (Started at:"
00107 + new SimpleDateFormat("HH:mm:ss").format(new Date()) + ")", "",
00108 dataset, PlotOrientation.VERTICAL, true, false, false);
00109 panel = new ChartPanel(chart);
00110 panel.setSize(frameWidth, frameHeight);
00111
00112 if (display)
00113 display();
00114 }
00115
00116
00117
00118
00119 public void display()
00120 {
00121 chart.setBorderVisible(false);
00122 panel.setVisible(true);
00123 if (framed)
00124 {
00125 frame = new JFrame(collector.getDescription());
00126 frame.getContentPane().add(panel);
00127 frame.setSize(new java.awt.Dimension(frameWidth, frameHeight));
00128 RefineryUtilities.centerFrameOnScreen(frame);
00129 frame.setVisible(true);
00130 frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
00131 }
00132 }
00133
00134
00135
00136
00137
00138
00139
00140 public void addData(long newValue)
00141 {
00142 long now = (System.currentTimeMillis() / 1000) - timeStarted;
00143 if (displayFrequency == 0)
00144 {
00145 series.add(now, newValue);
00146 }
00147 else if (displayFrequencyCount < displayFrequency)
00148 {
00149 displayFrequencyCount++;
00150 buffer.add(now, newValue);
00151 }
00152 else
00153 {
00154 int count = buffer.getItemCount();
00155 for (int i = 0; i < count; i++)
00156 {
00157 series.add(buffer.getDataItem(i));
00158 }
00159 buffer = new XYSeries("buffer");
00160 displayFrequencyCount = 0;
00161 }
00162 }
00163
00164
00165
00166
00167
00168
00169 public void saveAs() throws IOException
00170 {
00171 String fileName = collector.getTargetName() + "-"
00172 + new SimpleDateFormat().format(new Date());
00173 ChartUtilities.saveChartAsJPEG(new File(fileName), this.chart, frameWidth,
00174 frameHeight);
00175 }
00176
00177
00178
00179
00180 public void run()
00181 {
00182 timeStarted = System.currentTimeMillis() / 1000;
00183 int count = 0;
00184 while (repeat == -1 || count < repeat)
00185 {
00186 count++;
00187 synchronized (this)
00188 {
00189 try
00190 {
00191 addData(jmxClient.retrieveData(collector));
00192 wait(frequency);
00193 if (stop)
00194 break;
00195
00196 if (display == true && panel.isShowing() == false)
00197 {
00198 if (panel.getParent().isShowing() == false)
00199 stop = true;
00200 }
00201
00202 }
00203 catch (Exception e)
00204 {
00205 stop = true;
00206 throw new RuntimeException(e.getMessage());
00207 }
00208 }
00209 }
00210 if (saveOnFinish)
00211 {
00212 try
00213 {
00214 saveAs();
00215 }
00216 catch (Exception e)
00217 {
00218
00219 }
00220 }
00221 }
00222
00223
00224
00225
00226 public AbstractDataCollector getCollector()
00227 {
00228 return collector;
00229 }
00230
00231
00232
00233
00234 public void setCollector(AbstractDataCollector collector)
00235 {
00236 this.collector = collector;
00237 }
00238
00239
00240
00241
00242 public JFrame getFrame()
00243 {
00244 return frame;
00245 }
00246
00247
00248
00249
00250 public void setFrame(JFrame frame)
00251 {
00252 this.frame = frame;
00253 }
00254
00255
00256
00257
00258 public boolean getFramed()
00259 {
00260 return framed;
00261 }
00262
00263
00264
00265
00266 public void setFramed(boolean framed)
00267 {
00268 this.framed = framed;
00269 }
00270
00271
00272
00273
00274 public long getFrequency()
00275 {
00276 return frequency;
00277 }
00278
00279
00280
00281
00282 public void setFrequency(long frequency)
00283 {
00284 this.frequency = frequency;
00285 }
00286
00287
00288
00289
00290 public long getRepeat()
00291 {
00292 return repeat;
00293 }
00294
00295
00296
00297
00298 public void setRepeat(long repeat)
00299 {
00300 this.repeat = repeat;
00301 }
00302
00303
00304
00305
00306 public boolean getStop()
00307 {
00308 return stop;
00309 }
00310
00311
00312
00313
00314 public void setStop(boolean stop)
00315 {
00316 this.stop = stop;
00317 }
00318
00319
00320
00321
00322 public int getTimeFrame()
00323 {
00324 return timeFrame;
00325 }
00326
00327
00328
00329
00330 public void setTimeFrame(int timeFrame)
00331 {
00332 this.timeFrame = timeFrame;
00333 series.setMaximumItemCount(timeFrame);
00334 }
00335
00336
00337
00338
00339 public JFreeChart getChart()
00340 {
00341 return chart;
00342 }
00343
00344
00345
00346
00347 public ChartPanel getPanel()
00348 {
00349 return panel;
00350 }
00351
00352
00353
00354
00355 public long getTimeStarted()
00356 {
00357 return timeStarted;
00358 }
00359
00360
00361
00362
00363 public boolean getDisplay()
00364 {
00365 return display;
00366 }
00367
00368
00369
00370
00371 public void setDisplay(boolean display)
00372 {
00373 this.display = display;
00374 }
00375
00376
00377
00378
00379 public int getFrameHeight()
00380 {
00381 return frameHeight;
00382 }
00383
00384
00385
00386
00387 public void setFrameHeight(int frameHeight)
00388 {
00389 this.frameHeight = frameHeight;
00390 }
00391
00392
00393
00394
00395 public int getFrameWidth()
00396 {
00397 return frameWidth;
00398 }
00399
00400
00401
00402
00403 public void setFrameWidth(int frameWidth)
00404 {
00405 this.frameWidth = frameWidth;
00406 }
00407
00408
00409
00410
00411 public boolean getSaveOnFinish()
00412 {
00413 return saveOnFinish;
00414 }
00415
00416
00417
00418
00419 public void setSaveOnFinish(boolean saveOnFinish)
00420 {
00421 this.saveOnFinish = saveOnFinish;
00422 }
00423
00424
00425
00426
00427 public String getText()
00428 {
00429 return text;
00430 }
00431
00432
00433
00434
00435 public void setText(String text)
00436 {
00437 this.text = text;
00438 }
00439
00440
00441
00442
00443 public long getPoolingSpeed()
00444 {
00445 return poolingSpeed;
00446 }
00447
00448
00449
00450
00451 public void setPoolingSpeed(long poolingSpeed)
00452 {
00453 this.poolingSpeed = poolingSpeed;
00454 }
00455
00456
00457
00458
00459 public long getDisplayFrequency()
00460 {
00461 return displayFrequency;
00462 }
00463
00464
00465
00466
00467 public void setDisplayFrequency(long displayFrequency)
00468 {
00469 this.displayFrequency = displayFrequency;
00470 }
00471 }