📄 config.java
字号:
package com.easyjf.cache;
import com.easyjf.cache.store.*;
import org.dom4j.*;
import org.dom4j.io.*;
//~--- JDK imports ------------------------------------------------------------
import java.util.*;
//~--- classes ----------------------------------------------------------------
/**
*
* <p>
* Title:通过XML文件读取Cache配置文件,配置文件信息主要用于创建ICache对象
* </p>
*
* <p>
* Description: <easyjf-cache> <cache name="cache名称" storePolicy="存诸策略"
* maxElements="最大元素" expiredInterval="最大超时" type="chche类" /> </easyjf-cache
* </p>
*
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* <p>
* Company: EasyJF开源团队-EasyDBO项目组
* </p>
*
* @author 大峡
* @version 1.0
*/
public class Config {
private static Config singleton;
// ~--- fields -------------------------------------------------------------
private Map caches = new HashMap();
private ICache defaultCache;// 默认缓存
private String fileName;// 配置文件名
// ~--- constructors -------------------------------------------------------
public Config(String fileName) {
this.fileName = fileName;
}
// ~--- methods ------------------------------------------------------------
/**
* 初始化
*/
private void init() {
Document doc = null;
if (fileName != null) {
SAXReader reader = new SAXReader();
try {
doc = reader.read(Config.class.getResourceAsStream(fileName));
} catch (Exception e) {
System.out.println("配置文件错误:" + e);
e.printStackTrace();
}
if (doc != null) {
List list = doc.selectNodes("/easyjf-cache/cache");
if (list != null) {
for (int i = 0; i < list.size(); i++) {
org.dom4j.Element el = (org.dom4j.Element) list.get(i);
String name = el.attributeValue("name");
int storePolicy = StorePolicy.get(el
.attributeValue("storePolicy"));
int maxElements = Integer.parseInt(el
.attributeValue("maxElements"));
long expiredInterval = Long.parseLong(el
.attributeValue("expiredInterval"));
ICache cache = new EasyCache(name, storePolicy,
maxElements, expiredInterval);
if (i == 0) {
this.defaultCache = cache;
}
caches.put(name, cache);
}
}
}
}
}
/**
* 获取Cache
*
* @param name
* String 缓存名称
* @return ICache
*/
public ICache getCache(String name) {
return (ICache) caches.get(name);
}
/**
*
* @return Set
*/
public Set getCacheKeySet() {
return caches.keySet();
}
/**
*
* @return ICache
*/
public ICache getDefaultCache() {
return defaultCache;
}
/**
*
* @return Config
*/
public static Config getInstance() {
if (singleton == null) {
singleton = new Config("/easyjf-cache.xml");
singleton.init();
}
return singleton;
}
/**
*
* @param fileName
* String
* @return Config
*/
public static Config getInstance(String fileName) {
if (singleton == null) {
singleton = new Config(fileName);
singleton.init();
}
return singleton;
}
/**
* @param args
*/
public static void main(String[] args) {
Config.getInstance("/easyjf-cache.xml");
System.out.println(Config.getInstance().defaultCache.getName());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -