您的位置:首页 > 编程语言 > Java开发

Java程序员从笨鸟到菜鸟之(三十八)细谈struts2(三)struts2拦截器源码分析

2012-04-21 12:08 761 查看
本文来自:曹胜欢博客专栏。转载请注明出处:http://blog.csdn.net/csh624366188

前面博客我们介绍了开发struts2应用程序的基本流程(细谈struts2之开发第一个struts2的实例),通过前面我们知道了struts2实现请求转发和配置文件加载都是拦截器进行的操作,这也就是为什么我们要在web.xml配置struts2的拦截器的原因了。我们知道,在开发struts2应用开发的时候我们要在web.xml进行配置拦截器org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter(在一些老版的一般配置org.apache.struts2.dispatcher.FilterDispatcher),不知道大家刚开始学的时候有没有这个疑问,为什么通过这个拦截器我们就可以拦截到我们提交的请求,并且一些配置文件就可以得到加载呢?不管你有没有,反正我是有。我想这个问题的答案,我们是非常有必要去看一下这个拦截器的源码去找。
打开StrutsPrepareAndExecuteFilter拦截器源码我们可以看出以下类的信息
属性摘要: Protected List<Pattern> excludedPatterns protected ExecuteOperations execute
protected PrepareOperations prepare 我们可以看出StrutsPrepareAndExecuteFilter与普通的Filter并无区别,方法除继承自Filter外,仅有一个回调方法,第三部分我们将按照Filter方法调用顺序,init―>doFilter―>destroy顺序地分析源码。
提供的方法:
destroy()
继承自Filter,用于资源释放
doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
继承自Filter,执行方法
init(FilterConfig filterConfig)
继承自Filter,初始化参数
postInit(Dispatcher dispatcher, FilterConfig filterConfig)

Callback for post initialization(一个空的方法,用于方法回调初始化)
下面我们一一对这些方法看一下:
1.init方法:我们先整体看一下这个方法:
public void init(FilterConfig filterConfig) throws ServletException {    InitOperations init = new InitOperations();    try {    //封装filterConfig,其中有个主要方法getInitParameterNames将参数名字以String格式存储在List中    FilterHostConfig config = new FilterHostConfig(filterConfig);    // 初始化struts内部日志    init.initLogging(config);    //创建dispatcher ,并初始化,这部分下面我们重点分析,初始化时加载那些资源    Dispatcher dispatcher = init.initDispatcher(config);    init.initStaticContentLoader(config, dispatcher);    //初始化类属性:prepare 、execute    prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);    execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);    this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);    //回调空的postInit方法    postInit(dispatcher, filterConfig);    } finally {    init.cleanup();    }    }

首先开一下FilterHostConfig 这个封装configfilter的类: 这个类总共不超过二三十行代码getInitParameterNames是这个类的核心,将Filter初始化参数名称有枚举类型转为Iterator。此类的主要作为是对filterConfig 封装。具体代码如下:
public Iterator<String> getInitParameterNames() {            return   MakeIterator.convert(config.getInitParameterNames());        }
下面咱接着一块看Dispatcher dispatcher = init.initDispatcher(config);这是重点,创建并初始化Dispatcher ,看一下具体代码:

public Dispatcher initDispatcher( HostConfig filterConfig ) {            Dispatcher dispatcher = createDispatcher(filterConfig);            dispatcher.init();            return dispatcher;        }              创建Dispatcher,会读取 filterConfig 中的配置信息,将配置信息解析出来,封装成为一个Map,然后根据servlet上下文和参数Map构造Dispatcher :
private Dispatcher createDispatcher( HostConfig filterConfig ) {    Map<String, String> params = new HashMap<String, String>();    for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {    String name = (String) e.next();    String value = filterConfig.getInitParameter(name);    params.put(name, value);    }    return new Dispatcher(filterConfig.getServletContext(), params);    }

Dispatcher构造玩以后,开始对他进行初始化,加载struts2的相关配置文件,将按照顺序逐一加载:default.properties,struts-default.xml,struts-plugin.xml,struts.xml,……我们一起看看他是怎么一步步的加载这些文件的 dispatcher的init()方法:

public void init() {          if (configurationManager == null) {        configurationManager = createConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);        }          try {                init_DefaultProperties(); // [1]                init_TraditionalXmlConfigurations(); // [2]                init_LegacyStrutsProperties(); // [3]                init_CustomConfigurationProviders(); // [5]                init_FilterInitParameters() ; // [6]                init_AliasStandardObjects() ; // [7]                  Container container = init_PreloadConfiguration();                container.inject(this);                init_CheckConfigurationReloading(container);                init_CheckWebLogicWorkaround(container);                  if (!dispatcherListeners.isEmpty()) {                    for (DispatcherListener l : dispatcherListeners) {                        l.dispatcherInitialized(this);                    }                }            } catch (Exception ex) {                if (LOG.isErrorEnabled())                    LOG.error("Dispatcher initialization failed", ex);                throw new StrutsException(ex);            }        }

下面我们一起来看一下【1】,【2】,【3】,【5】,【6】的源码,看一下什么都一目了然了:
1.这个方法中是将一个DefaultPropertiesProvider对象追加到ConfigurationManager对象内部的ConfigurationProvider队列中。 DefaultPropertiesProvider的register()方法可以载入org/apache/struts2/default.properties中定义的属性。
try {                defaultSettings = new PropertiesSettings("org/apache/struts2/default");            } catch (Exception e) {                throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties", e);            }

2. 调用init_TraditionalXmlConfigurations()方法,实现载入FilterDispatcher的配置中所定义的config属性。 如果用户没有定义config属性,struts默认会载入DEFAULT_CONFIGURATION_PATHS这个值所代表的xml文件。它的值为"struts-default.xml,struts-plugin.xml,struts.xml"。也就是说框架默认会载入这三个项目xml文件。如果文件类型是XML格式,则按照xwork-x.x.dtd模板进行读取。如果,是Struts的配置文件,则按struts-2.X.dtd模板进行读取。 private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml"; 3.创建一个LegacyPropertiesConfigurationProvider类,并将它追加到ConfigurationManager对象内部的ConfigurationProvider队列中。LegacyPropertiesConfigurationProvider类载入struts.properties中的配置,这个文件中的配置可以覆盖default.properties中的。其子类是DefaultPropertiesProvider类 5.init_CustomConfigurationProviders()此方法处理的是FilterDispatcher的配置中所定义的configProviders属性。负责载入用户自定义的ConfigurationProvider。

String configProvs = initParams.get("configProviders");            if (configProvs != null) {                String[] classes = configProvs.split("\\s*[,]\\s*");                for (String cname : classes) {                    try {                        Class cls = ClassLoaderUtils.loadClass(cname, this.getClass());                        ConfigurationProvider prov = (ConfigurationProvider)cls.newInstance();                        configurationManager.addConfigurationProvider(prov);                    } catch (InstantiationException e) {                        throw new ConfigurationException("Unable to instantiate provider: "+cname, e);                    } catch (IllegalAccessException e) {                        throw new ConfigurationException("Unable to access provider: "+cname, e);                    } catch (ClassNotFoundException e) {                        throw new ConfigurationException("Unable to locate provider class: "+cname, e);                    }                }            }

6.init_FilterInitParameters()此方法用来处理FilterDispatcher的配置中所定义的所有属性
7. init_AliasStandardObjects(),将一个BeanSelectionProvider类追加到ConfigurationManager对象内部的ConfigurationProvider队列中。BeanSelectionProvider类主要实现加载org/apache/struts2/struts-messages。

private void init_AliasStandardObjects() {          configurationManager.addConfigurationProvider(  new BeanSelectionProvider());  }


相信看到这大家应该明白了,struts2的一些配置的加载顺序和加载时所做的工作,其实有些地方我也不是理解的很清楚。其他具体的就不在说了,init方法占时先介绍到这
2、doFilter方法 doFilter是过滤器的执行方法,它拦截提交的HttpServletRequest请求,HttpServletResponse响应,作为strtus2的核心拦截器,在doFilter里面到底做了哪些工作,我们将逐行解读其源码,大体源码如下:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {    //父类向子类转:强转为http请求、响应    HttpServletRequest request = (HttpServletRequest) req;    HttpServletResponse response = (HttpServletResponse) res;    try {    //设置编码和国际化    prepare.setEncodingAndLocale(request, response);    //创建Action上下文(重点)    prepare.createActionContext(request, response);    prepare.assignDispatcherToThread();    if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {    chain.doFilter(request, response);    } else {    request = prepare.wrapRequest(request);    ActionMapping mapping = prepare.findActionMapping(request, response, true);    if (mapping == null) {    boolean handled = execute.executeStaticResourceRequest(request, response);    if (!handled) {    chain.doFilter(request, response);    }    } else {    execute.executeAction(request, response, mapping);    }    }    } finally {    prepare.cleanupRequest(request);    }    }

下面我们就逐句的来的看一下:设置字符编码和国际化很简单prepare调用了setEncodingAndLocale,然后调用了dispatcher方法的prepare方法:
/**    * Sets the request encoding and locale on the response    */    public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {    dispatcher.prepare(request, response);    }

看下prepare方法,这个方法很简单只是设置了encoding 、locale ,做的只是一些辅助的工作:
public void prepare(HttpServletRequest request, HttpServletResponse response) {    String encoding = null;    if (defaultEncoding != null) {    encoding = defaultEncoding;    }    Locale locale = null;    if (defaultLocale != null) {    locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());    }    if (encoding != null) {    try {    request.setCharacterEncoding(encoding);    } catch (Exception e) {    LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);    }    }    if (locale != null) {    response.setLocale(locale);    }    if (paramsWorkaroundEnabled) {    request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request    }    }

下面咱重点看一下创建Action上下文重点 Action上下文创建(重点) ActionContext是一个容器,这个容易主要存储request、session、application、parameters等相关信息.ActionContext是一个线程的本地变量,这意味着不同的action之间不会共享ActionContext,所以也不用考虑线程安全问题。其实质是一个Map,key是标示request、session、……的字符串,值是其对应的对象: static ThreadLocal actionContext = new ThreadLocal(); Map<String, Object> context; 下面我们看起来下创建action上下文的源码:
/**    *创建Action上下文,初始化thread local    */    public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {    ActionContext ctx;    Integer counter = 1;    Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);    if (oldCounter != null) {    counter = oldCounter + 1;    }        //注意此处是从ThreadLocal中获取此ActionContext变量    ActionContext oldContext = ActionContext.getContext();    if (oldContext != null) {    // detected existing context, so we are probably in a forward    ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));    } else {    ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();    stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));    //stack.getContext()返回的是一个Map<String,Object>,根据此Map构造一个ActionContext    ctx = new ActionContext(stack.getContext());    }    request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);    //将ActionContext存如ThreadLocal    ActionContext.setContext(ctx);    return ctx;    }

一句句来看: ValueStackstack= dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack(); dispatcher.getContainer().getInstance(ValueStackFactory.class)根据字面估计一下就是创建ValueStackFactory的实例。这个地方我也只是根据字面来理解的。ValueStackFactory是接口,其默认实现是OgnlValueStackFactory,调用OgnlValueStackFactory的createValueStack(): 下面看一下OgnlValueStack的构造方法
protected OgnlValueStack(XWorkConverter xworkConverter, CompoundRootAccessor accessor, TextProvider prov, boolean allowStaticAccess) {             //new一个CompoundRoot出来          setRoot(xworkConverter, accessor, new CompoundRoot(), allowStaticAccess);           push(prov);       }

接下来看一下setRoot方法:
protected void setRoot(XWorkConverter xworkConverter, CompoundRootAccessor accessor, CompoundRoot compoundRoot,                              boolean allowStaticMethodAccess) {           //OgnlValueStack.root = compoundRoot;                          this.root = compoundRoot;       1    //方法/属性访问策略。          this.securityMemberAccess = new SecurityMemberAccess(allowStaticMethodAccess);           //创建context了,创建context使用的是ongl的默认方式。          //Ognl.createDefaultContext返回一个OgnlContext类型的实例          //这个OgnlContext里面,root是OgnlValueStack中的compoundRoot,map是OgnlContext自己创建的private Map _values = new HashMap(23);          this.context = Ognl.createDefaultContext(this.root, accessor, new OgnlTypeConverterWrapper(xworkConverter), securityMemberAccess);                         //不是太理解,猜测如下:          //context是刚刚创建的OgnlContext,其中的HashMap类型_values加入如下k-v:         //key:com.opensymphony.xwork2.util.ValueStack.ValueStack          //value:this,这个应该是当前的OgnlValueStack实例。          //刚刚用断点跟了一下,_values里面是:          //com.opensymphony.xwork2.ActionContext.container=com.opensymphony.xwork2.inject.ContainerImpl@96231e          //com.opensymphony.xwork2.util.ValueStack.ValueStack=com.opensymphony.xwork2.ognl.OgnlValueStack@4d912          context.put(VALUE_STACK, this);         //此时:OgnlValueStack中的compoundRoot是空的;         //context是一个OgnlContext,其中的_root指向OgnlValueStack中的root,_values里面的东西,如刚才所述。          //OgnlContext中的额外设置。          Ognl.setClassResolver(context, accessor);           ((OgnlContext) context).setTraceEvaluations(false);           ((OgnlContext) context).setKeepLastEvaluation(false);       }
上面代码中dispatcher.createContextMap,如何封装相关参数:,我们以RequestMap为例,其他的原理都一样:主要方法实现:
//map的get实现    public Object get(Object key) {    return request.getAttribute(key.toString());    }    //map的put实现    public Object put(Object key, Object value) {    Object oldValue = get(key);    entries = null;    request.setAttribute(key.toString(), value);    return oldValue;    }

到此,几乎StrutsPrepareAndExecuteFilter大部分的源码都涉及到了。自己感觉都好乱,所以还请大家见谅,能力有限,希望大家可以共同学习

本文出自 “曹胜欢” 博客,请务必保留此出处http://javacsh.blog.51cto.com/3545281/1129152
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐