This commit is contained in:
Redkale
2016-08-27 13:50:24 +08:00
parent b981307768
commit db32656c8a
4 changed files with 116 additions and 50 deletions

View File

@@ -32,25 +32,29 @@ public final class ClassFilter<T> {
private static final boolean finer = logger.isLoggable(Level.FINER);
private final Set<FilterEntry<T>> entrys = new HashSet<>();
private final Set<FilterEntry<T>> entrys = new HashSet<>(); //符合条件的结果
private final Set<FilterEntry<T>> expectEntrys = new HashSet<>();
private final Set<FilterEntry<T>> expectEntrys = new HashSet<>(); //准备符合条件的结果
private boolean refused;
private boolean refused; //是否拒绝所有数据,设置true则其他规则失效,都是拒绝.
private Class superClass;
private Class superClass; //符合的父类型。不为空时扫描结果的class必须是superClass的子类
private Class<? extends Annotation> annotationClass;
private Class<? extends Annotation> annotationClass;//符合的注解。不为空时扫描结果的class必须包含该注解
private Pattern[] includePatterns;
private Pattern[] includePatterns; //符合的classname正则表达式
private Pattern[] excludePatterns;
private Pattern[] excludePatterns;//拒绝的classname正则表达式
private List<ClassFilter> ors;
private Set<String> privilegeIncludes; //特批符合条件的classname
private List<ClassFilter> ands;
private Set<String> privilegeExcludes;//特批拒绝条件的classname
private AnyValue conf;
private List<ClassFilter> ors; //或关系的其他ClassFilter
private List<ClassFilter> ands;//与关系的其他ClassFilter
private AnyValue conf; //基本配置信息, 当符合条件时将conf的属性赋值到FilterEntry中去。
public ClassFilter(Class<? extends Annotation> annotationClass, Class superClass) {
this(annotationClass, superClass, null);
@@ -62,6 +66,15 @@ public final class ClassFilter<T> {
this.conf = conf;
}
public static ClassFilter create(String includeregs, String excluderegs, Set<String> includeValues, Set<String> excludeValues) {
ClassFilter filter = new ClassFilter(null, null);
filter.setIncludePatterns(includeregs == null ? null : includeregs.split(";"));
filter.setExcludePatterns(excluderegs == null ? null : excluderegs.split(";"));
filter.setPrivilegeIncludes(includeValues);
filter.setPrivilegeExcludes(excludeValues);
return filter;
}
public ClassFilter<T> or(ClassFilter<T> filter) {
if (ors == null) ors = new ArrayList<>();
ors.add(filter);
@@ -169,19 +182,15 @@ public final class ClassFilter<T> {
}
}
public static Pattern[] toPattern(String[] regs) {
if (regs == null) return null;
int i = 0;
Pattern[] rs = new Pattern[regs.length];
for (String reg : regs) {
if (reg == null || reg.trim().isEmpty()) continue;
rs[i++] = Pattern.compile(reg.trim());
}
if (i == 0) return null;
if (i == rs.length) return rs;
Pattern[] ps = new Pattern[i];
System.arraycopy(rs, 0, ps, 0, i);
return ps;
/**
* 判断class是否有效
*
* @param classname String
*
* @return boolean
*/
public boolean accept(String classname) {
return accept(null, classname);
}
/**
@@ -209,6 +218,8 @@ public final class ClassFilter<T> {
private boolean accept0(AnyValue property, String classname) {
if (this.refused) return false;
if (this.privilegeIncludes != null && this.privilegeIncludes.contains(classname)) return true;
if (this.privilegeExcludes != null && this.privilegeExcludes.contains(classname)) return false;
if (classname.startsWith("java.") || classname.startsWith("javax.")) return false;
if (excludePatterns != null) {
for (Pattern reg : excludePatterns) {
@@ -239,6 +250,21 @@ public final class ClassFilter<T> {
return superClass == null || (clazz != superClass && superClass.isAssignableFrom(clazz));
}
public static Pattern[] toPattern(String[] regs) {
if (regs == null || regs.length == 0) return null;
int i = 0;
Pattern[] rs = new Pattern[regs.length];
for (String reg : regs) {
if (reg == null || reg.trim().isEmpty()) continue;
rs[i++] = Pattern.compile(reg.trim());
}
if (i == 0) return null;
if (i == rs.length) return rs;
Pattern[] ps = new Pattern[i];
System.arraycopy(rs, 0, ps, 0, i);
return ps;
}
public void setSuperClass(Class superClass) {
this.superClass = superClass;
}
@@ -279,6 +305,22 @@ public final class ClassFilter<T> {
this.refused = refused;
}
public Set<String> getPrivilegeIncludes() {
return privilegeIncludes;
}
public void setPrivilegeIncludes(Set<String> privilegeIncludes) {
this.privilegeIncludes = privilegeIncludes == null || privilegeIncludes.isEmpty() ? null : privilegeIncludes;
}
public Set<String> getPrivilegeExcludes() {
return privilegeExcludes;
}
public void setPrivilegeExcludes(Set<String> privilegeExcludes) {
this.privilegeExcludes = privilegeExcludes == null || privilegeExcludes.isEmpty() ? null : privilegeExcludes;
}
/**
* 存放符合条件的class与class指定的属性项
*

View File

@@ -9,7 +9,6 @@ import java.lang.reflect.*;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.logging.Level;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import org.redkale.boot.ClassFilter.FilterEntry;
import org.redkale.net.*;
@@ -28,13 +27,19 @@ import org.redkale.util.*;
* @author zhangjx
*/
@NodeProtocol({"HTTP"})
public final class NodeHttpServer extends NodeServer {
public class NodeHttpServer extends NodeServer {
private final HttpServer httpServer;
protected boolean rest;
protected boolean sncp;
protected final HttpServer httpServer;
public NodeHttpServer(Application application, AnyValue serconf) {
super(application, createServer(application, serconf));
this.httpServer = (HttpServer) server;
this.rest = serconf == null ? false : serconf.getAnyValue("rest") != null;
this.sncp = serconf == null ? false : serconf.getBoolValue("_$sncp", false); //SNCP服务以REST启动时会赋值_$sncp=true
}
private static Server createServer(Application application, AnyValue serconf) {
@@ -137,10 +142,11 @@ public final class NodeHttpServer extends NodeServer {
}
}
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString());
loadRestServlet(servletsConf);
if (rest) loadRestServlet(servletsConf);
}
protected void loadRestServlet(final AnyValue servletsConf) throws Exception {
if (!rest) return;
final String prefix = servletsConf == null ? "" : servletsConf.getValue("path", "");
AnyValue restConf = serverConf == null ? null : serverConf.getAnyValue("rest");
if (restConf == null) return; //不存在REST服务
@@ -152,13 +158,19 @@ public final class NodeHttpServer extends NodeServer {
final boolean autoload = restConf.getBoolValue("autoload", true);
final boolean mustsign = restConf.getBoolValue("mustsign", true); //是否只加载标记@RestService的Service类
final Pattern[] includes = ClassFilter.toPattern(restConf.getValue("includes", "").split(";"));
final Pattern[] excludes = ClassFilter.toPattern(restConf.getValue("excludes", "").split(";"));
final Set<String> hasServices = new HashSet<>();
final Set<String> includeValues = new HashSet<>();
final Set<String> excludeValues = new HashSet<>();
for (AnyValue item : restConf.getAnyValues("service")) {
hasServices.add(item.getValue("value", ""));
if (item.getBoolValue("ignore", false)) {
excludeValues.add(item.getValue("value", ""));
} else {
includeValues.add(item.getValue("value", ""));
}
}
final boolean sncp = this.serverConf.getBoolValue("_$sncp", false); //SNCP服务以REST启动时会赋值_$sncp=true
final ClassFilter restFilter = ClassFilter.create(restConf.getValue("includes", ""), restConf.getValue("excludes", ""), includeValues, excludeValues);
super.interceptorServiceWrappers.forEach((wrapper) -> {
if (!wrapper.getName().isEmpty()) return; //只加载resourceName为空的service
final Class stype = wrapper.getType();
@@ -168,22 +180,8 @@ public final class NodeHttpServer extends NodeServer {
if (stype.getAnnotation(LocalService.class) != null && rs == null) return;
final String stypename = stype.getName();
if (!autoload && !hasServices.contains(stypename)) return;
if (excludes != null && !hasServices.contains(stypename)) {
for (Pattern reg : excludes) {
if (reg.matcher(stypename).matches()) return;
}
}
if (includes != null && !hasServices.contains(stypename)) {
boolean match = false;
for (Pattern reg : includes) {
if (reg.matcher(stypename).matches()) {
match = true;
break;
}
}
if (!match) return;
}
if (!autoload && !includeValues.contains(stypename)) return;
if (!restFilter.accept(stypename)) return;
RestHttpServlet servlet = RestServletBuilder.createRestServlet(baseServletClass, wrapper.getName(), stype, sncp);
if (servlet == null) return;

View File

@@ -27,6 +27,32 @@ import org.redkale.service.*;
import org.redkale.source.*;
import org.redkale.util.AnyValue.DefaultAnyValue;
import org.redkale.util.*;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
import static java.lang.Class.forName;
/**
* Server节点的初始化配置类

View File

@@ -20,9 +20,9 @@ import org.redkale.util.*;
* @author zhangjx
*/
@NodeProtocol({"SNCP"})
public final class NodeSncpServer extends NodeServer {
public class NodeSncpServer extends NodeServer {
private final SncpServer sncpServer;
protected final SncpServer sncpServer;
private NodeSncpServer(Application application, AnyValue serconf) {
super(application, createServer(application, serconf));