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

CjdbcGuiListener.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.gui;
00026 
00027 import java.awt.event.ActionEvent;
00028 import java.awt.event.ActionListener;
00029 import java.awt.event.FocusEvent;
00030 import java.awt.event.FocusListener;
00031 import java.awt.event.MouseEvent;
00032 import java.awt.event.MouseListener;
00033 import java.util.Hashtable;
00034 
00035 import javax.management.MBeanAttributeInfo;
00036 import javax.management.MBeanOperationInfo;
00037 import javax.management.Notification;
00038 import javax.management.NotificationListener;
00039 import javax.management.ObjectName;
00040 import javax.swing.JButton;
00041 import javax.swing.JList;
00042 import javax.swing.JTable;
00043 import javax.swing.event.ListSelectionEvent;
00044 import javax.swing.event.ListSelectionListener;
00045 
00046 import org.objectweb.cjdbc.common.jmx.notifications.CjdbcNotificationList;
00047 import org.objectweb.cjdbc.common.jmx.notifications.JmxNotification;
00048 import org.objectweb.cjdbc.console.gui.constants.GuiCommands;
00049 import org.objectweb.cjdbc.console.gui.constants.GuiConstants;
00050 import org.objectweb.cjdbc.console.gui.model.AttributeModel;
00051 import org.objectweb.cjdbc.console.gui.model.OperationModel;
00052 import org.objectweb.cjdbc.console.gui.objects.DatabaseObject;
00053 
00054 /**
00055  * This class defines a CjdbcGuiListener
00056  * 
00057  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00058  * @author <a href="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
00059  *         </a>
00060  * @version 1.0
00061  */
00062 public class CjdbcGuiListener
00063     implements
00064       MouseListener,
00065       ActionListener,
00066       NotificationListener,
00067       FocusListener,
00068       ListSelectionListener
00069 {
00070   private CjdbcGui  gui;
00071   static final int  JMX_SEQUENCE_CACHE = 100;
00072   private Hashtable sequences;
00073 
00074   /**
00075    * Creates a new <code>CjdbcGuiListener.java</code> object
00076    * 
00077    * @param gui the main frame
00078    */
00079   public CjdbcGuiListener(CjdbcGui gui)
00080   {
00081     this.gui = gui;
00082   }
00083 
00084   /**
00085    * Handle jmx notifications
00086    * 
00087    * @param notification the jmx notification from javax.managerment
00088    * @param handback not used
00089    */
00090   public void handleNotification(Notification notification, Object handback)
00091   {
00092     String xml = (String) notification.getUserData();
00093     // If the content of the notification is null, just debug it
00094     if (xml == null)
00095     {
00096       gui.appendDebugText("Got notification with null string");
00097       return;
00098     }
00099     try
00100     {
00101       JmxNotification notif = JmxNotification
00102           .createNotificationFromXmlString(xml);
00103       gui.appendDebugText("----- Jmx Notification -------");
00104       gui.appendDebugText(notif.toString());
00105       gui.appendDebugText("-----------------------------------");
00106       handleNotification(notif.getType(), notif);
00107     }
00108     catch (Exception e)
00109     {
00110       gui.appendDebugText("Exception while handling notification", e);
00111     }
00112   }
00113 
00114   private void handleNotification(String type, JmxNotification notification)
00115   {
00116     // have we processed the notification already ?
00117     if (processedSequence(notification.getSequence()))
00118     {
00119       gui.appendDebugText("Notification [" + notification.getSequence()
00120           + "] already processed");
00121       return;
00122     }
00123 
00124     if (type.equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_ADDED))
00125     {
00126       String backendName = notification
00127           .getDataValue(CjdbcNotificationList.DATA_NAME);
00128       String databaseName = notification
00129           .getDataValue(CjdbcNotificationList.DATA_DATABASE);
00130       String controller = notification.getControllerJmxName();
00131       gui.actionLoadBackend(databaseName, backendName, controller, true);
00132     }
00133     else if (type.equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_ENABLED))
00134     {
00135       String backendName = notification
00136           .getDataValue(CjdbcNotificationList.DATA_NAME);
00137       gui.actionSetBackendState(backendName);
00138     }
00139     else if (type
00140         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_ENABLED_WRITE))
00141     {
00142       String backendName = notification
00143           .getDataValue(CjdbcNotificationList.DATA_NAME);
00144       gui.actionSetBackendState(backendName);
00145     }
00146     else if (type
00147         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_DISABLED))
00148     {
00149       String backendName = notification
00150           .getDataValue(CjdbcNotificationList.DATA_NAME);
00151       gui.actionSetBackendState(backendName);
00152     }
00153     else if (type
00154         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_RECOVERING))
00155     {
00156       String backendName = notification
00157           .getDataValue(CjdbcNotificationList.DATA_NAME);
00158       gui.actionSetBackendState(backendName);
00159     }
00160     else if (type
00161         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_BACKINGUP))
00162     {
00163       String backendName = notification
00164           .getDataValue(CjdbcNotificationList.DATA_NAME);
00165       gui.actionSetBackendState(backendName);
00166     }
00167     else if (type
00168         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_DISABLING))
00169     {
00170       String backendName = notification
00171           .getDataValue(CjdbcNotificationList.DATA_NAME);
00172       gui.actionSetBackendState(backendName);
00173     }
00174     else if (type
00175         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_REPLAYING))
00176     {
00177       String backendName = notification
00178           .getDataValue(CjdbcNotificationList.DATA_NAME);
00179       gui.actionSetBackendState(backendName,
00180           GuiConstants.BACKEND_STATE_RECOVERY);
00181       gui.actionSetBackendState(backendName);
00182     }
00183     else if (type.equals(CjdbcNotificationList.VIRTUALDATABASE_NEW_DUMP_LIST))
00184     {
00185       String databaseName = notification
00186           .getDataValue(CjdbcNotificationList.DATA_DATABASE);
00187       gui.publicActionLoadDumpList(databaseName);
00188     }
00189     else if (type.equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_REMOVED))
00190     {
00191       String backendName = notification
00192           .getDataValue(CjdbcNotificationList.DATA_NAME);
00193       String controller = notification.getControllerJmxName();
00194       gui.publicActionRemoveBackendFromGui(backendName, controller);
00195     }
00196     else
00197     {
00198       gui.appendDebugText("Jmx notification type not recognized:" + type);
00199     }
00200 
00201   }
00202 
00203   /**
00204    * Check whether we have received this notification already...
00205    * 
00206    * @param sequence the sequence id
00207    * @return <tt>true</tt> if this id was already received
00208    */
00209   private boolean processedSequence(String sequence)
00210   {
00211     if (sequences == null)
00212     {
00213       sequences = new Hashtable();
00214       return false;
00215     }
00216     Object o = sequences.get(sequence);
00217     if (o == null)
00218     {
00219       sequences.put(sequence, Boolean.TRUE);
00220       if (sequences.size() > JMX_SEQUENCE_CACHE)
00221         sequences.clear();
00222       return false;
00223     }
00224     else
00225       return true;
00226   }
00227 
00228   /**
00229    * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
00230    */
00231   public void mouseClicked(MouseEvent e)
00232   {
00233 
00234     if (e.getClickCount() <= 1)
00235     {
00236       return;
00237     }
00238     if (e.getSource() instanceof JTable)
00239     {
00240       JTable table = (JTable) e.getSource();
00241       if (table.getName().equals(GuiConstants.TABLE_JMX_ATTRIBUTES))
00242       {
00243         int row = table.getSelectedRow();
00244         AttributeModel model = (AttributeModel) table.getModel();
00245         MBeanAttributeInfo[] info = model.getInfo();
00246         Object o = gui.mbeanList.getSelectedValue();
00247         gui.appendDebugText("Got attribute selection for mbean:" + o
00248             + " and attribute is:" + info[row].getName());
00249         gui.getAttributeChangeDialog((ObjectName) o, info[row]);
00250       }
00251       if (table.getName().equals(GuiConstants.TABLE_JMX_OPERATIONS))
00252       {
00253         int row = table.getSelectedRow();
00254         OperationModel model = (OperationModel) table.getModel();
00255         MBeanOperationInfo[] info = model.getInfo();
00256         Object o = gui.mbeanList.getSelectedValue();
00257         gui.appendDebugText("Got operation selection for mbean:" + o
00258             + " and operation is:" + info[row].getName());
00259         gui.getOperationCallDialog((ObjectName) o, info[row]);
00260       }
00261     }
00262   }
00263 
00264   /**
00265    * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
00266    */
00267   public void mousePressed(MouseEvent e)
00268   {
00269 
00270   }
00271 
00272   /**
00273    * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
00274    */
00275   public void mouseReleased(MouseEvent e)
00276   {
00277 
00278   }
00279 
00280   /**
00281    * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
00282    */
00283   public void mouseEntered(MouseEvent e)
00284   {
00285 
00286   }
00287 
00288   /**
00289    * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
00290    */
00291   public void mouseExited(MouseEvent e)
00292   {
00293 
00294   }
00295 
00296   /**
00297    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
00298    */
00299   public void actionPerformed(ActionEvent e)
00300   {
00301     String action = e.getActionCommand();
00302     gui.appendDebugText("Got action:" + action);
00303     if (action.equals(GuiCommands.COMMAND_QUIT))
00304     {
00305       gui.publicActionQuit();
00306     }
00307     else if (action.equals(GuiCommands.COMMAND_ADD_CONTROLLER))
00308     {
00309       gui.publicActionAddControllerView();
00310     }
00311     else if (action.equals(GuiCommands.COMMAND_SAVE_CONFIGURATION_FILE))
00312     {
00313       gui.publicActionSaveConfigurationFile();
00314     }
00315     if (action.equals(GuiCommands.COMMAND_CLEAN_LOGGING_PANEL))
00316     {
00317       gui.publicActioncleanLoggingPane();
00318     }
00319     else if (action.equals(GuiCommands.COMMAND_ADD_CONFIG_FILE))
00320     {
00321       gui.publicActionAddXmlFile();
00322     }
00323     else if (action.equals(GuiCommands.COMMAND_SELECT_CONTROLLER))
00324     {
00325       String controllerName = ((JButton) e.getSource()).getName();
00326       gui.appendDebugText("Changed controller selection to:" + controllerName);
00327       gui.publicActionSelectNewController(controllerName);
00328     }
00329     else if (action.equals(GuiCommands.COMMAND_SELECT_DATABASE))
00330     {
00331       DatabaseObject source = (DatabaseObject) e.getSource();
00332       String databaseName = source.getName();
00333       gui.appendDebugText("Changed database selection to:" + databaseName);
00334       gui.publicActionSelectNewDatabase(databaseName);
00335     }
00336     else if (action.equals(GuiCommands.COMMAND_ADD_CONTROLLER_CANCEL))
00337     {
00338       gui.newControllerFrame.setVisible(false);
00339     }
00340     else if (action.equals(GuiCommands.COMMAND_ADD_CONTROLLER_APPROVE))
00341     {
00342       gui.publicActionAddController();
00343     }
00344     else if (action.equals(GuiCommands.COMMAND_SAVE_CONFIGURATION_FILE))
00345     {
00346       gui.publicActionSaveConfigurationFile();
00347     }
00348     else if (action.equals(GuiCommands.COMMAND_CLEAN_DEBUG_BUFFER))
00349     {
00350       gui.publicActionCleanDebugBuffer();
00351     }
00352     else if (action.equals(GuiCommands.COMMAND_DATABASE_AUTHENTICATE))
00353     {
00354       gui.publicActionLoadAuthenticatedDatabase();
00355     }
00356     else if (action.equals(GuiCommands.COMMAND_CREATE_BACKEND_APPROVE))
00357     {
00358       gui.publicActionCreateBackendExecute();
00359     }
00360     else if (action.equals(GuiCommands.COMMAND_CREATE_BACKEND_CANCEL))
00361     {
00362       gui.newBackendFrame.setVisible(false);
00363     }
00364     else if (action.equals(GuiCommands.COMMAND_HIDE_CHECKPOINT_FRAME))
00365     {
00366       gui.selectCheckpointFrame.setVisible(false);
00367     }
00368     else if (action.equals(GuiCommands.COMMAND_HIDE_SHUTDOWN_FRAME))
00369     {
00370       gui.selectShutdownFrame.setVisible(false);
00371     }
00372     else if (action.equals(GuiCommands.COMMAND_HIDE_BACKUP_FRAME))
00373     {
00374       gui.inputBackupFrame.setVisible(false);
00375     }
00376     else if (action.equals(GuiCommands.COMMAND_MONITOR_CURRENT_CONTROLLER))
00377     {
00378       gui.publicActionStartMonitor(gui.getSelectedController(), true, true,
00379           true);
00380     }
00381 
00382   }
00383 
00384   /**
00385    * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
00386    */
00387   public void focusGained(FocusEvent e)
00388   {
00389     gui.publicActionTileJmxFrames(false);
00390     gui.publicActionRefreshMBeans();
00391   }
00392 
00393   /**
00394    * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
00395    */
00396   public void focusLost(FocusEvent e)
00397   {
00398 
00399   }
00400 
00401   /**
00402    * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
00403    */
00404   public void valueChanged(ListSelectionEvent e)
00405   {
00406     JList list = (JList) e.getSource();
00407     ObjectName name = (ObjectName) list.getSelectedValue();
00408     gui.publicActionRefreshMBeanAttributes(name);
00409     gui.publicActionRefreshMBeanMethods(name);
00410   }
00411 }

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