KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > monitoring > MonitoringConsole


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.monitoring;
26
27 import java.awt.BorderLayout JavaDoc;
28 import java.awt.Color JavaDoc;
29 import java.awt.Dimension JavaDoc;
30 import java.awt.Font JavaDoc;
31 import java.awt.GridLayout JavaDoc;
32 import java.awt.Point JavaDoc;
33 import java.awt.Toolkit JavaDoc;
34 import java.awt.Window JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.ActionListener JavaDoc;
37 import java.awt.event.MouseEvent JavaDoc;
38 import java.awt.event.MouseListener JavaDoc;
39 import java.awt.event.WindowEvent JavaDoc;
40 import java.awt.event.WindowListener JavaDoc;
41 import java.io.BufferedOutputStream JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44 import java.io.FileOutputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Enumeration JavaDoc;
48 import java.util.Hashtable JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.PropertyResourceBundle JavaDoc;
51 import java.util.StringTokenizer JavaDoc;
52 import java.util.Vector JavaDoc;
53
54 import javax.swing.JButton JavaDoc;
55 import javax.swing.JComboBox JavaDoc;
56 import javax.swing.JComponent JavaDoc;
57 import javax.swing.JFrame JavaDoc;
58 import javax.swing.JLabel JavaDoc;
59 import javax.swing.JMenu JavaDoc;
60 import javax.swing.JMenuBar JavaDoc;
61 import javax.swing.JMenuItem JavaDoc;
62 import javax.swing.JPanel JavaDoc;
63 import javax.swing.JScrollPane JavaDoc;
64 import javax.swing.JTextField JavaDoc;
65 import javax.swing.JToolBar JavaDoc;
66 import javax.swing.SwingConstants JavaDoc;
67
68 import org.objectweb.cjdbc.common.exceptions.DataCollectorException;
69 import org.objectweb.cjdbc.common.i18n.MonitorTranslate;
70 import org.objectweb.cjdbc.common.jmx.mbeans.ControllerMBean;
71 import org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean;
72 import org.objectweb.cjdbc.common.jmx.mbeans.VirtualDatabaseMBean;
73 import org.objectweb.cjdbc.common.monitor.AbstractDataCollector;
74 import org.objectweb.cjdbc.common.monitor.DataCollection;
75 import org.objectweb.cjdbc.common.monitor.DataCollectionNames;
76 import org.objectweb.cjdbc.console.jmx.RmiJmxClient;
77
78 /**
79  * New Monitoring Console bootstrap for starting stopping monitoring graphs
80  *
81  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
82  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
83  * </a>
84  * @version 1.0
85  */

86 public class MonitoringConsole extends JFrame JavaDoc
87     implements
88       MouseListener JavaDoc,
89       ActionListener JavaDoc,
90       WindowListener JavaDoc
91 {
92   // Window parameters
93
private int frameWidth = 400;
94   private int frameHeight = 700;
95
96   // Swing Components
97
private JLabel JavaDoc label;
98   private JScrollPane JavaDoc jScroll;
99
100   // MBean components
101
private ControllerMBean controllerMBean;
102   private VirtualDatabaseMBean virtualDatabaseMBean;
103   private DataCollectorMBean dataCollectorMBean;
104
105   // Menu components
106
private static final String JavaDoc GRAPH_CONTROLLER = MonitorTranslate
107                                                           .get("heading.controller");
108   private static final String JavaDoc GRAPH_VIRTUAL_DATABASE = MonitorTranslate
109                                                           .get("heading.virtualdatabase");
110   private static final String JavaDoc GRAPH_CACHE = MonitorTranslate
111                                                           .get("heading.cache");
112   private static final String JavaDoc GRAPH_SCHEDULER = MonitorTranslate
113                                                           .get("heading.scheduler");
114   private static final String JavaDoc GRAPH_BACKENDS = MonitorTranslate
115                                                           .get("heading.backends");
116
117   private Hashtable JavaDoc windows = new Hashtable JavaDoc();
118
119   private Vector JavaDoc comboBoxesItems = new Vector JavaDoc();
120   private Hashtable JavaDoc comboBoxes = new Hashtable JavaDoc();
121
122   // Fonts & Colors
123
private Font JavaDoc boxFont = new Font JavaDoc("Arial",
124                                                           Font.PLAIN, 10);
125   private Font JavaDoc labelFont = new Font JavaDoc("Arial",
126                                                           Font.BOLD, 10);
127   private Font JavaDoc headerFont = new Font JavaDoc("Arial",
128                                                           Font.BOLD, 12);
129   private Color JavaDoc toolBarColor = Color.white;
130
131   // Graph options
132
private int graphRepeat = -1;
133   private int graphTimeframe = 3600;
134   private int graphFrequency = 1000;
135   private int graphDisplayFrequency = 1;
136
137   // Actions
138
private static final String JavaDoc COMMAND_SAVE = MonitorTranslate
139                                                           .get("command.save");
140   private static final String JavaDoc COMMAND_LOAD = MonitorTranslate
141                                                           .get("command.load");
142   private static final String JavaDoc COMMAND_CLOSE_GRAPHS = MonitorTranslate
143                                                           .get("command.close.all");
144   private static final String JavaDoc COMMAND_OPTIONS = MonitorTranslate
145                                                           .get("command.set.options");
146   private static final String JavaDoc COMMAND_CLOSE = MonitorTranslate
147                                                           .get("command.quit");
148   private static final String JavaDoc COMMAND_REFRESH = MonitorTranslate
149                                                           .get("command.refresh");
150
151   private static final String JavaDoc OPTIONS_APPLY = "OptionsApply";
152   private static final String JavaDoc OPTIONS_CANCEL = "OptionsCancel";
153
154   // Combox
155
private static final String JavaDoc COMBO_HIDE = MonitorTranslate
156                                                           .get("command.hide");
157   private static final String JavaDoc COMBO_FLOATING = MonitorTranslate
158                                                           .get("command.float");
159
160   private boolean isLoading = false;
161
162   // Option window
163
private JFrame JavaDoc options;
164   private JTextField JavaDoc ftimeframe;
165   private JTextField JavaDoc ffrequency;
166   private JTextField JavaDoc frepeat;
167   private JTextField JavaDoc displayBuffer;
168
169   private boolean displayController;
170   private boolean displayVirtualDatabase;
171   private boolean displayBackends;
172
173   /**
174    * Creates a new <code>MonitoringConsole</code> object
175    *
176    * @param jmxUrl JMX URL
177    * @param controllerMBean controller MBean if controller monitoring must be
178    * activated
179    * @param virtualDatabaseMBean virtual database MBean if virtual database
180    * monitoring must be activated
181    * @param backends display backends monitoring menu
182    * @throws IOException if an error occurs
183    */

184   public MonitoringConsole(String JavaDoc jmxUrl, ControllerMBean controllerMBean,
185       VirtualDatabaseMBean virtualDatabaseMBean, boolean backends)
186       throws IOException JavaDoc
187   {
188     super(MonitorTranslate.get("monitor.frame.title", jmxUrl));
189
190     this.displayController = controllerMBean == null;
191     this.displayVirtualDatabase = virtualDatabaseMBean == null;
192     this.displayBackends = backends;
193
194     // Get MBeans reference
195
RmiJmxClient jmxClient = new RmiJmxClient(jmxUrl, null);
196     dataCollectorMBean = jmxClient.getDataCollectorProxy();
197     this.controllerMBean = controllerMBean;
198     this.virtualDatabaseMBean = virtualDatabaseMBean;
199
200     // Get options for combo boxes
201
comboBoxesItems.add(COMBO_HIDE);
202     comboBoxesItems.add(COMBO_FLOATING);
203
204     Toolkit JavaDoc toolkit;
205     Dimension JavaDoc dim;
206     int screenHeight, screenWidth;
207
208     // Initialize basic layout properties
209
setForeground(Color.white);
210     getContentPane().setLayout(new BorderLayout JavaDoc());
211
212     // Set the frame's display to be WIDTH x HEIGHT in the middle of the screen
213
toolkit = Toolkit.getDefaultToolkit();
214     dim = toolkit.getScreenSize();
215     screenHeight = dim.height;
216     screenWidth = dim.width;
217
218     // Reduce height by two if display only controller
219
if (displayController && (!displayVirtualDatabase) && (!displayBackends))
220       frameHeight = 270;
221
222     setBounds((screenWidth - frameWidth) / 2, (screenHeight - frameHeight) / 2,
223         frameWidth, frameHeight);
224
225     try
226     {
227       // Init Frame with dynamic content
228
initConsole();
229     }
230     catch (Exception JavaDoc e)
231     {
232       throw new IOException JavaDoc(e.getMessage());
233     }
234
235     // Status Bar
236
label = new JLabel JavaDoc("Select Graphs ...");
237     label.setFont(labelFont);
238     getContentPane().add(label, BorderLayout.SOUTH);
239     getContentPane().setBackground(toolBarColor);
240     getContentPane().setForeground(toolBarColor);
241
242     // Menu Bar
243
JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
244     JMenu JavaDoc menu = new JMenu JavaDoc(MonitorTranslate.get("monitor.menu.bar"));
245     JMenuItem JavaDoc item1 = new JMenuItem JavaDoc(COMMAND_SAVE);
246     JMenuItem JavaDoc item2 = new JMenuItem JavaDoc(COMMAND_LOAD);
247     JMenuItem JavaDoc item3 = new JMenuItem JavaDoc(COMMAND_CLOSE);
248     JMenuItem JavaDoc item4 = new JMenuItem JavaDoc(COMMAND_CLOSE_GRAPHS);
249     JMenuItem JavaDoc item5 = new JMenuItem JavaDoc(COMMAND_OPTIONS);
250     JMenuItem JavaDoc item6 = new JMenuItem JavaDoc(COMMAND_REFRESH);
251     item1.setBackground(toolBarColor);
252     item2.setBackground(toolBarColor);
253     item3.setBackground(toolBarColor);
254     item4.setBackground(toolBarColor);
255     item5.setBackground(toolBarColor);
256     item6.setBackground(toolBarColor);
257     menu.add(item1).addActionListener(this);
258     menu.add(item2).addActionListener(this);
259     menu.add(item4).addActionListener(this);
260     menu.add(item5).addActionListener(this);
261     menu.add(item6).addActionListener(this);
262     menu.addSeparator();
263     menu.add(item3).addActionListener(this);
264     menu.setVisible(true);
265     menu.setBackground(toolBarColor);
266     menuBar.add(menu);
267     menuBar.setBackground(toolBarColor);
268     this.setJMenuBar(menuBar);
269
270     // Prepare options window
271
options = new JFrame JavaDoc(MonitorTranslate.get("options.frame.title"));
272     options.setSize(200, 200);
273     JPanel JavaDoc optionsPanel = new JPanel JavaDoc();
274     optionsPanel.setLayout(new GridLayout JavaDoc(5, 2));
275     optionsPanel.add(new JLabel JavaDoc(MonitorTranslate.get("options.repeat")));
276     frepeat = new JTextField JavaDoc(0);
277     frepeat.setAlignmentX(RIGHT_ALIGNMENT);
278     frepeat.setText(graphRepeat + "");
279     frepeat.addActionListener(this);
280     optionsPanel.add(frepeat);
281
282     optionsPanel
283         .add(new JLabel JavaDoc(MonitorTranslate.get("options.display.buffer")));
284     displayBuffer = new JTextField JavaDoc(0);
285     displayBuffer.setText("" + graphDisplayFrequency);
286     displayBuffer.addActionListener(this);
287     displayBuffer.setAlignmentX(RIGHT_ALIGNMENT);
288     optionsPanel.add(displayBuffer);
289
290     optionsPanel.add(new JLabel JavaDoc(MonitorTranslate.get("options.frequency")));
291     ffrequency = new JTextField JavaDoc(0);
292     ffrequency.setText("" + graphFrequency);
293     ffrequency.addActionListener(this);
294     ffrequency.setAlignmentX(RIGHT_ALIGNMENT);
295     optionsPanel.add(ffrequency);
296
297     optionsPanel.add(new JLabel JavaDoc(MonitorTranslate.get("options.timeframe")));
298     ftimeframe = new JTextField JavaDoc(0);
299     ftimeframe.setText(graphTimeframe + "");
300     ftimeframe.addActionListener(this);
301     ftimeframe.setAlignmentX(RIGHT_ALIGNMENT);
302     optionsPanel.add(ftimeframe);
303
304     JButton JavaDoc optionConfirm = new JButton JavaDoc(MonitorTranslate.get("options.ok"));
305     optionConfirm.setActionCommand(OPTIONS_APPLY);
306     optionConfirm.addActionListener(this);
307     optionsPanel.add(optionConfirm);
308
309     JButton JavaDoc optionCancel = new JButton JavaDoc(MonitorTranslate.get("options.cancel"));
310     optionCancel.setActionCommand(OPTIONS_CANCEL);
311     optionCancel.addActionListener(this);
312     optionsPanel.add(optionCancel);
313
314     options.getContentPane().add(optionsPanel);
315     options.setVisible(false);
316     options.setDefaultCloseOperation(HIDE_ON_CLOSE);
317     options.validate();
318
319     //Put the final touches to the JFrame object
320
validate();
321     setVisible(true);
322     this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
323   }
324
325   private void initConsole() throws Exception JavaDoc
326   {
327     // Get the menus with all commands
328
this.setVisible(false);
329     BorderLayout JavaDoc layout = new BorderLayout JavaDoc();
330     JPanel JavaDoc pane = new JPanel JavaDoc(layout);
331     if (displayController)
332       pane.add(initializeControllerBar(), BorderLayout.NORTH);
333     if (displayVirtualDatabase)
334       pane.add(initializaDatabaseBar(displayBackends), BorderLayout.CENTER);
335     jScroll = new JScrollPane JavaDoc();
336     jScroll.getViewport().add(pane);
337     getContentPane().add(jScroll, BorderLayout.CENTER);
338
339     validate();
340     this.setVisible(true);
341   }
342
343   private JLabel JavaDoc getHeaderLabel(String JavaDoc text)
344   {
345     JLabel JavaDoc headerLabel = new JLabel JavaDoc(text);
346     headerLabel.setFont(headerFont);
347     headerLabel.setAlignmentX(CENTER_ALIGNMENT);
348     return headerLabel;
349   }
350
351   private JLabel JavaDoc getSubHeaderLabel(String JavaDoc text)
352   {
353     JLabel JavaDoc subHeaderLabel = new JLabel JavaDoc(text);
354     subHeaderLabel.setFont(labelFont);
355     subHeaderLabel.setAlignmentX(CENTER_ALIGNMENT);
356     return subHeaderLabel;
357   }
358
359   private JToolBar JavaDoc initializeControllerBar()
360   {
361     JToolBar JavaDoc toolbar = new JToolBar JavaDoc(GRAPH_CONTROLLER, SwingConstants.VERTICAL);
362     toolbar.setOrientation(SwingConstants.VERTICAL);
363     toolbar.addMouseListener(this);
364     toolbar.add(getHeaderLabel(GRAPH_CONTROLLER));
365     toolbar
366         .add(getGraphMenuItem(DataCollection.CONTROLLER_USED_MEMORY, "", ""));
367     toolbar.add(getGraphMenuItem(DataCollection.CONTROLLER_THREADS_NUMBER, "",
368         ""));
369     toolbar.add(getGraphMenuItem(
370         DataCollection.CONTROLLER_WORKER_PENDING_QUEUE, "", ""));
371     toolbar.add(getGraphMenuItem(DataCollection.CONTROLLER_IDLE_WORKER_THREADS,
372         "", ""));
373     toolbar.setVisible(true);
374     return toolbar;
375   }
376
377   private JToolBar JavaDoc initializeCacheBar(String JavaDoc vdb)
378   {
379     JToolBar JavaDoc toolbar = new JToolBar JavaDoc(GRAPH_CACHE, SwingConstants.VERTICAL);
380
381     try
382     {
383       if (virtualDatabaseMBean.hasResultCache() == false)
384         toolbar.setEnabled(false);
385       else
386       {
387         toolbar.add(getSubHeaderLabel(GRAPH_CACHE + " [" + vdb + "]"));
388         toolbar.add(getGraphMenuItem(DataCollection.CACHE_STATS_COUNT_HITS,
389             vdb, ""));
390         toolbar.add(getGraphMenuItem(DataCollection.CACHE_STATS_COUNT_INSERT,
391             vdb, ""));
392         toolbar.add(getGraphMenuItem(DataCollection.CACHE_STATS_COUNT_SELECT,
393             vdb, ""));
394         toolbar.add(getGraphMenuItem(
395             DataCollection.CACHE_STATS_HITS_PERCENTAGE, vdb, ""));
396         toolbar.add(getGraphMenuItem(DataCollection.CACHE_STATS_NUMBER_ENTRIES,
397             vdb, ""));
398       }
399     }
400     catch (Exception JavaDoc e)
401     {
402       toolbar.setEnabled(false);
403     }
404     return toolbar;
405   }
406
407   private JToolBar JavaDoc initializeSchedulerBar(String JavaDoc vdb)
408   {
409     JToolBar JavaDoc toolbar = new JToolBar JavaDoc(GRAPH_SCHEDULER, SwingConstants.VERTICAL);
410     toolbar.add(getSubHeaderLabel(GRAPH_SCHEDULER + " [" + vdb + "]"));
411     toolbar
412         .add(getGraphMenuItem(DataCollection.SCHEDULER_NUMBER_READ, vdb, ""));
413     toolbar.add(getGraphMenuItem(DataCollection.SCHEDULER_NUMBER_REQUESTS, vdb,
414         ""));
415     toolbar.add(getGraphMenuItem(DataCollection.SCHEDULER_NUMBER_WRITES, vdb,
416         ""));
417     toolbar.add(getGraphMenuItem(DataCollection.SCHEDULER_PENDING_TRANSACTIONS,
418         vdb, ""));
419     toolbar.add(getGraphMenuItem(DataCollection.SCHEDULER_PENDING_WRITES, vdb,
420         ""));
421     return toolbar;
422   }
423
424   private JToolBar JavaDoc initializaBackendBar(String JavaDoc vdb, String JavaDoc backendName)
425   {
426     JToolBar JavaDoc backendMenu = new JToolBar JavaDoc(GRAPH_BACKENDS, SwingConstants.VERTICAL);
427     backendMenu.add(getSubHeaderLabel(GRAPH_BACKENDS + " [" + backendName
428         + " on " + vdb + "]"));
429     backendMenu.add(getGraphMenuItem(DataCollection.BACKEND_ACTIVE_TRANSACTION,
430         vdb, backendName));
431     backendMenu.add(getGraphMenuItem(DataCollection.BACKEND_PENDING_REQUESTS,
432         vdb, backendName));
433     backendMenu.add(getGraphMenuItem(
434         DataCollection.BACKEND_TOTAL_ACTIVE_CONNECTIONS, vdb, backendName));
435     backendMenu.add(getGraphMenuItem(DataCollection.BACKEND_TOTAL_REQUEST, vdb,
436         backendName));
437     backendMenu.add(getGraphMenuItem(DataCollection.BACKEND_TOTAL_READ_REQUEST,
438         vdb, backendName));
439     backendMenu.add(getGraphMenuItem(
440         DataCollection.BACKEND_TOTAL_WRITE_REQUEST, vdb, backendName));
441     backendMenu.add(getGraphMenuItem(DataCollection.BACKEND_TOTAL_TRANSACTIONS,
442         vdb, backendName));
443     return backendMenu;
444   }
445
446   private JToolBar JavaDoc initializaDatabaseBar(boolean dispBackends) throws Exception JavaDoc
447   {
448     JToolBar JavaDoc toolbar = new JToolBar JavaDoc(GRAPH_VIRTUAL_DATABASE, JToolBar.VERTICAL);
449     toolbar.addMouseListener(this);
450
451     ArrayList JavaDoc dbs = controllerMBean.getVirtualDatabaseNames();
452     ArrayList JavaDoc backends;
453     String JavaDoc vdb = null;
454     for (int i = 0; i < dbs.size(); i++)
455     {
456       vdb = (String JavaDoc) dbs.get(i);
457       toolbar.add(getHeaderLabel(GRAPH_VIRTUAL_DATABASE + " [" + vdb + "]"));
458       // Virtual Database main graphs
459
toolbar.add(getGraphMenuItem(DataCollection.DATABASES_ACTIVE_THREADS,
460           vdb, ""));
461       toolbar.add(getGraphMenuItem(DataCollection.DATABASES_NUMBER_OF_THREADS,
462           vdb, ""));
463       toolbar.add(getGraphMenuItem(
464           DataCollection.DATABASES_PENDING_CONNECTIONS, vdb, ""));
465       // Cache
466
toolbar.add(initializeCacheBar(vdb));
467       // Scheduler
468
toolbar.add(initializeSchedulerBar(vdb));
469
470       if (dispBackends)
471       { // Backends
472
backends = virtualDatabaseMBean.getAllBackendNames();
473         for (int j = 0; j < backends.size(); j++)
474         {
475           String JavaDoc backendName = (String JavaDoc) backends.get(j);
476           toolbar.add(initializaBackendBar(vdb, backendName));
477         }
478       }
479     }
480     return toolbar;
481   }
482
483   private JComponent JavaDoc getGraphMenuItem(int type, String JavaDoc virtualDbName,
484       String JavaDoc targetName)
485   {
486     String JavaDoc name = DataCollectionNames.get(type);
487     JComboBox JavaDoc item = new JComboBox JavaDoc(comboBoxesItems);
488     item.setFont(boxFont);
489     item.setName(name);
490     item.addActionListener(this);
491     item.addMouseListener(this);
492     if (virtualDbName == null)
493       virtualDbName = "";
494     if (targetName == null)
495       targetName = "";
496     String JavaDoc actionCommand = getBackendActionCommand(name, virtualDbName,
497         targetName);
498     actionCommand = actionCommand.trim();
499     item.setActionCommand(actionCommand);
500     item.setVisible(true);
501     BorderLayout JavaDoc layout = new BorderLayout JavaDoc();
502     JPanel JavaDoc panel = new JPanel JavaDoc(layout);
503     JLabel JavaDoc nameLabel = new JLabel JavaDoc(name);
504     nameLabel.setAlignmentX(CENTER_ALIGNMENT);
505     nameLabel.setFont(labelFont);
506     panel.add(nameLabel, BorderLayout.WEST);
507     panel.add(item, BorderLayout.EAST);
508
509     comboBoxes.put(actionCommand, item);
510     return panel;
511   }
512
513   /**
514    * Get the backend action command for displaying monitoring window
515    *
516    * @param typeName type of info to monitor
517    * @param vdbName database name
518    * @param backendName backend name
519    * @return <code>String</code> describing the command
520    */

521   public static String JavaDoc getBackendActionCommand(String JavaDoc typeName, String JavaDoc vdbName,
522       String JavaDoc backendName)
523   {
524     return "graph " + typeName.toLowerCase().replace(' ', '_') + " " + vdbName
525         + " " + backendName;
526   }
527
528   /**
529    * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
530    */

531   public void mouseClicked(MouseEvent JavaDoc e)
532   {
533     status(e.getComponent().getName() + " was clicked");
534   }
535
536   /**
537    * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
538    */

539   public void mouseEntered(MouseEvent JavaDoc e)
540   {
541
542   }
543
544   /**
545    * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
546    */

547   public void mouseExited(MouseEvent JavaDoc e)
548   {
549
550   }
551
552   /**
553    * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
554    */

555   public void mousePressed(MouseEvent JavaDoc e)
556   {
557
558   }
559
560   /**
561    * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
562    */

563   public void mouseReleased(MouseEvent JavaDoc e)
564   {
565
566   }
567
568   private void doSaveConfiguration() throws IOException JavaDoc
569   {
570     Iterator JavaDoc iter = comboBoxes.keySet().iterator();
571     File JavaDoc f = new File JavaDoc("monitor.properties");
572     BufferedOutputStream JavaDoc bof = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
573     String JavaDoc temp;
574     while (iter.hasNext())
575     {
576       Object JavaDoc o = iter.next();
577       String JavaDoc key = o.toString().trim().replace(' ', '.');
578       JComboBox JavaDoc box = (JComboBox JavaDoc) comboBoxes.get(o);
579       temp = (key + "=" + box.getSelectedItem())
580           + System.getProperty("line.separator");
581       bof.write(temp.getBytes());
582     }
583
584     Iterator JavaDoc iter2 = windows.keySet().iterator();
585     Window JavaDoc win;
586     Point JavaDoc p;
587     String JavaDoc name, winX, winY;
588     while (iter2.hasNext())
589     {
590       win = ((Window JavaDoc) windows.get(iter2.next()));
591       p = win.getLocation();
592       name = win.getName().trim().replace(' ', '.');
593       winX = name + ".X=" + (int) p.getX()
594           + System.getProperty("line.separator");
595       winY = name + ".Y=" + (int) p.getY()
596           + System.getProperty("line.separator");
597       bof.write(winX.getBytes());
598       bof.write(winY.getBytes());
599     }
600     bof.write(("options.repeat=" + graphRepeat + System
601         .getProperty("line.separator")).getBytes());
602     bof.write(("options.timeframe=" + graphTimeframe + System
603         .getProperty("line.separator")).getBytes());
604     bof.write(("options.frequency=" + graphFrequency + System
605         .getProperty("line.separator")).getBytes());
606     bof.write(("options.displayfrequency=" + graphDisplayFrequency + System
607         .getProperty("line.separator")).getBytes());
608
609     bof.flush();
610     bof.close();
611   }
612
613   private void doLoadConfiguration() throws IOException JavaDoc
614   {
615     closeAllWindows();
616     isLoading = true;
617     File JavaDoc f = new File JavaDoc("monitor.properties");
618     PropertyResourceBundle JavaDoc props = new PropertyResourceBundle JavaDoc(
619         new FileInputStream JavaDoc(f));
620     Enumeration JavaDoc enume = props.getKeys();
621     String JavaDoc key = "", keyr = "", value = "";
622     JFrame JavaDoc frame;
623
624     try
625     {
626       graphRepeat = Integer
627           .parseInt((String JavaDoc) props.getObject("options.repeat"));
628       frepeat.setText("" + graphRepeat);
629       graphFrequency = Integer.parseInt((String JavaDoc) props
630           .getObject("options.frequency"));
631       ffrequency.setText("" + graphFrequency);
632       graphTimeframe = Integer.parseInt((String JavaDoc) props
633           .getObject("options.timeframe"));
634       ftimeframe.setText("" + graphTimeframe);
635       graphDisplayFrequency = Integer.parseInt((String JavaDoc) props
636           .getObject("options.displayfrequency"));
637       displayBuffer.setText("" + graphDisplayFrequency);
638     }
639     catch (Exception JavaDoc e)
640     {
641       error(e.getMessage());
642     }
643
644     while (enume.hasMoreElements())
645     {
646       key = (String JavaDoc) enume.nextElement();
647       value = (String JavaDoc) props.getObject(key);
648       if (key.startsWith("options"))
649       {
650         // done
651
}
652       else if (key.endsWith(".X") || key.endsWith(".Y"))
653       {
654         // do nothing
655
}
656       else
657       {
658         if (value.equals(COMBO_FLOATING))
659           try
660           {
661             frame = graph(key);
662             keyr = key.trim().replace('.', ' ');
663             //System.out.println(key);
664
JComboBox JavaDoc box = (JComboBox JavaDoc) comboBoxes.get(keyr);
665             if (box != null)
666               box.setSelectedItem(COMBO_FLOATING);
667             try
668             {
669               int x = Integer.parseInt((String JavaDoc) props.getObject(key + ".X"));
670               int y = Integer.parseInt((String JavaDoc) props.getObject(key + ".Y"));
671
672               //Window win = ((Window) windows.get(keyr));
673
//System.out.println(frame.getName()+"-"+x+"-"+y);
674
frame.setLocation(x, y);
675               //win.getComponent(0).setLocation(x, y);
676
//win.setVisible(true);
677
//win.validate();
678
}
679             catch (Exception JavaDoc e)
680             {
681               // ignore does not exist
682
error(e.getMessage());
683             }
684           }
685           catch (Exception JavaDoc e)
686           {
687             // cannot load ...
688
}
689       }
690     }
691     isLoading = false;
692   }
693
694   /**
695    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
696    */

697   public void actionPerformed(ActionEvent JavaDoc e)
698   {
699     if (isLoading)
700       return;
701     String JavaDoc actionCommand = e.getActionCommand();
702     status("Action:" + actionCommand);
703     if (actionCommand.equals(COMMAND_SAVE))
704     {
705       try
706       {
707         doSaveConfiguration();
708       }
709       catch (IOException JavaDoc e1)
710       {
711         // TODO: display dialog box
712
// Dialog d = new Dialog(this, "Saving Failed");
713
// d.add(new JLabel("Saving Failed because of:" + e1.getMessage()));
714
// d.setVisible(true);
715
// d.setSize(100,50);
716
// d.setModal(true);
717
//d.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
718
e1.printStackTrace();
719       }
720     }
721     else if (actionCommand.equals(COMMAND_LOAD))
722     {
723       try
724       {
725         doLoadConfiguration();
726       }
727       catch (IOException JavaDoc e1)
728       {
729         // TODO: display dialog box
730
e1.printStackTrace();
731       }
732     }
733     else if (actionCommand.equals(OPTIONS_APPLY))
734     {
735
736       try
737       {
738         graphRepeat = Integer.parseInt(frepeat.getText());
739         graphTimeframe = Integer.parseInt(ftimeframe.getText());
740         graphFrequency = Integer.parseInt(ffrequency.getText());
741         graphDisplayFrequency = Integer.parseInt(displayBuffer.getText());
742         options.setVisible(false);
743       }
744       catch (Exception JavaDoc failed)
745       {
746         error(failed.getMessage());
747       }
748     }
749     else if (actionCommand.equals(OPTIONS_CANCEL))
750     {
751       options.setVisible(false);
752       frepeat.setText(graphRepeat + "");
753       ftimeframe.setText(graphTimeframe + "");
754       ffrequency.setText(graphFrequency + "");
755     }
756     else if (actionCommand.equals(COMMAND_REFRESH))
757     {
758       try
759       {
760         initConsole();
761       }
762       catch (Exception JavaDoc error)
763       {
764         error(error.getMessage());
765       }
766     }
767     else if (actionCommand.equals(COMMAND_OPTIONS))
768     {
769       options.setVisible(true);
770     }
771     else if (actionCommand.equals(COMMAND_CLOSE))
772     {
773       closeAllWindows();
774       this.dispose();
775     }
776     else if (actionCommand.equals(COMMAND_CLOSE_GRAPHS))
777     {
778       closeAllWindows();
779     }
780     Object JavaDoc o = e.getSource();
781     if (o instanceof JComboBox JavaDoc)
782     {
783       JComboBox JavaDoc box = (JComboBox JavaDoc) o;
784       String JavaDoc selected = (String JavaDoc) box.getSelectedItem();
785       status(selected.toString());
786       try
787       {
788         Window JavaDoc win = (Window JavaDoc) windows.get(e.getActionCommand().trim());
789         if (!selected.equals(COMBO_HIDE))
790         {
791           if (win == null)
792           {
793             graph(e.getActionCommand());
794           }
795         }
796         else
797         {
798           windows.remove(win.getName());
799           win.setVisible(false);
800           win.dispose();
801         }
802       }
803       catch (Exception JavaDoc f)
804       {
805         error(f.getMessage());
806       }
807     }
808
809   }
810
811   private void closeAllWindows()
812   {
813     while (true)
814     {
815       try
816       {
817         Iterator JavaDoc iter = windows.keySet().iterator();
818         while (iter.hasNext())
819         {
820           Window JavaDoc win = ((Window JavaDoc) windows.get(iter.next()));
821           JComboBox JavaDoc box = (JComboBox JavaDoc) comboBoxes.get(win.getName());
822           if (box != null)
823             box.setSelectedItem(COMBO_HIDE);
824         }
825         break;
826       }
827       catch (RuntimeException JavaDoc e)
828       {
829         //concurrent modification exception
830
continue;
831       }
832     }
833   }
834
835   private void status(String JavaDoc message)
836   {
837     label.setBackground(Color.white);
838     label.setText(message);
839   }
840
841   private void error(String JavaDoc message)
842   {
843     label.setBackground(Color.red);
844     label.setText(message);
845   }
846
847   /**
848    * Starts a new graph
849    *
850    * @param command command line
851    * @throws DataCollectorException if fails
852    */

853   private JFrame JavaDoc graph(String JavaDoc command) throws DataCollectorException
854   {
855
856     return graph(command, dataCollectorMBean, graphRepeat, graphTimeframe,
857         graphFrequency, graphDisplayFrequency, this);
858   }
859
860   /**
861    * Starts a graph !
862    *
863    * @param command graph command
864    * @param dataCollectorMBean jmx client to get info from
865    * @param graphRepeat parameter
866    * @param graphTimeframe parameter
867    * @param graphFrequency parameter
868    * @param graphDisplayFrequency parameter
869    * @param listener to receive updates
870    * @return <code>JFrame</code> containing the monitoring window
871    * @throws DataCollectorException if an error occurs
872    */

873   public static final JFrame JavaDoc graph(String JavaDoc command,
874       DataCollectorMBean dataCollectorMBean, int graphRepeat,
875       int graphTimeframe, int graphFrequency, int graphDisplayFrequency,
876       WindowListener JavaDoc listener) throws DataCollectorException
877   {
878     // Used for saving configuration
879
command = command.replace('.', ' ');
880
881     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(command, " ");
882     String JavaDoc token0 = tokenizer.nextToken();
883     if (token0.equals("graph"))
884     {
885       String JavaDoc token1 = tokenizer.nextToken();
886       int type = DataCollectionNames.getTypeFromCommand(token1);
887       String JavaDoc token2 = (tokenizer.hasMoreTokens()) ? tokenizer.nextToken() : "";
888       String JavaDoc token3 = (tokenizer.hasMoreTokens()) ? tokenizer.nextToken() : "";
889       AbstractDataCollector collector = dataCollectorMBean
890           .retrieveDataCollectorInstance(type, token3, token2);
891       MonitoringGraph graph = new MonitoringGraph(collector, dataCollectorMBean);
892       graph.setRepeat(graphRepeat);
893       graph.setTimeFrame(graphTimeframe);
894       graph.setFrequency(graphFrequency);
895       graph.setDisplayFrequency(graphDisplayFrequency);
896       graph.start();
897       graph.setText(command);
898       // Do not do before, as frame is null before starts of thread!
899
if (listener != null)
900         graph.getFrame().addWindowListener(listener);
901       graph.getFrame().setName(command.trim());
902       return graph.getFrame();
903     }
904     else
905     {
906       return null;
907     }
908   }
909
910   /**
911    * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
912    */

913   public void windowActivated(WindowEvent JavaDoc e)
914   {
915
916   }
917
918   /**
919    * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
920    */

921   public void windowClosed(WindowEvent JavaDoc e)
922   {
923
924   }
925
926   /**
927    * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
928    */

929   public void windowClosing(WindowEvent JavaDoc e)
930   {
931     Window JavaDoc win = e.getWindow();
932     JComboBox JavaDoc box = (JComboBox JavaDoc) comboBoxes.get(win.getName());
933     windows.remove(win.getName());
934     if (box != null)
935       box.setSelectedIndex(0);
936     status(win.getName() + " is closing");
937   }
938
939   /**
940    * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
941    */

942   public void windowDeactivated(WindowEvent JavaDoc e)
943   {
944
945   }
946
947   /**
948    * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
949    */

950   public void windowDeiconified(WindowEvent JavaDoc e)
951   {
952
953   }
954
955   /**
956    * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
957    */

958   public void windowIconified(WindowEvent JavaDoc e)
959   {
960
961   }
962
963   /**
964    * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
965    */

966   public void windowOpened(WindowEvent JavaDoc e)
967   {
968     Window JavaDoc win = e.getWindow();
969     status(win.getName() + " has opened");
970     windows.put(win.getName(), e.getWindow());
971   }
972 }
Popular Tags