1 24 25 package org.objectweb.clif.console.lib.gui; 26 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.awt.event.KeyEvent ; 30 import java.awt.Cursor ; 31 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Observer ; 35 import java.util.Observable ; 36 import java.io.FileOutputStream ; 37 import java.io.FileInputStream ; 38 import java.io.ByteArrayOutputStream ; 39 import java.io.PrintStream ; 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 ; 55 import javax.swing.JMenuBar ; 56 import javax.swing.JInternalFrame ; 57 import javax.swing.JCheckBoxMenuItem ; 58 import javax.swing.JMenuItem ; 59 import javax.swing.Timer ; 60 import javax.swing.ImageIcon ; 61 import javax.swing.JMenu ; 62 import javax.swing.KeyStroke ; 63 import javax.swing.JFileChooser ; 64 65 66 73 public class GuiConsoleImpl 74 implements Observer , ActionListener 75 { 76 static public final String LOCAL_NAME = "local host"; 77 81 static public int clifWidth = 900; 82 static public int clifHeight = 700; 83 84 static private final String ABOUT_CMD = "about"; 85 static private final String DEPLOY_CMD = "deploy"; 86 static private final String INIT_CMD = "init"; 87 static private final String START_CMD = "start"; 88 static private final String STOP_CMD = "stop"; 89 static private final String COLLECT_CMD = "collect"; 90 static private final String SUSPEND_CMD = "suspend"; 91 static private final String RESUME_CMD = "resume"; 92 static private final String UPDATE_CMD = "update"; 93 static private final String EXIT_CMD = "exit"; 94 static private final String SAVE_CMD = "save"; 95 static private final String OPEN_CMD = "open"; 96 static private final String EDIT_CMD = "edit"; 97 static private final String ANALYZER_CMD = "analyzer"; 98 static private Logger log = Monolog.getDefaultMonologFactory().getLogger(GuiConsoleImpl.class.getName()); 99 100 101 static public void main(String [] args) 102 { 103 try 104 { 105 new GuiConsoleImpl(new Deployer()); 106 } 107 catch (Exception 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 currentDirectory = "."; 118 private BladeState globalState = BladeState.UNDEPLOYED; 119 private TestControl testCtl = null; 120 private Map testPlan; 121 private Map bladeStates; 122 private JFrame frame; 123 private JMenuBar menuBar; 124 private GuiPanelOutput guiPanelOutput; 125 private JInternalFrame outputFrame; 126 private TestPlanWindow testPlanFrame; 127 private GuiAnalyserImpl analyzerFrame; 128 private JCheckBoxMenuItem boxMessageOutput; 129 private JCheckBoxMenuItem boxAnalysisTool; 130 private JMenuItem itemDeploy; 131 private JMenuItem itemEdit; 132 private JMenuItem itemInit; 133 private JMenuItem itemStart; 134 private JMenuItem itemStop; 135 private JMenuItem itemSuspend; 136 private JMenuItem itemResume; 137 private JMenuItem itemCollect; 138 private JMenuItem itemRebind; 139 private JMenuItem itemAbout; 140 private long ellapsedTime; 141 private long lastStartTime; 142 private Timer 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 ex) 153 { 154 throw new Error ( 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 clifIcon = new ImageIcon (getClass().getClassLoader().getResource("icons/clificon.png")); 162 163 frame = new JFrame ("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 ("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 (1000, this); 206 } 207 208 209 212 private void createMenuBar() 213 { 214 menuBar = new JMenuBar (); 215 JMenu menu; 216 JMenuItem item; 217 218 menu = new JMenu ("File"); 220 221 item = new JMenuItem ("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 ("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 ("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 241 menu = new JMenu ("Test plan"); 242 243 itemRebind = new JMenuItem ("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 289 menu = new JMenu ("Tools"); 290 291 boxMessageOutput = new JCheckBoxMenuItem ("Messages", false); 292 boxMessageOutput.setActionCommand("VIEW_OUTPUT"); 293 menu.add(boxMessageOutput).addActionListener(this); 294 295 boxAnalysisTool = new JCheckBoxMenuItem ("Analyzer", false); 296 boxAnalysisTool.setActionCommand(ANALYZER_CMD); 297 menu.add(boxAnalysisTool).addActionListener(this); 298 299 menuBar.add(menu); 300 301 304 menu = new JMenu ("?"); 305 itemAbout = new JMenuItem ("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 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 380 381 public void actionPerformed(ActionEvent e) 382 { 383 String 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 filechooser = new JFileChooser (currentDirectory); 396 try 397 { 398 if (filechooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) 399 { 400 currentDirectory = filechooser.getSelectedFile().getParent(); 401 Map testPlan = testPlanFrame.getTestPlan(); 402 if (testPlan != null) 403 { 404 TestPlanWriter.write2prop( 405 new FileOutputStream (filechooser.getSelectedFile()), 406 testPlan); 407 testPlanFrame.setTitle(filechooser.getSelectedFile().getName()); 408 } 409 } 410 } 411 catch (Exception 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 filechooser = new JFileChooser (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 (filechooser.getSelectedFile()))); 435 testPlanFrame.setTitle(filechooser.getSelectedFile().getName()); 436 } 437 } 438 catch (Exception ex) 439 { 440 String 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 testId = new GUIInitDialog(frame).ask(); 462 if (testId != null) 463 { 464 try 465 { 466 testCtl.init(new Object [] { testId, testPlan }); 467 } 468 catch (Exception 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 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 newTestPlan = testPlanFrame.getTestPlan(); 529 if (newTestPlan != null) 530 { 531 testPlanFrame.setEditable(false); 532 testPlan = newTestPlan; 533 bladeStates = new HashMap (); 534 Iterator 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 566 567 570 public void update(Observable supervisor, Object observation) 571 { 572 if (observation instanceof BladeObservation) 573 { 574 String 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 message = "Deployment failure"; 606 ByteArrayOutputStream out = new ByteArrayOutputStream (); 607 obs.getException().printStackTrace(new PrintStream (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 argument = null; 617 if (alarm.argument != null) 618 { 619 if (alarm.argument instanceof Throwable ) 620 { 621 ByteArrayOutputStream out = new ByteArrayOutputStream (); 622 ((Throwable )alarm.argument).printStackTrace(new PrintStream (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 |