KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/modifiers/CounterConfig.java,v 1.21.2.1 2004/10/24 01:08:47 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
23 import org.apache.jmeter.engine.event.LoopIterationEvent;
24 import org.apache.jmeter.engine.event.LoopIterationListener;
25 import org.apache.jmeter.engine.util.NoThreadClone;
26 import org.apache.jmeter.testelement.AbstractTestElement;
27 import org.apache.jmeter.testelement.property.BooleanProperty;
28 import org.apache.jmeter.testelement.property.IntegerProperty;
29 import org.apache.jmeter.threads.JMeterContextService;
30 import org.apache.jmeter.threads.JMeterVariables;
31 import org.apache.jorphan.logging.LoggingManager;
32 import org.apache.log.Logger;
33
34 /**
35  * @version $Revision: 1.21.2.1 $
36  */

37 public class CounterConfig
38     extends AbstractTestElement
39     implements Serializable JavaDoc, LoopIterationListener, NoThreadClone
40 {
41     private static Logger log = LoggingManager.getLoggerForClass();
42     public final static String JavaDoc START = "CounterConfig.start";
43     public final static String JavaDoc END = "CounterConfig.end";
44     public final static String JavaDoc INCREMENT = "CounterConfig.incr";
45     public final static String JavaDoc PER_USER = "CounterConfig.per_user";
46     public final static String JavaDoc VAR_NAME = "CounterConfig.name";
47
48     private int globalCounter = -1;
49     
50     /**
51      * @see LoopIterationListener#iterationStart(LoopIterationEvent)
52      */

53     public synchronized void iterationStart(LoopIterationEvent event)
54     {
55         // Cannot use getThreadContext() as not cloned per thread
56
JMeterVariables variables =
57             JMeterContextService.getContext().getVariables();
58         int start = getStart(), end = getEnd(), increment = getIncrement();
59         if (!isPerUser())
60         {
61             if (globalCounter == -1 || globalCounter > end)
62             {
63                 globalCounter = start;
64             }
65             variables.put(getVarName(), Integer.toString(globalCounter));
66             globalCounter += increment;
67         }
68         else
69         {
70             String JavaDoc value = variables.get(getVarName());
71             if (value == null)
72             {
73                 variables.put(getVarName(), Integer.toString(start));
74             }
75             else
76             {
77                 try
78                 {
79                     int current = Integer.parseInt(value);
80                     current += increment;
81                     if (current > end)
82                     {
83                         current = start;
84                     }
85                     variables.put(getVarName(), Integer.toString(current));
86                 }
87                 catch (NumberFormatException JavaDoc e)
88                 {
89                     log.info("Bad number in Counter config", e);
90                 }
91             }
92         }
93     }
94
95     public void setStart(int start)
96     {
97         setProperty(new IntegerProperty(START, start));
98     }
99
100     public void setStart(String JavaDoc start)
101     {
102         setProperty(START, start);
103     }
104
105     public int getStart()
106     {
107         return getPropertyAsInt(START);
108     }
109
110     public void setEnd(int end)
111     {
112         setProperty(new IntegerProperty(END, end));
113     }
114
115     public void setEnd(String JavaDoc end)
116     {
117         setProperty(END, end);
118     }
119
120     public int getEnd()
121     {
122         return getPropertyAsInt(END);
123     }
124
125     public void setIncrement(int inc)
126     {
127         setProperty(new IntegerProperty(INCREMENT, inc));
128     }
129
130     public void setIncrement(String JavaDoc incr)
131     {
132         setProperty(INCREMENT, incr);
133     }
134
135     public int getIncrement()
136     {
137         return getPropertyAsInt(INCREMENT);
138     }
139
140     public void setIsPerUser(boolean isPer)
141     {
142         setProperty(new BooleanProperty(PER_USER, isPer));
143     }
144
145     public boolean isPerUser()
146     {
147         return getPropertyAsBoolean(PER_USER);
148     }
149
150     public void setVarName(String JavaDoc name)
151     {
152         setProperty(VAR_NAME, name);
153     }
154
155     public String JavaDoc getVarName()
156     {
157         return getPropertyAsString(VAR_NAME);
158     }
159 }
160
Popular Tags