1 18 19 package org.apache.jmeter.protocol.http.proxy.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Dimension ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.FocusEvent ; 26 import java.awt.event.FocusListener ; 27 import java.awt.event.KeyEvent ; 28 import java.awt.event.KeyListener ; 29 import java.util.Arrays ; 30 import java.util.Collection ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 34 import javax.swing.BorderFactory ; 35 import javax.swing.Box ; 36 import javax.swing.DefaultComboBoxModel ; 37 import javax.swing.JButton ; 38 import javax.swing.JCheckBox ; 39 import javax.swing.JComboBox ; 40 import javax.swing.JLabel ; 41 import javax.swing.JOptionPane ; 42 import javax.swing.JPanel ; 43 import javax.swing.JScrollPane ; 44 import javax.swing.JTable ; 45 import javax.swing.JTextField ; 46 47 import org.apache.jmeter.control.Controller; 48 import org.apache.jmeter.control.gui.LogicControllerGui; 49 import org.apache.jmeter.engine.util.ValueReplacer; 50 import org.apache.jmeter.functions.InvalidVariableException; 51 import org.apache.jmeter.gui.GuiPackage; 52 import org.apache.jmeter.gui.JMeterGUIComponent; 53 import org.apache.jmeter.gui.UnsharedComponent; 54 import org.apache.jmeter.gui.tree.JMeterTreeNode; 55 import org.apache.jmeter.gui.util.HorizontalPanel; 56 import org.apache.jmeter.gui.util.MenuFactory; 57 import org.apache.jmeter.gui.util.PowerTableModel; 58 import org.apache.jmeter.protocol.http.proxy.ProxyControl; 59 import org.apache.jmeter.testelement.TestElement; 60 import org.apache.jmeter.testelement.TestPlan; 61 import org.apache.jmeter.testelement.WorkBench; 62 import org.apache.jmeter.testelement.property.PropertyIterator; 63 import org.apache.jmeter.util.JMeterUtils; 64 import org.apache.jorphan.logging.LoggingManager; 65 import org.apache.log.Logger; 66 67 70 public class ProxyControlGui 71 extends LogicControllerGui 72 implements 73 JMeterGUIComponent, 74 ActionListener , 75 KeyListener , 76 FocusListener , 77 UnsharedComponent 78 { 79 private static transient Logger log = LoggingManager.getLoggerForClass(); 80 private JTextField portField; 81 82 87 private JCheckBox httpHeaders; 88 89 93 private JComboBox groupingMode; 94 95 98 private JCheckBox addAssertions; 99 100 104 private JCheckBox useKeepAlive; 105 106 109 private JComboBox targetNodes; 110 private DefaultComboBoxModel targetNodesModel; 111 112 private ProxyControl model; 113 114 private JTable excludeTable; 115 private PowerTableModel excludeModel; 116 private JTable includeTable; 117 private PowerTableModel includeModel; 118 private static final String CHANGE_TARGET = "change_target"; 119 120 private JButton stop, start, restart; 121 private static final String STOP = "stop"; 122 private static final String START = "start"; 123 private static final String RESTART = "restart"; 124 private static final String ADD_INCLUDE = "add_include"; 125 private static final String ADD_EXCLUDE = "add_exclude"; 126 private static final String DELETE_INCLUDE = "delete_include"; 127 private static final String DELETE_EXCLUDE = "delete_exclude"; 128 129 private static final String INCLUDE_COL = 130 JMeterUtils.getResString("patterns_to_include"); 131 private static final String EXCLUDE_COL = 132 JMeterUtils.getResString("patterns_to_exclude"); 133 134 public ProxyControlGui() 135 { 136 super(); 137 log.debug("Creating ProxyControlGui"); 138 init(); 139 } 140 141 public TestElement createTestElement() 142 { 143 model = makeProxyControl(); 144 log.debug("creating/configuring model = " + model); 145 modifyTestElement(model); 146 return model; 147 } 148 149 protected ProxyControl makeProxyControl() 150 { 151 ProxyControl local = new ProxyControl(); 152 return local; 153 } 154 155 159 public void modifyTestElement(TestElement el) 160 { 161 configureTestElement(el); 162 if(el instanceof ProxyControl) 163 { 164 model = (ProxyControl)el; 165 model.setPort(portField.getText()); 166 setIncludeListInProxyControl(model); 167 setExcludeListInProxyControl(model); 168 model.setCaptureHttpHeaders(httpHeaders.isSelected()); 169 model.setGroupingMode(groupingMode.getSelectedIndex()); 170 model.setAssertions(addAssertions.isSelected()); 171 model.setUseKeepAlive(useKeepAlive.isSelected()); 172 TreeNodeWrapper nw= (TreeNodeWrapper)targetNodes.getSelectedItem(); 173 if (nw == null) 174 { 175 model.setTarget(null); 176 } 177 else 178 { 179 model.setTarget(nw.getTreeNode()); 180 } 181 } 182 } 183 184 protected void setIncludeListInProxyControl(ProxyControl element) 185 { 186 List includeList = getDataList(includeModel, INCLUDE_COL); 187 element.setIncludeList(includeList); 188 } 189 190 protected void setExcludeListInProxyControl(ProxyControl element) 191 { 192 List excludeList = getDataList(excludeModel, EXCLUDE_COL); 193 element.setExcludeList(excludeList); 194 } 195 196 private List getDataList(PowerTableModel model, String colName) 197 { 198 String [] dataArray = model.getData().getColumn(colName); 199 List list = new LinkedList (); 200 for (int i = 0; i < dataArray.length; i++) 201 { 202 list.add(dataArray[i]); 203 } 204 return list; 205 } 206 207 public String getLabelResource() 208 { 209 return "proxy_title"; 210 } 211 212 public Collection getMenuCategories() 213 { 214 return Arrays.asList(new String [] { MenuFactory.NON_TEST_ELEMENTS }); 215 } 216 217 public void configure(TestElement element) 218 { 219 log.debug("Configuring gui with " + element); 220 super.configure(element); 221 model = (ProxyControl)element; 222 portField.setText(model.getPropertyAsString(ProxyControl.PORT)); 223 httpHeaders.setSelected(model.getPropertyAsBoolean(ProxyControl.CAPTURE_HTTP_HEADERS)); 224 groupingMode.setSelectedIndex(model.getPropertyAsInt(ProxyControl.GROUPING_MODE)); 225 addAssertions.setSelected(model.getPropertyAsBoolean(ProxyControl.ADD_ASSERTIONS)); 226 useKeepAlive.setSelected(model.getPropertyAsBoolean(ProxyControl.USE_KEEPALIVE,true)); 227 228 reinitializeTargetCombo(); 229 230 populateTable(includeModel, model.getIncludePatterns().iterator()); 231 populateTable(excludeModel, model.getExcludePatterns().iterator()); 232 repaint(); 233 } 234 235 private void populateTable(PowerTableModel model, PropertyIterator iter) 236 { 237 model.clearData(); 238 while (iter.hasNext()) 239 { 240 model.addRow(new Object [] { iter.next().getStringValue()}); 241 } 242 model.fireTableDataChanged(); 243 } 244 245 public void focusLost(FocusEvent e) 246 { 247 try 248 { 249 ((JTable ) e.getSource()).getCellEditor().stopCellEditing(); 250 } 251 catch (Exception err) 252 {} 253 } 254 255 public void focusGained(FocusEvent e) 256 {} 257 258 263 public void actionPerformed(ActionEvent action) 264 { 265 String command = action.getActionCommand(); 266 267 if (command.equals(STOP)) 268 { 269 model.stopProxy(); 270 stop.setEnabled(false); 271 start.setEnabled(true); 272 restart.setEnabled(false); 273 } 274 else if (command.equals(START)) 275 { 276 startProxy(); 277 } 278 else if (command.equals(RESTART)) 279 { 280 model.stopProxy(); 281 startProxy(); 282 } 283 else if ( command.equals(ProxyControl.CAPTURE_HTTP_HEADERS) 284 || command.equals(ProxyControl.ADD_ASSERTIONS) 285 || command.equals(ProxyControl.GROUPING_MODE) 286 || command.equals(ProxyControl.USE_KEEPALIVE) 287 ) 288 { 289 enableRestart(); 290 } 291 else if (command.equals(ADD_EXCLUDE)) 292 { 293 excludeModel.addNewRow(); 294 excludeModel.fireTableDataChanged(); 295 enableRestart(); 296 } 297 else if (command.equals(ADD_INCLUDE)) 298 { 299 includeModel.addNewRow(); 300 includeModel.fireTableDataChanged(); 301 enableRestart(); 302 } 303 else if (command.equals(DELETE_EXCLUDE)) 304 { 305 excludeModel.removeRow(excludeTable.getSelectedRow()); 306 excludeModel.fireTableDataChanged(); 307 enableRestart(); 308 } 309 else if (command.equals(DELETE_INCLUDE)) 310 { 311 includeModel.removeRow(includeTable.getSelectedRow()); 312 includeModel.fireTableDataChanged(); 313 enableRestart(); 314 } 315 else if (command.equals(CHANGE_TARGET)) 316 { 317 log.debug("Change target "+targetNodes.getSelectedItem()); 318 log.debug("In model "+model); 319 TreeNodeWrapper nw= (TreeNodeWrapper)targetNodes.getSelectedItem(); 320 model.setTarget(nw.getTreeNode()); 321 } 322 } 323 324 private void startProxy() 325 { 326 ValueReplacer replacer = GuiPackage.getInstance().getReplacer(); 327 modifyTestElement(model); 328 try 329 { 330 replacer.replaceValues(model); 331 model.startProxy(); 332 start.setEnabled(false); 333 stop.setEnabled(true); 334 restart.setEnabled(false); 335 } 336 catch (InvalidVariableException e) 337 { 338 JOptionPane.showMessageDialog( 339 this, 340 JMeterUtils.getResString("invalid_variables"), 341 "Error", 342 JOptionPane.ERROR_MESSAGE); 343 } 344 } 345 346 private void enableRestart() 347 { 348 if (stop.isEnabled()) 349 { 350 restart.setEnabled(true); 351 } 352 } 353 354 359 public void keyPressed(KeyEvent e) 360 {} 361 362 367 public void keyTyped(KeyEvent e) 368 {} 369 370 375 public void keyReleased(KeyEvent e) 376 { 377 String fieldName = e.getComponent().getName(); 378 379 if (fieldName.equals(ProxyControl.PORT)) 380 { 381 try 382 { 383 Integer.parseInt(portField.getText()); 384 } 385 catch (NumberFormatException nfe) 386 { 387 if (portField.getText().length() > 0) 388 { 389 JOptionPane.showMessageDialog( 390 this, 391 "You must enter a valid number", 392 "Invalid data", 393 JOptionPane.WARNING_MESSAGE); 394 395 portField.setText(""); 403 } 404 } 405 enableRestart(); 406 } 407 } 408 409 private void init() 410 { 411 setLayout(new BorderLayout (0, 5)); 412 setBorder(makeBorder()); 413 414 add(makeTitlePanel(), BorderLayout.NORTH); 415 416 JPanel mainPanel = new JPanel (new BorderLayout ()); 417 418 Box myBox = Box.createVerticalBox(); 419 myBox.add(createPortPanel()); 420 myBox.add(Box.createVerticalStrut(5)); 421 myBox.add(createTargetPanel()); 422 myBox.add(Box.createVerticalStrut(5)); 423 myBox.add(createGroupingPanel()); 424 myBox.add(Box.createVerticalStrut(5)); 425 mainPanel.add(myBox, BorderLayout.NORTH); 426 427 Box includeExcludePanel = Box.createVerticalBox(); 428 includeExcludePanel.add(createIncludePanel()); 429 includeExcludePanel.add(createExcludePanel()); 430 mainPanel.add(includeExcludePanel, BorderLayout.CENTER); 431 432 mainPanel.add(createControls(), BorderLayout.SOUTH); 433 434 add(mainPanel, BorderLayout.CENTER); 435 } 436 437 private JPanel createControls() 438 { 439 start = new JButton (JMeterUtils.getResString("start")); 440 start.addActionListener(this); 441 start.setActionCommand(START); 442 start.setEnabled(true); 443 444 stop = new JButton (JMeterUtils.getResString("stop")); 445 stop.addActionListener(this); 446 stop.setActionCommand(STOP); 447 stop.setEnabled(false); 448 449 restart = new JButton (JMeterUtils.getResString("restart")); 450 restart.addActionListener(this); 451 restart.setActionCommand(RESTART); 452 restart.setEnabled(false); 453 454 JPanel panel = new JPanel (); 455 panel.add(start); 456 panel.add(stop); 457 panel.add(restart); 458 return panel; 459 } 460 461 private JPanel createPortPanel() 462 { 463 portField = new JTextField (ProxyControl.DEFAULT_PORT_S, 8); 464 portField.setName(ProxyControl.PORT); 465 portField.addKeyListener(this); 466 467 JLabel label = new JLabel (JMeterUtils.getResString("port")); 468 label.setLabelFor(portField); 469 470 httpHeaders = new JCheckBox (JMeterUtils.getResString("proxy_headers")); 471 httpHeaders.setName(ProxyControl.CAPTURE_HTTP_HEADERS); 472 httpHeaders.setSelected(true); httpHeaders.addActionListener(this); 474 httpHeaders.setActionCommand(ProxyControl.CAPTURE_HTTP_HEADERS); 475 476 addAssertions = new JCheckBox (JMeterUtils.getResString("proxy_assertions")); 477 addAssertions.setName(ProxyControl.ADD_ASSERTIONS); 478 addAssertions.setSelected(false); 479 addAssertions.addActionListener(this); 480 addAssertions.setActionCommand(ProxyControl.ADD_ASSERTIONS); 481 482 useKeepAlive = new JCheckBox (JMeterUtils.getResString("proxy_usekeepalive")); 483 useKeepAlive.setName(ProxyControl.USE_KEEPALIVE); 484 useKeepAlive.setSelected(true); 485 useKeepAlive.addActionListener(this); 486 useKeepAlive.setActionCommand(ProxyControl.USE_KEEPALIVE); 487 488 HorizontalPanel panel = new HorizontalPanel(); 489 panel.add(label); 490 panel.add(portField); 491 492 panel.add(Box.createHorizontalStrut(10)); 493 panel.add(httpHeaders); 494 495 panel.add(useKeepAlive); 496 panel.add(addAssertions); 497 498 return panel; 499 } 500 501 private JPanel createTargetPanel() 502 { 503 targetNodesModel= new DefaultComboBoxModel (); 504 targetNodes = new JComboBox (targetNodesModel); 505 targetNodes.setActionCommand(CHANGE_TARGET); 506 508 JLabel label = new JLabel (JMeterUtils.getResString("proxy_target")); 509 label.setLabelFor(targetNodes); 510 511 HorizontalPanel panel = new HorizontalPanel(); 512 panel.add(label); 513 panel.add(targetNodes); 514 515 526 547 548 return panel; 549 } 550 551 private JPanel createGroupingPanel() 552 { 553 DefaultComboBoxModel m= new DefaultComboBoxModel (); 554 m.addElement(JMeterUtils.getResString("grouping_no_groups")); 557 m.addElement(JMeterUtils.getResString("grouping_add_separators")); 558 m.addElement(JMeterUtils.getResString("grouping_in_controllers")); 559 m.addElement(JMeterUtils.getResString("grouping_store_first_only")); 560 groupingMode = new JComboBox (m); 561 groupingMode.setName(ProxyControl.GROUPING_MODE); 562 groupingMode.setSelectedIndex(0); 563 groupingMode.addActionListener(this); 564 groupingMode.setActionCommand(ProxyControl.GROUPING_MODE); 565 566 JLabel label2 = new JLabel (JMeterUtils.getResString("grouping_mode")); 567 label2.setLabelFor(groupingMode); 568 569 HorizontalPanel panel = new HorizontalPanel(); 570 panel.add(label2); 571 panel.add(groupingMode); 572 573 return panel; 574 } 575 576 private JPanel createIncludePanel() 577 { 578 includeModel = new PowerTableModel( 579 new String [] { INCLUDE_COL }, 580 new Class [] { String .class }); 581 includeTable = new JTable (includeModel); 582 includeTable.setPreferredScrollableViewportSize(new Dimension (100, 50)); 583 includeTable.addFocusListener(this); 584 585 JPanel panel = new JPanel (new BorderLayout ()); 586 panel.setBorder(BorderFactory.createTitledBorder( 587 BorderFactory.createEtchedBorder(), 588 JMeterUtils.getResString("patterns_to_include"))); 589 590 panel.add(new JScrollPane (includeTable), BorderLayout.CENTER); 591 panel.add(createTableButtonPanel(ADD_INCLUDE, DELETE_INCLUDE), 592 BorderLayout.SOUTH); 593 594 return panel; 595 } 596 597 private JPanel createExcludePanel() 598 { 599 excludeModel = new PowerTableModel( 600 new String [] { EXCLUDE_COL }, 601 new Class [] { String .class }); 602 excludeTable = new JTable (excludeModel); 603 excludeTable.setPreferredScrollableViewportSize(new Dimension (100, 50)); 604 excludeTable.addFocusListener(this); 605 606 JPanel panel = new JPanel (new BorderLayout ()); 607 panel.setBorder( 608 BorderFactory.createTitledBorder( 609 BorderFactory.createEtchedBorder(), 610 JMeterUtils.getResString("patterns_to_exclude"))); 611 612 panel.add(new JScrollPane (excludeTable), BorderLayout.CENTER); 613 panel.add(createTableButtonPanel(ADD_EXCLUDE, DELETE_EXCLUDE), 614 BorderLayout.SOUTH); 615 616 return panel; 617 } 618 619 private JPanel createTableButtonPanel( 620 String addCommand, 621 String deleteCommand) 622 { 623 JPanel buttonPanel = new JPanel (); 624 625 JButton addButton = new JButton (JMeterUtils.getResString("add")); 626 addButton.setActionCommand(addCommand); 627 addButton.addActionListener(this); 628 buttonPanel.add(addButton); 629 630 JButton deleteButton = new JButton (JMeterUtils.getResString("delete")); 631 deleteButton.setActionCommand(deleteCommand); 632 deleteButton.addActionListener(this); 633 buttonPanel.add(deleteButton); 634 635 return buttonPanel; 636 } 637 638 private void reinitializeTargetCombo() { 639 log.debug("Reinitializing target combo"); 640 641 targetNodes.removeActionListener(this); 643 644 targetNodesModel.removeAllElements(); 645 GuiPackage gp = GuiPackage.getInstance(); 646 JMeterTreeNode root; 647 if (gp != null) 648 { 649 root = 650 (JMeterTreeNode) GuiPackage 651 .getInstance() 652 .getTreeModel() 653 .getRoot(); 654 targetNodesModel.addElement( 655 new TreeNodeWrapper( 656 null, 657 JMeterUtils.getResString("use_recording_controller"))); 658 buildNodesModel(root, "", 0); 659 } 660 TreeNodeWrapper choice = null; 661 for (int i = 0; i < targetNodesModel.getSize(); i++) 662 { 663 choice = (TreeNodeWrapper) targetNodesModel.getElementAt(i); 664 log.debug("Selecting item "+choice+" for model "+model+" in "+this); 665 if (choice.getTreeNode() == model.getTarget()) { 667 break; 668 } 669 } 670 targetNodes.addActionListener(this); 672 targetNodesModel.setSelectedItem(choice); 674 675 log.debug("Reinitialization complete"); 676 } 677 678 private void buildNodesModel( 679 JMeterTreeNode node, 680 String parent_name, 681 int level) 682 { 683 String seperator = " > "; 684 if (node != null) 685 { 686 for (int i = 0; i < node.getChildCount(); i++) 687 { 688 StringBuffer name = new StringBuffer (); 689 JMeterTreeNode cur = (JMeterTreeNode) node.getChildAt(i); 690 TestElement te = cur.getTestElement(); 691 704 if (te instanceof Controller) 705 { 706 name.append(spaces(level)); 707 name.append(parent_name); 708 name.append(cur.getName()); 709 TreeNodeWrapper tnw = 710 new TreeNodeWrapper(cur, name.toString()); 711 targetNodesModel.addElement(tnw); 712 name = new StringBuffer (); 713 name.append(cur.getName()); 714 name.append(seperator); 715 buildNodesModel(cur, name.toString(), level + 1); 716 } 717 else if (te instanceof TestPlan || te instanceof WorkBench) 718 { 719 name.append(cur.getName()); 720 name.append(seperator); 721 buildNodesModel(cur, name.toString(), 0); 722 } 723 else { 724 log.error("Cannot process "+te); 725 } 726 } 727 } 728 } 729 730 private String spaces(int level) 731 { 732 int multi = 4; 733 StringBuffer spaces = new StringBuffer (level * multi); 734 for (int i = 0; i < level * multi; i++) 735 { 736 spaces.append(" "); 737 } 738 return spaces.toString(); 739 } 740 741 public void setNode(JMeterTreeNode node) 742 { 743 getNamePanel().setNode(node); 744 } 745 } 746 747 class TreeNodeWrapper 748 { 749 private JMeterTreeNode tn; 750 private String label; 751 752 private TreeNodeWrapper() 753 { 754 }; 755 756 public TreeNodeWrapper(JMeterTreeNode tn, String label) 757 { 758 this.tn = tn; 759 this.label = label; 760 } 761 762 public JMeterTreeNode getTreeNode() 763 { 764 return tn; 765 } 766 767 public String toString() 768 { 769 return label; 770 } 771 } 772 | Popular Tags |