KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > control > ForeachController


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/ForeachController.java,v 1.3.2.4 2005/03/17 03:11:46 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 JavaDoc;
22
23 import org.apache.jmeter.samplers.Sampler;
24 import org.apache.jmeter.threads.JMeterContext;
25 import org.apache.jmeter.testelement.property.BooleanProperty;
26 import org.apache.jmeter.testelement.property.StringProperty;
27 import org.apache.jorphan.logging.LoggingManager;
28 import org.apache.log.Logger;
29
30 /**
31  * @author Dolf Smits
32  * @author Michael Stover
33  * @author Thad Smith
34  * @version $Revision: 1.3.2.4 $
35  */

36 public class ForeachController extends GenericController implements Serializable JavaDoc
37 {
38     private static final Logger log = LoggingManager.getLoggerForClass();
39
40     private final static String JavaDoc INPUTVAL = "ForeachController.inputVal";
41     private final static String JavaDoc RETURNVAL ="ForeachController.returnVal";
42     private final static String JavaDoc USE_SEPARATOR ="ForeachController.useSeparator";
43     private int loopCount = 0;
44
45     private static final String JavaDoc DEFAULT_SEPARATOR = "_";
46     
47     public ForeachController()
48     {
49     }
50     
51     public void setInputVal(String JavaDoc inputValue)
52     {
53         setProperty(new StringProperty(INPUTVAL, inputValue));
54     }
55
56     private String JavaDoc getInputVal()
57     {
58         getProperty(INPUTVAL).recoverRunningVersion(null);
59         return getInputValString();
60     }
61     public String JavaDoc getInputValString()
62     {
63         return getPropertyAsString(INPUTVAL);
64     }
65
66     public void setReturnVal(String JavaDoc inputValue)
67     {
68         setProperty(new StringProperty(RETURNVAL, inputValue));
69     }
70
71     private String JavaDoc getReturnVal()
72     {
73         getProperty(RETURNVAL).recoverRunningVersion(null);
74         return getReturnValString();
75     }
76     public String JavaDoc getReturnValString()
77     {
78         return getPropertyAsString(RETURNVAL);
79     }
80
81     private String JavaDoc getSeparator()
82     {
83         return getUseSeparator() ? DEFAULT_SEPARATOR : "";
84     }
85     
86     public void setUseSeparator(boolean b)
87     {
88         setProperty(new BooleanProperty(USE_SEPARATOR, b));
89     }
90
91     public boolean getUseSeparator()
92     {
93         return getPropertyAsBoolean(USE_SEPARATOR,true);
94     }
95
96    /* (non-Javadoc)
97      * @see org.apache.jmeter.control.Controller#isDone()
98      */

99     public boolean isDone()
100     {
101         JMeterContext context = getThreadContext();
102         String JavaDoc inputVariable=getInputVal()+getSeparator()+(loopCount+1);
103         if (context.getVariables().get(inputVariable) != null)
104         {
105            context.getVariables().put(getReturnVal(), context.getVariables().get(inputVariable));
106                    log.debug("ForEach resultstring isDone="+context.getVariables().get(getReturnVal()));
107            return false;
108         }
109         return super.isDone();
110     }
111
112     private boolean endOfArguments()
113     {
114         JMeterContext context = getThreadContext();
115         String JavaDoc inputVariable=getInputVal()+getSeparator()+(loopCount+1);
116         if (context.getVariables().get(inputVariable) != null)
117         {
118            log.debug("ForEach resultstring eofArgs= false");
119            return false;
120         } else {
121            log.debug("ForEach resultstring eofArgs= true");
122            return true;
123         }
124     }
125
126     // Prevent entry if nothing to do
127
public Sampler next()
128     {
129         if(emptyList())
130         {
131             reInitialize();
132             return null;
133         }
134         return super.next();
135     }
136
137     /**
138      * Check if there are any matching entries
139      *
140      * @return whethere any entries in the list
141      */

142     private boolean emptyList() {
143         JMeterContext context = getThreadContext();
144         String JavaDoc inputVariable=getInputVal()+getSeparator()+"1";
145         if (context.getVariables().get(inputVariable) != null)
146         {
147            return false;
148         }
149         else
150         {
151             log.debug("No entries found - null first entry: "+inputVariable);
152             return true;
153         }
154     }
155
156     /* (non-Javadoc)
157      * @see org.apache.jmeter.control.GenericController#nextIsNull()
158      */

159     protected Sampler nextIsNull() throws NextIsNullException
160     {
161         reInitialize();
162         if (endOfArguments())
163         {
164 // setDone(true);
165
resetLoopCount();
166            return null;
167         }
168         else
169         {
170             return next();
171         }
172     }
173
174     protected void incrementLoopCount()
175     {
176         loopCount++;
177     }
178
179     protected void resetLoopCount()
180     {
181         loopCount = 0;
182     }
183
184     /* (non-Javadoc)
185      * @see org.apache.jmeter.control.GenericController#getIterCount()
186      */

187     protected int getIterCount()
188     {
189         return loopCount + 1;
190     }
191
192     /* (non-Javadoc)
193      * @see org.apache.jmeter.control.GenericController#reInitialize()
194      */

195     protected void reInitialize()
196     {
197         setFirst(true);
198         resetCurrent();
199         incrementLoopCount();
200     }
201 }
Popular Tags