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, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: *
017: */
018:
019: package org.apache.jmeter.testelement.property;
020:
021: import org.apache.jmeter.engine.util.CompoundVariable;
022: import org.apache.jmeter.testelement.TestElement;
023: import org.apache.jmeter.threads.JMeterContext;
024: import org.apache.jmeter.threads.JMeterContextService;
025:
026: /**
027: * @version $Revision: 509827 $
028: */
029: public class FunctionProperty extends AbstractProperty {
030: transient CompoundVariable function;
031:
032: int testIteration = -1;
033:
034: String cacheValue;
035:
036: public FunctionProperty(String name, CompoundVariable func) {
037: super (name);
038: function = func;
039: }
040:
041: public FunctionProperty() {
042: super ();
043: }
044:
045: public void setObjectValue(Object v) {
046: if (v instanceof CompoundVariable && !isRunningVersion()) {
047: function = (CompoundVariable) v;
048: } else {
049: cacheValue = v.toString();
050: }
051: }
052:
053: public boolean equals(Object o) {
054: if (o instanceof FunctionProperty) {
055: if (function != null) {
056: return function.equals(((JMeterProperty) o)
057: .getObjectValue());
058: }
059: }
060: return false;
061: }
062:
063: /**
064: * Executes the function (and caches the value for the duration of the test
065: * iteration) if the property is a running version. Otherwise, the raw
066: * string representation of the function is provided.
067: *
068: * @see JMeterProperty#getStringValue()
069: */
070: public String getStringValue() {
071: JMeterContext ctx = JMeterContextService.getContext();// Expensive, so
072: // do
073: // once
074: if (!isRunningVersion() /*|| !ctx.isSamplingStarted()*/) {
075: log
076: .debug("Not running version, return raw function string");
077: return function.getRawParameters();
078: } else {
079: if (!ctx.isSamplingStarted())
080: return function.execute();
081: log.debug("Running version, executing function");
082: int iter = ctx.getVariables() != null ? ctx.getVariables()
083: .getIteration() : -1;
084: if (iter < testIteration) {
085: testIteration = -1;
086: }
087: if (iter > testIteration || cacheValue == null) {
088: testIteration = iter;
089: cacheValue = function.execute();
090: }
091: return cacheValue;
092: }
093: }
094:
095: /**
096: * @see JMeterProperty#getObjectValue()
097: */
098: public Object getObjectValue() {
099: return function;
100: }
101:
102: public Object clone() {
103: FunctionProperty prop = (FunctionProperty) super .clone();
104: prop.cacheValue = cacheValue;
105: prop.testIteration = testIteration;
106: prop.function = function;
107: return prop;
108: }
109:
110: /**
111: * @see JMeterProperty#recoverRunningVersion(TestElement)
112: */
113: public void recoverRunningVersion(TestElement owner) {
114: cacheValue = null;
115: }
116: }
|