`
conquer0
  • 浏览: 76579 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

缓存cassandra二

阅读更多
package com.huawei.support.cache.impl;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.huawei.support.exception.ApplicationException;

/**
* 从cacheConfig.xml中读取配置文件.
*/
public final class CacheConfigReader
{
    /**
     * sLog
     */
    private static Log sLog = LogFactory.getLog(CacheConfigReader.class);
    /**
     * 本实例
     */
    private static CacheConfigReader sInstance;
    /**
     * 最终修改时间
     */
    private static long sLastModified = -1;
    /**
     * 缓存服务器列表
     */
    private static String[] sServerList;
    /**
     * 默认Keyspace
     */
    private static String sDefaultKeyspace;
    /**
     * 默认ColumnFamily
     */
    private static String sDefaultColumnFamily;
    /**
     * DataBinding Map
     */
    private static Map<String, String> sDataBindingMap =
        new HashMap<String, String>();

    /** 默认构造函数.
     * @param aConfigFile 配置文件名
     * @throws ApplicationException ApplicationException
     */
    private CacheConfigReader(String aConfigFile)
        throws ApplicationException
    {
        try
        {
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(aConfigFile);
            XPathFactory pathFactory = XPathFactory.newInstance();
            XPath xpath = pathFactory.newXPath();
            // 解析缓存服务器列表.
            sServerList = readServerList(xpath, doc);
            // 解析默认Keyspace.
            sDefaultKeyspace = readDefaultKeyspace(xpath, doc);
            // 解析默认ColumnFamily
            sDefaultColumnFamily = readDefaultColumnFamily(xpath, doc);
            // 解析DataBindings.
            sDataBindingMap = readDataBindingMap(xpath, doc);
        }
        catch (ParserConfigurationException e)
        {
            sLog.error(e);
            throw new ApplicationException(e);
        }
        catch (XPathExpressionException e)
        {
            sLog.error(e);
            throw new ApplicationException(e);
        }
        catch (IOException e)
        {
            sLog.error(e);
            throw new ApplicationException(e);
        }
        catch (SAXException e)
        {
            sLog.error(e);
            throw new ApplicationException(e);
        }
    }

    /** 获取本实例.
     *
     * @return CacheConfigReader cache配置文件读取实例
     * @throws ApplicationException [参数说明]
     */
    public static CacheConfigReader getInstance() throws ApplicationException
    {
        // 从classpath中读取配置文件
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        URL url = loader.getResource("cacheConfig.xml");
        File configFile = new File(url.getFile());
        // 实例为null或者配置文件被修改则重新读取配置文件
        if ((null == sInstance) || (sLastModified != configFile.lastModified()))
        {
            sInstance = new CacheConfigReader(url.getFile());
            // 设置最终修改时间
            sLastModified = configFile.lastModified();
        }
        return sInstance;
    }

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics