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

MonitoringConsole.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 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): Emmanuel Cecchet
00023  */
00024 
00025 package org.objectweb.cjdbc.console.monitoring;
00026 
00027 import java.awt.BorderLayout;
00028 import java.awt.Color;
00029 import java.awt.Dimension;
00030 import java.awt.Font;
00031 import java.awt.GridLayout;
00032 import java.awt.Point;
00033 import java.awt.Toolkit;
00034 import java.awt.Window;
00035 import java.awt.event.ActionEvent;
00036 import java.awt.event.ActionListener;
00037 import java.awt.event.MouseEvent;
00038 import java.awt.event.MouseListener;
00039 import java.awt.event.WindowEvent;
00040 import java.awt.event.WindowListener;
00041 import java.io.BufferedOutputStream;
00042 import java.io.File;
00043 import java.io.FileInputStream;
00044 import java.io.FileOutputStream;
00045 import java.io.IOException;
00046 import java.util.ArrayList;
00047 import java.util.Enumeration;
00048 import java.util.Hashtable;
00049 import java.util.Iterator;
00050 import java.util.PropertyResourceBundle;
00051 import java.util.StringTokenizer;
00052 import java.util.Vector;
00053 
00054 import javax.swing.JButton;
00055 import javax.swing.JComboBox;
00056 import javax.swing.JComponent;
00057 import javax.swing.JFrame;
00058 import javax.swing.JLabel;
00059 import javax.swing.JMenu;
00060 import javax.swing.JMenuBar;
00061 import javax.swing.JMenuItem;
00062 import javax.swing.JPanel;
00063 import javax.swing.JScrollPane;
00064 import javax.swing.JTextField;
00065 import javax.swing.JToolBar;
00066 import javax.swing.SwingConstants;
00067 
00068 import org.objectweb.cjdbc.common.exceptions.DataCollectorException;
00069 import org.objectweb.cjdbc.common.i18n.MonitorTranslate;
00070 import org.objectweb.cjdbc.common.jmx.mbeans.ControllerMBean;
00071 import org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean;
00072 import org.objectweb.cjdbc.common.jmx.mbeans.VirtualDatabaseMBean;
00073 import org.objectweb.cjdbc.common.monitor.AbstractDataCollector;
00074 import org.objectweb.cjdbc.common.monitor.DataCollection;
00075 import org.objectweb.cjdbc.common.monitor.DataCollectionNames;
00076 import org.objectweb.cjdbc.console.jmx.RmiJmxClient;
00077 
00078 /**
00079  * New Monitoring Console bootstrap for starting stopping monitoring graphs
00080  * 
00081  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00082  * @author <a href="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
00083  *         </a>
00084  * @version 1.0
00085  */
00086 public class MonitoringConsole extends JFrame
00087     implements
00088       DataCollection,
00089       MouseListener,
00090       ActionListener,
00091       WindowListener
00092 {
00093   // Window parameters
00094   private int                  frameWidth             = 400;
00095   private int                  frameHeight            = 700;
00096 
00097   // Swing Components
00098   private JLabel               label;
00099   private JScrollPane          jScroll;
00100 
00101   // MBean components
00102   private ControllerMBean      controllerMBean;
00103   private VirtualDatabaseMBean virtualDatabaseMBean;
00104   private DataCollectorMBean   dataCollectorMBean;
00105 
00106   // Menu components
00107   private static final String  GRAPH_CONTROLLER       = MonitorTranslate
00108                                                           .get("heading.controller");
00109   private static final String  GRAPH_VIRTUAL_DATABASE = MonitorTranslate
00110                                                           .get("heading.virtualdatabase");
00111   private static final String  GRAPH_CACHE            = MonitorTranslate
00112                                                           .get("heading.cache");
00113   private static final String  GRAPH_SCHEDULER        = MonitorTranslate
00114                                                           .get("heading.scheduler");
00115   private static final String  GRAPH_BACKENDS         = MonitorTranslate
00116                                                           .get("heading.backends");
00117 
00118   private Hashtable            windows                = new Hashtable();
00119 
00120   private Vector               comboBoxesItems        = new Vector();
00121   private Hashtable            comboBoxes             = new Hashtable();
00122 
00123   // Fonts & Colors
00124   private Font                 boxFont                = new Font("Arial",
00125                                                           Font.PLAIN, 10);
00126   private Font                 labelFont              = new Font("Arial",
00127                                                           Font.BOLD, 10);
00128   private Font                 headerFont             = new Font("Arial",
00129                                                           Font.BOLD, 12);
00130   private Color                toolBarColor           = Color.white;
00131 
00132   // Graph options
00133   private int                  graphRepeat            = -1;
00134   private int                  graphTimeframe         = 3600;
00135   private int                  graphFrequency         = 1000;
00136   private int                  graphDisplayFrequency  = 1;
00137 
00138   // Actions
00139   private static final String  COMMAND_SAVE           = MonitorTranslate
00140                                                           .get("command.save");
00141   private static final String  COMMAND_LOAD           = MonitorTranslate
00142                                                           .get("command.load");
00143   private static final String  COMMAND_CLOSE_GRAPHS   = MonitorTranslate
00144                                                           .get("command.close.all");
00145   private static final String  COMMAND_OPTIONS        = MonitorTranslate
00146                                                           .get("command.set.options");
00147   private static final String  COMMAND_CLOSE          = MonitorTranslate
00148                                                           .get("command.quit");
00149   private static final String  COMMAND_REFRESH        = MonitorTranslate
00150                                                           .get("command.refresh");
00151 
00152   private static final String  OPTIONS_APPLY          = "OptionsApply";
00153   private static final String  OPTIONS_CANCEL         = "OptionsCancel";
00154 
00155   // Combox
00156   private static final String  COMBO_HIDE             = MonitorTranslate
00157                                                           .get("command.hide");
00158   private static final String  COMBO_FLOATING         = MonitorTranslate
00159                                                           .get("command.float");
00160 
00161   private boolean              isLoading              = false;
00162 
00163   // Option window
00164   private JFrame               options;
00165   private JTextField           ftimeframe;
00166   private JTextField           ffrequency;
00167   private JTextField           frepeat;
00168   private JTextField           displayBuffer;
00169 
00170   private boolean              displayController;
00171   private boolean              displayVirtualDatabase;
00172   private boolean              displayBackends;
00173 
00174   /**
00175    * Creates a new <code>MonitoringConsole</code> object
00176    * 
00177    * @param jmxUrl JMX URL
00178    * @param controllerMBean controller MBean if controller monitoring must be
00179    *          activated
00180    * @param virtualDatabaseMBean virtual database MBean if virtual database
00181    *          monitoring must be activated
00182    * @param backends display backends monitoring menu
00183    * @throws IOException if an error occurs
00184    */
00185   public MonitoringConsole(String jmxUrl, ControllerMBean controllerMBean,
00186       VirtualDatabaseMBean virtualDatabaseMBean, boolean backends)
00187       throws IOException
00188   {
00189     super(MonitorTranslate.get("monitor.frame.title", jmxUrl));
00190 
00191     this.displayController = controllerMBean == null;
00192     this.displayVirtualDatabase = virtualDatabaseMBean == null;
00193     this.displayBackends = backends;
00194 
00195     // Get MBeans reference
00196     RmiJmxClient jmxClient = new RmiJmxClient(jmxUrl, null);
00197     dataCollectorMBean = jmxClient.getDataCollectorProxy();
00198     this.controllerMBean = controllerMBean;
00199     this.virtualDatabaseMBean = virtualDatabaseMBean;
00200 
00201     // Get options for combo boxes
00202     comboBoxesItems.add(COMBO_HIDE);
00203     comboBoxesItems.add(COMBO_FLOATING);
00204 
00205     Toolkit toolkit;
00206     Dimension dim;
00207     int screenHeight, screenWidth;
00208 
00209     // Initialize basic layout properties
00210     setForeground(Color.white);
00211     getContentPane().setLayout(new BorderLayout());
00212 
00213     // Set the frame's display to be WIDTH x HEIGHT in the middle of the screen
00214     toolkit = Toolkit.getDefaultToolkit();
00215     dim = toolkit.getScreenSize();
00216     screenHeight = dim.height;
00217     screenWidth = dim.width;
00218 
00219     // Reduce height by two if display only controller
00220     if (displayController && (!displayVirtualDatabase) && (!displayBackends))
00221       frameHeight = 270;
00222 
00223     setBounds((screenWidth - frameWidth) / 2, (screenHeight - frameHeight) / 2,
00224         frameWidth, frameHeight);
00225 
00226     try
00227     {
00228       // Init Frame with dynamic content
00229       initConsole();
00230     }
00231     catch (Exception e)
00232     {
00233       throw new IOException(e.getMessage());
00234     }
00235 
00236     // Status Bar
00237     label = new JLabel("Select Graphs ...");
00238     label.setFont(labelFont);
00239     getContentPane().add(label, BorderLayout.SOUTH);
00240     getContentPane().setBackground(toolBarColor);
00241     getContentPane().setForeground(toolBarColor);
00242 
00243     // Menu Bar
00244     JMenuBar menuBar = new JMenuBar();
00245     JMenu menu = new JMenu(MonitorTranslate.get("monitor.menu.bar"));
00246     JMenuItem item1 = new JMenuItem(COMMAND_SAVE);
00247     JMenuItem item2 = new JMenuItem(COMMAND_LOAD);
00248     JMenuItem item3 = new JMenuItem(COMMAND_CLOSE);
00249     JMenuItem item4 = new JMenuItem(COMMAND_CLOSE_GRAPHS);
00250     JMenuItem item5 = new JMenuItem(COMMAND_OPTIONS);
00251     JMenuItem item6 = new JMenuItem(COMMAND_REFRESH);
00252     item1.setBackground(toolBarColor);
00253     item2.setBackground(toolBarColor);
00254     item3.setBackground(toolBarColor);
00255     item4.setBackground(toolBarColor);
00256     item5.setBackground(toolBarColor);
00257     item6.setBackground(toolBarColor);
00258     menu.add(item1).addActionListener(this);
00259     menu.add(item2).addActionListener(this);
00260     menu.add(item4).addActionListener(this);
00261     menu.add(item5).addActionListener(this);
00262     menu.add(item6).addActionListener(this);
00263     menu.addSeparator();
00264     menu.add(item3).addActionListener(this);
00265     menu.setVisible(true);
00266     menu.setBackground(toolBarColor);
00267     menuBar.add(menu);
00268     menuBar.setBackground(toolBarColor);
00269     this.setJMenuBar(menuBar);
00270 
00271     // Prepare options window
00272     options = new JFrame(MonitorTranslate.get("options.frame.title"));
00273     options.setSize(200, 200);
00274     JPanel optionsPanel = new JPanel();
00275     optionsPanel.setLayout(new GridLayout(5, 2));
00276     optionsPanel.add(new JLabel(MonitorTranslate.get("options.repeat")));
00277     frepeat = new JTextField(0);
00278     frepeat.setAlignmentX(RIGHT_ALIGNMENT);
00279     frepeat.setText(graphRepeat + "");
00280     frepeat.addActionListener(this);
00281     optionsPanel.add(frepeat);
00282 
00283     optionsPanel
00284         .add(new JLabel(MonitorTranslate.get("options.display.buffer")));
00285     displayBuffer = new JTextField(0);
00286     displayBuffer.setText("" + graphDisplayFrequency);
00287     displayBuffer.addActionListener(this);
00288     displayBuffer.setAlignmentX(RIGHT_ALIGNMENT);
00289     optionsPanel.add(displayBuffer);
00290 
00291     optionsPanel.add(new JLabel(MonitorTranslate.get("options.frequency")));
00292     ffrequency = new JTextField(0);
00293     ffrequency.setText("" + graphFrequency);
00294     ffrequency.addActionListener(this);
00295     ffrequency.setAlignmentX(RIGHT_ALIGNMENT);
00296     optionsPanel.add(ffrequency);
00297 
00298     optionsPanel.add(new JLabel(MonitorTranslate.get("options.timeframe")));
00299     ftimeframe = new JTextField(0);
00300     ftimeframe.setText(graphTimeframe + "");
00301     ftimeframe.addActionListener(this);
00302     ftimeframe.setAlignmentX(RIGHT_ALIGNMENT);
00303     optionsPanel.add(ftimeframe);
00304 
00305     JButton optionConfirm = new JButton(MonitorTranslate.get("options.ok"));
00306     optionConfirm.setActionCommand(OPTIONS_APPLY);
00307     optionConfirm.addActionListener(this);
00308     optionsPanel.add(optionConfirm);
00309 
00310     JButton optionCancel = new JButton(MonitorTranslate.get("options.cancel"));
00311     optionCancel.setActionCommand(OPTIONS_CANCEL);
00312     optionCancel.addActionListener(this);
00313     optionsPanel.add(optionCancel);
00314 
00315     options.getContentPane().add(optionsPanel);
00316     options.setVisible(false);
00317     options.setDefaultCloseOperation(HIDE_ON_CLOSE);
00318     options.validate();
00319 
00320     //Put the final touches to the JFrame object
00321     validate();
00322     setVisible(true);
00323     this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
00324   }
00325 
00326   private void initConsole() throws Exception
00327   {
00328     // Get the menus with all commands
00329     this.setVisible(false);
00330     BorderLayout layout = new BorderLayout();
00331     JPanel pane = new JPanel(layout);
00332     if (displayController)
00333       pane.add(initializeControllerBar(), BorderLayout.NORTH);
00334     if (displayVirtualDatabase)
00335       pane.add(initializaDatabaseBar(displayBackends), BorderLayout.CENTER);
00336     jScroll = new JScrollPane();
00337     jScroll.getViewport().add(pane);
00338     getContentPane().add(jScroll, BorderLayout.CENTER);
00339 
00340     validate();
00341     this.setVisible(true);
00342   }
00343 
00344   private JLabel getHeaderLabel(String text)
00345   {
00346     JLabel label = new JLabel(text);
00347     label.setFont(headerFont);
00348     label.setAlignmentX(CENTER_ALIGNMENT);
00349     return label;
00350   }
00351 
00352   private JLabel getSubHeaderLabel(String text)
00353   {
00354     JLabel label = new JLabel(text);
00355     label.setFont(labelFont);
00356     label.setAlignmentX(CENTER_ALIGNMENT);
00357     return label;
00358   }
00359 
00360   private JToolBar initializeControllerBar()
00361   {
00362     JToolBar toolbar = new JToolBar(GRAPH_CONTROLLER, SwingConstants.VERTICAL);
00363     toolbar.setOrientation(SwingConstants.VERTICAL);
00364     toolbar.addMouseListener(this);
00365     toolbar.add(getHeaderLabel(GRAPH_CONTROLLER));
00366     toolbar.add(getGraphMenuItem(CONTROLLER_USED_MEMORY, "", ""));
00367     toolbar.add(getGraphMenuItem(CONTROLLER_THREADS_NUMBER, "", ""));
00368     toolbar.add(getGraphMenuItem(CONTROLLER_WORKER_PENDING_QUEUE, "", ""));
00369     toolbar.add(getGraphMenuItem(CONTROLLER_IDLE_WORKER_THREADS, "", ""));
00370     toolbar.setVisible(true);
00371     return toolbar;
00372   }
00373 
00374   private JToolBar initializeCacheBar(String vdb)
00375   {
00376     JToolBar toolbar = new JToolBar(GRAPH_CACHE, SwingConstants.VERTICAL);
00377 
00378     try
00379     {
00380       if (virtualDatabaseMBean.hasResultCache() == false)
00381         toolbar.setEnabled(false);
00382       else
00383       {
00384         toolbar.add(getSubHeaderLabel(GRAPH_CACHE + " [" + vdb + "]"));
00385         toolbar.add(getGraphMenuItem(CACHE_STATS_COUNT_HITS, vdb, ""));
00386         toolbar.add(getGraphMenuItem(CACHE_STATS_COUNT_INSERT, vdb, ""));
00387         toolbar.add(getGraphMenuItem(CACHE_STATS_COUNT_SELECT, vdb, ""));
00388         toolbar.add(getGraphMenuItem(CACHE_STATS_HITS_PERCENTAGE, vdb, ""));
00389         toolbar.add(getGraphMenuItem(CACHE_STATS_NUMBER_ENTRIES, vdb, ""));
00390       }
00391     }
00392     catch (Exception e)
00393     {
00394       toolbar.setEnabled(false);
00395     }
00396     return toolbar;
00397   }
00398 
00399   private JToolBar initializeSchedulerBar(String vdb)
00400   {
00401     JToolBar toolbar = new JToolBar(GRAPH_SCHEDULER, SwingConstants.VERTICAL);
00402     toolbar.add(getSubHeaderLabel(GRAPH_SCHEDULER + " [" + vdb + "]"));
00403     toolbar.add(getGraphMenuItem(SCHEDULER_NUMBER_READ, vdb, ""));
00404     toolbar.add(getGraphMenuItem(SCHEDULER_NUMBER_REQUESTS, vdb, ""));
00405     toolbar.add(getGraphMenuItem(SCHEDULER_NUMBER_WRITES, vdb, ""));
00406     toolbar.add(getGraphMenuItem(SCHEDULER_PENDING_TRANSACTIONS, vdb, ""));
00407     toolbar.add(getGraphMenuItem(SCHEDULER_PENDING_WRITES, vdb, ""));
00408     return toolbar;
00409   }
00410 
00411   private JToolBar initializaBackendBar(String vdb, String backendName)
00412   {
00413     JToolBar backendMenu = new JToolBar(GRAPH_BACKENDS, SwingConstants.VERTICAL);
00414     backendMenu.add(getSubHeaderLabel(GRAPH_BACKENDS + " [" + backendName
00415         + " on " + vdb + "]"));
00416     backendMenu.add(getGraphMenuItem(BACKEND_ACTIVE_TRANSACTION, vdb,
00417         backendName));
00418     backendMenu
00419         .add(getGraphMenuItem(BACKEND_PENDING_REQUESTS, vdb, backendName));
00420     backendMenu.add(getGraphMenuItem(BACKEND_TOTAL_ACTIVE_CONNECTIONS, vdb,
00421         backendName));
00422     backendMenu.add(getGraphMenuItem(BACKEND_TOTAL_REQUEST, vdb, backendName));
00423     backendMenu.add(getGraphMenuItem(BACKEND_TOTAL_READ_REQUEST, vdb,
00424         backendName));
00425     backendMenu.add(getGraphMenuItem(BACKEND_TOTAL_WRITE_REQUEST, vdb,
00426         backendName));
00427     backendMenu.add(getGraphMenuItem(BACKEND_TOTAL_TRANSACTIONS, vdb,
00428         backendName));
00429     return backendMenu;
00430   }
00431 
00432   private JToolBar initializaDatabaseBar(boolean dispBackends) throws Exception
00433   {
00434     JToolBar toolbar = new JToolBar(GRAPH_VIRTUAL_DATABASE, JToolBar.VERTICAL);
00435     toolbar.addMouseListener(this);
00436 
00437     ArrayList dbs = controllerMBean.getVirtualDatabaseNames();
00438     ArrayList backends;
00439     String vdb = null;
00440     for (int i = 0; i < dbs.size(); i++)
00441     {
00442       vdb = (String) dbs.get(i);
00443       toolbar.add(getHeaderLabel(GRAPH_VIRTUAL_DATABASE + " [" + vdb + "]"));
00444       // Virtual Database main graphs
00445       toolbar.add(getGraphMenuItem(DATABASES_ACTIVE_THREADS, vdb, ""));
00446       toolbar.add(getGraphMenuItem(DATABASES_NUMBER_OF_THREADS, vdb, ""));
00447       toolbar.add(getGraphMenuItem(DATABASES_PENDING_CONNECTIONS, vdb, ""));
00448       // Cache
00449       toolbar.add(initializeCacheBar(vdb));
00450       // Scheduler
00451       toolbar.add(initializeSchedulerBar(vdb));
00452 
00453       if (dispBackends)
00454       { // Backends
00455         backends = virtualDatabaseMBean.getAllBackendNames();
00456         for (int j = 0; j < backends.size(); j++)
00457         {
00458           String backendName = (String) backends.get(j);
00459           toolbar.add(initializaBackendBar(vdb, backendName));
00460         }
00461       }
00462     }
00463     return toolbar;
00464   }
00465 
00466   private JComponent getGraphMenuItem(int type, String virtualDbName,
00467       String targetName)
00468   {
00469     String name = DataCollectionNames.get(type);
00470     JComboBox item = new JComboBox(comboBoxesItems);
00471     item.setFont(boxFont);
00472     item.setName(name);
00473     item.addActionListener(this);
00474     item.addMouseListener(this);
00475     if (virtualDbName == null)
00476       virtualDbName = "";
00477     if (targetName == null)
00478       targetName = "";
00479     String actionCommand = getBackendActionCommand(name, virtualDbName,
00480         targetName);
00481     actionCommand = actionCommand.trim();
00482     item.setActionCommand(actionCommand);
00483     item.setVisible(true);
00484     BorderLayout layout = new BorderLayout();
00485     JPanel panel = new JPanel(layout);
00486     JLabel label = new JLabel(name);
00487     label.setAlignmentX(CENTER_ALIGNMENT);
00488     label.setFont(labelFont);
00489     panel.add(label, BorderLayout.WEST);
00490     panel.add(item, BorderLayout.EAST);
00491 
00492     comboBoxes.put(actionCommand, item);
00493     return panel;
00494   }
00495 
00496   /**
00497    * Get the backend action command for displaying monitoring window
00498    * 
00499    * @param typeName type of info to monitor
00500    * @param vdbName database name
00501    * @param backendName backend name
00502    * @return <code>String</code> describing the command
00503    */
00504   public static String getBackendActionCommand(String typeName, String vdbName,
00505       String backendName)
00506   {
00507     return "graph " + typeName.toLowerCase().replace(' ', '_') + " " + vdbName
00508         + " " + backendName;
00509   }
00510 
00511   /**
00512    * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
00513    */
00514   public void mouseClicked(MouseEvent e)
00515   {
00516     status(e.getComponent().getName() + " was clicked");
00517   }
00518 
00519   /**
00520    * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
00521    */
00522   public void mouseEntered(MouseEvent e)
00523   {
00524 
00525   }
00526 
00527   /**
00528    * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
00529    */
00530   public void mouseExited(MouseEvent e)
00531   {
00532 
00533   }
00534 
00535   /**
00536    * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
00537    */
00538   public void mousePressed(MouseEvent e)
00539   {
00540 
00541   }
00542 
00543   /**
00544    * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
00545    */
00546   public void mouseReleased(MouseEvent e)
00547   {
00548 
00549   }
00550 
00551   private void doSaveConfiguration() throws IOException
00552   {
00553     Iterator iter = comboBoxes.keySet().iterator();
00554     File f = new File("monitor.properties");
00555     BufferedOutputStream bof = new BufferedOutputStream(new FileOutputStream(f));
00556     String temp;
00557     while (iter.hasNext())
00558     {
00559       Object o = iter.next();
00560       String key = o.toString().trim().replace(' ', '.');
00561       JComboBox box = (JComboBox) comboBoxes.get(o);
00562       temp = (key + "=" + box.getSelectedItem())
00563           + System.getProperty("line.separator");
00564       bof.write(temp.getBytes());
00565     }
00566 
00567     Iterator iter2 = windows.keySet().iterator();
00568     Window win;
00569     Point p;
00570     String name, winX, winY;
00571     while (iter2.hasNext())
00572     {
00573       win = ((Window) windows.get(iter2.next()));
00574       p = win.getLocation();
00575       name = win.getName().trim().replace(' ', '.');
00576       winX = name + ".X=" + (int) p.getX()
00577           + System.getProperty("line.separator");
00578       winY = name + ".Y=" + (int) p.getY()
00579           + System.getProperty("line.separator");
00580       bof.write(winX.getBytes());
00581       bof.write(winY.getBytes());
00582     }
00583     bof.write(("options.repeat=" + graphRepeat + System
00584         .getProperty("line.separator")).getBytes());
00585     bof.write(("options.timeframe=" + graphTimeframe + System
00586         .getProperty("line.separator")).getBytes());
00587     bof.write(("options.frequency=" + graphFrequency + System
00588         .getProperty("line.separator")).getBytes());
00589     bof.write(("options.displayfrequency=" + graphDisplayFrequency + System
00590         .getProperty("line.separator")).getBytes());
00591 
00592     bof.flush();
00593     bof.close();
00594   }
00595 
00596   private void doLoadConfiguration() throws IOException
00597   {
00598     closeAllWindows();
00599     isLoading = true;
00600     File f = new File("monitor.properties");
00601     PropertyResourceBundle props = new PropertyResourceBundle(
00602         new FileInputStream(f));
00603     Enumeration enume = props.getKeys();
00604     String key = "", keyr = "", value = "";
00605     JFrame frame;
00606 
00607     try
00608     {
00609       graphRepeat = Integer
00610           .parseInt((String) props.getObject("options.repeat"));
00611       frepeat.setText("" + graphRepeat);
00612       graphFrequency = Integer.parseInt((String) props
00613           .getObject("options.frequency"));
00614       ffrequency.setText("" + graphFrequency);
00615       graphTimeframe = Integer.parseInt((String) props
00616           .getObject("options.timeframe"));
00617       ftimeframe.setText("" + graphTimeframe);
00618       graphDisplayFrequency = Integer.parseInt((String) props
00619           .getObject("options.displayfrequency"));
00620       displayBuffer.setText("" + graphDisplayFrequency);
00621     }
00622     catch (Exception e)
00623     {
00624       error(e.getMessage());
00625     }
00626 
00627     while (enume.hasMoreElements())
00628     {
00629       key = (String) enume.nextElement();
00630       value = (String) props.getObject(key);
00631       if (key.startsWith("options"))
00632       {
00633         // done
00634       }
00635       else if (key.endsWith(".X") || key.endsWith(".Y"))
00636       {
00637         // do nothing
00638       }
00639       else
00640       {
00641         if (value.equals(COMBO_FLOATING))
00642           try
00643           {
00644             frame = graph(key);
00645             keyr = key.trim().replace('.', ' ');
00646             //System.out.println(key);
00647             JComboBox box = (JComboBox) comboBoxes.get(keyr);
00648             if (box != null)
00649               box.setSelectedItem(COMBO_FLOATING);
00650             try
00651             {
00652               int x = Integer.parseInt((String) props.getObject(key + ".X"));
00653               int y = Integer.parseInt((String) props.getObject(key + ".Y"));
00654 
00655               //Window win = ((Window) windows.get(keyr));
00656               //System.out.println(frame.getName()+"-"+x+"-"+y);
00657               frame.setLocation(x, y);
00658               //win.getComponent(0).setLocation(x, y);
00659               //win.setVisible(true);
00660               //win.validate();
00661             }
00662             catch (Exception e)
00663             {
00664               // ignore does not exist
00665               error(e.getMessage());
00666             }
00667           }
00668           catch (Exception e)
00669           {
00670             // cannot load ...
00671           }
00672       }
00673     }
00674     isLoading = false;
00675   }
00676 
00677   /**
00678    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
00679    */
00680   public void actionPerformed(ActionEvent e)
00681   {
00682     if (isLoading)
00683       return;
00684     String actionCommand = e.getActionCommand();
00685     status("Action:" + actionCommand);
00686     if (actionCommand.equals(COMMAND_SAVE))
00687     {
00688       try
00689       {
00690         doSaveConfiguration();
00691       }
00692       catch (IOException e1)
00693       {
00694         // TODO: display dialog box
00695         //        Dialog d = new Dialog(this, "Saving Failed");
00696         //        d.add(new JLabel("Saving Failed because of:" + e1.getMessage()));
00697         //        d.setVisible(true);
00698         //        d.setSize(100,50);
00699         //        d.setModal(true);
00700         //d.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
00701         e1.printStackTrace();
00702       }
00703     }
00704     else if (actionCommand.equals(COMMAND_LOAD))
00705     {
00706       try
00707       {
00708         doLoadConfiguration();
00709       }
00710       catch (IOException e1)
00711       {
00712         // TODO: display dialog box
00713         e1.printStackTrace();
00714       }
00715     }
00716     else if (actionCommand.equals(OPTIONS_APPLY))
00717     {
00718 
00719       try
00720       {
00721         graphRepeat = Integer.parseInt(frepeat.getText());
00722         graphTimeframe = Integer.parseInt(ftimeframe.getText());
00723         graphFrequency = Integer.parseInt(ffrequency.getText());
00724         graphDisplayFrequency = Integer.parseInt(displayBuffer.getText());
00725         options.setVisible(false);
00726       }
00727       catch (Exception failed)
00728       {
00729         error(failed.getMessage());
00730       }
00731     }
00732     else if (actionCommand.equals(OPTIONS_CANCEL))
00733     {
00734       options.setVisible(false);
00735       frepeat.setText(graphRepeat + "");
00736       ftimeframe.setText(graphTimeframe + "");
00737       ffrequency.setText(graphFrequency + "");
00738     }
00739     else if (actionCommand.equals(COMMAND_REFRESH))
00740     {
00741       try
00742       {
00743         initConsole();
00744       }
00745       catch (Exception error)
00746       {
00747         error(error.getMessage());
00748       }
00749     }
00750     else if (actionCommand.equals(COMMAND_OPTIONS))
00751     {
00752       options.setVisible(true);
00753     }
00754     else if (actionCommand.equals(COMMAND_CLOSE))
00755     {
00756       closeAllWindows();
00757       this.dispose();
00758     }
00759     else if (actionCommand.equals(COMMAND_CLOSE_GRAPHS))
00760     {
00761       closeAllWindows();
00762     }
00763     Object o = e.getSource();
00764     if (o instanceof JComboBox)
00765     {
00766       JComboBox box = (JComboBox) o;
00767       String selected = (String) box.getSelectedItem();
00768       status(selected.toString());
00769       try
00770       {
00771         Window win = (Window) windows.get(e.getActionCommand().trim());
00772         if (!selected.equals(COMBO_HIDE))
00773         {
00774           if (win == null)
00775           {
00776             graph(e.getActionCommand());
00777           }
00778         }
00779         else
00780         {
00781           windows.remove(win.getName());
00782           win.setVisible(false);
00783           win.dispose();
00784         }
00785       }
00786       catch (Exception f)
00787       {
00788         error(f.getMessage());
00789       }
00790     }
00791 
00792   }
00793 
00794   private void closeAllWindows()
00795   {
00796     while (true)
00797     {
00798       try
00799       {
00800         Iterator iter = windows.keySet().iterator();
00801         while (iter.hasNext())
00802         {
00803           Window win = ((Window) windows.get(iter.next()));
00804           JComboBox box = (JComboBox) comboBoxes.get(win.getName());
00805           if (box != null)
00806             box.setSelectedItem(COMBO_HIDE);
00807         }
00808         break;
00809       }
00810       catch (RuntimeException e)
00811       {
00812         //concurrent modification exception
00813         continue;
00814       }
00815     }
00816   }
00817 
00818   private void status(String message)
00819   {
00820     label.setBackground(Color.white);
00821     label.setText(message);
00822   }
00823 
00824   private void error(String message)
00825   {
00826     label.setBackground(Color.red);
00827     label.setText(message);
00828   }
00829 
00830   /**
00831    * Starts a new graph
00832    * 
00833    * @param command command line
00834    * @throws DataCollectorException if fails
00835    */
00836   private JFrame graph(String command) throws DataCollectorException
00837   {
00838 
00839     return graph(command, dataCollectorMBean, graphRepeat, graphTimeframe,
00840         graphFrequency, graphDisplayFrequency, this);
00841   }
00842 
00843   /**
00844    * Starts a graph !
00845    * 
00846    * @param command graph command
00847    * @param dataCollectorMBean jmx client to get info from
00848    * @param graphRepeat parameter
00849    * @param graphTimeframe parameter
00850    * @param graphFrequency parameter
00851    * @param graphDisplayFrequency parameter
00852    * @param listener to receive updates
00853    * @return <code>JFrame</code> containing the monitoring window
00854    * @throws DataCollectorException if an error occurs
00855    */
00856   public static final JFrame graph(String command,
00857       DataCollectorMBean dataCollectorMBean, int graphRepeat,
00858       int graphTimeframe, int graphFrequency, int graphDisplayFrequency,
00859       WindowListener listener) throws DataCollectorException
00860   {
00861     // Used for saving configuration
00862     command = command.replace('.', ' ');
00863 
00864     StringTokenizer tokenizer = new StringTokenizer(command, " ");
00865     String token0 = tokenizer.nextToken();
00866     if (token0.equals("graph"))
00867     {
00868       String token1 = tokenizer.nextToken();
00869       int type = DataCollectionNames.getTypeFromCommand(token1);
00870       String token2 = (tokenizer.hasMoreTokens()) ? tokenizer.nextToken() : "";
00871       String token3 = (tokenizer.hasMoreTokens()) ? tokenizer.nextToken() : "";
00872       AbstractDataCollector collector = dataCollectorMBean
00873           .retrieveDataCollectorInstance(type, token3, token2);
00874       MonitoringGraph graph = new MonitoringGraph(collector, dataCollectorMBean);
00875       graph.setRepeat(graphRepeat);
00876       graph.setTimeFrame(graphTimeframe);
00877       graph.setFrequency(graphFrequency);
00878       graph.setDisplayFrequency(graphDisplayFrequency);
00879       graph.start();
00880       graph.setText(command);
00881       // Do not do before, as frame is null before starts of thread!
00882       if (listener != null)
00883         graph.getFrame().addWindowListener(listener);
00884       graph.getFrame().setName(command.trim());
00885       return graph.getFrame();
00886     }
00887     else
00888     {
00889       return null;
00890     }
00891   }
00892 
00893   /**
00894    * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
00895    */
00896   public void windowActivated(WindowEvent e)
00897   {
00898 
00899   }
00900 
00901   /**
00902    * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
00903    */
00904   public void windowClosed(WindowEvent e)
00905   {
00906 
00907   }
00908 
00909   /**
00910    * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
00911    */
00912   public void windowClosing(WindowEvent e)
00913   {
00914     Window win = e.getWindow();
00915     JComboBox box = (JComboBox) comboBoxes.get(win.getName());
00916     windows.remove(win.getName());
00917     if (box != null)
00918       box.setSelectedIndex(0);
00919     status(win.getName() + " is closing");
00920   }
00921 
00922   /**
00923    * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
00924    */
00925   public void windowDeactivated(WindowEvent e)
00926   {
00927 
00928   }
00929 
00930   /**
00931    * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
00932    */
00933   public void windowDeiconified(WindowEvent e)
00934   {
00935 
00936   }
00937 
00938   /**
00939    * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
00940    */
00941   public void windowIconified(WindowEvent e)
00942   {
00943 
00944   }
00945 
00946   /**
00947    * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
00948    */
00949   public void windowOpened(WindowEvent e)
00950   {
00951     Window win = e.getWindow();
00952     status(win.getName() + " has opened");
00953     windows.put(win.getName(), e.getWindow());
00954   }
00955 }

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