weblogic FilterChain实现,创建一个Filter,随便打一个断点,观察此时的堆栈信息,如图
通过跟踪堆栈信息,我们可以找到,在wrapRun函数中,会判断系统中是否存在filter以及listener。如果存在,则获取FilterChain,然后依次调用Filter。原理与tomcat类似。相关代码如下
- weblogic.servlet.internal.WebAppServletContext.ServletInvocationAction#wrapRun 函数
- if (!invocationContext.hasFilters() && !invocationContext.hasRequestListeners()) {
- this.stub.execute(this.req, this.rsp);
- } else {
- FilterChainImpl fc = invocationContext.getFilterChain(this.stub, this.req, this.rsp);
- if (fc == null) {
- this.stub.execute(this.req, this.rsp);
- } else {
- fc.doFilter(this.req, this.rsp);
- }
- }
而getFilterChain的代码在 weblogic.servlet.internal.FilterManager中。weblogic中主要使用FilterManager去管理系统中的Filter,包括动态注册一个Filter,获取FilterChain等。动态注册一个Filter的代码如下
- void registerFilter(String filterName, String filterClassName, String[] urlPatterns, String[] servletNames, Map initParams, String[] dispatchers) throws DeploymentException {
- FilterWrapper fw = new FilterWrapper(filterName, filterClassName, initParams, this.context);
- if (this.loadFilter(fw)) {
- EnumSet<DispatcherType> types = FilterManager.FilterInfo.translateDispatcherType(dispatchers, this.context, filterName);
- if (urlPatterns != null) {
- this.addMappingForUrlPatterns(filterName, types, true, urlPatterns);
- }
- if (servletNames != null) {
- this.addMappingForServletNames(filterName, types, true, servletNames);
- }
- this.filters.put(filterName, fw);
- }
- }