📄 configurehelper.java
字号:
package org.speedframework.util;
import org.jdom.Document;
import org.jdom.Element;
import java.util.Hashtable;
import java.util.List;
import org.speedframework.exception.SpeedException;
import java.util.Iterator;
import java.io.InputStream;
import org.speedframework.exception.ConfigureException;
import org.jdom.input.SAXBuilder;
import org.apache.log4j.Logger;
public class ConfigureHelper {
private static Logger log=Logger.getLogger(ConfigureHelper.class);
private static SAXBuilder bu;
// private static final CachePool cp=CachePoolFactory.getProviderCache();
private static Hashtable cache_ = new Hashtable();
/**
* 提取连接类型(普通连接 数据源)
* @param doc Document
* @throws SpeedException
* @return String
*/
public static String getConnectionType(Document doc,String id) throws SpeedException {
String type = null;
Element root = doc.getRootElement();
List list=root.getChildren("connection-factory");
//Element factory = root.getChild("connection-factory");
if(list.size()==0){
throw new SpeedException("no connectin configure found");
}if(list.size()>1 && id.equals("")){
throw new SpeedException("The id of connection-factory doesn't specify");
}
Element factory=null;
if(!id.equals("")){
for (int i = 0; i < list.size(); i++) {
Element e = (Element) list.get(i);
if (e.getAttribute("id").getValue().equals(id)) {
factory = e;
}
}
}else{
factory=root.getChild("connection-factory");
}
if(factory==null){
throw new SpeedException("no connection configure of "+ id +" found");
}
List child_list = factory.getChildren();
if (child_list.size() == 0) {
throw new SpeedException("no connection configure found");
}
Iterator it = child_list.iterator();
while (it.hasNext()) {
Element node = (Element) it.next();
String value = node.getAttribute("name").getValue();
if (value.equals(ConfigureParam.DRIVER)) {
type = "connection";
break;
}
if (value.equals(ConfigureParam.JNDI_NAME)) {
type = "datasource";
break;
}
}
return type;
}
/**
* 提取连接类型(普通连接 数据源)单一连接,默认
* @param doc Document
* @throws SpeedException
* @return String
*/
public static String getConnectionType(Document doc) throws SpeedException {
return getConnectionType(doc,"");
}
/**
* 读取配置文件
* @param sourceFile String
* @throws ConfigureException
* @return Document
*/
public static Document loadFile(String sourceFile) throws ConfigureException {
Document conf;
try{
// cp.newInstance("speedCache");
if (cache_.get("con_conf") != null) {
conf = (Document) cache_.get("con_conf");
}
else {
bu = new SAXBuilder();
conf = bu.build(getResourceAsStream(sourceFile));
cache_.put("con_conf", conf);
log.info("success to load " + sourceFile);
}
}catch(Exception e){
throw new ConfigureException(e.getMessage());
}
return conf;
}
/**
* 读取源文件
* @param sourceFile String
* @throws ConfigureException
* @return InputStream
*/
private static InputStream getResourceAsStream(String sourceFile) throws
ConfigureException {
String stripped = sourceFile.startsWith("/") ?
sourceFile.substring(1) : sourceFile;
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader != null) {
stream = classLoader.getResourceAsStream(stripped);
}
if(stream==null){
ConfigureHelper.class.getResourceAsStream(sourceFile);
}
if(stream==null){
stream=ConfigureHelper.class.getResourceAsStream(stripped);
}
if(stream==null){
throw new ConfigureException(sourceFile +" not found");
}
return stream;
}
/**
* 读取用户配置缓存状态
* @return boolean
*/
public static boolean getGlobalCacheStatus() throws ConfigureException {
Document doc=loadFile("/speed.cfg.xml");
Element root=doc.getRootElement();
//Element factory = root.getChild("connection-factory");
Element cache=root.getChild("speed-global-cache");
String status=cache.getValue();
if(status.equals("true")){
return true;
}else if(status.equals("false")){
return false;
}else{
throw new ConfigureException("status no spacify,check the configure file");
}
}
/**
* Provider Cache
* @return
*/
public static String getProviderCache(){
Document doc=loadFile("/speed.cfg.xml");
Element root=doc.getRootElement();
//Element factory = root.getChild("connection-factory");
Element cache=root.getChild("speed-cache-provider");
String provider=cache.getValue();
if(provider==null || provider.equals("") || provider.toLowerCase().equals("default")){
provider="jcs";
}
return provider.toLowerCase();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -