Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

MonitoringGraph.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): 
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  * Encapsulate the JFreeChart classes and methods to provide easiness of
00047  * configuration for a monitoring graph. This <code>MonitoringGraph</code>
00048  * also contains the thread that retrieves the information at the moment. TODO:
00049  * Should take out this <code>Thread</code> so that it connects to a
00050  * monitoring repository.
00051  * 
00052  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00053  */
00054 public class MonitoringGraph extends Thread
00055 {
00056   /*
00057    * Graph Components
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    * Graph settings
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   // Buffer values
00085   private long                   displayFrequencyCount = 0;
00086   private XYSeries               buffer;
00087 
00088   /**
00089    * Creates a new <code>MonitoringGraph</code> object
00090    * 
00091    * @param collector An <code>AbstractDataCollector</code> object
00092    * @param jmxClient a <code>DataCollectorJmxClient</code> object
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    * Display the graph on screen
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    * Add a data entry to the current serie. Bufferize given the buffer series
00136    * size
00137    * 
00138    * @param newValue new data to add
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    * Save the graph into a file
00166    * 
00167    * @throws IOException if an error occurs
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    * @see java.lang.Runnable#run()
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         //ignore
00219       }
00220     }
00221   }
00222 
00223   /**
00224    * @return Returns the collector.
00225    */
00226   public AbstractDataCollector getCollector()
00227   {
00228     return collector;
00229   }
00230 
00231   /**
00232    * @param collector The collector to set.
00233    */
00234   public void setCollector(AbstractDataCollector collector)
00235   {
00236     this.collector = collector;
00237   }
00238 
00239   /**
00240    * @return Returns the frame.
00241    */
00242   public JFrame getFrame()
00243   {
00244     return frame;
00245   }
00246 
00247   /**
00248    * @param frame The frame to set.
00249    */
00250   public void setFrame(JFrame frame)
00251   {
00252     this.frame = frame;
00253   }
00254 
00255   /**
00256    * @return Returns the framed.
00257    */
00258   public boolean getFramed()
00259   {
00260     return framed;
00261   }
00262 
00263   /**
00264    * @param framed The framed to set.
00265    */
00266   public void setFramed(boolean framed)
00267   {
00268     this.framed = framed;
00269   }
00270 
00271   /**
00272    * @return Returns the frequency.
00273    */
00274   public long getFrequency()
00275   {
00276     return frequency;
00277   }
00278 
00279   /**
00280    * @param frequency The frequency to set.
00281    */
00282   public void setFrequency(long frequency)
00283   {
00284     this.frequency = frequency;
00285   }
00286 
00287   /**
00288    * @return Returns the repeat.
00289    */
00290   public long getRepeat()
00291   {
00292     return repeat;
00293   }
00294 
00295   /**
00296    * @param repeat The repeat to set.
00297    */
00298   public void setRepeat(long repeat)
00299   {
00300     this.repeat = repeat;
00301   }
00302 
00303   /**
00304    * @return Returns the stop.
00305    */
00306   public boolean getStop()
00307   {
00308     return stop;
00309   }
00310 
00311   /**
00312    * @param stop The stop to set.
00313    */
00314   public void setStop(boolean stop)
00315   {
00316     this.stop = stop;
00317   }
00318 
00319   /**
00320    * @return Returns the timeFrame.
00321    */
00322   public int getTimeFrame()
00323   {
00324     return timeFrame;
00325   }
00326 
00327   /**
00328    * @param timeFrame The timeFrame to set.
00329    */
00330   public void setTimeFrame(int timeFrame)
00331   {
00332     this.timeFrame = timeFrame;
00333     series.setMaximumItemCount(timeFrame);
00334   }
00335 
00336   /**
00337    * @return Returns the chart.
00338    */
00339   public JFreeChart getChart()
00340   {
00341     return chart;
00342   }
00343 
00344   /**
00345    * @return Returns the panel.
00346    */
00347   public ChartPanel getPanel()
00348   {
00349     return panel;
00350   }
00351 
00352   /**
00353    * @return Returns the timeStarted.
00354    */
00355   public long getTimeStarted()
00356   {
00357     return timeStarted;
00358   }
00359 
00360   /**
00361    * @return Returns the display.
00362    */
00363   public boolean getDisplay()
00364   {
00365     return display;
00366   }
00367 
00368   /**
00369    * @param display The display to set.
00370    */
00371   public void setDisplay(boolean display)
00372   {
00373     this.display = display;
00374   }
00375 
00376   /**
00377    * @return Returns the frameHeight.
00378    */
00379   public int getFrameHeight()
00380   {
00381     return frameHeight;
00382   }
00383 
00384   /**
00385    * @param frameHeight The frameHeight to set.
00386    */
00387   public void setFrameHeight(int frameHeight)
00388   {
00389     this.frameHeight = frameHeight;
00390   }
00391 
00392   /**
00393    * @return Returns the frameWidth.
00394    */
00395   public int getFrameWidth()
00396   {
00397     return frameWidth;
00398   }
00399 
00400   /**
00401    * @param frameWidth The frameWidth to set.
00402    */
00403   public void setFrameWidth(int frameWidth)
00404   {
00405     this.frameWidth = frameWidth;
00406   }
00407 
00408   /**
00409    * @return Returns the saveOnFinish.
00410    */
00411   public boolean getSaveOnFinish()
00412   {
00413     return saveOnFinish;
00414   }
00415 
00416   /**
00417    * @param saveOnFinish The saveOnFinish to set.
00418    */
00419   public void setSaveOnFinish(boolean saveOnFinish)
00420   {
00421     this.saveOnFinish = saveOnFinish;
00422   }
00423 
00424   /**
00425    * @return Returns the text.
00426    */
00427   public String getText()
00428   {
00429     return text;
00430   }
00431 
00432   /**
00433    * @param text The text to set.
00434    */
00435   public void setText(String text)
00436   {
00437     this.text = text;
00438   }
00439 
00440   /**
00441    * @return Returns the poolingSpeed.
00442    */
00443   public long getPoolingSpeed()
00444   {
00445     return poolingSpeed;
00446   }
00447 
00448   /**
00449    * @param poolingSpeed The poolingSpeed to set.
00450    */
00451   public void setPoolingSpeed(long poolingSpeed)
00452   {
00453     this.poolingSpeed = poolingSpeed;
00454   }
00455 
00456   /**
00457    * @return Returns the displayFrequency.
00458    */
00459   public long getDisplayFrequency()
00460   {
00461     return displayFrequency;
00462   }
00463 
00464   /**
00465    * @param displayFrequency The displayFrequency to set.
00466    */
00467   public void setDisplayFrequency(long displayFrequency)
00468   {
00469     this.displayFrequency = displayFrequency;
00470   }
00471 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1