filter-chain设计模式
if (filters.size() > 0) {
FilterChainImpl filterChain = new FilterChainImpl(this);
return filterChain.dataSource_connect(this, maxWaitMillis);
}
if (this.pos < filterSize) {
return nextFilter()
.connection_connect(this, info);
}
filter-chain设计模式的类或接口主要有一下几个Filter、FilterAdapter、FilterChain、FilterChainImpl、FilterEventAdapter、FilterManager
以及两个具体的filter:StatFilter、LogFilter
Filter接口
定义了过滤器的功能,入参是FilterChain
ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException;
FilterChain接口
定义了这个过滤器链的事件方法。
ConnectionProxy connection_connect(Properties info) throws SQLException;
FilterChainImpl实现类
1、具体处理chain中事件的具体事件;
2、将请求分发给下一个filter‘“处理” (pos位置)
public ConnectionProxy connection_connect(Properties info) throws SQLException {
if (this.pos < filterSize) {
return nextFilter()
.connection_connect(this, info);
}
Driver driver = dataSource.getRawDriver();
String url = dataSource.getRawJdbcUrl();
Connection nativeConnection = driver.connect(url, info);
if (nativeConnection == null) {
return null;
}
return new ConnectionProxyImpl(dataSource, nativeConnection, info, dataSource.createConnectionId());
}
FilterEventAdapter类
将我们关注的具体事件,通过适配器的设计模式,分为doBefore,do,doAfter三种事件
public ConnectionProxy connection_connect(FilterChain chai