001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.control;
020:
021: import java.io.Serializable;
022:
023: import org.apache.jmeter.samplers.Sampler;
024: import org.apache.jmeter.testelement.property.BooleanProperty;
025: import org.apache.jmeter.testelement.property.IntegerProperty;
026: import org.apache.jmeter.testelement.property.JMeterProperty;
027: import org.apache.jmeter.testelement.property.StringProperty;
028:
029: /**
030: * @author Michael Stover
031: * @author Thad Smith
032: * @version $Revision: 537660 $
033: */
034: public class LoopController extends GenericController implements
035: Serializable {
036:
037: private final static String LOOPS = "LoopController.loops"; // $NON-NLS-1$
038:
039: private final static String CONTINUE_FOREVER = "LoopController.continue_forever"; // $NON-NLS-1$
040:
041: private transient int loopCount = 0;
042:
043: public LoopController() {
044: setContinueForever(true);
045: }
046:
047: public void setLoops(int loops) {
048: setProperty(new IntegerProperty(LOOPS, loops));
049: }
050:
051: public void setLoops(String loopValue) {
052: setProperty(new StringProperty(LOOPS, loopValue));
053: }
054:
055: public int getLoops() {
056: try {
057: JMeterProperty prop = getProperty(LOOPS);
058: return Integer.parseInt(prop.getStringValue());
059: } catch (NumberFormatException e) {
060: return 0;
061: }
062: }
063:
064: public String getLoopString() {
065: return getPropertyAsString(LOOPS);
066: }
067:
068: /**
069: * Determines whether the loop will return any samples if it is rerun.
070: *
071: * @param forever
072: * true if the loop must be reset after ending a run
073: */
074: public void setContinueForever(boolean forever) {
075: setProperty(new BooleanProperty(CONTINUE_FOREVER, forever));
076: }
077:
078: public boolean getContinueForever() {
079: return getPropertyAsBoolean(CONTINUE_FOREVER);
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see org.apache.jmeter.control.Controller#next()
086: */
087: public Sampler next() {
088: if (endOfLoop()) {
089: return null;
090: } else {
091: return super .next();
092: }
093: }
094:
095: private boolean endOfLoop() {
096: return (getLoops() > -1) && loopCount >= getLoops();
097: }
098:
099: /*
100: * (non-Javadoc)
101: *
102: * @see org.apache.jmeter.control.GenericController#nextIsNull()
103: */
104: protected Sampler nextIsNull() throws NextIsNullException {
105: reInitialize();
106: if (endOfLoop()) {
107: if (!getContinueForever()) {
108: setDone(true);
109: } else {
110: resetLoopCount();
111: }
112: return null;
113: }
114: return next();
115: }
116:
117: protected void incrementLoopCount() {
118: loopCount++;
119: }
120:
121: protected void resetLoopCount() {
122: loopCount = 0;
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see org.apache.jmeter.control.GenericController#getIterCount()
129: */
130: protected int getIterCount() {
131: return loopCount + 1;
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see org.apache.jmeter.control.GenericController#reInitialize()
138: */
139: protected void reInitialize() {
140: setFirst(true);
141: resetCurrent();
142: incrementLoopCount();
143: recoverRunningVersion();
144: }
145: }
|