Search in sources :

Example 46 with MapContext

use of org.apache.commons.jexl3.MapContext 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)

Example 47 with MapContext

use of org.apache.commons.jexl3.MapContext in project nutch by apache.

the class JexlIndexingFilter method metadataToContext.

private JexlContext metadataToContext(Metadata metadata) {
    JexlContext context = new MapContext();
    for (String name : metadata.names()) {
        String[] values = metadata.getValues(name);
        context.set(name, values.length > 1 ? values : values[0]);
    }
    return context;
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext)

Example 48 with MapContext

use of org.apache.commons.jexl3.MapContext in project cxf by apache.

the class JexlClaimsMapper method mapClaims.

public ProcessedClaimCollection mapClaims(String sourceRealm, ProcessedClaimCollection sourceClaims, String targetRealm, ClaimsParameters parameters) {
    JexlContext context = new MapContext();
    context.set("sourceClaims", sourceClaims);
    context.set("targetClaims", new ProcessedClaimCollection());
    context.set("sourceRealm", sourceRealm);
    context.set("targetRealm", targetRealm);
    context.set("claimsParameters", parameters);
    JexlScript s = getScript();
    if (s == null) {
        LOG.warning("No claim mapping script defined");
        // TODO Check if null or an exception would be more
        return new ProcessedClaimCollection();
    // appropriate
    }
    return (ProcessedClaimCollection) s.execute(context);
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) JexlContext(org.apache.commons.jexl3.JexlContext) JexlScript(org.apache.commons.jexl3.JexlScript) MapContext(org.apache.commons.jexl3.MapContext)

Example 49 with MapContext

use of org.apache.commons.jexl3.MapContext in project waltz by khartec.

the class PredicateEvaluatorTest method createContext.

private MapContext createContext(Set<ContextVariable<?>> contextVariables) {
    MapContext ctx = new MapContext();
    contextVariables.forEach(ctxVar -> ctx.set(ctxVar.name(), ctxVar.value()));
    return ctx;
}
Also used : MapContext(org.apache.commons.jexl3.MapContext)

Example 50 with MapContext

use of org.apache.commons.jexl3.MapContext in project shifu by ShifuML.

the class JexlTest method testJavaNull.

@Test
public void testJavaNull() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "is_bad_new != null";
    String jexlExpEqual = "is_bad_new == null";
    Expression e = jexl.createExpression(jexlExp);
    Expression exp = jexl.createExpression(jexlExpEqual);
    JexlContext jc = new MapContext();
    jc.set("is_bad_new", null);
    Assert.assertEquals(Boolean.FALSE, e.evaluate(jc));
    Assert.assertEquals(Boolean.TRUE, exp.evaluate(jc));
    jc.set("is_bad_new", new Object());
    Assert.assertEquals(Boolean.TRUE, e.evaluate(jc));
    Assert.assertEquals(Boolean.FALSE, exp.evaluate(jc));
}
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) Test(org.testng.annotations.Test)

Aggregations

MapContext (org.apache.commons.jexl2.MapContext)32 MapContext (org.apache.commons.jexl3.MapContext)26 JexlContext (org.apache.commons.jexl2.JexlContext)23 Expression (org.apache.commons.jexl2.Expression)20 JexlEngine (org.apache.commons.jexl2.JexlEngine)20 JexlContext (org.apache.commons.jexl3.JexlContext)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)7 Map (java.util.Map)7 HashSet (java.util.HashSet)4 JexlException (org.apache.commons.jexl2.JexlException)4 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)4 JexlExpression (org.apache.commons.jexl3.JexlExpression)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 JexlEngine (org.apache.commons.jexl3.JexlEngine)3 Test (org.junit.jupiter.api.Test)3 Device (org.traccar.model.Device)3 IOException (java.io.IOException)2