You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by ne...@apache.org on 2002/01/19 05:23:52 UTC

cvs commit: jakarta-jmeter/src/org/apache/jmeter/ejb/jndi/config/gui MethodConfigGui.java

neth        02/01/18 20:23:52

  Added:       src/org/apache/jmeter/ejb/jndi/config/gui
                        MethodConfigGui.java
  Log:
  Gui allowing the user to select the Home Interface method to be executed and add the param values of the selected method.
  
  Revision  Changes    Path
  1.1                  jakarta-jmeter/src/org/apache/jmeter/ejb/jndi/config/gui/MethodConfigGui.java
  
  Index: MethodConfigGui.java
  ===================================================================
  /*
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed by the
   * Apache Software Foundation (http://www.apache.org/)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   * "Apache JMeter" must not be used to endorse or promote products
   * derived from this software without prior written permission. For
   * written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   * "Apache JMeter", nor may "Apache" appear in their name, without
   * prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.jmeter.ejb.jndi.config.gui;
  
  import javax.swing.*;
  import javax.swing.event.*;
  import javax.swing.tree.*;
  import javax.swing.border.*;
  import java.awt.event.*;
  import javax.naming.InitialContext;
  import java.awt.*;
  import java.util.*;
  
  import java.lang.reflect.Field;
  
  import org.apache.jmeter.control.TestPlan;
  import org.apache.jmeter.engine.ReflectionJMeterEngine;
  import org.apache.jmeter.gui.GuiPackage;
  import org.apache.jmeter.gui.ModelSupported;
  import org.apache.jmeter.gui.NamePanel;
  import org.apache.jmeter.gui.VerticalLayout;
  import org.apache.jmeter.config.gui.ArgumentsPanel;
  import org.apache.jmeter.config.Arguments;
  import org.apache.jmeter.threads.ThreadGroup;
  import org.apache.jmeter.util.JMeterUtils;
  import org.apache.jmeter.ejb.jndi.config.MethodConfig;
  import org.apache.jmeter.ejb.jndi.config.MethodConfigUserObject;
  import org.apache.jmeter.ejb.jndi.config.MethodConfigUserObjectException;
  import org.apache.log4j.Category;
  
  /**
   * Provides the gui interface to configure remote method execution
   * @author	Khor Soon Hin
   * @version	1.0
   * @created	2001 Dec 24
   * @modified	2001 Dec 30
   */
  public class MethodConfigGui extends JPanel implements ModelSupported,
  	ActionListener, TreeSelectionListener
  {
    private static Category catClass = Category.getInstance(
  	MethodConfigGui.class.getName());
  
    protected static final String REFLECT = "MethodConfigGui.reflect";
    protected static final String INVOKE = "MethodConfigGui.invoke";
    protected static final String STRING_CLASS = "java.lang.String";
  
    protected JComboBox methodNameBox;
  
    protected DefaultMutableTreeNode root;
    protected DefaultTreeModel treeModel;
    protected NamePanel namePanel;
    protected JPanel methodNamePanel;
    protected JPanel methodParmsPanel;
    protected JPanel controlPanel;
    protected JButton actionButton;
    protected JTree jTree;
    protected boolean displayName;
    protected MethodConfig model;
    protected Frame frame;
  	// The frame is required because the JDialog we create must be modal
  	// and to be modal it must be modal relative to a frame.  We want
  	// the dialog boxes to be modal so that user must fill them up before
  	// they can do anything with jmeter
  
    public MethodConfigGui()
    {
      displayName = true;
    }
  
    public MethodConfigGui(boolean displayName)
    {
      this.displayName = displayName;
    }
  
    //----- ModelSupported interface : start -----
  
    public void setModel(Object model)
    {
      this.model = (MethodConfig)model;
      init();
    }
  
    public void updateGui()
    {
      catClass.debug("Start : updateGui1");
  System.out.println("updateGui1");
      // the method name box will always be displayed regardless of the state
      // of the MethodConfig of this gui
      String methodName = model.getMethodHomeName();
      catClass.debug("updateGui1 : method name - " + methodName);
      // change the methodNameBox selected item only if there's a change
      // to avoid triggering actionPerformed each time
      String methodNameBoxSelected = (String)methodNameBox.getSelectedItem();
      if((methodName != null) && !methodName.equals(methodNameBoxSelected))
      {
        methodNameBox.setSelectedItem(methodName);
      }
      // if the list is empty then try to fill it
      if(methodNameBox.getItemCount() == 0)
      {
        String[] strings = model.getMethodHomeList();
        if(strings != null)
        {
          for(int i = 0; i < strings.length; i++)
          {
            catClass.debug("updateGui1 : adding method - " + strings[i]);
            methodNameBox.addItem(strings[i]);
          }
        }
      }
      // if the state of the MethodConfig of this gui after 
      // MethodConfig.METHOD_GET_HOME_NAMES, display panel to get
      // parms of the selected method
      int childCount = treeModel.getChildCount(root);
      if(catClass.isDebugEnabled())
      {
        catClass.debug("updateGui1 : state - " + model.getState());
        catClass.debug("updateGui1 : root child count - " + childCount);
      }
      // if model is in state after getting method name AND
      // root(method) has no child(parms set) yet then get them
      if(model.getState() > MethodConfig.METHOD_GET_HOME_PARMS  && 
  	childCount == 0)
      {
        root.setUserObject(model.getMethodHomeName());
  //      methodParmsPanel.setVisible(true);
        // get all the parms
        Class[] parmTypes = model.getMethodHomeParms();
        // add all the parms into a JTree
        for(int i = 0 ;i < parmTypes.length; i++)
        {
          catClass.debug("updateGui1 : parmType #" + i + " - " + parmTypes[i]);
          recurseParm(parmTypes[i], root, i);
        }
      }
      // if the state of the MethodConfig of this gui is
      // MethodConfig.METHOD_GET_HOME_PARMS, display panel
      if(displayName)
      {
        namePanel.updateGui();
      }
      catClass.debug("End : updateGui1");
    }
  
    //----- ModelSupported interface : end -----
  
    protected void init()
    {
      catClass.info("Start : init1");
      // The frame is required because the JDialog we create must be modal
      // and to be modal it must be modal relative to a frame.  We want
      // the dialog boxes to be modal so that user must fill them up before
      // they can do anything with jmeter
      GuiPackage guiPkg = GuiPackage.getInstance();
      frame = guiPkg.getMainFrame();
  
      model.setState(MethodConfig.METHOD_GET_HOME_NAMES);
      model.setGui(this);
      methodNameBox = new JComboBox();
      methodNameBox.setEditable(false);
      methodNameBox.setAlignmentX(Component.LEFT_ALIGNMENT);
      methodNameBox.addActionListener(new ActionListener()
  	{
  	  public void actionPerformed(ActionEvent e)
  	  {
  System.out.println("action!!! - " + e);
  System.out.println("actionEvent - " + e.getActionCommand());
  	    // change in method name so do the following
  	    JComboBox comboBox = (JComboBox)e.getSource();
  	    String method = (String)methodNameBox.getSelectedItem();
  	    model.setMethodHomeName(method);
              model.setState(MethodConfig.METHOD_GET_HOME_PARMS);
              // remove all parms of the old method
              int totalChild = root.getChildCount();
              for(int i = 0; i < totalChild; i++)
              {
                // the child to be removed will always be 0 'cos as the child
                // is removed the nth node will become (n-1)th
                treeModel.removeNodeFromParent(
  			(DefaultMutableTreeNode)root.getChildAt(0));
              }
              root.setUserObject("Root");
  //            methodParmsPanel.setVisible(false);
              actionButton.setText(
  		JMeterUtils.getResString("jndi_method_button_reflect"));
              updateGui();
   	  }
  	});
      if(displayName)
      {
        this.setLayout(new VerticalLayout(5, VerticalLayout.LEFT, 
  	VerticalLayout.TOP));
        // main panel
        JPanel mainPanel = new JPanel();
        Border margin = new EmptyBorder(10, 10, 5, 10);
        mainPanel.setBorder(margin);
        mainPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT));
  
        // title
        JLabel panelTitleLabel = new JLabel(
  	JMeterUtils.getResString("jndi_method_title"));
        Font curFont = panelTitleLabel.getFont();
        int curFontSize = curFont.getSize();
        curFontSize += 4;
        panelTitleLabel.setFont(new Font(curFont.getFontName(), 
  	curFont.getStyle(), curFontSize));
        mainPanel.add(panelTitleLabel);
  
        // name
        namePanel = new NamePanel(model);
        mainPanel.add(namePanel);
  
        // method properties
        JPanel jndiPanel = new JPanel();
        jndiPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT));
        jndiPanel.setBorder(BorderFactory.createTitledBorder(
  	JMeterUtils.getResString("jndi_method_name")));
  
        methodNamePanel = getMethodHomeNamePanel();
        methodParmsPanel = getMethodHomeParmsPanel();
        jndiPanel.add(methodNamePanel);
        jndiPanel.add(methodParmsPanel);
        controlPanel = new JPanel();
        actionButton = new JButton(
  	JMeterUtils.getResString("jndi_method_button_reflect"));
        actionButton.addActionListener(this);
        actionButton.setActionCommand(REFLECT);
        controlPanel.add(actionButton);
        jndiPanel.add(controlPanel);
  
  
        mainPanel.add(jndiPanel);
  
        this.add(mainPanel);
  
        // during initial stages i.e. before method name has been selected
        // the other panels are not required
  //      methodParmsPanel.setVisible(false);
      }
      else
      {
        this.setLayout(new VerticalLayout(5, VerticalLayout.LEFT));
  
        // url and driver class
        JPanel jndiPanel = new JPanel();
        jndiPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT));
        jndiPanel.setBorder(BorderFactory.createTitledBorder(
  	JMeterUtils.getResString("jndi_method_name")));
  
        methodNamePanel = getMethodHomeNamePanel();
        methodParmsPanel = getMethodHomeParmsPanel();
        jndiPanel.add(methodNamePanel);
        jndiPanel.add(methodParmsPanel);
        JPanel controlPanel = new JPanel();
        actionButton = new JButton(
  	JMeterUtils.getResString("jndi_method_button_reflect"));
        actionButton.addActionListener(this);
        actionButton.setActionCommand(REFLECT);
        controlPanel.add(actionButton);
        jndiPanel.add(controlPanel);
  
        this.add(jndiPanel);
  
        // during initial stages i.e. before method name has been selected
        // the other panels are not required
  //      methodParmsPanel.setVisible(false);
      }
      catClass.info("End : init1");
    }
  
    /**
     * Given a parameter type of a method, this method will find out
     * if the parm type is a primitive.  If so, it'll just add the parm type
     * as a node into the tree model (used to store all parm types of the method).
     * If however, the parm type is not a primitive, then the parm type will be
     * further recursed i.e. a reflection of all the fields is obtained
     * and each of which will be examined to determined if they are primitives.
     * For those which are primitives, they'll be added to the tree model
     * and those otherwise will be further recursed.
     *
     * @param parmType	the parmType of a method which will be examined
     * @param parentNode	the node under which the parmType will be added to
     * @param childIndex	the index of the this parmType if it were to be added
     *			under the parent node
     */
  
    protected void recurseParm(Class parmType, DefaultMutableTreeNode parentNode,
  	int childIndex)
    {
      catClass.debug("Start - recurseParm1");
      DefaultMutableTreeNode node = new DefaultMutableTreeNode(parmType);
      treeModel.insertNodeInto(node, parentNode, childIndex);
  System.out.println("recurseParm1 - parent : " + parentNode);
  System.out.println("recurseParm1 - parent : " + treeModel.getChildCount(parentNode));
  System.out.println("recurseParm1 - child : " + treeModel.getChildCount(node));
      if(parmType.isPrimitive())
      {
        // if parmType is primitive then no need to recurse the parm
      }
      else if(parmType.getName().equals(STRING_CLASS))
      {
        // consider String as a primitive
      }
      else if(parmType.isArray())
      {
        // if parmType is array then need to handle differently
      }
      else
      {
        // if parmType is NOT primitive then need to recurse the parm since
        // it's an object.
        // to recurse the object, use reflections to get all fields
        Field[] fields = parmType.getFields();
        for(int i = 0; i < fields.length; i++)
        {
          Class fieldClass = fields[i].getType();
          catClass.debug("recurseParm1 : field #" + i + " - " + fieldClass);
          recurseParm(fieldClass, node, i);
        }
      }
      catClass.debug("End - recurseParm1");
    }
  
    protected JPanel getMethodHomeNamePanel()
    {
      catClass.info("Start : getMethodHomeNamePanel1");
      JPanel panel = new JPanel();
      panel.add(new JLabel(JMeterUtils.getResString("jndi_method_name")));
      String methodName = model.getMethodHomeName();
      if(methodName != null)
      {
        methodNameBox.setSelectedItem(methodName);
      }
      panel.add(methodNameBox);
      catClass.info("End : getMethodHomeNamePanel1");
      return panel;
    }
  
    protected JPanel getMethodHomeParmsPanel()
    {
      catClass.info("Start : getMethodHomeParmsPanel1");
      JPanel panel = new JPanel();
      panel.add(new JLabel(JMeterUtils.getResString("jndi_method_parms")));
      root = new DefaultMutableTreeNode("Root");
      treeModel = new DefaultTreeModel(root);
      jTree = new JTree(treeModel);
      jTree.getSelectionModel().setSelectionMode(
  	TreeSelectionModel.SINGLE_TREE_SELECTION);
      jTree.addTreeSelectionListener(this);
      jTree.setPreferredSize(new Dimension(200, 50));
      JPanel jTreePanel = new JPanel();
      jTreePanel.add(jTree);
      panel.add(jTreePanel);
      // set mouse listener to listen for double clicks
      MouseListener ml = new MouseAdapter()
  	{
  	  public void mousePressed(MouseEvent e)
  	  {
  	    TreePath selPath = jTree.getPathForLocation(e.getX(), e.getY());
  	    if(e.getClickCount() == 2)
  	    {
  System.out.println("Double clicked on - " + selPath.getLastPathComponent());
  	      DefaultMutableTreeNode node =
  			(DefaultMutableTreeNode)selPath.getLastPathComponent();
  	      int childCount = node.getChildCount();
  	      if(childCount == 0)
  	      {
  System.out.println("Pop!!!");
                  Object userObject = node.getUserObject();
  	        Class type = null;
                  if(userObject instanceof Class)
                  {
                    type = (Class)userObject;
  	        }
  	        else if(userObject instanceof MethodConfigUserObject)
  	        {
  	          type = (Class)((MethodConfigUserObject)userObject).getType();
  	        }
  	        MethodConfigDialog dialog = new MethodConfigDialog(frame, type);
  	        dialog.pack();
  	        dialog.setVisible(true);
  	        MethodConfigUserObject input = dialog.getValidatedInput();
  System.out.println("input - " + input);
  	        if(input != null)
  	        {
  	          node.setUserObject(input);
  	        }
  	      }
  	    }
  	  }
  	};
      jTree.addMouseListener(ml);
      catClass.info("End : getMethodHomeParmsPanel1");
      return panel;
    }
  
    public MethodConfig getModel()
    {
      return model;
    }
  
    public Object[] getMethodParmsValues()
  	throws MethodConfigUserObjectException
    {
      catClass.info("Start : getMethodParmsValues1");
      // go through jTree to get all arguments
      int childCount = root.getChildCount();
      Object[] parmsValues = new Object[childCount];
      for(int i = 0; i < childCount; i++)
      {
        parmsValues[i] = formObject((DefaultMutableTreeNode)
  		treeModel.getChild(root, i));
      }
      catClass.info("End : getMethodParmsValues1");
      return parmsValues;
    }
  
    protected Object formObject(DefaultMutableTreeNode node)
  	throws MethodConfigUserObjectException
    {
      catClass.info("Start : formObject1");
      Object obj = node.getUserObject();
      Object returnVal = null;
      if(obj instanceof MethodConfigUserObject)
      {
        // then node contains a primitive so just get the object
        MethodConfigUserObject userObject = (MethodConfigUserObject)obj;
        returnVal = userObject.getObject();
        if(catClass.isDebugEnabled())
        {
          catClass.debug("formObject1 : primitive - " + userObject);
        }
      }
      else if(obj instanceof Class)
      {
        // there are cases when the tree node will contain only class
        // and not MethodConfigUserObject -
        // 1. its value has not been input by the user but it's a primitive
        // 2. it's not a primitive but an object
        Class type = (Class)obj;
        if(type.isPrimitive())
        {
          // it's a primitive but the user has not input a value for it
          String errorStr = type.getName() + 
  		" is a primitive with uninitialized values";
          catClass.error("formObject1 : " + errorStr);
          throw new MethodConfigUserObjectException(errorStr);
        }
        else
        {
          // then node is an object which contains other primitives
          if(catClass.isDebugEnabled())
          { 
            catClass.debug("formObject1 : Creating object - " + type);
          }
          int childCount = node.getChildCount();
          Object[] constituents = new Object[childCount];
          for(int i = 0; i < childCount; i++)
          {
            constituents[i] = formObject((DefaultMutableTreeNode)
  		treeModel.getChild(node, i));
          }
          // get the fields of the class
          // gather all constituents to form object
          Field[] fields = type.getFields();
          try
          {
            for(int i = 0; i < constituents.length; i++)
            {
              catClass.debug("formObject1 : setting - " + fields[i].getName());
              catClass.debug("formObject1 : to value - " + constituents[i]
  		+ " of - " + constituents[i].getClass());
              returnVal = type.newInstance();
              fields[i].set(returnVal, constituents[i]);
            }
          }
          catch(IllegalAccessException e)
          {
            catClass.error(e);
            throw new MethodConfigUserObjectException(e.getMessage());
          }
          catch(InstantiationException e)
          {
            catClass.error(e);
            throw new MethodConfigUserObjectException(e.getMessage());
          }
        }
      }
      catClass.info("End : formObject1");
      return returnVal;
    }
  
    //----- ActionListener interface : start -----
  
    public void actionPerformed(ActionEvent e)
    {
      String command = e.getActionCommand();
      ReflectionJMeterEngine engine = null;
      GuiPackage guiPackage = null;
      if(catClass.isDebugEnabled())
      {
        catClass.debug("actionPerformed1 : command - " + command);
      }
      if(command.equals(REFLECT))
      {
        guiPackage = GuiPackage.getInstance();
        Collection groups = TestPlan.createTestPlan(null).compileTestPlan();
        engine = new ReflectionJMeterEngine();
        for(Iterator i = groups.iterator(); i.hasNext();)
        {
          ThreadGroup tg = (ThreadGroup)i.next();
          if(catClass.isDebugEnabled())
          {
            catClass.debug("actionPerformed1 : threadgroup - " + tg);
          }
          engine.addThreadGroup(tg);
        }
        guiPackage.getMainFrame().setRunning(true);
        engine.runTest();
        guiPackage.getMainFrame().setRunning(false);
        updateGui();
      }
      if(command.equals(INVOKE))
      { 
      }
    }
  
    //----- ActionListener interface : end -----
  
    //----- TreeSelectionListener interface : start -----
  
    public void valueChanged(TreeSelectionEvent e)
    {
      catClass.debug("Start : valueChanged1");
      catClass.debug("End : valueChanged1");
    }
  
    //----- TreeSelectionListener interface : end -----
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>