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.TestElement;
025: import org.apache.jmeter.testelement.property.IntegerProperty;
026:
027: public class InterleaveControl extends GenericController implements
028: Serializable {
029: private static final String STYLE = "InterleaveControl.style";// $NON-NLS-1$
030:
031: public static final int IGNORE_SUB_CONTROLLERS = 0;
032:
033: public static final int USE_SUB_CONTROLLERS = 1;
034:
035: private boolean skipNext;
036:
037: transient private TestElement searchStart = null;
038:
039: private boolean currentReturnedAtLeastOne;
040:
041: private boolean stillSame = true;
042:
043: /***************************************************************************
044: * Constructor for the InterleaveControl object
045: **************************************************************************/
046: public InterleaveControl() {
047: }
048:
049: /*
050: * (non-Javadoc)
051: *
052: * @see org.apache.jmeter.control.GenericController#reInitialize()
053: */
054: public void reInitialize() {
055: setFirst(true);
056: currentReturnedAtLeastOne = false;
057: searchStart = null;
058: stillSame = true;
059: skipNext = false;
060: incrementIterCount();
061: recoverRunningVersion();
062: }
063:
064: public void setStyle(int style) {
065: setProperty(new IntegerProperty(STYLE, style));
066: }
067:
068: public int getStyle() {
069: return getPropertyAsInt(STYLE);
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see org.apache.jmeter.control.Controller#next()
076: */
077: public Sampler next() {
078: if (isSkipNext()) {
079: reInitialize();
080: return null;
081: }
082: return super .next();
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see GenericController#nextIsAController(Controller)
089: */
090: protected Sampler nextIsAController(Controller controller)
091: throws NextIsNullException {
092: Sampler sampler = controller.next();
093: if (sampler == null) {
094: currentReturnedNull(controller);
095: return next();
096: }
097: currentReturnedAtLeastOne = true;
098: if (getStyle() == IGNORE_SUB_CONTROLLERS) {
099: incrementCurrent();
100: skipNext = true;
101: } else {
102: searchStart = null;
103: }
104: return sampler;
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see org.apache.jmeter.control.GenericController#nextIsASampler(Sampler)
111: */
112: protected Sampler nextIsASampler(Sampler element)
113: throws NextIsNullException {
114: skipNext = true;
115: incrementCurrent();
116: return element;
117: }
118:
119: /**
120: * If the current is null, reset and continue searching. The searchStart
121: * attribute will break us off when we start a repeat.
122: *
123: * @see org.apache.jmeter.testelement.AbstractTestElement#nextIsNull()
124: */
125: protected Sampler nextIsNull() {
126: resetCurrent();
127: return next();
128: }
129:
130: /*
131: * (non-Javadoc)
132: *
133: * @see GenericController#setCurrentElement(TestElement)
134: */
135: protected void setCurrentElement(TestElement currentElement)
136: throws NextIsNullException {
137: // Set the position when next is first called, and don't overwrite
138: // until reInitialize is called.
139: if (searchStart == null) {
140: searchStart = currentElement;
141: } else if (searchStart == currentElement && !stillSame) {
142: // We've gone through the whole list and are now back at the start
143: // point of our search.
144: reInitialize();
145: throw new NextIsNullException();
146: }
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see GenericController#currentReturnedNull(Controller)
153: */
154: protected void currentReturnedNull(Controller c) {
155: if (c.isDone()) {
156: removeCurrentElement();
157: } else if (getStyle() == USE_SUB_CONTROLLERS) {
158: incrementCurrent();
159: }
160: }
161:
162: /**
163: * @return skipNext
164: */
165: protected boolean isSkipNext() {
166: return skipNext;
167: }
168:
169: /**
170: * @param skipNext
171: */
172: protected void setSkipNext(boolean skipNext) {
173: this .skipNext = skipNext;
174: }
175:
176: /*
177: * (non-Javadoc)
178: *
179: * @see org.apache.jmeter.control.GenericController#incrementCurrent()
180: */
181: protected void incrementCurrent() {
182: if (currentReturnedAtLeastOne) {
183: skipNext = true;
184: }
185: stillSame = false;
186: super.incrementCurrent();
187: }
188: }
|