システム構成メモリキャッシュシステムコンフィギュレーション

10239 ワード

まずデータベースから構成情報を取得し、データベースがなければpropertiesファイルから取得します.
頻繁にデータベースを読み込む必要はありません.
 
静的HashMapで実現した.
/**
 *  
 *
 */
public class SystemConfiguration {

	private static final Logger logger = LoggerFactory.getLogger(SystemConfiguration.class);
	
	/**
	 * sysconfig.properties 
	 */
	private static Properties sysconfigprop;
	
	/**
	 * SYS_CONFIG 
	 */
	private static Map<String, String> sysconfigdb;

	static {
		sysconfigprop = new Properties();
		try {
			sysconfigprop.load(SystemConfiguration.class.getClassLoader()
					.getResourceAsStream("sysconfig.properties"));
		} catch (IOException e) {
			logger.error("load sysconfig.properties failed...");
			e.printStackTrace();
		}
		loadDatabase();
	}
	
	/**
	 *  
	 * com.vispractice.soa.lightesb.common.listener.EsbInitListener 
	 * 
	 */
	private static void loadDatabase() {
		logger.debug("loadDatabase begin...");
		
		sysconfigdb = new HashMap<String, String>();
		//  spring bean 
		WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		ISysConfigDao sysconfigdao = (ISysConfigDao) wac.getBean("iSysConfigDao");
		List<SysConfig> config = sysconfigdao.getAll();
		
		logger.debug("config [db] list :");
		
		for(SysConfig c : config){
			if(Constants.ENABLED_FLAG_Y.equals(c.getEnabledFlag())){
				logger.debug(c.toString());
				
				sysconfigdb.put(c.getConfigName(), c.getConfigValue());
			}
		}
		logger.debug("loadDatabase end...");
	}

	/**
	 *  
	 *  , 
	 * 
	 * @param name
	 * @return
	 */
	public static String getConfigByName(String name) {
		String config = sysconfigdb.get(name);
		if(config == null){
			config = sysconfigprop.getProperty(name);
			
			logger.debug("this config from properties... " + name + "=" + config);
		}
		return config;
	}
	
	/**
	 *  
	 * 
	 * @param config
	 */
	public static void addOrUpdateToSysConfigDb(Map<String, String> config){
		logger.debug("addOrUpdateToSysConfigDb : " + config);
		
		//  
		synchronized (sysconfigdb){
			sysconfigdb.putAll(config);
		}
		
		logger.debug("sysconfigdb : " + sysconfigdb);
	}
	
	/**
	 *  
	 * 
	 * @param configName
	 */
	public static void removeFromSysConfigDb(List<String> configName){
		logger.debug("removeFromSysConfigDb : " + configName);
		
		//  
		synchronized (sysconfigdb){
			for(String c : configName){
				sysconfigdb.remove(c);
			}
		}
		
		logger.debug("sysconfigdb : " + sysconfigdb);
	}
	
}

 
呼び出し:
String serverHome = SystemConfiguration.getConfigByName(Constants.MIDDLEWARE_HOME); //serverHome
String proxyBuildPath = SystemConfiguration.getConfigByName(Constants.SRV_PKG_PATH); //proxyBuildPath
String address = SystemConfiguration.getConfigByName(Constants.SERVER_ADDRESS);

 ..
 
例2:
(各種スイッチもキャッシュに入れ、使用時に再照会する必要はありません)
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  
 *
 */
public class SysConfigCache {

	private static final Logger logger = LoggerFactory.getLogger(SysConfigCache.class);
	/**
	 * sysconfig.properties 
	 */
	private static Properties sysconfigprop;
	/**
	 * SYS_CONFIG 
	 */
	private static Map<String, String> sysconfigdb = new Hashtable<String, String>();
	/**
	 *  
	 */
	private static boolean logSwitch = true;
	
	private static boolean inited = false;
	
	private static boolean parameterCheck = true;
	
	private static boolean serviceAccess = false;

	public final static String LOG_SWITCH="log.switch";
	public final static String SRV_PARAMETER_CHECK_ACCESS="SRV_PARAMETER_CHECK_ACCESS";
	public final static String SERVICE_ACCESS = "SERVICE_ACCESS";
	
	static {
		sysconfigprop = new Properties();
		try {
			InputStream in = SysConfigCache.class.getClassLoader().getResourceAsStream("sysconfig.properties");
			if(in!=null){
				sysconfigprop.load(in);
				setProperty(LOG_SWITCH,sysconfigprop.getProperty(LOG_SWITCH));
				inited = true;
			}else{
				logger.error("failed to found sysconfig.properties file ");
			}
			
		} catch (IOException e) {
			logger.error("load sysconfig.properties failed...",e);
		}
	}
	
	/**
	 *  
	 * @param key
	 * @param value
	 */
	public static void putSysConfigDB(String key,String value){
		if(sysconfigdb!=null){
			logger.debug("put ["+key+":"+value+"] into SysConfigCache.");
			sysconfigdb.put(key, value);
			setProperty(key,value);
		}
	}
	
	/**
	 *  
	 *  , 
	 * 
	 * @param name
	 * @return
	 */
	public static String getConfigByName(String name) {
		String config = sysconfigdb.get(name);
		if(config == null){
			config = sysconfigprop.getProperty(name);
			logger.debug("this config from properties... " + name + "=" + config);
		}
		return config;
	}
	
	/**
	 *  
	 * 
	 * @param configName
	 */
	public static void removeFromSysConfigDb(List<String> configName){
		logger.debug("removeFromSysConfigDb : " + configName);
		//  
		synchronized (sysconfigdb){
			for(String c : configName){
				logger.debug("remove ["+c+"] from SysConfigCache.");
				sysconfigdb.remove(c);
				setProperty(c,null);
			}
		}
		logger.debug("sysconfigdb : " + sysconfigdb);
	}
	
	/**
	 *  
	 * 
	 * @param configName
	 */
	public static void removeFromSysConfigDb(String configName){
		logger.debug("remove ["+configName+"] from SysConfigCache.");
		//  
		synchronized (sysconfigdb){
				sysconfigdb.remove(configName);
				setProperty(configName,null);
		}
		logger.debug("sysconfigdb : " + sysconfigdb);
	}
	
	private static void setProperty(String key,String value){
		if(SysConfigCache.LOG_SWITCH.equals(key)){
			SysConfigCache.setLogSwitch("1".equals(SysConfigCache.getConfigByName(SysConfigCache.LOG_SWITCH)));
		}
		if(SysConfigCache.SRV_PARAMETER_CHECK_ACCESS.equals(key)){
			SysConfigCache.setParameterCheck("1".equals(SysConfigCache.getConfigByName(SysConfigCache.SRV_PARAMETER_CHECK_ACCESS)));
		}
		
		if(SysConfigCache.SERVICE_ACCESS.equals(key)){
			SysConfigCache.setServiceAccess("1".equals(SysConfigCache.getConfigByName(SysConfigCache.SERVICE_ACCESS)));
		}
	}
	

	public static boolean isLogSwitch() {
		return logSwitch;
	}

	public static void setLogSwitch(boolean logSwitch) {
		SysConfigCache.logSwitch = logSwitch;
	}

	public static boolean isInited() {
		return inited;
	}

	public static void setInited(boolean inited) {
		SysConfigCache.inited = inited;
	}

	public static boolean isParameterCheck() {
		return parameterCheck;
	}

	public static void setParameterCheck(boolean parameterCheck) {
		SysConfigCache.parameterCheck = parameterCheck;
	}

	public static boolean isServiceAccess() {
		return serviceAccess;
	}

	public static void setServiceAccess(boolean serviceAccess) {
		SysConfigCache.serviceAccess = serviceAccess;
	}
	
}

 
例3:
単純Cache
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  
 *
 */
public class ServiceDataCache {

	private static final Logger logger = LoggerFactory.getLogger(ServiceDataCache.class);
	private static HashMap<String,List> cache = new HashMap<String,List>();
	public static boolean checkAccess = false;

	/**
	 *  
	 * @param serviceNameEn
	 * @param paramsList
	 */
	public static void setCache(String serviceNameEn, List paramsList){
		logger.info("set Data to Cache begin..");
		if(cache != null){
			if(!cache.containsKey(serviceNameEn)){
				cache.put(serviceNameEn, paramsList);			
			}
		}	
		
		logger.info("set Data to Cache end.");
	}
	
	
	/**
	 *  
	 * @param serviceNameEn
	 * @return
	 */
	public static List GetCache(String serviceNameEn){
		logger.info("Get Data from Cache begin.");
		if(cache != null){
			if(cache.containsKey(serviceNameEn)){
				return cache.get(serviceNameEn);				
			}
			
		}
		return null;
	}
	
	
	/**
	 *  
	 * @param serviceNameEn
	 * @return
	 */
	public static boolean checkCacheExist(String serviceNameEn){
		logger.info("check Cache Exist begin.");
		if(cache != null){
			return cache.containsKey(serviceNameEn.trim());			
		}
		return false;
	}
}

 
 
..