//Source file: D://dhrm//src//com//common//SysCommonEnvMap.java
package com.common;
import java.util.*;
import com.common.exception.CommonEnvException;
import java.sql.*;
/**
单例类,系统运行过程中需要用到的配置信息映射对象
@author zbb
*/
public class SysCommonEnvMap
{
/**
映射信息存储对象
*/
private Map envMap;
private static SysCommonEnvMap sysEnvMap ;
/**
* 取得映射信息对象
*/
public static SysCommonEnvMap getInstance() throws Exception
{
if(sysEnvMap == null)
{
sysEnvMap = new SysCommonEnvMap();
}
return sysEnvMap;
}
}
protected SysCommonEnvMap() throws Exception
{
this.init();
}
/**
通过关键字key,取得相应的值
*/
public String get(String key) throws Exception
{
//检测
if(this.getIsNeedToRefreshByCtx())
{
this.refresh();
}
return ((String)this.envMap.get(key));
}
/**
* 以Map对象的形式返回对乡存储的所有信息
*/
public Map getMap() throws Exception
{
//检测
if(this.getIsNeedToRefreshByCtx())
{
this.refresh();
}
return this.envMap;
}
public void refresh() throws Exception
{
CommonTools.log("Refresh :"+this.getClass().getName());
Connection conn = null;
Statement sta = null;
ResultSet rs = null;
this.envMap = new HashMap();
try
{
conn = CommonTools.getConnection("dhrm.common.Proxy.dataSource.ProxyDataSource");
sta = conn.createStatement();
rs = sta.executeQuery("Select * from STPARAMETER");
while(rs.next())
{
this.envMap.put(rs.getString("key"),rs.getString("value"));
}
CommonTools.setRefreshComplated(this.getClass());
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
throw new CommonEnvException("环境变量映射初始化失败");
}
finally
{
CommonTools.closeStatement(sta);
CommonTools.closeResultSet(rs);
CommonTools.closeConnection(conn);
}
}
/**
初始化对象包含的映射信息
*/
public void init() throws Exception
{
Connection conn = null;
Statement sta = null;
ResultSet rs = null;
this.envMap = new HashMap();
try
{
conn = CommonTools.getConnection("dhrm.common.Proxy.dataSource.ProxyDataSource");
sta = conn.createStatement();
rs = sta.executeQuery("Select * from STPARAMETER");
while(rs.next())
{
this.envMap.put(rs.getString("key"),rs.getString("value"));
}
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
throw new CommonEnvException("环境变量映射初始化失败");
}
finally
{
CommonTools.closeStatement(sta);
CommonTools.closeResultSet(rs);
CommonTools.closeConnection(conn);
}
}
}
Top
3 楼zhangbaobin(保斌)回复于 2004-07-27 17:09:49 得分 0 调用的时候
SysCommonEnvMap temp = SysCommonEnvMap .getInstance();
temp.getXXXX();
如果想定时刷新,里面再加一个timer定时器就可以了