01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
22: import java.util.Collection;
23: import java.util.LinkedList;
24: import java.util.List;
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.util.JMeterUtils;
30:
31: /**
32: * Function to get a JMeter Variable
33: *
34: * Parameters:
35: * - variable name
36: *
37: * Returns:
38: * - the variable value, but if not found
39: * - the variable name itself
40: *
41: */
42: public class Variable extends AbstractFunction implements Serializable {
43:
44: private static final List desc = new LinkedList();
45:
46: private static final String KEY = "__V"; //$NON-NLS-1$
47:
48: // Number of parameters expected - used to reject invalid calls
49: private static final int MIN_PARAMETER_COUNT = 1;
50: private static final int MAX_PARAMETER_COUNT = 1;
51:
52: static {
53: desc.add(JMeterUtils.getResString("variable_name_param")); //$NON-NLS-1$
54: }
55:
56: private Object[] values;
57:
58: public Variable() {
59: }
60:
61: public Object clone() throws CloneNotSupportedException {
62: return super .clone();
63: }
64:
65: public synchronized String execute(SampleResult previousResult,
66: Sampler currentSampler) throws InvalidVariableException {
67: String variableName = ((CompoundVariable) values[0]).execute();
68: String variableValue = getVariables().get(variableName);
69: return variableValue == null ? variableName : variableValue;
70:
71: }
72:
73: public synchronized void setParameters(Collection parameters)
74: throws InvalidVariableException {
75:
76: values = parameters.toArray();
77:
78: if ((values.length < MIN_PARAMETER_COUNT)
79: || (values.length > MAX_PARAMETER_COUNT)) {
80: throw new InvalidVariableException(
81: "Parameter Count not between "
82: + MIN_PARAMETER_COUNT + " & "
83: + MAX_PARAMETER_COUNT);
84: }
85:
86: }
87:
88: public String getReferenceKey() {
89: return KEY;
90: }
91:
92: public List getArgumentDesc() {
93: return desc;
94: }
95:
96: }
|