Example usage for org.apache.commons.jexl3 JexlContext set

List of usage examples for org.apache.commons.jexl3 JexlContext set

Introduction

In this page you can find the example usage for org.apache.commons.jexl3 JexlContext set.

Prototype

void set(String name, Object value);

Source Link

Document

Sets the value of a variable.

Usage

From source file:com.streamsets.datacollector.antennadoctor.engine.AntennaDoctorEngine.java

public List<AntennaDoctorMessage> onRest(AntennaDoctorContext context, ErrorCode errorCode, Object... args) {
    JexlContext jexlContext = new MapContext();
    jexlContext.set("issue", new StageIssueJexl(errorCode, args));
    return evaluate(context, AntennaDoctorRuleBean.Entity.REST, jexlContext);
}

From source file:com.streamsets.datacollector.antennadoctor.engine.AntennaDoctorEngine.java

public List<AntennaDoctorMessage> onRest(AntennaDoctorContext context, Exception exception) {
    JexlContext jexlContext = new MapContext();
    jexlContext.set("issue", new StageIssueJexl(exception));
    return evaluate(context, AntennaDoctorRuleBean.Entity.REST, jexlContext);
}

From source file:com.streamsets.datacollector.antennadoctor.engine.AntennaDoctorEngine.java

public List<AntennaDoctorMessage> onStage(AntennaDoctorStageContext context, Exception exception) {
    JexlContext jexlContext = new MapContext();
    jexlContext.set("issue", new StageIssueJexl(exception));
    jexlContext.set("stageDef", context.getStageDefinition());
    jexlContext.set("stageConf", context.getStageConfiguration());
    return evaluate(context, AntennaDoctorRuleBean.Entity.STAGE, jexlContext);
}

From source file:com.streamsets.datacollector.antennadoctor.engine.AntennaDoctorEngine.java

public List<AntennaDoctorMessage> onStage(AntennaDoctorStageContext context, String errorMessage) {
    JexlContext jexlContext = new MapContext();
    jexlContext.set("issue", new StageIssueJexl(errorMessage));
    jexlContext.set("stageDef", context.getStageDefinition());
    jexlContext.set("stageConf", context.getStageConfiguration());
    return evaluate(context, AntennaDoctorRuleBean.Entity.STAGE, jexlContext);
}

From source file:com.streamsets.datacollector.antennadoctor.engine.AntennaDoctorEngine.java

public List<AntennaDoctorMessage> onStage(AntennaDoctorStageContext context, ErrorCode errorCode,
        Object... args) {/*from   w w  w. j a  v  a2  s  .co  m*/
    JexlContext jexlContext = new MapContext();
    jexlContext.set("issue", new StageIssueJexl(errorCode, args));
    jexlContext.set("stageDef", context.getStageDefinition());
    jexlContext.set("stageConf", context.getStageConfiguration());
    return evaluate(context, AntennaDoctorRuleBean.Entity.STAGE, jexlContext);
}

From source file:com.streamsets.datacollector.antennadoctor.engine.AntennaDoctorEngine.java

public AntennaDoctorEngine(AntennaDoctorContext context, List<AntennaDoctorRuleBean> rules) {
    ImmutableList.Builder<RuntimeRule> builder = ImmutableList.builder();

    Map<String, Object> namespaces = new HashMap<>();
    namespaces.put("collection", CollectionEL.class);
    namespaces.put("file", FileEL.class);

    // Main engine used to evaluate all expressions and templates
    engine = new JexlBuilder().cache(512).strict(true).silent(false).debug(true).namespaces(namespaces)
            .create();//ww  w .ja v  a2s  .  c o  m
    templateEngine = engine.createJxltEngine();

    // Context for preconditions
    JexlContext jexlContext = new MapContext();
    jexlContext.set("version", new Version(context.getBuildInfo().getVersion()));
    jexlContext.set("sdc", new SdcJexl(context));

    // Evaluate each rule to see if it's applicable to this runtime (version, stage libs, ...)
    for (AntennaDoctorRuleBean ruleBean : rules) {
        LOG.trace("Loading rule {}", ruleBean.getUuid());

        // We're running in SDC and currently only in STAGE 'mode', other modes will be added later
        if (ruleBean.getEntity() == null || !ruleBean.getEntity().isOneOf(AntennaDoctorRuleBean.Entity.STAGE,
                AntennaDoctorRuleBean.Entity.REST)) {
            continue;
        }

        // Reset the variables that the rule is keeping
        jexlContext.set("context", new HashMap<>());

        for (String precondition : ruleBean.getPreconditions()) {
            try {
                LOG.trace("Evaluating precondition: {}", precondition);
                if (!evaluateCondition(precondition, jexlContext)) {
                    LOG.trace("Precondition {} failed, skipping rule {}", precondition, ruleBean.getUuid());
                    continue;
                }
            } catch (Throwable e) {
                LOG.error("Precondition {} failed, skipping rule {}: {}", precondition, ruleBean.getUuid(),
                        e.toString(), e);
                continue;
            }
        }

        // All checks passed, so we will accept this rule
        builder.add(new RuntimeRule(ruleBean));
    }

    this.rules = builder.build();
    LOG.info("Loaded new Antenna Doctor engine with {} rules", this.rules.size());
}

From source file:datainstiller.data.DataPersistence.java

private void initJexlContext() {
    if (jexlContext != null) {
        System.out.println("WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    }/*  www  .  ja v  a  2s . co m*/
    JexlContext jContext = new MapContext();
    jContext.set("AddressGen", new AddressGenerator());
    jContext.set("AlphaNumericGen", new AlphaNumericGenerator());
    jContext.set("ListGen", new CustomListGenerator());
    jContext.set("DateGen", new DateGenerator());
    jContext.set("HumanNameGen", new HumanNameGenerator());
    jContext.set("NumberGen", new NumberGenerator());
    jContext.set("WordGen", new WordGenerator());
    jContext.set("File2ListGen", new File2ListGenerator());
    LocalDateTime now = LocalDateTime.now();
    jContext.set("now", now);
    jContext.set("DateTimeFormatter", DateTimeFormatter.BASIC_ISO_DATE);
    initJexlContext(jContext);
    jexlContext = jContext;
}

From source file:com.streamsets.datacollector.antennadoctor.engine.AntennaDoctorEngine.java

private List<AntennaDoctorMessage> evaluate(AntennaDoctorContext context, AntennaDoctorRuleBean.Entity entity,
        JexlContext jexlContext) {
    // All our expressions have the sdc object available
    jexlContext.set("sdc", new SdcJexl(context));

    ImmutableList.Builder<AntennaDoctorMessage> builder = ImmutableList.builder();

    // Iterate over rules and try to match them
    for (RuntimeRule rule : this.rules) {
        // Static check to execute only relevant rules
        if (rule.getEntity() != entity) {
            continue;
        }/*  w w w.  j  av  a  2s  . c om*/

        // Reset the variables that the rule is keeping
        jexlContext.set("context", new HashMap<>());

        // Firstly evaluate conditions
        boolean matched = true;
        for (String condition : rule.getConditions()) {
            LOG.trace("Evaluating rule {} condition {}", rule.getUuid(), condition);
            try {
                if (!evaluateCondition(condition, jexlContext)) {
                    matched = false;
                    break;
                }
            } catch (JexlException e) {
                matched = false;
                LOG.error("Failed to evaluate rule {} condition {}: {}", rule.getUuid(), condition,
                        e.toString(), e);
                break;
            }
        }

        // If all rules succeeded, evaluate message
        if (matched) {
            LOG.trace("Rule {} matched!", rule.getUuid());
            try {
                StringWriter summaryWriter = new StringWriter();
                StringWriter descriptionWriter = new StringWriter();

                LOG.trace("Evaluating summary for rule {}: {}", rule.getUuid(), rule.getSummary());
                templateEngine.createTemplate(rule.getSummary()).evaluate(jexlContext, summaryWriter);

                LOG.trace("Evaluating description for rule {}: {}", rule.getUuid(), rule.getDescription());
                templateEngine.createTemplate(rule.getDescription()).evaluate(jexlContext, descriptionWriter);

                builder.add(new AntennaDoctorMessage(summaryWriter.toString(), descriptionWriter.toString()));
            } catch (JexlException e) {
                LOG.error("Failed to evaluate message for rule {}: {}", rule.getUuid(), e.toString(), e);
            }
        } else {
            LOG.trace("Rule {} did not match", rule.getUuid());
        }
    }

    return builder.build();
}

From source file:org.apache.jmeter.functions.Jexl3Function.java

/** {@inheritDoc} */
@Override//from   ww  w.  ja v a 2s  .  c o  m
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String str = ""; //$NON-NLS-1$

    CompoundVariable var = (CompoundVariable) values[0];
    String exp = var.execute();

    String varName = ""; //$NON-NLS-1$
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }

    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();

    try {
        JexlContext jc = new MapContext();
        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()); //$NON-NLS-1$
        // Previously mis-spelt as theadName
        jc.set("threadName", Thread.currentThread().getName()); //$NON-NLS-1$
        jc.set("sampler", currentSampler); //$NON-NLS-1$ (may be null)
        jc.set("sampleResult", previousResult); //$NON-NLS-1$ (may be null)
        jc.set("OUT", System.out);//$NON-NLS-1$

        // 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;
}

From source file:org.apache.syncope.core.provisioning.java.data.JEXLItemTransformerImpl.java

@Override
public List<PlainAttrValue> beforePropagation(final Item item, final Entity entity,
        final List<PlainAttrValue> values) {

    if (StringUtils.isNotBlank(propagationJEXL) && values != null) {
        values.forEach(value -> {//from w w w  .ja  v a2 s .c o  m
            JexlContext jexlContext = new MapContext();
            if (entity != null) {
                JexlUtils.addFieldsToContext(entity, jexlContext);
                if (entity instanceof Any) {
                    JexlUtils.addPlainAttrsToContext(((Any<?>) entity).getPlainAttrs(), jexlContext);
                    JexlUtils.addDerAttrsToContext(((Any<?>) entity), jexlContext);
                }
            }
            jexlContext.set("value", value.getValueAsString());

            value.setStringValue(JexlUtils.evaluate(propagationJEXL, jexlContext));
        });

        return values;
    }

    return values;
}