Struts源码学习之ActionServlet (三)

④ 调用 initChain(); 读取web.xml 中命令链文件初始值chainConfig

protected String chainConfig = "org/apache/struts/chain/chain-config.xml" ;

// initChain();

 

// 如没有chainConfig 参数,则使用默认 "org/apache/struts/chain/chain-config.xml"

String value;

value = getServletConfig().getInitParameter("chainConfig");

if (value != null) {

    chainConfig = value;

}

 

ConfigParser parser = new ConfigParser();

List urls = splitAndResolvePaths(chainConfig);

URL resource;

 

// chainConfig 替换了原来传统的在 RequestProcessor 类中执行的 HTTP 请求处理

for (Iterator i = urls.iterator(); i.hasNext();) {

     resource = (URL) i.next();

     log.info("Loading chain catalog from " + resource);

     parser.parse(resource);

}

 

/************************************************************

// org.apache.struts.action. RequestProcessor .java process 方法中,一些方法如

processLocale (request, response);

processContent (request, response);

processNoCache (request, response);

.......

"org/apache/struts/chain/chain-config.xml" 中下列配置所取代

<command

          className="org.apache.struts.chain.commands.servlet.SelectLocale"/>

<command

          className="org.apache.struts.chain.commands.servlet.SetContentType"/>

<command

          className="org.apache.struts.chain.commands.servlet.RequestNoCache"/>

.......

好处是充分降低了代码内部方法与方法之间的耦合度

************************************************************/

 

 

/ 之间

// servlet 对象存储到servletContext 中,属性名为Globals.ACTION_SERVLET_KEY

" org.apache.struts.action.ACTION_SERVLET "

getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);

 

 

⑤ 调用 initModuleConfigFactory(); initModuleConfig("", config); 创建 ModuleConfig 对象。Struts 中的MessageResourcePlugIn 、数据源等,都是通过ModuleConfig 来实现的。

// 初始化ModuleConfig 配置工厂

initModuleConfigFactory(); //

// 由配置工厂实例化一个ModuleConfig 的对象

ModuleConfig moduleConfig = initModuleConfig ("", config); //

 

initModuleConfigFactory(); 所做的工作:

// 得到web.xml"configFactory" 参数,如果找不到,则使用 默认工厂

String configFactory = getServletConfig().getInitParameter("configFactory ");

if (configFactory != null) {

     ModuleConfigFactory.setFactoryClass (configFactory);

}

<servlet>

        <servlet-name >action</servlet-name>

        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

        <init-param>

        <param-name>configFactory </param-name>   <!-- 得到"configFactory" 参数-->

      <param-value>com.lively.base.webapp.UserModuleConfigFactory</param-value>

        </init-param>

        .......

    </servlet>

 

ModuleConfigFactory.java 中的setFactoryClass 方法,

public static void setFactoryClass(String factoryClass) {

        ModuleConfigFactory.factoryClass = factoryClass;

        ModuleConfigFactory.clazz = null;

}

其中

protected static Class clazz = null;

protected static String factoryClass =

        "org.apache.struts.config.impl.DefaultModuleConfigFactory " ;

 

initModuleConfig ("", config); 所做的工作:

// Parse the configuration for this module

ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory (); //

ModuleConfig config = factoryObject.createModuleConfig (prefix); //

 

ModuleConfigFactory.createFactory (); 方法中:

protected static Class clazz = null;

protected static String factoryClass =

        "org.apache.struts.config.impl.DefaultModuleConfigFactory " ;

if (clazz == null) {

     clazz = RequestUtils.applicationClass (factoryClass);

}

factory = (ModuleConfigFactory) clazz.newInstance();

 

RequestUtils.applicationClass 通过classLoader 加载一个

org.apache.struts.config.impl.DefaultModuleConfigFactory

 

ModuleConfig config = factoryObject.createModuleConfig (prefix); 方法中:

// 通过new ModuleConfigImpl(prefix); 设置一些变量的初始值,在 initModuleConfig ("", config); 方法的最后会 ModuleConfig 对象放置到servletContext 中 (参见

protected String prefix = null;

protected HashMap actionConfigs = null;

protected List actionConfigList = null;

protected String actionFormBeanClass = "org.apache.struts.action.ActionFormBean";

protected String actionMappingClass = "org.apache.struts.action.ActionMapping";

protected String actionForwardClass = "org.apache.struts.action.ActionForward";

protected boolean configured = false;

protected ControllerConfig controllerConfig = null;

protected HashMap exceptions = null;

protected HashMap formBeans = null;

protected HashMap forwards = null;

protected HashMap messageResources = null;

protected ArrayList plugIns = null;

public ModuleConfigImpl(String prefix) {

        super();

        this.prefix = prefix;

        this.actionConfigs = new HashMap();

        this.actionConfigList = new ArrayList();

        this.actionFormBeanClass = "org.apache.struts.action.ActionFormBean";

        this.actionMappingClass = "org.apache.struts.action.ActionMapping";

        this.actionForwardClass = "org.apache.struts.action.ActionForward";

        this.configured = false;

        this.controllerConfig = null;

        this.exceptions = new HashMap();

        this.formBeans = new HashMap();

        this.forwards = new HashMap();

         this.messageResources = new HashMap();

        this.plugIns = new ArrayList();

}

 

protected String config = "/WEB-INF/struts-config.xml"; // initOther(); initModuleConfig ();

protected Digester configDigester = null; // initModuleConfig ();

 

// 初始化Digester

Digester digester = initConfigDigester();

 

initConfigDigester(); 所做的工作:

// Create a new Digester instance with standard capabilities

configDigester = new Digester();

configDigester.setNamespaceAware(true);

configDigester.setValidating(this.isValidating());

configDigester.setUseContextClassLoader(true);

// 解析struts 配置文件之前,首先添加默认的解析规则

configDigester.addRuleSet(new ConfigRuleSet());

for (int i = 0; i < registrations.length; i += 2) {

    URL url = this.getClass().getResource(registrations[i + 1]);

    if (url != null) {

         configDigester.register(registrations[i], url.toString());

    }

}

// 通过getServletConfig().getInitParameter("rulesets");web.xml 中读取用户自定义的解析规则(用"," 分开的org.apache.commons.digester.RuleSet 列表)

this.addRuleSets();

/* 循环struts 配置文件(用"," 分开的多个struts 配置文件)并解析, parseModuleConfigFile 执行之后可以 struts-config.xml 等配置文件中得到 actionConfigs/actionConfigList exceptions formBeansforwardsmessageResourcesplugIns 等的配置,并把得到的所有值封装到对象 ModuleConfig 对象(config) */

List urls = splitAndResolvePaths(paths);

URL url;

for (Iterator i = urls.iterator(); i.hasNext();) {

    url = (URL) i.next();

    digester.push(config);

    this.parseModuleConfigFile (digester, url);

}

 

// config 存储到servletContext ,属性名为Globals.MODULE_KEY

" org.apache.struts.action.MODULE "

getServletContext().setAttribute(Globals.MODULE_KEY

            + config.getPrefix(), config);

 

 

⑥ 用户资源文件的初始化 initModuleMessageResources(moduleConfig);

在上面第 步的 中我们已经创建了ModuleConfig 对象 并在struts-config.xml 等配置文件中得到 得到一些配置且封装到ModuleConfig 对象 中,所以在下面可以直接使用initModuleMessageResources(moduleConfig);

 

initModuleMessageResources(moduleConfig); 所做的工作:

/* moduleConfig 中读取所有的资源文件(包括 ① 内部资源文件 利用Digester 读取的struts 配置文件指定的用户资源文件) */

MessageResourcesConfig[] mrcs = config.findMessageResourcesConfigs();

注: 此时moduleConfig 中默认只含有① 内部资源文件 ActionResources.properties

 

// resources (包括 ① 内部资源文件 ⑥ 用户资源文件 )存储到servletContext // 属性名为mrcs[i].getKey() + config.getPrefix()

        for (int i = 0; i < mrcs.length; i++) {

            if ((mrcs[i].getFactory() == null)

                || (mrcs[i].getParameter() == null)) {

                continue;

            }

 

            if (log.isDebugEnabled()) {

                log.debug("Initializing module path '" + config.getPrefix()

                    + "' message resources from '" + mrcs[i].getParameter()

                    + "'");

            }

 

            String factory = mrcs[i].getFactory();

 

            MessageResourcesFactory.setFactoryClass(factory);

 

            MessageResourcesFactory factoryObject =

                MessageResourcesFactory.createFactory();

 

            factoryObject.setConfig(mrcs[i]);

 

            MessageResources resources =

                factoryObject.createResources(mrcs[i].getParameter());

 

            resources.setReturnNull(mrcs[i].getNull());

            resources.setEscape(mrcs[i].isEscape());

            getServletContext().setAttribute(mrcs[i].getKey()

                + config.getPrefix(), resources);

        }

 

 

你可能感兴趣的:(apache,xml,Web,struts,servlet)