Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持拦截器顺序配置 #1

Merged
merged 3 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
> 参考 [mzt-biz-log](https://github.com/mouzt/mzt-biz-log) 实现的一款基于 spring aop 操作日志记录工具 支持自定义方法处理

<p align="center">
<img src="https://maven-badges.herokuapp.com/maven-central/cn.hangsman.operationlog/operation-log/badge.svg" />
<img src="https://maven-badges.herokuapp.com/maven-central/cn.hangsman.operationlog/operation-log-core/badge.svg" />
<a target="_blank" href="https://github.com/hangsman/operation-log/blob/master/LICENSE">
<img src="https://img.shields.io/apm/l/vim-mode.svg?color=yellow" />
</a>
Expand Down
11 changes: 6 additions & 5 deletions operation-log/pom.xml → operation-log-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>operation-log-parent</artifactId>
<groupId>cn.hangsman.operationlog</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>operation-log</artifactId>
<artifactId>operation-log-core</artifactId>
<version>1.0.0</version>


<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -34,5 +36,4 @@
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class OperationLog {
private String fail;
private String detail;
private String category;
private Date operatingTime;
private Date operationTime;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import cn.hangsman.operationlog.interceptor.OperationLogParam;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -34,14 +37,25 @@ private Collection<OperationLogParam> parseAnnotations(AnnotatedElement ae, bool
if (ans.isEmpty()) {
return null;
}
return ans.stream().map(an -> OperationLogParam.builder()
.name(ae.toString())
.content(an.content())
.fail(an.fail())
.category(an.category())
.detail(an.detail())
.condition(an.condition())
.before(an.before()).build()).collect(Collectors.toCollection(ArrayList::new));
return ans.stream().map(an -> {
Map<String, String> beforeHandles = new HashMap<>();
for (String template : an.before()) {
if (StringUtils.hasText(template)) {
int delimiterIndex = template.indexOf("=");
String variableName = template.substring(0, delimiterIndex);
String expressionStr = template.substring(delimiterIndex + 1);
beforeHandles.put(variableName, expressionStr);
}
}
return OperationLogParam.builder()
.name(ae.toString())
.content(an.content())
.fail(an.fail())
.category(an.category())
.detail(an.detail())
.condition(an.condition())
.before(beforeHandles).build();
}).collect(Collectors.toCollection(ArrayList::new));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package cn.hangsman.operationlog.expression;

import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateAwareExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
* Shared utility class used to evaluate and cache SpEL expressions that
* are defined on {@link java.lang.reflect.AnnotatedElement}.
*
* @author Stephane Nicoll
* @see AnnotatedElementKey
* @since 4.2
*/
public class CachedExpressionEvaluator {

private final ExpressionParser parser;

private final ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();


/**
* Create a new instance with the specified {@link ExpressionParser}.
*/
protected CachedExpressionEvaluator(ExpressionParser parser) {
Assert.notNull(parser, "SpelExpressionParser must not be null");
this.parser = parser;
}

/**
* Create a new instance with a default {@link SpelExpressionParser}.
*/
protected CachedExpressionEvaluator() {
this(new SpelExpressionParser());
}


/**
* Return the {@link TemplateAwareExpressionParser} to use.
*/
protected ExpressionParser getParser() {
return this.parser;
}

/**
* Return a shared parameter name discoverer which caches data internally.
*
* @since 4.3
*/
protected ParameterNameDiscoverer getParameterNameDiscoverer() {
return this.parameterNameDiscoverer;
}


/**
* Return the {@link Expression} for the specified SpEL value
* <p>Parse the expression if it hasn't been already.
*
* @param cache the cache to use
* @param elementKey the element on which the expression is defined
* @param expression the expression to parse
*/
protected Expression getExpression(Map<ExpressionKey, Expression> cache,
AnnotatedElementKey elementKey, String expression) {

ExpressionKey expressionKey = createKey(elementKey, expression);
Expression expr = cache.get(expressionKey);
if (expr == null) {
expr = getParser().parseExpression(expression);
cache.put(expressionKey, expr);
}
return expr;
}

private ExpressionKey createKey(AnnotatedElementKey elementKey, String expression) {
return new ExpressionKey(elementKey, expression);
}


/**
* An expression key.
*/
protected static class ExpressionKey implements Comparable<ExpressionKey> {

private final AnnotatedElementKey element;

private final String expression;

protected ExpressionKey(AnnotatedElementKey element, String expression) {
Assert.notNull(element, "AnnotatedElementKey must not be null");
Assert.notNull(expression, "Expression must not be null");
this.element = element;
this.expression = expression;
}

@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ExpressionKey)) {
return false;
}
ExpressionKey otherKey = (ExpressionKey) other;
return (this.element.equals(otherKey.element) &&
ObjectUtils.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 + "\"";
}

@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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.hangsman.operationlog.spel;
package cn.hangsman.operationlog.expression;

import org.springframework.expression.ParseException;

Expand All @@ -11,7 +11,7 @@
* @author hangsman
* @since 1.0
*/
public class SpelUtil {
public class ExpressionUtil {

private static boolean isSuffixHere(String expressionString, int pos, String suffix) {
int suffixPosition = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cn.hangsman.operationlog.expression;

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.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.TypedValue;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by 2022/1/16 9:20
*
* @author hangsman
* @since 1.0
*/
public class OperationLogExpressionEvaluator extends CachedExpressionEvaluator {

private final Map<ExpressionKey, Expression> expressionCache = new ConcurrentHashMap<>(64);

public OperationLogExpressionEvaluator(ExpressionParser parser) {
super(parser);
}

public EvaluationContext createEvaluationContext(Method method, Object[] arguments, BeanFactory beanFactory) {
MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(
TypedValue.NULL, method, arguments, getParameterNameDiscoverer());
if (beanFactory != null) {
evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
}
return evaluationContext;
}

public <T> T parseExpression(String expressionStr, AnnotatedElementKey methodKey, EvaluationContext evaluationContext, Class<T> desiredResultType) {
return getExpression(this.expressionCache, methodKey, expressionStr).getValue(evaluationContext, desiredResultType);
}

public boolean condition(String conditionExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
return (Boolean.TRUE.equals(parseExpression(conditionExpression, methodKey, evalContext, Boolean.class)));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cn.hangsman.operationlog.spel;
package cn.hangsman.operationlog.expression;

/**
* Created by 2022/1/12 11:17
* Created by 2022/1/16 9:50
*
* @author hangsman
* @since 1.0
*/
public interface SpelFunction {

Object apply(Object value);

String functionName();

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.hangsman.operationlog.spel;
package cn.hangsman.operationlog.expression;

import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationContext;
Expand All @@ -11,19 +11,19 @@
import java.util.Map;

/**
* Created by 2022/1/12 11:47
* Created by 2022/1/16 9:24
*
* @author hangsman
* @since 1.0
*/
public class FunctionExpression implements Expression {
public class SpelFunctionExpression implements Expression {

private final String expressionStr;
private final Expression[] expressions;
private final SpelFunction function;

public FunctionExpression(String expressionStr, Expression[] expressions,
SpelFunction function) {
public SpelFunctionExpression(String expressionStr, Expression[] expressions,
SpelFunction function) {
this.expressionStr = expressionStr;
this.expressions = expressions;
this.function = function;
Expand Down Expand Up @@ -161,4 +161,5 @@ public void setValue(EvaluationContext context, Object value) throws EvaluationE
public void setValue(EvaluationContext context, Object rootObject, Object value) throws EvaluationException {

}

}
Loading