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 set a JMeter property
033: *
034: * Parameters: - property name - value
035: *
036: * Usage:
037: *
038: * Set the property value in the appropriate GUI by using the string:
039: * ${__setProperty(propname,propvalue[,returnvalue?])}
040: *
041: * Returns: nothing or original value if the 3rd parameter is true
042: *
043: */
044: public class SetProperty extends AbstractFunction implements
045: Serializable {
046:
047: private static final List desc = new LinkedList();
048:
049: private static final String KEY = "__setProperty"; //$NON-NLS-1$
050:
051: // Number of parameters expected - used to reject invalid calls
052: private static final int MIN_PARAMETER_COUNT = 2;
053:
054: private static final int MAX_PARAMETER_COUNT = 3;
055: static {
056: desc.add(JMeterUtils.getResString("property_name_param")); //$NON-NLS-1$
057: desc.add(JMeterUtils.getResString("property_value_param")); //$NON-NLS-1$
058: desc
059: .add(JMeterUtils
060: .getResString("property_returnvalue_param")); //$NON-NLS-1$
061: }
062:
063: private Object[] values;
064:
065: public SetProperty() {
066: }
067:
068: public Object clone() throws CloneNotSupportedException {
069: return super .clone();
070: }
071:
072: public synchronized String execute(SampleResult previousResult,
073: Sampler currentSampler) throws InvalidVariableException {
074: String propertyName = ((CompoundVariable) values[0]).execute();
075:
076: String propertyValue = ((CompoundVariable) values[1]).execute();
077:
078: boolean returnValue = false;// should we return original value?
079: if (values.length > 2) {
080: returnValue = ((CompoundVariable) values[2]).execute()
081: .equalsIgnoreCase("true"); //$NON-NLS-1$
082: }
083:
084: if (returnValue) { // Only obtain and cast the return if needed
085: return (String) JMeterUtils.setProperty(propertyName,
086: propertyValue);
087: } else {
088: JMeterUtils.setProperty(propertyName, propertyValue);
089: return "";
090: }
091: }
092:
093: public synchronized void setParameters(Collection parameters)
094: throws InvalidVariableException {
095:
096: values = parameters.toArray();
097:
098: if ((values.length < MIN_PARAMETER_COUNT)
099: || (values.length > MAX_PARAMETER_COUNT)) {
100: throw new InvalidVariableException(
101: "Parameter Count not between "
102: + MIN_PARAMETER_COUNT + " & "
103: + MAX_PARAMETER_COUNT);
104: }
105:
106: }
107:
108: public String getReferenceKey() {
109: return KEY;
110: }
111:
112: public List getArgumentDesc() {
113: return desc;
114: }
115:
116: }
|