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);
}
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);
}
}
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();
}
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;
}
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);
}
Aggregations