Search in sources :

Example 1 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-boot-admin by codecentric.

the class MailNotifier method doNotify.

@Override
protected void doNotify(ClientApplicationEvent event) {
    EvaluationContext context = new StandardEvaluationContext(event);
    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(to);
    message.setFrom(from);
    message.setSubject(subject.getValue(context, String.class));
    message.setText(text.getValue(context, String.class));
    message.setCc(cc);
    sender.send(message);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationContext(org.springframework.expression.EvaluationContext)

Example 2 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project opennms by OpenNMS.

the class ResponseHandlingUtils method matchesFilter.

public static boolean matchesFilter(String spelFilter, ListMultimap<String, String> valuesByName) {
    // Compile the expression
    final ExpressionParser parser = new SpelExpressionParser();
    final Expression exp = parser.parseExpression(spelFilter);
    // Build the context with the first values for all of the attributes
    final StandardEvaluationContext context = new StandardEvaluationContext();
    for (String name : valuesByName.keySet()) {
        final List<String> values = valuesByName.get(name);
        if (values.size() > 0) {
            context.setVariable(name, values.get(0));
        }
    }
    // Evaluate our expression
    try {
        return exp.getValue(context, Boolean.class);
    } catch (Exception e) {
        LOG.error("Failed to evaluate expression {}. Assuming match is negative. Msg: {}", exp.getExpressionString(), e.getMessage());
        throw Throwables.propagate(e);
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project opennms by OpenNMS.

the class ResponseHandlingUtils method getMatchingIndex.

private static int getMatchingIndex(Expression exp, ListMultimap<String, String> valuesByName, int depth) throws NoSuchElementException {
    // Build the context with values from all the attributes at the current depth
    final StandardEvaluationContext context = new StandardEvaluationContext();
    int maxDepth = 0;
    for (String name : valuesByName.keySet()) {
        List<String> values = valuesByName.get(name);
        // Keep track of the largest depth
        maxDepth = Math.max(maxDepth, values.size());
        // Skip the variable if there are no values are the current depth
        if (values.size() < depth + 1) {
            continue;
        }
        // Store the value for the current depth in the context
        context.setVariable(name, values.get(depth));
    }
    // Evaluate our expression
    try {
        if (exp.getValue(context, Boolean.class)) {
            return depth;
        }
    } catch (Exception e) {
        LOG.error("Failed to evaluate expression {}. Msg: {}", exp.getExpressionString(), e.getMessage());
        throw new NoSuchElementException();
    }
    if (maxDepth > depth) {
        // Recurse
        return getMatchingIndex(exp, valuesByName, depth + 1);
    }
    throw new NoSuchElementException();
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) NoSuchElementException(java.util.NoSuchElementException) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project opennms by OpenNMS.

the class WSManDataCollectionConfigDaoJaxb method isAgentSupportedBySystemDefinition.

public static boolean isAgentSupportedBySystemDefinition(SystemDefinition sysDef, CollectionAgent agent, WsmanAgentConfig agentConfig, OnmsNode node) {
    // Determine the effective values for the productVendor and productVersion:
    // The detected values are stored in the assets table, we allow these
    // to be overridden by the agent specific configuration
    String productVendor;
    if (agentConfig.getProductVendor() != null) {
        productVendor = agentConfig.getProductVendor();
    } else {
        productVendor = node.getAssetRecord().getVendor();
        // Guarantee that the values are non-null
        productVendor = Strings.nullToEmpty(productVendor);
    }
    String productVersion;
    if (agentConfig.getProductVersion() != null) {
        productVersion = agentConfig.getProductVersion();
    } else {
        productVersion = node.getAssetRecord().getModelNumber();
        // Guarantee that the values are non-null
        productVersion = Strings.nullToEmpty(productVersion);
    }
    // Build the evaluation context
    StandardEvaluationContext context = new StandardEvaluationContext(node);
    // Add the agent, so that the rule can determine the IP address in question, if required
    context.setVariable("agent", agent);
    context.setVariable("productVendor", productVendor);
    context.setVariable("productVersion", productVersion);
    // Evaluate the rules. Multiple rules are logically ORed.
    for (String rule : sysDef.getRule()) {
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(rule);
        boolean passed = false;
        try {
            passed = exp.getValue(context, Boolean.class);
        } catch (Exception e) {
            LOG.error("Failed to evaluate expression {} for agent {} with context {}. System defintion with name {} will not be used. Msg: {}", rule, agent, context, sysDef.getName(), e.getMessage());
        }
        LOG.debug("Rule '{}' on {} passed? {}", rule, agent, passed);
        if (passed) {
            return true;
        }
    }
    return false;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Example 5 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project beam by apache.

the class ConsumerSpEL method evaluateSeek2End.

public void evaluateSeek2End(Consumer consumer, TopicPartition topicPartitions) {
    StandardEvaluationContext mapContext = new StandardEvaluationContext();
    mapContext.setVariable("consumer", consumer);
    mapContext.setVariable("tp", topicPartitions);
    seek2endExpression.getValue(mapContext);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext)

Aggregations

StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)369 Test (org.junit.jupiter.api.Test)229 Expression (org.springframework.expression.Expression)202 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)202 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)115 ExpressionParser (org.springframework.expression.ExpressionParser)103 EvaluationContext (org.springframework.expression.EvaluationContext)69 ArrayList (java.util.ArrayList)34 Test (org.junit.Test)34 HashMap (java.util.HashMap)33 List (java.util.List)26 Map (java.util.Map)26 TypedValue (org.springframework.expression.TypedValue)24 MapAccessor (org.springframework.context.expression.MapAccessor)17 EvaluationException (org.springframework.expression.EvaluationException)14 Date (java.util.Date)13 StandardCharsets (java.nio.charset.StandardCharsets)12 Arrays (java.util.Arrays)12 Collections (java.util.Collections)12 BeanFactoryResolver (org.springframework.context.expression.BeanFactoryResolver)12