⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 applicationcontext.java

📁 以前做的一个j2ee的项目
💻 JAVA
字号:
package gov.gdlt.ssgly.taxcore.comm.config;

import java.util.*;
import java.net.URL;
import java.io.InputStream;
import gov.gdlt.ssgly.taxcore.comm.log.LogWritter;
import gov.gdlt.ssgly.taxcore.comm.util.FileURL;

import sun.jdbc.rowset.CachedRowSet;
import java.sql.SQLException;
import gov.gdlt.ssgly.taxcore.comm.util.StringUtils;

/**
 * 税收管理员平台专用全局参数缓冲池
 * <code>ApplicationContext</code>是采用了Singletone的模式来是实现系统的参数的初始化过程,
 * 从配置文件中读取应用系统配置属性.并且将这些参数存放于一个缓冲池中;在初始化的过程中会创建一个
 * 全局的实例对象,通过此对象可以实现对整个系统的配置参数的访问.<br>
 * 系统的初始化文件有:
 * 1.系统启动文件:gdlt_bootstrap.properties,存放系统开始启动的时候需要的参数,包括系
 * 统配置参数的路径,业务逻辑与实现类的映射关系配置文件路径和名称,JDNI服务器的配置信息,
 * 日志文件的配置信息,数据源的配置信息
 * 2.业务逻辑与实现类的映射关系配置文件:actionhandler.xml,存放具体实现与业务逻辑对应的实现类映射关系表.<br>
 *
 * 从ApplicationContext对象的实例中获取参数的方法是:
 *
 * 获取业务逻辑映射文件的名称的方法:
 * String filename=(String)Application.singleton().getValue("gdlt.action.filename"),
 *
 * 其他的参数的获取方式如此类推.
 *
 */
public class ApplicationContext {
    /**
     * 税收管理员平台配置文件路径和名称
     */
    public static final String SSGLY_CONFIG = "ssgly_config.properties";

    /**
     * JDBC提供者的URL的Key
     */
    public static final String JDBC_PROVIDER_URL = "ssgly.jdbc.provider.url";

    /**
     * EJB提供者的URL的Key
     */
    public static final String EJB_PROVIDER_URL = "ssgly.ejb.provider.url";

    /**
     * 数据源的JNDI名的Key
     */

    /**
     * 后台调度的启动时间和定时间隔
     */
    public static final String HTDL_SCHEDULER_FIRSTTIME = "ssgly.htdl.scheduler.firsttime";
    public static final long HTDL_SCHEDULER_PERIOD = 86400000;   //24小时,以millisecond为单位

    public static final String DATASOURCE_JNDINAME =
            "ssgly.jndi.name.datasource";

    public static final int MAX_OUTPUT_LINE_LENGTH = 100;
    public static final String WEBLOGIC_INITIAL_CONTEXT_FACTORY =
            "weblogic.jndi.WLInitialContextFactory";
    //
    public static final int VALIDATE_USER_SUCCESS = 1; //
    public static final int VALIDATE_USER_NOT_EXIST = 2; //
    public static final int VALIDATE_USER_WRONG_PASSWORD = 3; //
    public static final int VALIDATE_USER_NULL = 4; //
    //Ssgly FacadeGateWay Bean Remote JNDI名称
    public static final String SSGLY_FACADE_JNDINAME = "SsglyGateWayLocal";
    //SQL Map file name
    public static final String SQLMAP_CONFIG_FILENAME =
            "config/ibatis/SqlMapConfig.xml";
    //通用函数返回值
    public static final int RET_SUCCESS = 0; //返回成功
    public static final int RET_FAIL = -1; //返回失败
    //SQL查询执行结果
    public static final int ROWS_FOUND = 0; //找到记录
    public static final int ROWS_NOT_FOUND = 1; //找不到记录

    /**
     * 标记了系统是否处于Debug状态:
     *    true, 表示为处于Debug状态
     *    false,表示为处于非Debug状态
     */
    private static boolean appDebug = true;

    /**
     * 表示系统参数是否已经被初始化了.
     *    false:表示系统参数没有初始化
     *    true :表示系统已经初始化了参数
     * 通过这个参数判断是否要重新加载系统的初始化参数。
     */
    private static boolean initialized = false;

    /**
     * 是一个静态的Map变量,存放系统的参数值,是以key=value的方式来存放的.
     */
    private static Map contextPool = null;

    /**
     * ApplicationContext的静态变量,经过类的初始化后成为全局的一个实例.
     */
    private static ApplicationContext applicationContext = null;

    /**
     * ApplicationContext的构造器,初始化创建ApplicationContext的实例和存放系统参数
     * 的Map的实例。
     *
     * 输入参数:无
     * 返回:无
     */
    private ApplicationContext() {
        init();
    }

    private void init() {
        contextPool = new HashMap();
        if (this.initialized) {
            return;
        }

        loadProperties();
//        loadApplicationConfigData();

        this.setInitialized(true);

        if (this.appDebug) {
            LogWritter.sysInfo("完成缓存中所有的参数数据初始化.");
        }
    }


    /**
     * 保存从系统启动属性文件中读到的属性值到 appcontext 的变量 contextPool 中.
     *
     * 输入参数:无
     * 返回:无
     */
    private void loadProperties() {
        Properties props = this.getProperties();

        Iterator iter = props.keySet().iterator();
        while (iter != null && iter.hasNext()) {
            String key = (String) iter.next();
            String value = props.getProperty(key);
            synchronized (contextPool) {
                contextPool.put(key, value);
            }

            if (this.appDebug) {
                LogWritter.sysInfo("初始化系统属性: " + key + " = " + value);
            }
        }
    }

    /**-------------------------------------------------------------------------
     * 读取全部配置信息
     * @return 配置信息属性对象
     */
    private Properties getProperties() {
        Properties props = new Properties();
        InputStream is = null;
        try {
            FileURL fileUrl = new FileURL();
            URL url = fileUrl.getFileURL(this.SSGLY_CONFIG);
            is = url.openStream();
            props.load(is);
        } catch (Exception e) {
            props = null;
            if (this.appDebug) {
                LogWritter.sysError("读取系统属性配置文件时发生错误:", e);
            }
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (Exception e) {}
            if (this.appDebug) {
                LogWritter.sysInfo("读取系统属性配置文件: " + this.SSGLY_CONFIG);
            }
        }

        return props;
    }

    /**
     * 通过此方法返回访问系统参数的全局实例,并且通过这个实例可以访问系统的所有参数.
     *
     * 输入参数:无
     * 返回:ApplicationContext的实例.
     * modified duanxx
     * since 2003-08-19
     * description 修改程序的逻辑判断
     */
    public static ApplicationContext singleton() {
        if (applicationContext == null) {
            applicationContext = new ApplicationContext();
        }
        return applicationContext;
    };

    /**
     * 通过输入系统的参数key值来在ApplicaationContext对象实例中查找参数列表中是否存在此参数值。
     *
     * 输入参数:
     *   key:Object,参数关键字
     * 返回:
     *   false:不存在
     *   true :存在
     */
    private boolean containsKey(Object key) {
        return contextPool.containsKey(key);
    }

    /**
     * 输入系统的参数关键字对应的值,通过ApplicationContext对象实例在参数列表中查找是否存在此参数值。
     *
     * 输入参数:
     *  value:Object
     *  参数关键字的对应值
     * 返回:
     *   false:不存在
     *   true :存在
     */
    private boolean containsValue(Object value) {
        return contextPool.containsValue(value);
    }

    /**
     * 请除参数缓存中所有的参数数据.
     *
     * 输入参数:无
     * 返回:无
     */
    public void clear() {
        contextPool.clear();
        this.initialized = false;
        if (this.appDebug) {
            LogWritter.sysInfo("清除参数缓存中所有的参数数据");
        }
    }

    /**
     * 根据给定的参数key值返回相应的参数值对象。
     *
     * 输入参数:
     *   key:Object,参数关键字
     * 返回:参数值对象。
     */
    public Object getValue(String key) {
        return contextPool.get(key);
    }

    /**
     * 根据给定的key值以字符串的方式返回值.
     *
     * 输入参数:
     *   key:String,参数关键字
     * 返回:字符串类型的参数值
     */
    public String getValueAsString(String key) {
        return (String)this.getValue(key);
    }

    /**
     * 获取含有键值key的property集合
     * @param key 键值
     * @return 含有键值key的property集合
     */
    public Map getValWith(String key) {
        Map vals = new HashMap();
        Set keys = contextPool.keySet();
        Iterator itKeys = keys.iterator();
        while (itKeys.hasNext()) {
            Object objKey = itKeys.next();
            if (objKey instanceof String) {
                String strKey = (String) objKey;
                if (strKey.indexOf(key) != -1) {
                    vals.put(strKey, contextPool.get(strKey));
                }
            }
        }
        return vals;
    }

    /**
     * 判断系统是否已经初始化
     *
     * 输入参数:无
     * 返回:
     *    true:已经初始化
     *    false:没有初始化
     */
    public boolean isInitialized() {
        return this.initialized;
    }

    /**
     * 系统Degub开关是否已经打开。
     *
     * 输入参数:无
     * 返回:
     *   true:debug开关打开
     *   false:debug开关没有打开
     */
    public boolean isAppDebug() {
        return this.appDebug;
    }

    /**
     * 设置系统参数是否已经初始化。
     *
     * 输入参数:
     *   flag:boolean.
     *      true:系统已经初始化
     *      false:系统没有初始化
     * 返回:无
     */
    private void setInitialized(boolean flag) {
        this.initialized = flag;
    }

    /**
     * 设置系统的debug状态值。
     *
     * 输入参数:
     *   flag:boolean.
     *      true: 系统处于debug状态
     *      false:系统没有非debug状态
     * 返回:无
     */
    public void setIsDebug(boolean flag) {
        this.appDebug = flag;
    }

    /**
     * 重新加载系统配置参数
     */
    public void reLoadConfig() {
        this.clear();
        this.init();
    }

    /**
     * 覆盖方法 toString
     * @return
     */
    public String toString() {
        return "{ appDebug = " + this.appDebug
                + ", initialized = " + this.initialized
                + ", contextPool = [" + this.contextPool + "] }";
    }


    /**
     * 启动方法
     * @param args
     */
    public static void main(String[] args) {
        String msg = ApplicationContext.singleton().getValueAsString(
                "ssgly.jdbc.provider.url");
        System.out.println(msg);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -