Search in sources :

Example 1 with Script

use of org.apache.commons.jexl2.Script in project opennms by OpenNMS.

the class ExpressionConfigWrapper method evaluate.

@Override
public double evaluate(Map<String, Double> values) throws ThresholdExpressionException {
    // Add all of the variable values to the script context
    Map<String, Object> context = new HashMap<String, Object>();
    context.putAll(values);
    // To workaround NMS-5019
    context.put("datasources", new HashMap<String, Double>(values));
    context.put("math", new MathBinding());
    double result = Double.NaN;
    try {
        // Fetch an instance of the JEXL script engine to evaluate the script expression
        Object resultObject = new JexlEngine().createExpression(m_expression.getExpression()).evaluate(new MapContext(context));
        result = Double.parseDouble(resultObject.toString());
    } catch (Throwable e) {
        throw new ThresholdExpressionException("Error while evaluating expression " + m_expression.getExpression() + ": " + e.getMessage(), e);
    }
    return result;
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) HashMap(java.util.HashMap) MapContext(org.apache.commons.jexl2.MapContext)

Example 2 with Script

use of org.apache.commons.jexl2.Script in project jmeter by apache.

the class Jexl2Function method execute.

/**
 * {@inheritDoc}
 */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    // $NON-NLS-1$
    String str = "";
    CompoundVariable var = (CompoundVariable) values[0];
    String exp = var.execute();
    // $NON-NLS-1$
    String varName = "";
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    try {
        JexlContext jc = new MapContext();
        // $NON-NLS-1$
        jc.set("log", log);
        // $NON-NLS-1$
        jc.set("ctx", jmctx);
        // $NON-NLS-1$
        jc.set("vars", vars);
        // $NON-NLS-1$
        jc.set("props", JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        // $NON-NLS-1$
        jc.set("threadName", Thread.currentThread().getName());
        // $NON-NLS-1$ (may be null)
        jc.set("sampler", currentSampler);
        // $NON-NLS-1$ (may be null)
        jc.set("sampleResult", previousResult);
        // $NON-NLS-1$
        jc.set("OUT", System.out);
        // Now evaluate the script, getting the result
        Script e = getJexlEngine().createScript(exp);
        Object o = e.execute(jc);
        if (o != null) {
            str = o.toString();
        }
        if (vars != null && varName.length() > 0) {
            // vars will be null on TestPlan
            vars.put(varName, str);
        }
    } catch (Exception e) {
        log.error("An error occurred while evaluating the expression \"" + exp + "\"\n", e);
    }
    return str;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) Script(org.apache.commons.jexl2.Script) JMeterContext(org.apache.jmeter.threads.JMeterContext) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Aggregations

MapContext (org.apache.commons.jexl2.MapContext)2 HashMap (java.util.HashMap)1 JexlContext (org.apache.commons.jexl2.JexlContext)1 JexlEngine (org.apache.commons.jexl2.JexlEngine)1 Script (org.apache.commons.jexl2.Script)1 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)1 JMeterContext (org.apache.jmeter.threads.JMeterContext)1 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)1