缓存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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值