KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > modifiers > UserParameters


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/modifiers/UserParameters.java,v 1.22.2.1 2004/06/20 00:53:16 sebb Exp $
2
/*
3  * Copyright 2002-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.modifiers;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.LinkedList JavaDoc;
24
25 import org.apache.jmeter.engine.event.LoopIterationEvent;
26 import org.apache.jmeter.engine.event.LoopIterationListener;
27 import org.apache.jmeter.processor.PreProcessor;
28 import org.apache.jmeter.testelement.AbstractTestElement;
29 import org.apache.jmeter.testelement.TestElement;
30 import org.apache.jmeter.testelement.property.BooleanProperty;
31 import org.apache.jmeter.testelement.property.CollectionProperty;
32 import org.apache.jmeter.testelement.property.PropertyIterator;
33 import org.apache.jmeter.threads.JMeterVariables;
34 import org.apache.jorphan.logging.LoggingManager;
35 import org.apache.log.Logger;
36
37 /**
38  * @version $Revision: 1.22.2.1 $
39  */

40 public class UserParameters
41     extends AbstractTestElement
42     implements Serializable JavaDoc, PreProcessor, LoopIterationListener
43 {
44     private static final Logger log = LoggingManager.getLoggerForClass();
45
46     public static final String JavaDoc NAMES = "UserParameters.names";
47     public static final String JavaDoc THREAD_VALUES = "UserParameters.thread_values";
48     public static final String JavaDoc PER_ITERATION = "UserParameters.per_iteration";
49     
50     /*
51      * Although the lock appears to be an instance lock, in fact
52      * the lock is shared between all threads in a thread group, but different
53      * thread groups have different locks - see the clone() method below
54      *
55      * The lock ensures that all the variables are processed together, which is
56      * important for functions such as __CSVRead and _StringFromFile.
57     */

58     private Integer JavaDoc lock = new Integer JavaDoc(0);
59
60     public CollectionProperty getNames()
61     {
62         return (CollectionProperty) getProperty(NAMES);
63     }
64
65     public CollectionProperty getThreadLists()
66     {
67         return (CollectionProperty) getProperty(THREAD_VALUES);
68     }
69
70     /**
71      * The list of names of the variables to hold values. This list must come
72      * in the same order as the sub lists that are given to
73      * {@link #setThreadLists(Collection)}.
74      */

75     public void setNames(Collection JavaDoc list)
76     {
77         setProperty(new CollectionProperty(NAMES, list));
78     }
79
80     /**
81      * The list of names of the variables to hold values. This list must come
82      * in the same order as the sub lists that are given to
83      * {@link #setThreadLists(CollectionProperty)}.
84      */

85     public void setNames(CollectionProperty list)
86     {
87         setProperty(list);
88     }
89
90     /**
91      * The thread list is a list of lists. Each list within the parent list is
92      * a collection of values for a simulated user. As many different sets of
93      * values can be supplied in this fashion to cause JMeter to set different
94      * values to variables for different test threads.
95      */

96     public void setThreadLists(Collection JavaDoc threadLists)
97     {
98         setProperty(new CollectionProperty(THREAD_VALUES, threadLists));
99     }
100
101     /**
102      * The thread list is a list of lists. Each list within the parent list is
103      * a collection of values for a simulated user. As many different sets of
104      * values can be supplied in this fashion to cause JMeter to set different
105      * values to variables for different test threads.
106      */

107     public void setThreadLists(CollectionProperty threadLists)
108     {
109         setProperty(threadLists);
110     }
111
112     private CollectionProperty getValues()
113     {
114         CollectionProperty threadValues =
115             (CollectionProperty) getProperty(THREAD_VALUES);
116         if (threadValues.size() > 0)
117         {
118             return (CollectionProperty) threadValues.get(
119                 getThreadContext().getThreadNum()
120                     % threadValues.size());
121         }
122         else
123         {
124             return new CollectionProperty("noname", new LinkedList JavaDoc());
125         }
126     }
127
128     public boolean isPerIteration()
129     {
130         return getPropertyAsBoolean(PER_ITERATION);
131     }
132
133     public void setPerIteration(boolean perIter)
134     {
135         setProperty(new BooleanProperty(PER_ITERATION, perIter));
136     }
137
138     public void process()
139     {
140         if (log.isDebugEnabled())
141         {
142             log.debug(Thread.currentThread().getName() + " process " + isPerIteration());//$NON-NLS-1$
143
}
144         if (!isPerIteration())
145         {
146             setValues();
147         }
148     }
149
150     private void setValues()
151     {
152         synchronized (lock)
153         {
154             if (log.isDebugEnabled())
155             {
156                 log.debug(Thread.currentThread().getName() + " Running up named: " + getName());//$NON-NLS-1$
157
}
158             PropertyIterator namesIter = getNames().iterator();
159             PropertyIterator valueIter = getValues().iterator();
160             JMeterVariables jmvars = getThreadContext().getVariables();
161             while (namesIter.hasNext() && valueIter.hasNext())
162             {
163                 String JavaDoc name = namesIter.next().getStringValue();
164                 String JavaDoc value = valueIter.next().getStringValue();
165                 if (log.isDebugEnabled())
166                 {
167                     log.debug(Thread.currentThread().getName()+" saving variable: "+name+"="+value);//$NON-NLS-1$
168
}
169                 jmvars.put(name, value);
170             }
171         }
172     }
173
174     /**
175      * @see LoopIterationListener#iterationStart(LoopIterationEvent)
176      */

177     public void iterationStart(LoopIterationEvent event)
178     {
179         if (log.isDebugEnabled())
180         {
181             log.debug(Thread.currentThread().getName() + " iteration start " + isPerIteration());//$NON-NLS-1$
182
}
183         if (isPerIteration())
184         {
185             setValues();
186         }
187     }
188
189     /* (non-Javadoc)
190      * A new instance is created for each thread group, and the
191      * clone() method is then called to create copies for each thread
192      * in a thread group.
193      * This means that the lock object is common to a thread group;
194      * separate thread groups have separate locks.
195      * If this is not intended, the lock object could be made static.
196      *
197      * @see java.lang.Object#clone()
198      */

199     public Object JavaDoc clone()
200     {
201         UserParameters up = (UserParameters) super.clone();
202         up.lock = lock; // ensure that clones share the same lock object
203
return up;
204     }
205
206     /* (non-Javadoc)
207      * @see AbstractTestElement#mergeIn(TestElement)
208      */

209     protected void mergeIn(TestElement element)
210     {
211         // super.mergeIn(element);
212
}
213 }
214
Popular Tags