1 24 25 package org.objectweb.cjdbc.console.monitoring; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.text.SimpleDateFormat ; 30 import java.util.Date ; 31 32 import javax.swing.JFrame ; 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 55 public class MonitoringGraph extends Thread 56 { 57 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 frame; 67 68 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 text = ""; 84 85 private long displayFrequencyCount = 0; 87 private XYSeries buffer; 88 89 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 ("HH:mm:ss").format(new Date ()) + ")", "", 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 120 public void display() 121 { 122 chart.setBorderVisible(false); 123 panel.setVisible(true); 124 if (framed) 125 { 126 frame = new JFrame (collector.getDescription()); 127 frame.getContentPane().add(panel); 128 frame.setSize(new java.awt.Dimension (frameWidth, frameHeight)); 129 RefineryUtilities.centerFrameOnScreen(frame); 130 frame.setVisible(true); 131 frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 132 } 133 } 134 135 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 170 public void saveAs() throws IOException 171 { 172 String fileName = collector.getTargetName() + "-" 173 + new SimpleDateFormat ().format(new Date ()); 174 ChartUtilities.saveChartAsJPEG(new File (fileName), this.chart, frameWidth, 175 frameHeight); 176 } 177 178 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 e) 205 { 206 stop = true; 207 throw new RuntimeException (e.getMessage()); 208 } 209 } 210 } 211 if (saveOnFinish) 212 { 213 try 214 { 215 saveAs(); 216 } 217 catch (Exception e) 218 { 219 } 221 } 222 } 223 224 227 public AbstractDataCollector getCollector() 228 { 229 return collector; 230 } 231 232 235 public void setCollector(AbstractDataCollector collector) 236 { 237 this.collector = collector; 238 } 239 240 243 public JFrame getFrame() 244 { 245 return frame; 246 } 247 248 251 public void setFrame(JFrame frame) 252 { 253 this.frame = frame; 254 } 255 256 259 public boolean getFramed() 260 { 261 return framed; 262 } 263 264 267 public void setFramed(boolean framed) 268 { 269 this.framed = framed; 270 } 271 272 275 public long getFrequency() 276 { 277 return frequency; 278 } 279 280 283 public void setFrequency(long frequency) 284 { 285 this.frequency = frequency; 286 } 287 288 291 public long getRepeat() 292 { 293 return repeat; 294 } 295 296 299 public void setRepeat(long repeat) 300 { 301 this.repeat = repeat; 302 } 303 304 307 public boolean getStop() 308 { 309 return stop; 310 } 311 312 315 public void setStop(boolean stop) 316 { 317 this.stop = stop; 318 } 319 320 323 public int getTimeFrame() 324 { 325 return timeFrame; 326 } 327 328 331 public void setTimeFrame(int timeFrame) 332 { 333 this.timeFrame = timeFrame; 334 series.setMaximumItemCount(timeFrame); 335 } 336 337 340 public JFreeChart getChart() 341 { 342 return chart; 343 } 344 345 348 public ChartPanel getPanel() 349 { 350 return panel; 351 } 352 353 356 public long getTimeStarted() 357 { 358 return timeStarted; 359 } 360 361 364 public boolean getDisplay() 365 { 366 return display; 367 } 368 369 372 public void setDisplay(boolean display) 373 { 374 this.display = display; 375 } 376 377 380 public int getFrameHeight() 381 { 382 return frameHeight; 383 } 384 385 388 public void setFrameHeight(int frameHeight) 389 { 390 this.frameHeight = frameHeight; 391 } 392 393 396 public int getFrameWidth() 397 { 398 return frameWidth; 399 } 400 401 404 public void setFrameWidth(int frameWidth) 405 { 406 this.frameWidth = frameWidth; 407 } 408 409 412 public boolean getSaveOnFinish() 413 { 414 return saveOnFinish; 415 } 416 417 420 public void setSaveOnFinish(boolean saveOnFinish) 421 { 422 this.saveOnFinish = saveOnFinish; 423 } 424 425 428 public String getText() 429 { 430 return text; 431 } 432 433 436 public void setText(String text) 437 { 438 this.text = text; 439 } 440 441 444 public long getPoolingSpeed() 445 { 446 return poolingSpeed; 447 } 448 449 452 public void setPoolingSpeed(long poolingSpeed) 453 { 454 this.poolingSpeed = poolingSpeed; 455 } 456 457 460 public long getDisplayFrequency() 461 { 462 return displayFrequency; 463 } 464 465 468 public void setDisplayFrequency(long displayFrequency) 469 { 470 this.displayFrequency = displayFrequency; 471 } 472 } | Popular Tags |