com.nepxion.discovery.plugin.strategy.condition.ExpressionStrategyResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of discovery-plugin-strategy Show documentation
Show all versions of discovery-plugin-strategy Show documentation
Nepxion Discovery is an enhancement for Spring Cloud Discovery
package com.nepxion.discovery.plugin.strategy.condition;
/**
* Title: Nepxion Discovery
* Description: Nepxion Discovery
* Copyright: Copyright (c) 2017-2050
* Company: Nepxion
* @author Haojun Ren
* @version 1.0
*/
import java.util.Map;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.TypeComparator;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class ExpressionStrategyResolver {
private static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();
public static boolean eval(String expression, String key, Map map, TypeComparator typeComparator) {
StandardEvaluationContext context = new StandardEvaluationContext();
context.setTypeComparator(typeComparator);
context.setVariable(key, map);
return eval(expression, context);
}
public static boolean eval(String expression, Map> map, TypeComparator typeComparator) {
StandardEvaluationContext context = new StandardEvaluationContext();
context.setTypeComparator(typeComparator);
for (Map.Entry> entry : map.entrySet()) {
context.setVariable(entry.getKey(), entry.getValue());
}
return eval(expression, context);
}
public static boolean eval(String expression, StandardEvaluationContext context) {
try {
Boolean result = EXPRESSION_PARSER.parseExpression(expression).getValue(context, Boolean.class);
return result != null ? result.booleanValue() : false;
} catch (Exception e) {
return false;
}
}
}