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: package org.apache.jmeter.functions;
018:
019: import java.io.Serializable;
020: import java.util.Collection;
021: import java.util.LinkedList;
022: import java.util.List;
023:
024: import org.apache.commons.jexl.Expression;
025: import org.apache.commons.jexl.ExpressionFactory;
026: import org.apache.commons.jexl.JexlContext;
027: import org.apache.commons.jexl.JexlHelper;
028: import org.apache.jmeter.engine.util.CompoundVariable;
029: import org.apache.jmeter.samplers.SampleResult;
030: import org.apache.jmeter.samplers.Sampler;
031: import org.apache.jmeter.util.JMeterUtils;
032: import org.apache.jorphan.logging.LoggingManager;
033: import org.apache.log.Logger;
034:
035: /**
036: * A function which understands Commons JEXL
037: */
038: public class JexlFunction extends AbstractFunction implements
039: Serializable {
040: /**
041: * <code>serialVersionUID</code>
042: */
043: private static final long serialVersionUID = 3546359539474968625L;
044:
045: private static Logger log = LoggingManager.getLoggerForClass();
046:
047: private static final String KEY = "__jexl"; //$NON-NLS-1$
048:
049: private static final List desc = new LinkedList();
050:
051: static {
052: desc.add(JMeterUtils.getResString("jexl_expression")); //$NON-NLS-1$
053: }
054:
055: private Object[] values;
056:
057: public synchronized String execute(SampleResult result,
058: Sampler sampler) throws InvalidVariableException {
059: String str = ""; //$NON-NLS-1$
060:
061: CompoundVariable var = (CompoundVariable) values[0];
062: String exp = var.execute();
063:
064: try {
065: Expression e = ExpressionFactory.createExpression(exp);
066: JexlContext jc = JexlHelper.createContext();
067: jc.getVars().put("ctx", sampler.getThreadContext()); //$NON-NLS-1$
068: jc.getVars().put("vars", getVariables()); //$NON-NLS-1$
069: jc.getVars().put("theadName", sampler.getThreadName()); //$NON-NLS-1$
070: jc.getVars().put("sampler", sampler); //$NON-NLS-1$
071: jc.getVars().put("sampleResult", result); //$NON-NLS-1$
072:
073: // Now evaluate the expression, getting the result
074: Object o = e.evaluate(jc);
075: if (o != null) {
076: str = o.toString();
077: }
078: } catch (Exception e) {
079: log
080: .error("An error occurred while evaluating the expression \""
081: + exp + "\"");
082: }
083: return str;
084: }
085:
086: public List getArgumentDesc() {
087: return desc;
088: }
089:
090: public String getReferenceKey() {
091: return KEY;
092: }
093:
094: public synchronized void setParameters(Collection parameters)
095: throws InvalidVariableException {
096: values = parameters.toArray();
097: if (values.length != 1) {
098: throw new InvalidVariableException(
099: "it only accepts one parameter");
100: }
101: }
102:
103: }
|