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.functions;
020:
021: import java.io.Serializable;
022: import java.util.Collection;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: import org.apache.jmeter.engine.util.CompoundVariable;
027: import org.apache.jmeter.samplers.SampleResult;
028: import org.apache.jmeter.samplers.Sampler;
029: import org.apache.jmeter.threads.JMeterVariables;
030: import org.apache.jmeter.util.JMeterUtils;
031:
032: public class IterationCounter extends AbstractFunction implements
033: Serializable {
034:
035: private static final long serialVersionUID = 1L;
036:
037: private static final List desc = new LinkedList();
038:
039: private static final String KEY = "__counter"; //$NON-NLS-1$
040:
041: private transient ThreadLocal perThreadInt;
042:
043: private void init() {
044: perThreadInt = new ThreadLocal() {
045: protected synchronized Object initialValue() {
046: return new Integer(0);
047: };
048: };
049: }
050:
051: static {
052: desc.add(JMeterUtils.getResString("iteration_counter_arg_1")); //$NON-NLS-1$
053: desc.add(JMeterUtils.getResString("function_name_param")); //$NON-NLS-1$
054: }
055:
056: transient private Object[] variables;
057:
058: transient private int globalCounter;//MAXINT = 2,147,483,647
059:
060: public IterationCounter() {
061: init();
062: globalCounter = 0;
063: }
064:
065: private Object readResolve() {
066: init();
067: globalCounter = 0;
068: return this ;
069: }
070:
071: public Object clone() throws CloneNotSupportedException {
072: return super .clone();
073: }
074:
075: /*
076: * (non-Javadoc)
077: *
078: * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
079: */
080: public synchronized String execute(SampleResult previousResult,
081: Sampler currentSampler) throws InvalidVariableException {
082:
083: new Integer(1);
084: globalCounter++;
085:
086: JMeterVariables vars = getVariables();
087:
088: boolean perThread = Boolean.valueOf(
089: ((CompoundVariable) variables[0]).execute())
090: .booleanValue();
091:
092: String varName = ""; //$NON-NLS-1$
093: if (variables.length >= 2) {// Ensure variable has been provided
094: varName = ((CompoundVariable) variables[1]).execute();
095: }
096:
097: String counterString = ""; //$NON-NLS-1$
098:
099: if (perThread) {
100: int threadCounter;
101: threadCounter = ((Integer) perThreadInt.get()).intValue() + 1;
102: perThreadInt.set(new Integer(threadCounter));
103: counterString = String.valueOf(threadCounter);
104: } else {
105: counterString = String.valueOf(globalCounter);
106: }
107:
108: if (varName.length() > 0)
109: vars.put(varName, counterString);
110: return counterString;
111: }
112:
113: /*
114: * (non-Javadoc)
115: *
116: * @see org.apache.jmeter.functions.Function#setParameters(Collection)
117: */
118: public synchronized void setParameters(Collection parameters)
119: throws InvalidVariableException {
120:
121: variables = parameters.toArray();
122:
123: if (variables.length < 1) {
124: throw new InvalidVariableException(
125: "Need at least 1 parameter");
126: }
127: }
128:
129: /*
130: * (non-Javadoc)
131: *
132: * @see org.apache.jmeter.functions.Function#getReferenceKey()
133: */
134: public String getReferenceKey() {
135: return KEY;
136: }
137:
138: /*
139: * (non-Javadoc)
140: *
141: * @see org.apache.jmeter.functions.Function#getArgumentDesc()
142: */
143: public List getArgumentDesc() {
144: return desc;
145: }
146: }
|