com.giffing.bucket4j.spring.boot.starter.service.ExpressionService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bucket4j-spring-boot-starter Show documentation
Show all versions of bucket4j-spring-boot-starter Show documentation
Spring Boot Starter für Bucket4J
package com.giffing.bucket4j.spring.boot.starter.service;
import com.giffing.bucket4j.spring.boot.starter.context.ExpressionParams;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.util.Map;
/**
* The expression service wraps Springs {@link ExpressionParser} to execute SpEl expressions.
*/
@RequiredArgsConstructor
@Slf4j
public class ExpressionService {
private final ExpressionParser expressionParser;
private final ConfigurableBeanFactory beanFactory;
public String parseString(String expression, ExpressionParams params) {
var context = getContext(params.getParams());
var expr = expressionParser.parseExpression(expression);
String result = expr.getValue(context, params.getRootObject(), String.class);
log.info("parse-string-expression;result:{};expression:{};root:{};params:{}", result, expression, params.getRootObject(), params.getParams());
return result;
}
public Boolean parseBoolean(String expression, ExpressionParams params) {
var context = getContext(params.getParams());
var expr = expressionParser.parseExpression(expression);
boolean result = Boolean.TRUE.equals(expr.getValue(context, params.getRootObject(), Boolean.class));
log.info("parse-boolean-expression;result:{};expression:{};root:{};params:{}", result, expression, params.getRootObject(), params.getParams());
return result;
}
private StandardEvaluationContext getContext(Map params) {
var context = new StandardEvaluationContext();
params.forEach(context::setVariable);
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
return context;
}
}