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.threads.JMeterContext;
025: import org.apache.jmeter.threads.JMeterVariables;
026: import org.apache.jmeter.testelement.property.BooleanProperty;
027: import org.apache.jmeter.testelement.property.StringProperty;
028: import org.apache.jorphan.logging.LoggingManager;
029: import org.apache.log.Logger;
030:
031: public class ForeachController extends GenericController implements
032: Serializable {
033: private static final Logger log = LoggingManager
034: .getLoggerForClass();
035:
036: private final static String INPUTVAL = "ForeachController.inputVal";// $NON-NLS-1$
037:
038: private final static String RETURNVAL = "ForeachController.returnVal";// $NON-NLS-1$
039:
040: private final static String USE_SEPARATOR = "ForeachController.useSeparator";// $NON-NLS-1$
041:
042: private int loopCount = 0;
043:
044: private static final String DEFAULT_SEPARATOR = "_";// $NON-NLS-1$
045:
046: public ForeachController() {
047: }
048:
049: public void setInputVal(String inputValue) {
050: setProperty(new StringProperty(INPUTVAL, inputValue));
051: }
052:
053: private String getInputVal() {
054: getProperty(INPUTVAL).recoverRunningVersion(null);
055: return getInputValString();
056: }
057:
058: public String getInputValString() {
059: return getPropertyAsString(INPUTVAL);
060: }
061:
062: public void setReturnVal(String inputValue) {
063: setProperty(new StringProperty(RETURNVAL, inputValue));
064: }
065:
066: private String getReturnVal() {
067: getProperty(RETURNVAL).recoverRunningVersion(null);
068: return getReturnValString();
069: }
070:
071: public String getReturnValString() {
072: return getPropertyAsString(RETURNVAL);
073: }
074:
075: private String getSeparator() {
076: return getUseSeparator() ? DEFAULT_SEPARATOR : "";// $NON-NLS-1$
077: }
078:
079: public void setUseSeparator(boolean b) {
080: setProperty(new BooleanProperty(USE_SEPARATOR, b));
081: }
082:
083: public boolean getUseSeparator() {
084: return getPropertyAsBoolean(USE_SEPARATOR, true);
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see org.apache.jmeter.control.Controller#isDone()
091: */
092: public boolean isDone() {
093: JMeterContext context = getThreadContext();
094: String inputVariable = getInputVal() + getSeparator()
095: + (loopCount + 1);
096: final JMeterVariables variables = context.getVariables();
097: final Object currentVariable = variables
098: .getObject(inputVariable);
099: if (currentVariable != null) {
100: variables.putObject(getReturnVal(), currentVariable);
101: if (log.isDebugEnabled()) {
102: log.debug("ForEach resultstring isDone="
103: + variables.get(getReturnVal()));
104: }
105: return false;
106: }
107: return super .isDone();
108: }
109:
110: private boolean endOfArguments() {
111: JMeterContext context = getThreadContext();
112: String inputVariable = getInputVal() + getSeparator()
113: + (loopCount + 1);
114: if (context.getVariables().getObject(inputVariable) != null) {
115: log.debug("ForEach resultstring eofArgs= false");
116: return false;
117: }
118: log.debug("ForEach resultstring eofArgs= true");
119: return true;
120: }
121:
122: // Prevent entry if nothing to do
123: public Sampler next() {
124: if (emptyList()) {
125: reInitialize();
126: resetLoopCount();
127: return null;
128: }
129: return super .next();
130: }
131:
132: /**
133: * Check if there are any matching entries
134: *
135: * @return whether any entries in the list
136: */
137: private boolean emptyList() {
138: JMeterContext context = getThreadContext();
139: String inputVariable = getInputVal() + getSeparator() + "1";// $NON-NLS-1$
140: if (context.getVariables().getObject(inputVariable) != null) {
141: return false;
142: }
143: if (log.isDebugEnabled()) {
144: log.debug("No entries found - null first entry: "
145: + inputVariable);
146: }
147: return true;
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see org.apache.jmeter.control.GenericController#nextIsNull()
154: */
155: protected Sampler nextIsNull() throws NextIsNullException {
156: reInitialize();
157: if (endOfArguments()) {
158: // setDone(true);
159: resetLoopCount();
160: return null;
161: }
162: return next();
163: }
164:
165: protected void incrementLoopCount() {
166: loopCount++;
167: }
168:
169: protected void resetLoopCount() {
170: loopCount = 0;
171: }
172:
173: /*
174: * (non-Javadoc)
175: *
176: * @see org.apache.jmeter.control.GenericController#getIterCount()
177: */
178: protected int getIterCount() {
179: return loopCount + 1;
180: }
181:
182: /*
183: * (non-Javadoc)
184: *
185: * @see org.apache.jmeter.control.GenericController#reInitialize()
186: */
187: protected void reInitialize() {
188: setFirst(true);
189: resetCurrent();
190: incrementLoopCount();
191: recoverRunningVersion();
192: }
193: }
|