org.aoju.bus.limiter.expression.LimiterOperationExpressionEvaluator Maven / Gradle / Ivy
/*********************************************************************************
* *
* The MIT License (MIT) *
* *
* Copyright (c) 2015-2020 aoju.org and other contributors. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
* THE SOFTWARE. *
********************************************************************************/
package org.aoju.bus.limiter.expression;
import org.aoju.bus.core.lang.Symbol;
import org.aoju.bus.core.toolkit.ObjectKit;
import org.aoju.bus.limiter.Limiter;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Kimi Liu
* @version 6.1.1
* @since JDK 1.8+
*/
public class LimiterOperationExpressionEvaluator {
private final SpelExpressionParser parser = new SpelExpressionParser();
private final ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
private final Map keyCache = new ConcurrentHashMap<>(64);
public EvaluationContext createEvaluationContext(Limiter limiter, Method method, Object[] args, Object target, Class> targetClass, Method targetMethod,
Map injectArgs, BeanFactory beanFactory) {
LimiterExpressionRootObject rootObject = new LimiterExpressionRootObject(limiter, method, args, target, targetClass);
MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, targetMethod, args, this.parameterNameDiscoverer);
for (String key : injectArgs.keySet()) {
evaluationContext.setVariable(key, injectArgs.get(key));
}
if (beanFactory != null) {
evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
}
return evaluationContext;
}
public Object key(String keyExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
return getExpression(methodKey, keyExpression).getValue(evalContext);
}
protected Expression getExpression(AnnotatedElementKey elementKey, String expression) {
ExpressionKey expressionKey = new ExpressionKey(elementKey, expression);
Expression expr = keyCache.get(expressionKey);
if (expr == null) {
expr = this.parser.parseExpression(expression);
keyCache.put(expressionKey, expr);
}
return expr;
}
protected static class ExpressionKey implements Comparable {
private final AnnotatedElementKey element;
private final String expression;
protected ExpressionKey(AnnotatedElementKey element, String expression) {
this.element = element;
this.expression = expression;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ExpressionKey)) {
return false;
}
ExpressionKey otherKey = (ExpressionKey) other;
return (this.element.equals(otherKey.element) &&
ObjectKit.nullSafeEquals(this.expression, otherKey.expression));
}
@Override
public int hashCode() {
return this.element.hashCode() * 29 + this.expression.hashCode();
}
@Override
public String toString() {
return this.element + " with expression \"" + this.expression + Symbol.DOUBLE_QUOTES;
}
@Override
public int compareTo(ExpressionKey other) {
int result = this.element.toString().compareTo(other.element.toString());
if (result == 0) {
result = this.expression.compareTo(other.expression);
}
return result;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy