KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > IterationCounter


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/IterationCounter.java,v 1.13.2.1 2004/10/10 23:59:56 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.functions;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.jmeter.engine.util.CompoundVariable;
27 import org.apache.jmeter.samplers.SampleResult;
28 import org.apache.jmeter.samplers.Sampler;
29 import org.apache.jmeter.threads.JMeterVariables;
30 import org.apache.jmeter.util.JMeterUtils;
31
32 public class IterationCounter extends AbstractFunction implements Serializable JavaDoc
33 {
34
35     private static final List JavaDoc desc = new LinkedList JavaDoc();
36     private static final String JavaDoc KEY = "__counter";
37
38     static {
39         desc.add(JMeterUtils.getResString("iteration_counter_arg_1"));
40         desc.add(JMeterUtils.getResString("function_name_param"));
41     }
42
43     transient private Object JavaDoc[] variables;
44     transient private int[] counter;
45     transient private String JavaDoc key; // Used to keep track of counter
46

47     public IterationCounter()
48     {
49         counter = new int[1];
50         // TODO use better key if poss. Can't use varName - it may not be present
51
key=KEY+System.identityHashCode(this);
52     }
53
54     public Object JavaDoc clone()
55     {
56         IterationCounter newCounter = new IterationCounter();
57         newCounter.counter = counter;
58         return newCounter;
59     }
60
61     /* (non-Javadoc)
62      * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
63      */

64     public synchronized String JavaDoc execute(
65         SampleResult previousResult,
66         Sampler currentSampler)
67         throws InvalidVariableException
68     {
69         counter[0]++;
70
71         JMeterVariables vars = getVariables();
72
73         boolean perThread =
74             Boolean.valueOf(((CompoundVariable) variables[0]).execute()).booleanValue();
75
76         String JavaDoc varName =
77             ((CompoundVariable) variables[variables.length - 1]).execute();
78         String JavaDoc counterString = "";
79
80         if (perThread)
81         {
82             counterString = vars.get(key);
83             if (null==counterString){
84                 counterString= "1";
85             }
86             else
87             {
88                 counterString = Integer.toString(Integer.parseInt(counterString)+1);
89             }
90             vars.put(key,counterString);
91             
92         }
93         else
94         {
95             counterString = String.valueOf(counter[0]);
96         }
97
98         if (varName.length()>0) vars.put(varName, counterString);
99         return counterString;
100     }
101
102     /* (non-Javadoc)
103      * @see org.apache.jmeter.functions.Function#setParameters(Collection)
104      */

105     public void setParameters(Collection JavaDoc parameters)
106         throws InvalidVariableException
107     {
108
109         variables = parameters.toArray();
110
111         if (variables.length < 2)
112         {
113             throw new InvalidVariableException("Fewer than 2 parameters");
114         }
115     }
116
117     /* (non-Javadoc)
118      * @see org.apache.jmeter.functions.Function#getReferenceKey()
119      */

120     public String JavaDoc getReferenceKey()
121     {
122         return KEY;
123     }
124
125     /* (non-Javadoc)
126      * @see org.apache.jmeter.functions.Function#getArgumentDesc()
127      */

128     public List JavaDoc getArgumentDesc()
129     {
130         return desc;
131     }
132 }
133
Popular Tags