4-(E-Teller源代码分析)AgreeApplicationInitializer

/*
 * Created on 2007-1-1 22:33:24 by mlrain
 */
package cn.com.agree.web.tapestry.presentation.plugin;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.apache.tapestry.engine.state.ApplicationStateManager;
import org.apache.tapestry.services.ApplicationInitializer;
import org.mlstudio.util.StringUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import cn.com.agree.web.tapestry.constants.AgreeTapestry;
import cn.com.agree.web.tapestry.pub.Manage;
import cn.com.agree.web.tapestry.pub.check.CheckLoader;
import cn.com.agree.web.tapestry.pub.transaction.TransactionLoader;
import cn.com.agree.web.tapestry.util.SystemConfigUtil;

/**
 * 应用初始化入口。<BR>
 * <ul>
 * 主要功能:
 * <li>加载Spring配置文件</li>
 * <li>设置WEB项目运行根目录</li>
 * <li>加载系统配置</li>
 * <li>加载具体应用的事务提醒的配置文件</li>
 * <li>加载具体应用的统一审批的配置文件</li>
 * </ul>
 *
 * @author mlrain
 * @version 1.1.0, 2007-6-28 09:13:24
 */
public class AgreeApplicationInitializer implements ApplicationInitializer
{
    /**
     * Spring根配置
     */
    public static final String APPLICATION_CONTEXT_KEY = "appContext";

    /**
     * Tapestry4的应用程序状态管理器
     */
    private ApplicationStateManager _appStateManager;

    /**
     * 日志实例
     */
    private Logger logger = Logger.getLogger(getClass());

    /*
     * (non-Javadoc)
     *
     * @see org.apache.tapestry.services.ApplicationInitializer#initialize(javax.servlet.http.HttpServlet)
     */
    public void initialize(HttpServlet servlet)
    {
        Map global = (Map) _appStateManager.get("global");

        ServletContext context = servlet.getServletContext();

        // 设置WEB项目运行目录等信息
        String rootpath = context.getRealPath("/").replace('//', '/');
        if (rootpath.charAt(rootpath.length() - 1) != '/')
        {
            rootpath += "/";
        }
        Manage.WEBAPP_ROOT_PATH = rootpath;

        // 6 row(s) below added by mlrain @2007-4-7 18:49:53
        // for: 增加系统配置的加载
        logger.info("loading System Config informations, Please wait........");
        global.put(AgreeTapestry.SYSP_NAME, SystemConfigUtil
                .getValue(AgreeTapestry.SYSP_NAME));
        global.put(AgreeTapestry.SYSP_COPYRIGHT, SystemConfigUtil
                .getValue(AgreeTapestry.SYSP_COPYRIGHT));
        global.put(AgreeTapestry.SYSP_DATABASE_TYPE, SystemConfigUtil
                .getValue(AgreeTapestry.SYSP_DATABASE_TYPE));
        global.put(AgreeTapestry.SYSP_VERSION, SystemConfigUtil
                .getValue(AgreeTapestry.SYSP_VERSION));
        // 15 row(s) below added by mlrain @2007-4-8 8:45:51
        // for: 增加“快捷操作页面”的加载
        String sc = SystemConfigUtil
                .getValue(AgreeTapestry.SYSP_PAGES_SHORTCUTS);
        List scList = new ArrayList();
        if (!StringUtil.empty(sc))
        {
            String[] scs = StringUtil.split(sc, ",");
            for (int i = 0; i < scs.length; ++i)
            {
                String[] s = StringUtil.split(scs[i], "|");
                if (s.length != 2)
                    continue;
                scList.add(s);
            }
        }
        global.put(AgreeTapestry.SYSP_PAGES_SHORTCUTS, scList);
        // 10 row(s) below added by mlrain @2007-8-23 上午12:08:14
        // for: 根据系统配置文件中的值设置用户合法性校验和权限树来源,主要用于开发时调试
        String checkUser = SystemConfigUtil
                .getValue(AgreeTapestry.SYSP_UM_CHECKUSER);
        AgreeTapestry.IS_DEBUG = !(StringUtil.empty(checkUser) || "yes"
                .equals(checkUser));
        String treeSource = SystemConfigUtil
                .getValue(AgreeTapestry.SYSP_UM_TREESOURCE);
        if ("file".equals(treeSource))
            AgreeTapestry.userRightsTreeSource = AgreeTapestry.USER_RIGHTS_TREE_SOURCE_XML;
        if (StringUtil.empty(treeSource) || "db".equals(treeSource))
            AgreeTapestry.userRightsTreeSource = AgreeTapestry.USER_RIGHTS_TREE_SOURCE_DATABASE;
        // 2 row(s) below added by mlrain @2007-9-4 下午08:50:26
        // for: 增加用户是否单一登录的变量设值
        String singleton = SystemConfigUtil
                .getValue(AgreeTapestry.SYSP_UM_USER_SINGLETON);
        AgreeTapestry.userSingleton = "true".equals(singleton);
        // 11 row(s) below added by mlrain @2007-9-5 下午12:28:56
        // for: 增加只允许单一登录时的超时时间
        String timeout = SystemConfigUtil
                .getValue(AgreeTapestry.SYSP_UM_USER_TIMEOUT_DELAY);
        if (!StringUtil.empty(timeout))
        {
            try
            {
                AgreeTapestry.userLoginTimeoutDelay = Integer.parseInt(timeout);
            } catch (NumberFormatException e)
            {
            }
        }
        // 2 row(s) below added by mlrain @2007-8-23 上午12:21:32
        // for: 增加事务提醒的刷新相关的配置
        global
                .put(
                        AgreeTapestry.SYSP_PAGES_TRUNSACTION_REFRESH_OPEN,
                        SystemConfigUtil
                                .getValue(AgreeTapestry.SYSP_PAGES_TRUNSACTION_REFRESH_OPEN));
        global
                .put(
                        AgreeTapestry.SYSP_PAGES_TRANSACTION_REFRESH_DELAY,
                        SystemConfigUtil
                                .getValue(AgreeTapestry.SYSP_PAGES_TRANSACTION_REFRESH_DELAY));
        logger
                .info("System Config informations have been loaded successfully!");

        // 加载各应用的Spring配置文件,并置于Global中
        ApplicationContext parent = WebApplicationContextUtils
                .getWebApplicationContext(context);
        global.put(APPLICATION_CONTEXT_KEY, parent);

        logger.info("loading Spring Config files, Please wait........");

        Map mm = loadApplicationContext(context);
        for (Iterator i = mm.keySet().iterator(); i.hasNext();)
        {
            Object key = i.next();
            XmlWebApplicationContext xc = (XmlWebApplicationContext) mm
                    .get(key);
            /* must */
            xc.setParent(parent);
            /* 定义应用的命名空间 */
            try
            {
                xc.refresh();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            global.put(key, xc);
        }

        logger.info("Spring Config files have been loaded successfully!");

        // 统一审批相关的转换器和处理器的注册
        CheckLoader.loadAndRegister();

        // 事务相关信息的注册
        TransactionLoader.loadAndRegister();
    }

    public void setAppStateManager(ApplicationStateManager manager)
    {
        _appStateManager = manager;
    }

    /**
     *
     * <DL>
     * <DT><B>加载ApplicationContext配置文件 </B></DT>
     * <p>
     * <DD>加载WEB-INFO/conf目录下的Context配置文件。</DD>
     * <p>
     * <DD>
     * 应用中ApplicationContext的命名规则:WEB-INF下各个应用在conf目录下应用有一个该应用的Context,其命名为:
     * applicationName-context.xml,其中applicationName为应用名称,tapestry表现层调用spring提供的服务时,其命名的必须
     * 和该名称一致,例如用户管理子系统,其Context文件为:userManage-context.xml,tapestry调用该应用的接口的方法为:
     * global.userManage.getBean("beanName");</DD>
     * </DL>
     * <p>
     *
     * @param context
     *            ServletContext
     * @return XmlWebApplicationContext的MAP
     */
    private Map loadApplicationContext(ServletContext context)
    {
        Map mm = new HashMap();
        try
        {
            String path = context.getRealPath("/");
            File file = new File(path + "/WEB-INF/conf");
             //
            File[] contextFiles = file.listFiles(new FilenameFilter()
            {
                public boolean accept(File dir, String name)
                {
                    if (name.toLowerCase().endsWith("-context.xml"))
                        return true;
                    return false;
                }
            });
            // 增加判断,以解决没有配置文件时报空指针异常的BUG
            if (contextFiles == null)
                contextFiles = new File[0];

            List ts = new ArrayList();
            List tnm = new ArrayList();
            for (int j = 0; j < contextFiles.length; j++)
            {
                String p = contextFiles[j].getAbsolutePath();
                 //得到相对路径
                ts.add(p.substring(path.length()));
                String nm = contextFiles[j].getName();
                 //去掉-context.xml
                tnm.add(nm.substring(0, nm.length() - 12));
            }

            for (int i = 0; i < ts.size(); i++)
            {
                //子系统的划分可能是由Spring来划分的
                XmlWebApplicationContext xc = new XmlWebApplicationContext();
                //将配置文件的相对路径设置给Spring Context
                xc.setConfigLocations(new String[]
                { (String) ts.get(i) });
                xc.setServletContext(context);
                 //将-context.xml前的名字设置为namespace
                xc.setNamespace((String) tnm.get(i));
                //namespace名称,和对应的Spring Context 设置到Map中
                mm.put(tnm.get(i), xc);
            }
        } catch (Exception e)
        {
            logger.error("加载应用配置文件列表出错!", e);
        }

        return mm;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值