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, or a default. Does not offer the option to
033: * store the value, as it is just as easy to refetch it. This is a
034: * specialisation of the __property() function to make it simpler to use for
035: * ThreadGroup GUI etc. The name is also shorter.
036: *
037: * Parameters: - property name - default value (optional; defaults to "1")
038: *
039: * Usage:
040: *
041: * Define the property in jmeter.properties, or on the command-line: java ...
042: * -Jpropname=value
043: *
044: * Retrieve the value in the appropriate GUI by using the string:
045: * ${__P(propname)} $(__P(propname,default)}
046: *
047: * Returns: - the property value, but if not found - the default value, but if
048: * not present - "1" (suitable for use in ThreadGroup GUI)
049: *
050: */
051: public class Property2 extends AbstractFunction implements Serializable {
052:
053: private static final List desc = new LinkedList();
054:
055: private static final String KEY = "__P"; //$NON-NLS-1$
056:
057: // Number of parameters expected - used to reject invalid calls
058: private static final int MIN_PARAMETER_COUNT = 1;
059:
060: private static final int MAX_PARAMETER_COUNT = 2;
061: static {
062: desc.add(JMeterUtils.getResString("property_name_param")); //$NON-NLS-1$
063: desc.add(JMeterUtils.getResString("property_default_param")); //$NON-NLS-1$
064: }
065:
066: private Object[] values;
067:
068: public Property2() {
069: }
070:
071: public Object clone() throws CloneNotSupportedException {
072: return super .clone();
073: }
074:
075: public synchronized String execute(SampleResult previousResult,
076: Sampler currentSampler) throws InvalidVariableException {
077: String propertyName = ((CompoundVariable) values[0]).execute();
078:
079: String propertyDefault = "1"; //$NON-NLS-1$
080: if (values.length > 1) { // We have a default
081: propertyDefault = ((CompoundVariable) values[1]).execute();
082: }
083:
084: String propertyValue = JMeterUtils.getPropDefault(propertyName,
085: propertyDefault);
086:
087: return propertyValue;
088:
089: }
090:
091: public synchronized void setParameters(Collection parameters)
092: throws InvalidVariableException {
093:
094: values = parameters.toArray();
095:
096: if ((values.length < MIN_PARAMETER_COUNT)
097: || (values.length > MAX_PARAMETER_COUNT)) {
098: throw new InvalidVariableException(
099: "Parameter Count not between "
100: + MIN_PARAMETER_COUNT + " & "
101: + MAX_PARAMETER_COUNT);
102: }
103:
104: }
105:
106: public String getReferenceKey() {
107: return KEY;
108: }
109:
110: public List getArgumentDesc() {
111: return desc;
112: }
113:
114: }
|