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.util.JMeterUtils;
030:
031: /**
032: * Function to get a JMeter property, and optionally store it
033: *
034: * Parameters:
035: * - property name
036: * - variable name (optional)
037: * - default value (optional)
038: *
039: * Returns:
040: * - the property value, but if not found:
041: * - the default value, but if not defined:
042: * - the property name itself
043: *
044: */
045: public class Property extends AbstractFunction implements Serializable {
046:
047: private static final List desc = new LinkedList();
048:
049: private static final String KEY = "__property"; //$NON-NLS-1$
050:
051: // Number of parameters expected - used to reject invalid calls
052: private static final int MIN_PARAMETER_COUNT = 1;
053: private static final int MAX_PARAMETER_COUNT = 3;
054:
055: static {
056: desc.add(JMeterUtils.getResString("property_name_param")); //$NON-NLS-1$
057: desc.add(JMeterUtils.getResString("function_name_param")); //$NON-NLS-1$
058: desc.add(JMeterUtils.getResString("property_default_param")); //$NON-NLS-1$
059: }
060:
061: private Object[] values;
062:
063: public Property() {
064: }
065:
066: public Object clone() throws CloneNotSupportedException {
067: return super .clone();
068: }
069:
070: public synchronized String execute(SampleResult previousResult,
071: Sampler currentSampler) throws InvalidVariableException {
072: String propertyName = ((CompoundVariable) values[0]).execute();
073: String propertyDefault = propertyName;
074: if (values.length > 2) { // We have a 3rd parameter
075: propertyDefault = ((CompoundVariable) values[2]).execute();
076: }
077: String propertyValue = JMeterUtils.getPropDefault(propertyName,
078: propertyDefault);
079: if (values.length > 1) {
080: String variableName = ((CompoundVariable) values[1])
081: .execute();
082: if (variableName.length() > 0) {// Allow for empty name
083: getVariables().put(variableName, propertyValue);
084: }
085: }
086: return propertyValue;
087:
088: }
089:
090: public synchronized void setParameters(Collection parameters)
091: throws InvalidVariableException {
092:
093: values = parameters.toArray();
094:
095: if ((values.length < MIN_PARAMETER_COUNT)
096: || (values.length > MAX_PARAMETER_COUNT)) {
097: throw new InvalidVariableException(
098: "Parameter Count not between "
099: + MIN_PARAMETER_COUNT + " & "
100: + MAX_PARAMETER_COUNT);
101: }
102:
103: }
104:
105: public String getReferenceKey() {
106: return KEY;
107: }
108:
109: public List getArgumentDesc() {
110: return desc;
111: }
112:
113: }
|