KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > gui > CjdbcGuiListener


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): Emmanuel Cecchet.
23  */

24
25 package org.objectweb.cjdbc.console.gui;
26
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.awt.event.FocusEvent JavaDoc;
30 import java.awt.event.FocusListener JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.awt.event.MouseListener JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35 import javax.management.MBeanAttributeInfo JavaDoc;
36 import javax.management.MBeanOperationInfo JavaDoc;
37 import javax.management.Notification JavaDoc;
38 import javax.management.NotificationListener JavaDoc;
39 import javax.management.ObjectName JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JList JavaDoc;
42 import javax.swing.JTable JavaDoc;
43 import javax.swing.event.ListSelectionEvent JavaDoc;
44 import javax.swing.event.ListSelectionListener JavaDoc;
45
46 import org.objectweb.cjdbc.common.jmx.notifications.CjdbcNotificationList;
47 import org.objectweb.cjdbc.common.jmx.notifications.JmxNotification;
48 import org.objectweb.cjdbc.console.gui.constants.GuiCommands;
49 import org.objectweb.cjdbc.console.gui.constants.GuiConstants;
50 import org.objectweb.cjdbc.console.gui.model.AttributeModel;
51 import org.objectweb.cjdbc.console.gui.model.OperationModel;
52 import org.objectweb.cjdbc.console.gui.objects.DatabaseObject;
53
54 /**
55  * This class defines a CjdbcGuiListener
56  *
57  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
58  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
59  * </a>
60  * @version 1.0
61  */

62 public class CjdbcGuiListener
63     implements
64       MouseListener JavaDoc,
65       ActionListener JavaDoc,
66       NotificationListener JavaDoc,
67       FocusListener JavaDoc,
68       ListSelectionListener JavaDoc
69 {
70   private CjdbcGui gui;
71   static final int JMX_SEQUENCE_CACHE = 100;
72   private Hashtable JavaDoc sequences;
73
74   /**
75    * Creates a new <code>CjdbcGuiListener.java</code> object
76    *
77    * @param gui the main frame
78    */

79   public CjdbcGuiListener(CjdbcGui gui)
80   {
81     this.gui = gui;
82   }
83
84   /**
85    * Handle jmx notifications
86    *
87    * @param notification the jmx notification from javax.managerment
88    * @param handback not used
89    */

90   public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
91   {
92     String JavaDoc xml = (String JavaDoc) notification.getUserData();
93     // If the content of the notification is null, just debug it
94
if (xml == null)
95     {
96       gui.appendDebugText("Got notification with null string");
97       return;
98     }
99     try
100     {
101       JmxNotification notif = JmxNotification
102           .createNotificationFromXmlString(xml);
103       gui.appendDebugText("----- Jmx Notification -------");
104       gui.appendDebugText(notif.toString());
105       gui.appendDebugText("-----------------------------------");
106       handleNotification(notif.getType(), notif);
107     }
108     catch (Exception JavaDoc e)
109     {
110       gui.appendDebugText("Exception while handling notification", e);
111     }
112   }
113
114   private void handleNotification(String JavaDoc type, JmxNotification notification)
115   {
116     // have we processed the notification already ?
117
if (processedSequence(notification.getSequence()))
118     {
119       gui.appendDebugText("Notification [" + notification.getSequence()
120           + "] already processed");
121       return;
122     }
123
124     if (type.equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_ADDED))
125     {
126       String JavaDoc backendName = notification
127           .getDataValue(CjdbcNotificationList.DATA_NAME);
128       String JavaDoc databaseName = notification
129           .getDataValue(CjdbcNotificationList.DATA_DATABASE);
130       String JavaDoc controller = notification.getControllerJmxName();
131       gui.actionLoadBackend(databaseName, backendName, controller, true);
132     }
133     else if (type.equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_ENABLED))
134     {
135       String JavaDoc backendName = notification
136           .getDataValue(CjdbcNotificationList.DATA_NAME);
137       gui.actionSetBackendState(backendName);
138     }
139     else if (type
140         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_ENABLED_WRITE))
141     {
142       String JavaDoc backendName = notification
143           .getDataValue(CjdbcNotificationList.DATA_NAME);
144       gui.actionSetBackendState(backendName);
145     }
146     else if (type
147         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_DISABLED))
148     {
149       String JavaDoc backendName = notification
150           .getDataValue(CjdbcNotificationList.DATA_NAME);
151       gui.actionSetBackendState(backendName);
152     }
153     else if (type
154         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_RECOVERING))
155     {
156       String JavaDoc backendName = notification
157           .getDataValue(CjdbcNotificationList.DATA_NAME);
158       gui.actionSetBackendState(backendName);
159     }
160     else if (type
161         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_BACKINGUP))
162     {
163       String JavaDoc backendName = notification
164           .getDataValue(CjdbcNotificationList.DATA_NAME);
165       gui.actionSetBackendState(backendName);
166     }
167     else if (type
168         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_DISABLING))
169     {
170       String JavaDoc backendName = notification
171           .getDataValue(CjdbcNotificationList.DATA_NAME);
172       gui.actionSetBackendState(backendName);
173     }
174     else if (type
175         .equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_REPLAYING))
176     {
177       String JavaDoc backendName = notification
178           .getDataValue(CjdbcNotificationList.DATA_NAME);
179       gui.actionSetBackendState(backendName,
180           GuiConstants.BACKEND_STATE_RECOVERY);
181       gui.actionSetBackendState(backendName);
182     }
183     else if (type.equals(CjdbcNotificationList.VIRTUALDATABASE_NEW_DUMP_LIST))
184     {
185       String JavaDoc databaseName = notification
186           .getDataValue(CjdbcNotificationList.DATA_DATABASE);
187       gui.publicActionLoadDumpList(databaseName);
188     }
189     else if (type.equals(CjdbcNotificationList.VIRTUALDATABASE_BACKEND_REMOVED))
190     {
191       String JavaDoc backendName = notification
192           .getDataValue(CjdbcNotificationList.DATA_NAME);
193       String JavaDoc controller = notification.getControllerJmxName();
194       gui.publicActionRemoveBackendFromGui(backendName, controller);
195     }
196     else
197     {
198       gui.appendDebugText("Jmx notification type not recognized:" + type);
199     }
200
201   }
202
203   /**
204    * Check whether we have received this notification already...
205    *
206    * @param sequence the sequence id
207    * @return <tt>true</tt> if this id was already received
208    */

209   private boolean processedSequence(String JavaDoc sequence)
210   {
211     if (sequences == null)
212     {
213       sequences = new Hashtable JavaDoc();
214       return false;
215     }
216     Object JavaDoc o = sequences.get(sequence);
217     if (o == null)
218     {
219       sequences.put(sequence, Boolean.TRUE);
220       if (sequences.size() > JMX_SEQUENCE_CACHE)
221         sequences.clear();
222       return false;
223     }
224     else
225       return true;
226   }
227
228   /**
229    * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
230    */

231   public void mouseClicked(MouseEvent JavaDoc e)
232   {
233
234     if (e.getClickCount() <= 1)
235     {
236       return;
237     }
238     if (e.getSource() instanceof JTable JavaDoc)
239     {
240       JTable JavaDoc table = (JTable JavaDoc) e.getSource();
241       if (table.getName().equals(GuiConstants.TABLE_JMX_ATTRIBUTES))
242       {
243         int row = table.getSelectedRow();
244         AttributeModel model = (AttributeModel) table.getModel();
245         MBeanAttributeInfo JavaDoc[] info = model.getInfo();
246         Object JavaDoc o = gui.mbeanList.getSelectedValue();
247         gui.appendDebugText("Got attribute selection for mbean:" + o
248             + " and attribute is:" + info[row].getName());
249         gui.getAttributeChangeDialog((ObjectName JavaDoc) o, info[row]);
250       }
251       if (table.getName().equals(GuiConstants.TABLE_JMX_OPERATIONS))
252       {
253         int row = table.getSelectedRow();
254         OperationModel model = (OperationModel) table.getModel();
255         MBeanOperationInfo JavaDoc[] info = model.getInfo();
256         Object JavaDoc o = gui.mbeanList.getSelectedValue();
257         gui.appendDebugText("Got operation selection for mbean:" + o
258             + " and operation is:" + info[row].getName());
259         gui.getOperationCallDialog((ObjectName JavaDoc) o, info[row]);
260       }
261     }
262   }
263
264   /**
265    * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
266    */

267   public void mousePressed(MouseEvent JavaDoc e)
268   {
269
270   }
271
272   /**
273    * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
274    */

275   public void mouseReleased(MouseEvent JavaDoc e)
276   {
277
278   }
279
280   /**
281    * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
282    */

283   public void mouseEntered(MouseEvent JavaDoc e)
284   {
285
286   }
287
288   /**
289    * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
290    */

291   public void mouseExited(MouseEvent JavaDoc e)
292   {
293
294   }
295
296   /**
297    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
298    */

299   public void actionPerformed(ActionEvent JavaDoc e)
300   {
301     String JavaDoc action = e.getActionCommand();
302     gui.appendDebugText("Got action:" + action);
303     if (action.equals(GuiCommands.COMMAND_QUIT))
304     {
305       gui.publicActionQuit();
306     }
307     else if (action.equals(GuiCommands.COMMAND_ADD_CONTROLLER))
308     {
309       gui.publicActionAddControllerView();
310     }
311     else if (action.equals(GuiCommands.COMMAND_SAVE_CONFIGURATION_FILE))
312     {
313       gui.publicActionSaveConfigurationFile();
314     }
315     if (action.equals(GuiCommands.COMMAND_CLEAN_LOGGING_PANEL))
316     {
317       gui.publicActioncleanLoggingPane();
318     }
319     else if (action.equals(GuiCommands.COMMAND_ADD_CONFIG_FILE))
320     {
321       gui.publicActionAddXmlFile();
322     }
323     else if (action.equals(GuiCommands.COMMAND_SELECT_CONTROLLER))
324     {
325       String JavaDoc controllerName = ((JButton JavaDoc) e.getSource()).getName();
326       gui.appendDebugText("Changed controller selection to:" + controllerName);
327       gui.publicActionSelectNewController(controllerName);
328     }
329     else if (action.equals(GuiCommands.COMMAND_SELECT_DATABASE))
330     {
331       DatabaseObject source = (DatabaseObject) e.getSource();
332       String JavaDoc databaseName = source.getName();
333       gui.appendDebugText("Changed database selection to:" + databaseName);
334       gui.publicActionSelectNewDatabase(databaseName);
335     }
336     else if (action.equals(GuiCommands.COMMAND_ADD_CONTROLLER_CANCEL))
337     {
338       gui.newControllerFrame.setVisible(false);
339     }
340     else if (action.equals(GuiCommands.COMMAND_ADD_CONTROLLER_APPROVE))
341     {
342       gui.publicActionAddController();
343     }
344     else if (action.equals(GuiCommands.COMMAND_SAVE_CONFIGURATION_FILE))
345     {
346       gui.publicActionSaveConfigurationFile();
347     }
348     else if (action.equals(GuiCommands.COMMAND_CLEAN_DEBUG_BUFFER))
349     {
350       gui.publicActionCleanDebugBuffer();
351     }
352     else if (action.equals(GuiCommands.COMMAND_DATABASE_AUTHENTICATE))
353     {
354       gui.publicActionLoadAuthenticatedDatabase();
355     }
356     else if (action.equals(GuiCommands.COMMAND_CREATE_BACKEND_APPROVE))
357     {
358       gui.publicActionCreateBackendExecute();
359     }
360     else if (action.equals(GuiCommands.COMMAND_CREATE_BACKEND_CANCEL))
361     {
362       gui.newBackendFrame.setVisible(false);
363     }
364     else if (action.equals(GuiCommands.COMMAND_HIDE_CHECKPOINT_FRAME))
365     {
366       gui.selectCheckpointFrame.setVisible(false);
367     }
368     else if (action.equals(GuiCommands.COMMAND_HIDE_SHUTDOWN_FRAME))
369     {
370       gui.selectShutdownFrame.setVisible(false);
371     }
372     else if (action.equals(GuiCommands.COMMAND_HIDE_BACKUP_FRAME))
373     {
374       gui.inputBackupFrame.setVisible(false);
375     }
376     else if (action.equals(GuiCommands.COMMAND_MONITOR_CURRENT_CONTROLLER))
377     {
378       gui.publicActionStartMonitor(gui.getSelectedController(), true, true,
379           true);
380     }
381
382   }
383
384   /**
385    * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
386    */

387   public void focusGained(FocusEvent JavaDoc e)
388   {
389     gui.publicActionTileJmxFrames(false);
390     gui.publicActionRefreshMBeans();
391   }
392
393   /**
394    * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
395    */

396   public void focusLost(FocusEvent JavaDoc e)
397   {
398
399   }
400
401   /**
402    * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
403    */

404   public void valueChanged(ListSelectionEvent JavaDoc e)
405   {
406     JList JavaDoc list = (JList JavaDoc) e.getSource();
407     ObjectName JavaDoc name = (ObjectName JavaDoc) list.getSelectedValue();
408     gui.publicActionRefreshMBeanAttributes(name);
409     gui.publicActionRefreshMBeanMethods(name);
410   }
411 }
Popular Tags