KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > console > lib > gui > GuiConsoleImpl


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2003-2004 France Telecom R&D
4 * Copyright (C) 2003 INRIA
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * CLIF $Name: $
21 *
22 * Contact: clif@objectweb.org
23 */

24
25 package org.objectweb.clif.console.lib.gui;
26
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.awt.Cursor JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Observer JavaDoc;
35 import java.util.Observable JavaDoc;
36 import java.io.FileOutputStream JavaDoc;
37 import java.io.FileInputStream JavaDoc;
38 import java.io.ByteArrayOutputStream JavaDoc;
39 import java.io.PrintStream JavaDoc;
40 import org.objectweb.clif.supervisor.api.BladeState;
41 import org.objectweb.clif.supervisor.api.TestControl;
42 import org.objectweb.clif.supervisor.lib.BladeObservation;
43 import org.objectweb.clif.deploy.Deployer;
44 import org.objectweb.clif.console.lib.TestPlanWriter;
45 import org.objectweb.clif.console.lib.TestPlanReader;
46 import org.objectweb.clif.deploy.DeployObservation;
47 import org.objectweb.util.monolog.api.Logger;
48 import org.objectweb.util.monolog.api.BasicLevel;
49 import org.objectweb.util.monolog.Monolog;
50 import org.objectweb.clif.analyser.lib.gui.GuiAnalyserImpl;
51 import org.objectweb.clif.storage.api.AlarmEvent;
52 import org.objectweb.fractal.api.NoSuchInterfaceException;
53 import org.objectweb.fractal.util.Fractal;
54 import javax.swing.JFrame JavaDoc;
55 import javax.swing.JMenuBar JavaDoc;
56 import javax.swing.JInternalFrame JavaDoc;
57 import javax.swing.JCheckBoxMenuItem JavaDoc;
58 import javax.swing.JMenuItem JavaDoc;
59 import javax.swing.Timer JavaDoc;
60 import javax.swing.ImageIcon JavaDoc;
61 import javax.swing.JMenu JavaDoc;
62 import javax.swing.KeyStroke JavaDoc;
63 import javax.swing.JFileChooser JavaDoc;
64
65
66 /**
67  * Implementation of a graphic user interface for Clif
68  *
69  * @author Julien Buret
70  * @author Nicolas Droze
71  * @author Bruno Dillenseger
72  */

73 public class GuiConsoleImpl
74     implements Observer JavaDoc, ActionListener JavaDoc
75 {
76     static public final String JavaDoc LOCAL_NAME = "local host";
77     /* Size definitions for the application.
78      * Internal frames width is not specified because it is automatically resized according to
79      * the main window width.
80      */

81     static public int clifWidth = 900;
82     static public int clifHeight = 700;
83
84     static private final String JavaDoc ABOUT_CMD = "about";
85     static private final String JavaDoc DEPLOY_CMD = "deploy";
86     static private final String JavaDoc INIT_CMD = "init";
87     static private final String JavaDoc START_CMD = "start";
88     static private final String JavaDoc STOP_CMD = "stop";
89     static private final String JavaDoc COLLECT_CMD = "collect";
90     static private final String JavaDoc SUSPEND_CMD = "suspend";
91     static private final String JavaDoc RESUME_CMD = "resume";
92     static private final String JavaDoc UPDATE_CMD = "update";
93     static private final String JavaDoc EXIT_CMD = "exit";
94     static private final String JavaDoc SAVE_CMD = "save";
95     static private final String JavaDoc OPEN_CMD = "open";
96     static private final String JavaDoc EDIT_CMD = "edit";
97     static private final String JavaDoc ANALYZER_CMD = "analyzer";
98     static private Logger log = Monolog.getDefaultMonologFactory().getLogger(GuiConsoleImpl.class.getName());
99
100
101     static public void main(String JavaDoc[] args)
102     {
103         try
104         {
105             new GuiConsoleImpl(new Deployer());
106         }
107         catch (Exception JavaDoc ex)
108         {
109             ex.printStackTrace(System.err);
110             log.log(BasicLevel.FATAL, "Clif GUI cannot run", ex);
111             System.exit(-1);
112         }
113     }
114
115
116     private Deployer deployer;
117     private String JavaDoc currentDirectory = ".";
118     private BladeState globalState = BladeState.UNDEPLOYED;
119     private TestControl testCtl = null;
120     private Map JavaDoc testPlan;
121     private Map JavaDoc bladeStates;
122     private JFrame JavaDoc frame;
123     private JMenuBar JavaDoc menuBar;
124     private GuiPanelOutput guiPanelOutput;
125     private JInternalFrame JavaDoc outputFrame;
126     private TestPlanWindow testPlanFrame;
127     private GuiAnalyserImpl analyzerFrame;
128     private JCheckBoxMenuItem JavaDoc boxMessageOutput;
129     private JCheckBoxMenuItem JavaDoc boxAnalysisTool;
130     private JMenuItem JavaDoc itemDeploy;
131     private JMenuItem JavaDoc itemEdit;
132     private JMenuItem JavaDoc itemInit;
133     private JMenuItem JavaDoc itemStart;
134     private JMenuItem JavaDoc itemStop;
135     private JMenuItem JavaDoc itemSuspend;
136     private JMenuItem JavaDoc itemResume;
137     private JMenuItem JavaDoc itemCollect;
138     private JMenuItem JavaDoc itemRebind;
139     private JMenuItem JavaDoc itemAbout;
140     private long ellapsedTime;
141     private long lastStartTime;
142     private Timer JavaDoc ellapsedTimeTimer;
143
144
145     public GuiConsoleImpl(Deployer deployer)
146     {
147         this.deployer = deployer;
148         try
149         {
150             deployer.getRegistry().rebind(LOCAL_NAME, Fractal.getBootstrapComponent());
151         }
152         catch (Exception JavaDoc ex)
153         {
154             throw new Error JavaDoc(
155                 "fatal, abnormal situation: could not register local host CLIF server",
156                 ex);
157         }
158         deployer.addObserver(this);
159         testPlan = null;
160         globalState = BladeState.UNDEPLOYED;
161         ImageIcon JavaDoc clifIcon = new ImageIcon JavaDoc(getClass().getClassLoader().getResource("icons/clificon.png"));
162
163         frame = new JFrame JavaDoc("CLIF is a Load Injection Framework");
164         frame.setIconImage(clifIcon.getImage());
165         frame.setSize(clifWidth, clifHeight);
166         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
167         createMenuBar();
168
169         ClifDesktopPane desktop = new ClifDesktopPane();
170
171         guiPanelOutput = new GuiPanelOutput();
172         outputFrame = new JInternalFrame JavaDoc("Message output", true, false, false, false);
173         outputFrame.setSize(200, 200);
174         outputFrame.setVisible(false);
175         outputFrame.getContentPane().add(guiPanelOutput);
176         desktop.add(outputFrame);
177
178         testPlanFrame = new TestPlanWindow(frame);
179         testPlanFrame.setFrameIcon(clifIcon);
180         testPlanFrame.setVisible(true);
181         testPlanFrame.setLocation(0, 0);
182         testPlanFrame.setAvailableServers(deployer.getServers());
183         desktop.add(testPlanFrame);
184
185         try
186         {
187             analyzerFrame = new GuiAnalyserImpl(
188                 deployer.getComponentByName("analyser").getFcInterface("Analyser control"),frame);
189         }
190         catch (NoSuchInterfaceException ex)
191         {
192             log.log(BasicLevel.ERROR, "Analyzer component does not provide requested interface", ex);
193         }
194         analyzerFrame.setFrameIcon(clifIcon);
195         analyzerFrame.setVisible(false);
196         testPlanFrame.setLocation(0, 0);
197         desktop.add(analyzerFrame);
198
199         frame.getContentPane().add(desktop);
200         frame.show();
201         testPlanFrame.setSize(desktop.getWidth(), desktop.getHeight());
202         analyzerFrame.setSize(desktop.getWidth(), desktop.getHeight());
203
204         updateMenus();
205         ellapsedTimeTimer = new Timer JavaDoc(1000, this);
206     }
207
208
209     /**
210      * Sets the menu bar of main window
211      */

212     private void createMenuBar()
213     {
214         menuBar = new JMenuBar JavaDoc();
215         JMenu JavaDoc menu;
216         JMenuItem JavaDoc item;
217
218         // File menu
219
menu = new JMenu JavaDoc("File");
220
221         item = new JMenuItem JavaDoc("Save this test plan");
222         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
223         item.setActionCommand(SAVE_CMD);
224         menu.add(item).addActionListener(this);
225
226         item = new JMenuItem JavaDoc("Open a test plan");
227         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
228         item.setActionCommand(OPEN_CMD);
229         menu.add(item).addActionListener(this);
230
231         menu.addSeparator();
232         item = new JMenuItem JavaDoc("Quit");
233         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
234         item.setActionCommand(EXIT_CMD);
235         menu.add(item).addActionListener(this);
236         menuBar.add(menu);
237
238         /*
239          * test plan Menu
240          */

241         menu = new JMenu JavaDoc("Test plan");
242
243         itemRebind = new JMenuItem JavaDoc("Refresh server list");
244         itemRebind.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0));
245         itemRebind.setActionCommand(UPDATE_CMD);
246         menu.add(itemRebind).addActionListener(this);
247
248         itemEdit = menu.add("Edit");
249         itemEdit.setActionCommand(EDIT_CMD);
250         itemEdit.addActionListener(this);
251         itemEdit.setEnabled(false);
252
253         itemDeploy = menu.add("Deploy");
254         itemDeploy.setActionCommand(DEPLOY_CMD);
255         itemDeploy.addActionListener(this);
256         itemDeploy.setEnabled(false);
257
258         menu.addSeparator();
259
260         itemInit = menu.add("Initialize");
261         itemInit.setActionCommand(INIT_CMD);
262         itemInit.addActionListener(this);
263
264         itemStart = menu.add("Start");
265         itemStart.setActionCommand(START_CMD);
266         itemStart.addActionListener(this);
267
268         itemStop = menu.add("Stop");
269         itemStop.setActionCommand(STOP_CMD);
270         itemStop.addActionListener(this);
271
272         itemSuspend = menu.add("Suspend");
273         itemSuspend.setActionCommand(SUSPEND_CMD);
274         itemSuspend.addActionListener(this);
275
276         itemResume = menu.add("Resume");
277         itemResume.setActionCommand(RESUME_CMD);
278         itemResume.addActionListener(this);
279
280         itemCollect = menu.add("Collect");
281         itemCollect.setActionCommand(COLLECT_CMD);
282         itemCollect.addActionListener(this);
283
284         menuBar.add(menu);
285
286         /*
287          * WINDOW Menu
288          */

289         menu = new JMenu JavaDoc("Tools");
290
291         boxMessageOutput = new JCheckBoxMenuItem JavaDoc("Messages", false);
292         boxMessageOutput.setActionCommand("VIEW_OUTPUT");
293         menu.add(boxMessageOutput).addActionListener(this);
294
295         boxAnalysisTool = new JCheckBoxMenuItem JavaDoc("Analyzer", false);
296         boxAnalysisTool.setActionCommand(ANALYZER_CMD);
297         menu.add(boxAnalysisTool).addActionListener(this);
298
299         menuBar.add(menu);
300
301         /*
302         * About menu
303         */

304         menu = new JMenu JavaDoc("?");
305         itemAbout = new JMenuItem JavaDoc("About...");
306         itemAbout.setActionCommand(ABOUT_CMD);
307         itemAbout.addActionListener(this);
308         menu.add(itemAbout);
309
310         menuBar.add(menu);
311
312         frame.setJMenuBar(menuBar);
313     }
314
315
316     /**
317      * Enables or disables menu items depending on the current state
318      */

319     public void updateMenus()
320     {
321         itemDeploy.setEnabled(testPlan == null);
322         itemRebind.setEnabled(testPlan == null);
323         itemEdit.setEnabled(testPlan != null);
324         itemInit.setEnabled(false);
325         itemStart.setEnabled(false);
326         itemStop.setEnabled(false);
327         itemSuspend.setEnabled(false);
328         itemResume.setEnabled(false);
329         itemCollect.setEnabled(false);
330
331         if (globalState.equals(BladeState.DEPLOYED))
332         {
333             itemInit.setEnabled(testPlan != null);
334             if (testPlan == null)
335             {
336                 testPlanFrame.setEditable(true);
337             }
338             else
339             {
340                 testPlanFrame.setStatusLine(globalState, 0);
341             }
342         }
343         else if (globalState.equals(BladeState.INITIALIZED))
344         {
345             itemStart.setEnabled(true);
346             itemStop.setEnabled(true);
347             testPlanFrame.initTest(testCtl);
348             testPlanFrame.setStatusLine(globalState, 0);
349         }
350         else if (globalState.equals(BladeState.RUNNING))
351         {
352             itemStop.setEnabled(true);
353             itemSuspend.setEnabled(true);
354         }
355         else if (globalState.equals(BladeState.SUSPENDED))
356         {
357             itemResume.setEnabled(true);
358             itemStop.setEnabled(true);
359         }
360         else if (globalState.equals(BladeState.STOPPED))
361         {
362             itemInit.setEnabled(testPlan != null);
363             itemCollect.setEnabled(testPlan != null);
364             if (testPlan == null)
365             {
366                 testPlanFrame.setEditable(true);
367             }
368         }
369         else
370         {
371             testPlanFrame.setStatusLine(globalState, 0);
372         }
373     }
374
375
376     //////////////////////////////
377
// interface ActionListener //
378
//////////////////////////////
379

380
381     public void actionPerformed(ActionEvent JavaDoc e)
382     {
383         String JavaDoc command = e.getActionCommand();
384
385         if (e.getSource() == ellapsedTimeTimer)
386         {
387             testPlanFrame.setStatusLine(
388                 globalState,
389                 globalState.equals(BladeState.RUNNING)
390                     ? ellapsedTime + System.currentTimeMillis() - lastStartTime
391                     : ellapsedTime);
392         }
393         else if (command.equals(SAVE_CMD))
394         {
395             JFileChooser JavaDoc filechooser = new JFileChooser JavaDoc(currentDirectory);
396             try
397             {
398                 if (filechooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION)
399                 {
400                     currentDirectory = filechooser.getSelectedFile().getParent();
401                     Map JavaDoc testPlan = testPlanFrame.getTestPlan();
402                     if (testPlan != null)
403                     {
404                         TestPlanWriter.write2prop(
405                             new FileOutputStream JavaDoc(filechooser.getSelectedFile()),
406                             testPlan);
407                         testPlanFrame.setTitle(filechooser.getSelectedFile().getName());
408                     }
409                 }
410             }
411             catch (Exception JavaDoc ex)
412             {
413                 log.log(BasicLevel.INFO, "Can't save test plan " + filechooser.getSelectedFile(), ex);
414             }
415         }
416         else if (command.equals(OPEN_CMD))
417         {
418             JFileChooser JavaDoc filechooser = new JFileChooser JavaDoc(currentDirectory);
419             try
420             {
421                 if ((testPlanFrame.isEmpty() || new GuiClearConfirm(frame).ask())
422                     && filechooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
423                 {
424                     currentDirectory = filechooser.getSelectedFile().getParent();
425                     testPlan = null;
426                     if (globalState.equals(BladeState.RUNNING)
427                         || globalState.equals(BladeState.SUSPENDED)
428                         || globalState.equals(BladeState.INITIALIZED))
429                     {
430                         testCtl.stop();
431                     }
432                     testPlanFrame.setTestPlan(
433                         TestPlanReader.readFromProp(
434                             new FileInputStream JavaDoc(filechooser.getSelectedFile())));
435                     testPlanFrame.setTitle(filechooser.getSelectedFile().getName());
436                 }
437             }
438             catch (Exception JavaDoc ex)
439             {
440                 String JavaDoc message = "Can't open test plan " + filechooser.getSelectedFile();
441                 log.log(BasicLevel.ERROR, message, ex);
442                 new GuiAlert(frame, "Can't open test plan", ex.toString()).alert();
443             }
444         }
445         else if (command.equals(EDIT_CMD) && new GuiClearConfirm(frame).ask())
446         {
447             testPlan = null;
448             if (globalState.equals(BladeState.RUNNING)
449                 || globalState.equals(BladeState.SUSPENDED)
450                 || globalState.equals(BladeState.INITIALIZED))
451             {
452                 testCtl.stop();
453             }
454         }
455         else if (command.equals(UPDATE_CMD))
456         {
457             testPlanFrame.setAvailableServers(deployer.getServers());
458         }
459         else if (command.equals(INIT_CMD))
460         {
461             String JavaDoc testId = new GUIInitDialog(frame).ask();
462             if (testId != null)
463             {
464                 try
465                 {
466                     testCtl.init(new Object JavaDoc[] { testId, testPlan });
467                 }
468                 catch (Exception JavaDoc ex)
469                 {
470                     log.log(BasicLevel.ERROR, ex);
471                     new GuiAlert(frame, "Can't initialize test plan", ex.toString()).alert();
472                 }
473             }
474         }
475         else if (command.equals(START_CMD))
476         {
477             testCtl.start();
478             ellapsedTime = 0;
479             lastStartTime = System.currentTimeMillis();
480             ellapsedTimeTimer.start();
481         }
482         else if (command.equals(STOP_CMD))
483         {
484             testCtl.stop();
485         }
486         else if (command.equals(SUSPEND_CMD))
487         {
488             testCtl.suspend();
489         }
490         else if (command.equals(RESUME_CMD))
491         {
492             testCtl.resume();
493             lastStartTime = System.currentTimeMillis();
494             ellapsedTimeTimer.start();
495         }
496         else if (command.equals(COLLECT_CMD))
497         {
498             Cursor JavaDoc cursor = frame.getCursor();
499             frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
500             testCtl.collect();
501             frame.setCursor(cursor);
502         }
503         else if (command.equals(EXIT_CMD)
504             && (testPlanFrame.isEmpty() || new GuiClearConfirm(frame).ask()))
505         {
506             if (globalState.equals(BladeState.RUNNING)
507                 || globalState.equals(BladeState.SUSPENDED)
508                 || globalState.equals(BladeState.INITIALIZED))
509             {
510                 testCtl.stop();
511             }
512             deployer.deploy(null);
513             System.exit(0);
514         }
515         else if (command.equals("VIEW_OUTPUT"))
516         {
517             if (boxMessageOutput.getState())
518             {
519                 outputFrame.setVisible(true);
520             }
521             else
522             {
523                 outputFrame.setVisible(false);
524             }
525         }
526         else if (command.equals(DEPLOY_CMD))
527         {
528             Map JavaDoc newTestPlan = testPlanFrame.getTestPlan();
529             if (newTestPlan != null)
530             {
531                 testPlanFrame.setEditable(false);
532                 testPlan = newTestPlan;
533                 bladeStates = new HashMap JavaDoc();
534                 Iterator JavaDoc iter = testPlan.keySet().iterator();
535                 while (iter.hasNext())
536                 {
537                     bladeStates.put(iter.next(), BladeState.UNDEPLOYED);
538                 }
539                 testPlanFrame.setStatusLine(BladeState.DEPLOYING, 0);
540                 deployer.deploy(testPlan);
541             }
542         }
543         else if (command.equals(ABOUT_CMD))
544         {
545             new GuiAboutDialog(this.frame).show();
546         }
547         else if (command.equals(ANALYZER_CMD))
548         {
549             if (boxAnalysisTool.getState())
550             {
551                 analyzerFrame.setVisible(true);
552             }
553             else
554             {
555                 analyzerFrame.setVisible(false);
556             }
557         }
558         updateMenus();
559     }
560
561
562     ////////////////////////
563
// interface Observer //
564
////////////////////////
565

566
567     /**
568      * Receive alarms and blade state changes from the supervisor
569      */

570     public void update(Observable JavaDoc supervisor, Object JavaDoc observation)
571     {
572         if (observation instanceof BladeObservation)
573         {
574             String JavaDoc id = ((BladeObservation)observation).getBladeId();
575             BladeState state = ((BladeObservation)observation).getState();
576             bladeStates.put(id, state);
577             if (! state.equals(BladeState.DEPLOYED))
578             {
579                 globalState = BladeState.getGlobalState(bladeStates.values());
580             }
581             if (globalState.equals(BladeState.STOPPED)
582                 || globalState.equals(BladeState.SUSPENDED))
583             {
584                 if (lastStartTime != 0)
585                 {
586                     ellapsedTime += System.currentTimeMillis() - lastStartTime;
587                     lastStartTime = 0;
588                     ellapsedTimeTimer.stop();
589                 }
590                 testPlanFrame.setStatusLine(globalState, ellapsedTime);
591             }
592             testPlanFrame.setBladeState(id, state);
593         }
594         else if (observation instanceof DeployObservation)
595         {
596             DeployObservation obs = (DeployObservation)observation;
597             if (obs.isSuccessful())
598             {
599                 testCtl = obs.getTestControl();
600                 testCtl.addObserver(this);
601                 globalState = BladeState.DEPLOYED;
602             }
603             else
604             {
605                 String JavaDoc message = "Deployment failure";
606                 ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
607                 obs.getException().printStackTrace(new PrintStream JavaDoc(out));
608                 new GuiAlert(frame, message, out.toString()).alert();
609                 log.log(BasicLevel.ERROR, message, obs.getException());
610                 testPlan = null;
611             }
612         }
613         else if (observation instanceof AlarmEvent)
614         {
615             AlarmEvent alarm = (AlarmEvent)observation;
616             String JavaDoc argument = null;
617             if (alarm.argument != null)
618             {
619                 if (alarm.argument instanceof Throwable JavaDoc)
620                 {
621                     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
622                     ((Throwable JavaDoc)alarm.argument).printStackTrace(new PrintStream JavaDoc(out));
623                     argument = out.toString();
624                 }
625                 else
626                 {
627                     argument = alarm.argument.toString();
628                 }
629             }
630             new GuiAlert(
631                 frame,
632                 "Alarm from blade " + alarm.getBladeId(),
633                 argument).alert();
634         }
635         updateMenus();
636     }
637 }
638
Popular Tags