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

📄 speedconfig.java

📁 羽量级数据持久层开发框架
💻 JAVA
字号:
package org.speedframework.config;

//~--- non-JDK imports --------------------------------------------------------


import org.speedframework.exception.SpeedFrameworkException;
import org.speedframework.utilities.ConfigureParam;
import org.speedframework.utilities.ConfigureUtility;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

//~--- JDK imports ------------------------------------------------------------



import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * Class SpeedConfig
 *
 * @author <a href="mailto:lzfxp@126.com"> lzfxp </a>
 * @version $Revision:1.0.0, $Date: Oct 9, 2007 5:02:56 PM $
 */
public class SpeedConfig
 {

   

    private static Map connectionFactoryMap = new HashMap();

    private static Map sqlCollection = new HashMap();

    private static Map sqlMapConfigCollection = new HashMap();


    private static boolean globalCacheStatus = false;

    private static Document doc = ConfigureUtility.buildDocument(ConfigureParam.CONFIG_FILE);

    private static Element root = ConfigureUtility.getRoot(doc);

    private static String providerCache;

    static {
        init();
    }

    private SpeedConfig() {}



    private static void initConnectionFactory() {
        NodeList connectionFactoryList = ConfigureUtility.getElementsByName(root, "connection-factory");

        if (connectionFactoryList.getLength() == 0) {
            throw new SpeedFrameworkException("no connectin configure found");
        }

        for (int i = 0; i < connectionFactoryList.getLength(); i++) {
            Element  connectionFactory   = (Element) connectionFactoryList.item(i);
            String   connectionFactoryId = ConfigureUtility.getElementAttr(connectionFactory, "id");
            Map      propertyMap         = new HashMap();
            NodeList connection          = ConfigureUtility.getElementsByName(connectionFactory, "property");

            for (int j = 0; j < connection.getLength(); j++) {
                Element property = (Element) connection.item(j);

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.DRIVER)) {
                    propertyMap.put(ConfigureParam.DRIVER, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.URL)) {
                    propertyMap.put(ConfigureParam.URL, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.USERNAME)) {
                    propertyMap.put(ConfigureParam.USERNAME, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.PASSWORD)) {
                    propertyMap.put(ConfigureParam.PASSWORD, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.MAX_COUNT)) {
                    propertyMap.put(ConfigureParam.MAX_COUNT, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.TIME_OUT)) {
                    propertyMap.put(ConfigureParam.TIME_OUT, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.JNDI_NAME)) {
                    propertyMap.put(ConfigureParam.JNDI_NAME, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.JNDI_CLASS)) {
                    propertyMap.put(ConfigureParam.JNDI_CLASS, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.JNDI_URL)) {
                    propertyMap.put(ConfigureParam.JNDI_URL, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.JNDI_USERNAME)) {
                    propertyMap.put(ConfigureParam.JNDI_USERNAME, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }

                if (ConfigureUtility.getElementAttr(property, "name").equals(ConfigureParam.JNDI_PASSWORD)) {
                    propertyMap.put(ConfigureParam.JNDI_PASSWORD, (ConfigureUtility.getElementValue(property) == null)
                            ? ""
                            : ConfigureUtility.getElementValue(property));

                    continue;
                }
            }

            connectionFactoryMap.put(connectionFactoryId, propertyMap);
        }
    }


    private static void initSqlCollection() {
        Element  node    = (Element) ConfigureUtility.getElementsByName(root, "sql-collection").item(0);
        NodeList subNode = ConfigureUtility.getElementsByName(node, "sqlFile");

        for (int i = 0; i < subNode.getLength(); i++) {
            Element n = (Element) subNode.item(i);

            sqlCollection.put(ConfigureUtility.getElementAttr(n, "id"), ConfigureUtility.getElementAttr(n, "path"));
        }
    }


    private static void initGlobalCacheStatus() {

        // Element factory = root.getChild("connection-factory");
        Element cache  = (Element) ConfigureUtility.getElementsByName(root, "speed-global-cache").item(0);
        String  status = ConfigureUtility.getElementValue(cache);

        if (status.equals("true")) {
            globalCacheStatus = true;
        } else if (status.equals("false")) {
            globalCacheStatus = false;
        } else {
            throw new SpeedFrameworkException("speed-global-cach no spacify,please check the configure file!");
        }
    }


    private static void initProviderCache() {

        // Element factory = root.getChild("connection-factory");
        Element cache    = (Element) ConfigureUtility.getElementsByName(root, "speed-cache-provider").item(0);
        String  provider = ConfigureUtility.getElementValue(cache);

//        if ((provider == null) || provider.equals("") || provider.toLowerCase().equals("default")) {
//            provider = "jcs";
//        }

        providerCache = provider.toLowerCase();
    }


    /**
     * Method getConnectionType
     * 返回指定id的connection-factory的连接类型,返回值为connection或datasource
     * @param id
     * @return String
     * @throws SpeedFrameworkException
     */
    public static String getConnectionType(String id) throws SpeedFrameworkException {
        String type       = null;
        Map    connection = getConnectionFactoryById(id);

        if (connection.get(ConfigureParam.DRIVER) != null) {
            type = "connection";
        } else if (connection.get(ConfigureParam.JNDI_NAME) != null) {
            type = "datasource";
        } else {
            throw new SpeedFrameworkException("There is no connection type config!");
        }

        return type;
    }


    /**
     * Method getConnectionType
     * 返回默认connection-factory的连接类型
     * @return String
     * @throws SpeedFrameworkException
     */
    public static String getConnectionType() throws SpeedFrameworkException {
        return getConnectionType("");
    }


    private static void load() throws SpeedFrameworkException {
        doc  = ConfigureUtility.buildDocument(ConfigureParam.CONFIG_FILE);
        root = ConfigureUtility.getRoot(doc);
    }

    private static void init() throws SpeedFrameworkException {
        load();
        initConnectionFactory();
        initGlobalCacheStatus();
        initProviderCache();
    }


    /**
     * Method reLoadFile
     * 重新载入配置文件
     * @throws SpeedFrameworkException
     */
    public static void reLoadFile() throws SpeedFrameworkException {
        init();
    }


    public static boolean getGlobalCacheStatus() throws SpeedFrameworkException {
        return globalCacheStatus;
    }


    public static String getProviderCache() {
        return providerCache;
    }


    public static Map getDefaultConnectionFactory() throws SpeedFrameworkException {
        if (connectionFactoryMap.isEmpty()) {
            initConnectionFactory();
        }

        if (connectionFactoryMap.size() == 0) {
            throw new SpeedFrameworkException("There is no connection-factory config information!");
        }

        Iterator it = connectionFactoryMap.keySet().iterator();

        return (Map) connectionFactoryMap.get(it.next());
    }


    public static Map getConnectionFactoryById(String id) {
        Iterator it = connectionFactoryMap.keySet().iterator();
        String   key;

        if (!id.equals("")) {
            while (it.hasNext()) {
                key = (String) it.next();

                if (key.equals(id)) {
                    return (Map) connectionFactoryMap.get(id);
                }
            }

            throw new SpeedFrameworkException("There is no match connection factory!Please check your config.");
        }

        return getDefaultConnectionFactory();
    }


    public static int getMaxConnectionCountById(String connectionFactoryId) {
        Map    connectionFactory = getConnectionFactoryById(connectionFactoryId);
        String maxCount          = (String) connectionFactory.get(ConfigureParam.MAX_COUNT);

        // return
        // Integer.valueOf(maxCount==null||maxCount.length()==0?"10":maxCount);
        return Integer.parseInt(((maxCount == null) || (maxCount.length() == 0))
                                ? "10"
                                : maxCount);
    }


    public static long getMaxWaitTimeById(String connectionFactoryId) {
        Map    connectionFactory = getConnectionFactoryById(connectionFactoryId);
        String waitTime          = (String) connectionFactory.get(ConfigureParam.TIME_OUT);

        // return
        // Long.valueOf(waitTime==null||waitTime.length()==0?"3000":waitTime);
        return Long.parseLong(((waitTime == null) || (waitTime.length() == 0))
                              ? "3000"
                              : waitTime);
    }


    protected static String getSqlPath(String key) {
        if (sqlCollection.isEmpty()) {
            initSqlCollection();
        }

        return (String) sqlCollection.get(key);
    }

    /**
     * Method getSqlMapConfigById
     * 返回SqlMapConfig对象,该对象中包含了与id对应的配置文件的信息
     * 
     * @param id
     * @return SqlMapConfig
     */
    public static SqlMapConfig getSqlMapConfigById(String id) {
        String       sqlPath      = getSqlPath(id);
        SqlMapConfig sqlMapConfig = (SqlMapConfig) sqlMapConfigCollection.get(id);

        if (sqlMapConfig == null) {
            sqlMapConfig = new SqlMapConfig(sqlPath);

            if (sqlMapConfig == null) {
                throw new SpeedFrameworkException("There is no SqlMap file named " + id);
            } else {
                sqlMapConfigCollection.put(id, sqlMapConfig);
            }
        }

        return sqlMapConfig;
    }


    public static void main(String[] args) {

        // int test = getMaxConnectionCountById("link_mysql");
        // long test1 = getMaxWaitTimeById("link_mysql");
        // System.out.println("maxcount=" + test);
        // System.out.println("timeout=" + test1);
        //
        // Map link_mysql = (Map) connectionFactoryMap.get("link_mysql");
        // Iterator it = link_mysql.keySet().iterator();
        // while (it.hasNext()) {
        // String key = (String) it.next();
        // System.out.println(key + "=" + link_mysql.get(key));
        // }
        Iterator con = connectionFactoryMap.keySet().iterator();

        while (con.hasNext()) {
            String id = (String) con.next();

            System.out.println("<connection-factory id=" + id + ">");

            Map      propertyMap = (Map) connectionFactoryMap.get(id);
            Iterator property    = propertyMap.keySet().iterator();

            while (property.hasNext()) {
                String propertyName = (String) property.next();

                System.out.println("  <property name=" + propertyName + ">" + propertyMap.get(propertyName)
                                   + "</property>");
            }

            System.out.println("</connection-factory>");
        }
    }
}

⌨️ 快捷键说明

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