Search in sources :

Example 11 with MapContext

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

the class Jexl3Function 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
        JexlExpression e = getJexlEngine().createExpression(exp);
        Object o = e.evaluate(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) JMeterContext(org.apache.jmeter.threads.JMeterContext) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 12 with MapContext

use of org.apache.commons.jexl2.MapContext in project dhis2-core by dhis2.

the class ExpressionUtils method evaluate.

/**
     * @param expression the expression.
     * @param vars the variables, can be null.
     * @param strict indicates whether to use strict or lenient engine mode.
     * @return the result of the evaluation.
     */
private static Object evaluate(String expression, Map<String, Object> vars, boolean strict) {
    expression = expression.replaceAll(IGNORED_KEYWORDS_REGEX, StringUtils.EMPTY);
    JexlEngine engine = strict ? JEXL_STRICT : JEXL;
    Expression exp = engine.createExpression(expression);
    JexlContext context = vars != null ? new MapContext(vars) : new MapContext();
    return exp.evaluate(context);
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Aggregations

MapContext (org.apache.commons.jexl2.MapContext)11 JexlContext (org.apache.commons.jexl2.JexlContext)8 Expression (org.apache.commons.jexl2.Expression)6 JexlEngine (org.apache.commons.jexl2.JexlEngine)5 HashMap (java.util.HashMap)3 Map (java.util.Map)2 JexlException (org.apache.commons.jexl2.JexlException)2 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)2 JMeterContext (org.apache.jmeter.threads.JMeterContext)2 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)2 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 ColumnConfig (ml.shifu.shifu.container.obj.ColumnConfig)1 ReadonlyContext (org.apache.commons.jexl2.ReadonlyContext)1 JexlContext (org.apache.commons.jexl3.JexlContext)1 JexlExpression (org.apache.commons.jexl3.JexlExpression)1 MapContext (org.apache.commons.jexl3.MapContext)1 ParameterMap (org.opennms.core.utils.ParameterMap)1 JmxConnectionManager (org.opennms.netmgt.jmx.connection.JmxConnectionManager)1