Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [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.control;
   20   
   21   import java.io.Serializable;
   22   import java.util.ArrayList;
   23   import java.util.Iterator;
   24   import java.util.LinkedList;
   25   import java.util.List;
   26   
   27   import org.apache.jmeter.engine.event.LoopIterationEvent;
   28   import org.apache.jmeter.engine.event.LoopIterationListener;
   29   import org.apache.jmeter.samplers.Sampler;
   30   import org.apache.jmeter.testelement.AbstractTestElement;
   31   import org.apache.jmeter.testelement.TestElement;
   32   import org.apache.jorphan.logging.LoggingManager;
   33   import org.apache.log.Logger;
   34   
   35   /**
   36    * <p>
   37    * This class is the basis for all the controllers.
   38    * It also implements SimpleController.
   39    * </p>
   40    * <p>
   41    * The main entry point is next(), which is called by by JMeterThread as follows:
   42    * </p>
   43    * <p>
   44    * <code>while (running && (sampler = controller.next()) != null)</code>
   45    * </p>
   46    */
   47   public class GenericController extends AbstractTestElement implements Controller, Serializable {
   48   
   49       private static final long serialVersionUID = 233L;
   50   
   51       private static final Logger log = LoggingManager.getLoggerForClass();
   52   
   53       private transient LinkedList iterationListeners = new LinkedList();
   54   
   55       // May be replaced by RandomOrderController
   56       protected transient List subControllersAndSamplers = new ArrayList();
   57   
   58       protected transient int current;
   59   
   60       private transient int iterCount;
   61   
   62       private transient boolean done, first;
   63   
   64       /**
   65        * Creates a Generic Controller
   66        */
   67       public GenericController() {
   68       }
   69   
   70       public void initialize() {
   71           resetCurrent();
   72           resetIterCount();
   73           done = false; // TODO should this use setDone()?
   74           first = true; // TODO should this use setFirst()?
   75           TestElement elem;
   76           for (int i = 0; i < subControllersAndSamplers.size(); i++) {
   77               elem = (TestElement) subControllersAndSamplers.get(i);
   78               if (elem instanceof Controller) {
   79                   ((Controller) elem).initialize();
   80               }
   81           }
   82       }
   83   
   84       /**
   85        * Resets the controller:
   86        * <ul>
   87        * <li>resetCurrent() (i.e. current=0)</li>
   88        * <li>increment iteration count</li>
   89        * <li>sets first=true</li>
   90        * <li>recoverRunningVersion() to set the controller back to the initial state</li>
   91        * </ul>
   92        *
   93        */
   94       protected void reInitialize() {
   95           resetCurrent();
   96           incrementIterCount();
   97           setFirst(true);
   98           recoverRunningVersion();
   99       }
  100   
  101       /**
  102        * <p>
  103        * Determines the next sampler to be processed.
  104        * </p>
  105        *
  106        * <p>
  107        * If isDone, returns null.
  108        * </p>
  109        *
  110        * <p>
  111        * Gets the list element using current pointer.
  112        * If this is null, calls {@link #nextIsNull()}.
  113        * </p>
  114        *
  115        * <p>
  116        * If the list element is a sampler, calls {@link #nextIsASampler(Sampler)},
  117        * otherwise calls {@link #nextIsAController(Controller)}
  118        * </p>
  119        *
  120        * <p>
  121        * If any of the called methods throws NextIsNullException, returns null,
  122        * otherwise the value obtained above is returned.
  123        * </p>
  124        *
  125        * @return the next sampler or null
  126        */
  127       public Sampler next() {
  128           fireIterEvents();
  129           if (log.isDebugEnabled()) {
  130               log.debug("Calling next on: " + this.getClass().getName());
  131           }
  132           if (isDone()) {
  133               return null;
  134           }
  135           Sampler returnValue = null;
  136           try {
  137               TestElement currentElement = getCurrentElement();
  138               setCurrentElement(currentElement);
  139               if (currentElement == null) {
  140                   // incrementCurrent();
  141                   returnValue = nextIsNull();
  142               } else {
  143                   if (currentElement instanceof Sampler) {
  144                       returnValue = nextIsASampler((Sampler) currentElement);
  145                   } else { // must be a controller
  146                       returnValue = nextIsAController((Controller) currentElement);
  147                   }
  148               }
  149           } catch (NextIsNullException e) {
  150           }
  151           return returnValue;
  152       }
  153   
  154       /**
  155        * @see org.apache.jmeter.control.Controller#isDone()
  156        */
  157       public boolean isDone() {
  158           return done;
  159       }
  160   
  161       protected void setDone(boolean done) {
  162           this.done = done;
  163       }
  164   
  165       protected boolean isFirst() {
  166           return first;
  167       }
  168   
  169       public void setFirst(boolean b) {
  170           first = b;
  171       }
  172   
  173       /**
  174        * Called by next() if the element is a Controller,
  175        * and returns the next sampler from the controller.
  176        * If this is null, then updates the current pointer and makes recursive call to next().
  177        * @param controller
  178        * @return the next sampler
  179        * @throws NextIsNullException
  180        */
  181       protected Sampler nextIsAController(Controller controller) throws NextIsNullException {
  182           Sampler sampler = controller.next();
  183           if (sampler == null) {
  184               currentReturnedNull(controller);
  185               sampler = next();
  186           }
  187           return sampler;
  188       }
  189   
  190       /**
  191        * Increment the current pointer and return the element.
  192        * Called by next() if the element is a sampler.
  193        * (May be overriden by sub-classes).
  194        *
  195        * @param element
  196        * @return input element
  197        * @throws NextIsNullException
  198        */
  199       protected Sampler nextIsASampler(Sampler element) throws NextIsNullException {
  200           incrementCurrent();
  201           return element;
  202       }
  203   
  204       /**
  205        * Called by next() when getCurrentElement() returns null.
  206        * Reinitialises the controller.
  207        *
  208        * @return null (always, for this class)
  209        * @throws NextIsNullException
  210        */
  211       protected Sampler nextIsNull() throws NextIsNullException {
  212           reInitialize();
  213           return null;
  214       }
  215   
  216       /**
  217        * If the controller is done, remove it from the list,
  218        * otherwise increment to next entry in list.
  219        *
  220        * @param c controller
  221        */
  222       protected void currentReturnedNull(Controller c) {
  223           if (c.isDone()) {
  224               removeCurrentElement();
  225           } else {
  226               incrementCurrent();
  227           }
  228       }
  229   
  230       /**
  231        * Gets the SubControllers attribute of the GenericController object
  232        *
  233        * @return the SubControllers value
  234        */
  235       protected List getSubControllers() {
  236           return subControllersAndSamplers;
  237       }
  238   
  239       private void addElement(TestElement child) {
  240           subControllersAndSamplers.add(child);
  241       }
  242   
  243       /**
  244        * Empty implementation - does nothing.
  245        *
  246        * @param currentElement
  247        * @throws NextIsNullException
  248        */
  249       protected void setCurrentElement(TestElement currentElement) throws NextIsNullException {
  250       }
  251   
  252       /**
  253        * <p>
  254        * Gets the element indicated by the <code>current</code> index, if one exists,
  255        * from the <code>subControllersAndSamplers</code> list.
  256        * </p>
  257        * <p>
  258        * If the <code>subControllersAndSamplers</code> list is empty,
  259        * then set done = true, and throw NextIsNullException.
  260        * </p>
  261        * @return the current element - or null if current index too large
  262        * @throws NextIsNullException if list is empty
  263        */
  264       protected TestElement getCurrentElement() throws NextIsNullException {
  265           if (current < subControllersAndSamplers.size()) {
  266               return (TestElement) subControllersAndSamplers.get(current);
  267           }
  268           if (subControllersAndSamplers.size() == 0) {
  269               setDone(true);
  270               throw new NextIsNullException();
  271           }
  272           return null;
  273       }
  274   
  275       protected void removeCurrentElement() {
  276           subControllersAndSamplers.remove(current);
  277       }
  278   
  279       /**
  280        * Increments the current pointer; called by currentReturnedNull to move the
  281        * controller on to its next child.
  282        */
  283       protected void incrementCurrent() {
  284           current++;
  285       }
  286   
  287       protected void resetCurrent() {
  288           current = 0;
  289       }
  290   
  291       public void addTestElement(TestElement child) {
  292           if (child instanceof Controller || child instanceof Sampler) {
  293               addElement(child);
  294           }
  295       }
  296   
  297       public void addIterationListener(LoopIterationListener lis) {
  298           /*
  299            * A little hack - add each listener to the start of the list - this
  300            * ensures that the thread running the show is the first listener and
  301            * can modify certain values before other listeners are called.
  302            */
  303           iterationListeners.addFirst(lis);
  304       }
  305   
  306       protected void fireIterEvents() {
  307           if (isFirst()) {
  308               fireIterationStart();
  309               first = false; // TODO - should this use setFirst() ?
  310           }
  311       }
  312   
  313       protected void fireIterationStart() {
  314           Iterator iter = iterationListeners.iterator();
  315           LoopIterationEvent event = new LoopIterationEvent(this, getIterCount());
  316           while (iter.hasNext()) {
  317               LoopIterationListener item = (LoopIterationListener) iter.next();
  318               item.iterationStart(event);
  319           }
  320       }
  321   
  322       protected int getIterCount() {
  323           return iterCount;
  324       }
  325   
  326       protected void incrementIterCount() {
  327           iterCount++;
  328       }
  329   
  330       protected void resetIterCount() {
  331           iterCount = 0;
  332       }
  333   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [javadoc | source]