Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.java.config.gui » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.jmeter.protocol.java.config.gui;
   20   
   21   import java.awt.BorderLayout;
   22   import java.awt.event.ActionEvent;
   23   import java.awt.event.ActionListener;
   24   import java.util.ArrayList;
   25   import java.util.List;
   26   import java.util.Map;
   27   
   28   import javax.swing.JComboBox;
   29   import javax.swing.JLabel;
   30   import javax.swing.JPanel;
   31   
   32   import org.apache.jmeter.config.Argument;
   33   import org.apache.jmeter.config.Arguments;
   34   import org.apache.jmeter.config.gui.AbstractConfigGui;
   35   import org.apache.jmeter.config.gui.ArgumentsPanel;
   36   import org.apache.jmeter.gui.util.HorizontalPanel;
   37   import org.apache.jmeter.protocol.java.config.JavaConfig;
   38   import org.apache.jmeter.protocol.java.sampler.JavaSampler;
   39   import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
   40   import org.apache.jmeter.testelement.TestElement;
   41   import org.apache.jmeter.testelement.property.PropertyIterator;
   42   import org.apache.jmeter.util.JMeterUtils;
   43   import org.apache.jorphan.reflect.ClassFinder;
   44   import org.apache.jorphan.logging.LoggingManager;
   45   import org.apache.log.Logger;
   46   
   47   /**
   48    * The <code>JavaConfigGui</code> class provides the user interface for the
   49    * {@link JavaConfig} object.
   50    *
   51    */
   52   public class JavaConfigGui extends AbstractConfigGui implements ActionListener {
   53       /** Logging */
   54       private static final Logger log = LoggingManager.getLoggerForClass();
   55   
   56       /** The name of the classnameCombo JComboBox */
   57       private static final String CLASSNAMECOMBO = "classnamecombo";
   58   
   59       /** A combo box allowing the user to choose a test class. */
   60       private JComboBox classnameCombo;
   61   
   62       /**
   63        * Indicates whether or not the name of this component should be displayed
   64        * as part of the GUI. If true, this is a standalone component. If false, it
   65        * is embedded in some other component.
   66        */
   67       private boolean displayName = true;
   68   
   69       /** A panel allowing the user to set arguments for this test. */
   70       private ArgumentsPanel argsPanel;
   71   
   72       /**
   73        * Create a new JavaConfigGui as a standalone component.
   74        */
   75       public JavaConfigGui() {
   76           this(true);
   77       }
   78   
   79       /**
   80        * Create a new JavaConfigGui as either a standalone or an embedded
   81        * component.
   82        *
   83        * @param displayNameField
   84        *            tells whether the component name should be displayed with the
   85        *            GUI. If true, this is a standalone component. If false, this
   86        *            component is embedded in some other component.
   87        */
   88       public JavaConfigGui(boolean displayNameField) {
   89           this.displayName = displayNameField;
   90           init();
   91       }
   92   
   93       public String getLabelResource() {
   94           return "java_request_defaults"; // $NON-NLS-1$
   95       }
   96   
   97       /**
   98        * Initialize the GUI components and layout.
   99        */
  100       private void init() {// called from ctor, so must not be overridable
  101           setLayout(new BorderLayout(0, 5));
  102   
  103           if (displayName) {
  104               setBorder(makeBorder());
  105               add(makeTitlePanel(), BorderLayout.NORTH);
  106           }
  107   
  108           JPanel classnameRequestPanel = new JPanel(new BorderLayout(0, 5));
  109           classnameRequestPanel.add(createClassnamePanel(), BorderLayout.NORTH);
  110           classnameRequestPanel.add(createParameterPanel(), BorderLayout.CENTER);
  111   
  112           add(classnameRequestPanel, BorderLayout.CENTER);
  113       }
  114   
  115       /**
  116        * Create a panel with GUI components allowing the user to select a test
  117        * class.
  118        *
  119        * @return a panel containing the relevant components
  120        */
  121       private JPanel createClassnamePanel() {
  122           List possibleClasses = new ArrayList();
  123   
  124           try {
  125               // Find all the classes which implement the JavaSamplerClient
  126               // interface.
  127               possibleClasses = ClassFinder.findClassesThatExtend(JMeterUtils.getSearchPaths(),
  128                       new Class[] { JavaSamplerClient.class });
  129   
  130               // Remove the JavaConfig class from the list since it only
  131               // implements the interface for error conditions.
  132   
  133               possibleClasses.remove(JavaSampler.class.getName() + "$ErrorSamplerClient");
  134           } catch (Exception e) {
  135               log.debug("Exception getting interfaces.", e);
  136           }
  137   
  138           JLabel label = new JLabel(JMeterUtils.getResString("protocol_java_classname")); // $NON-NLS-1$
  139   
  140           classnameCombo = new JComboBox(possibleClasses.toArray());
  141           classnameCombo.addActionListener(this);
  142           classnameCombo.setName(CLASSNAMECOMBO);
  143           classnameCombo.setEditable(false);
  144           label.setLabelFor(classnameCombo);
  145   
  146           HorizontalPanel panel = new HorizontalPanel();
  147           panel.add(label);
  148           panel.add(classnameCombo);
  149   
  150           return panel;
  151       }
  152   
  153       /**
  154        * Handle action events for this component. This method currently handles
  155        * events for the classname combo box.
  156        *
  157        * @param evt
  158        *            the ActionEvent to be handled
  159        */
  160       public void actionPerformed(ActionEvent evt) {
  161           if (evt.getSource() == classnameCombo) {
  162               String className = ((String) classnameCombo.getSelectedItem()).trim();
  163               try {
  164                   JavaSamplerClient client = (JavaSamplerClient) Class.forName(className, true,
  165                           Thread.currentThread().getContextClassLoader()).newInstance();
  166   
  167                   Arguments currArgs = new Arguments();
  168                   argsPanel.modifyTestElement(currArgs);
  169                   Map currArgsMap = currArgs.getArgumentsAsMap();
  170   
  171                   Arguments newArgs = new Arguments();
  172                   Arguments testParams = null;
  173                   try {
  174                       testParams = client.getDefaultParameters();
  175                   } catch (AbstractMethodError e) {
  176                       log.warn("JavaSamplerClient doesn't implement "
  177                               + "getDefaultParameters.  Default parameters won't "
  178                               + "be shown.  Please update your client class: " + className);
  179                   }
  180   
  181                   if (testParams != null) {
  182                       PropertyIterator i = testParams.getArguments().iterator();
  183                       while (i.hasNext()) {
  184                           Argument arg = (Argument) i.next().getObjectValue();
  185                           String name = arg.getName();
  186                           String value = arg.getValue();
  187   
  188                           // If a user has set parameters in one test, and then
  189                           // selects a different test which supports the same
  190                           // parameters, those parameters should have the same
  191                           // values that they did in the original test.
  192                           if (currArgsMap.containsKey(name)) {
  193                               String newVal = (String) currArgsMap.get(name);
  194                               if (newVal != null && newVal.length() > 0) {
  195                                   value = newVal;
  196                               }
  197                           }
  198                           newArgs.addArgument(name, value);
  199                       }
  200                   }
  201   
  202                   argsPanel.configure(newArgs);
  203               } catch (Exception e) {
  204                   log.error("Error getting argument list for " + className, e);
  205               }
  206           }
  207       }
  208   
  209       /**
  210        * Create a panel containing components allowing the user to provide
  211        * arguments to be passed to the test class instance.
  212        *
  213        * @return a panel containing the relevant components
  214        */
  215       private JPanel createParameterPanel() {
  216           argsPanel = new ArgumentsPanel(JMeterUtils.getResString("paramtable")); // $NON-NLS-1$
  217           return argsPanel;
  218       }
  219   
  220       /* Overrides AbstractJMeterGuiComponent.configure(TestElement) */
  221       public void configure(TestElement config) {
  222           super.configure(config);
  223   
  224           argsPanel.configure((Arguments) config.getProperty(JavaSampler.ARGUMENTS).getObjectValue());
  225   
  226           classnameCombo.setSelectedItem(config.getPropertyAsString(JavaSampler.CLASSNAME));
  227       }
  228   
  229       /* Implements JMeterGUIComponent.createTestElement() */
  230       public TestElement createTestElement() {
  231           JavaConfig config = new JavaConfig();
  232           modifyTestElement(config);
  233           return config;
  234       }
  235   
  236       /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
  237       public void modifyTestElement(TestElement config) {
  238           configureTestElement(config);
  239           ((JavaConfig) config).setArguments((Arguments) argsPanel.createTestElement());
  240           ((JavaConfig) config).setClassname(String.valueOf(classnameCombo.getSelectedItem()));
  241       }
  242   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.java.config.gui » [javadoc | source]