Skip to content

Commit

Permalink
add InterceptorBuilder support for Addon
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfuhai committed Sep 23, 2020
1 parent c109251 commit 58f04ea
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.jfinal.handler.Handler;
import com.jfinal.log.Log;
import com.jfinal.template.Directive;
import io.jboot.aop.InterceptorBuilder;
import io.jboot.aop.annotation.AutoLoad;
import io.jboot.aop.annotation.Bean;
import io.jboot.aop.annotation.BeanExclude;
import io.jboot.components.event.JbootEventListener;
Expand Down Expand Up @@ -107,6 +109,12 @@ else if (Interceptor.class.isAssignableFrom(loadedClass)) {
addonInfo.addInterceptor(loadedClass);
}
}
// interceptorBuilders
else if (InterceptorBuilder.class.isAssignableFrom(loadedClass)){
if (loadedClass.getAnnotation(AutoLoad.class) != null){
addonInfo.addInterceptorBuilder(loadedClass);
}
}
// handlers
else if (Handler.class.isAssignableFrom(loadedClass)) {
addonInfo.addHandler(loadedClass);
Expand Down
18 changes: 18 additions & 0 deletions jpress-core/src/main/java/io/jpress/core/addon/AddonInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.template.Directive;
import io.jboot.aop.InterceptorBuilder;
import io.jboot.db.annotation.Table;
import io.jboot.db.model.JbootModel;
import io.jboot.utils.StrUtil;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class AddonInfo implements Serializable {

private List<Class<? extends Controller>> controllers;
private List<Class<? extends Interceptor>> interceptors;
private List<Class<? extends InterceptorBuilder>> interceptorBuilders;
private List<Class<? extends Handler>> handlers;
private List<Class<? extends JbootModel>> models;
private List<Class<? extends Directive>> directives;
Expand Down Expand Up @@ -217,6 +219,22 @@ public void setInterceptors(List<Class<? extends Interceptor>> interceptors) {
this.interceptors = interceptors;
}

public void addInterceptorBuilder(Class<? extends InterceptorBuilder> clazz){
if (interceptorBuilders == null){
interceptorBuilders = new ArrayList<>();
}
interceptorBuilders.add(clazz);
}


public List<Class<? extends InterceptorBuilder>> getInterceptorBuilders() {
return interceptorBuilders;
}

public void setInterceptorBuilders(List<Class<? extends InterceptorBuilder>> interceptorBuilders) {
this.interceptorBuilders = interceptorBuilders;
}

public void addHandler(Class<? extends Handler> clazz) {
if (handlers == null) {
handlers = new ArrayList<>();
Expand Down
38 changes: 37 additions & 1 deletion jpress-core/src/main/java/io/jpress/core/addon/AddonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@
import com.jfinal.template.expr.ast.FieldKit;
import com.jfinal.template.expr.ast.MethodKit;
import io.jboot.Jboot;
import io.jboot.aop.InterceptorBuilder;
import io.jboot.aop.InterceptorBuilderManager;
import io.jboot.aop.cglib.JbootCglibProxyFactory;
import io.jboot.components.event.JbootEvent;
import io.jboot.components.event.JbootEventListener;
import io.jboot.db.annotation.Table;
import io.jboot.db.model.JbootModel;
import io.jboot.db.model.JbootModelConfig;
import io.jboot.utils.AnnotationUtil;
import io.jboot.utils.ClassUtil;
import io.jboot.utils.FileUtil;
import io.jboot.utils.StrUtil;
import io.jboot.web.directive.annotation.JFinalDirective;
Expand All @@ -60,6 +64,7 @@
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

/**
* 插件管理器:安装、卸载、启用、停用
Expand Down Expand Up @@ -396,9 +401,12 @@ private void doStart(AddonInfo addonInfo) {
//添加插件的 指令
addDirectives(addonInfo);

//添加插件的拦截器
//添加插件的全局拦截器
addInterceptors(addonInfo);

//添加拦截器构建
addInterceptorBuilders(addonInfo);

//添加微信消息插件
addWechatAddons(addonInfo);

Expand Down Expand Up @@ -491,6 +499,16 @@ private void addInterceptors(AddonInfo addonInfo) {
}
}


private void addInterceptorBuilders(AddonInfo addonInfo) {
List<Class<? extends InterceptorBuilder>> builderClasses = addonInfo.getInterceptorBuilders();
if (builderClasses != null) {
for (Class<? extends InterceptorBuilder> c : builderClasses) {
InterceptorBuilderManager.me().addInterceptorBuilder(ClassUtil.newInstance(c,false));
}
}
}

private void addDirectives(AddonInfo addonInfo) {
List<Class<? extends Directive>> directives = addonInfo.getDirectives();
if (directives != null) {
Expand Down Expand Up @@ -575,6 +593,13 @@ public boolean stop(AddonInfo addonInfo) {
LOG.error(ex.toString(), ex);
}

//删除插件的所有拦截器构建
try {
deleteInteceptorBuilders(addonInfo);
} catch (Exception ex) {
LOG.error(ex.toString(), ex);
}

//移除所有的微信插件
try {
deleteWechatAddons(addonInfo);
Expand Down Expand Up @@ -745,6 +770,17 @@ private void deleteInteceptors(AddonInfo addonInfo) {
}
}

private void deleteInteceptorBuilders(AddonInfo addonInfo) {
List<Class<? extends InterceptorBuilder>> interceptorBuilderClasses = addonInfo.getInterceptorBuilders();
if (interceptorBuilderClasses != null) {
for (Class<? extends InterceptorBuilder> c : interceptorBuilderClasses) {
List<InterceptorBuilder> builders = InterceptorBuilderManager.me().getInterceptorBuilders();
builders.removeIf(interceptorBuilder -> interceptorBuilder.getClass() == c);
}
JbootCglibProxyFactory.IntersCache.clear();
}
}

private void deleteDirectives(AddonInfo addonInfo) {
List<Class<? extends Directive>> directives = addonInfo.getDirectives();
if (directives != null) {
Expand Down

0 comments on commit 58f04ea

Please sign in to comment.