use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class SetValueTests method setValue.
/**
* For use when coercion is happening during a setValue(). The expectedValue should be
* the coerced form of the value.
*/
protected void setValue(String expression, Object value, Object expectedValue) {
try {
Expression e = parser.parseExpression(expression);
if (e == null) {
fail("Parser returned null for expression");
}
if (DEBUG) {
SpelUtilities.printAbstractSyntaxTree(System.out, e);
}
StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
assertTrue("Expression is not writeable but should be", e.isWritable(lContext));
e.setValue(lContext, value);
Object a = expectedValue;
Object b = e.getValue(lContext);
if (!a.equals(b)) {
fail("Not the same: [" + a + "] type=" + a.getClass() + " [" + b + "] type=" + b.getClass());
// assertEquals("Retrieved value was not equal to set value", expectedValue, e.getValue(lContext));
}
} catch (EvaluationException ee) {
ee.printStackTrace();
fail("Unexpected Exception: " + ee.getMessage());
} catch (ParseException pe) {
pe.printStackTrace();
fail("Unexpected Exception: " + pe.getMessage());
}
}
use of org.springframework.expression.Expression 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.Expression in project opennms by OpenNMS.
the class ResponseHandlingUtils method getMatchingIndex.
public static int getMatchingIndex(String spelExpression, ListMultimap<String, String> values) throws NoSuchElementException {
// Compile the expression
final ExpressionParser parser = new SpelExpressionParser();
final Expression exp = parser.parseExpression(spelExpression);
// Recursively check all levels, defaulting to the first element
return getMatchingIndex(exp, values, 0);
}
use of org.springframework.expression.Expression 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.Expression in project uPortal by Jasig.
the class StaticTransformerConfigurationSource method setParameterExpressions.
public void setParameterExpressions(Map<String, String> parameterExpressions) {
final Map<String, Expression> parameterExpressionsBuilder = new LinkedHashMap<String, Expression>();
for (final Map.Entry<String, String> expressionEntry : parameterExpressions.entrySet()) {
final String string = expressionEntry.getValue();
final Expression expression = this.portalSpELService.parseExpression(string);
parameterExpressionsBuilder.put(expressionEntry.getKey(), expression);
}
this.parameterExpressions = parameterExpressionsBuilder;
}
Aggregations