package com.walker.infrastructure.arguments;
import com.walker.infrastructure.arguments.support.DefaultVariable;
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
/**
* 抽象的参数管理器实现。
* @author shikeying
*
*/
public abstract class AbstractArgumentsManager implements ArgumentsManager {
protected Logger logger = LoggerFactory.getLogger(getClass());
private Object source;
/* 分组集合 */
private Map groupMap = new TreeMap();
/* 变量集合与组ID映射关系:key=groupId, value=variable ID set */
private ConcurrentHashMap> variableMap = new ConcurrentHashMap>(8);
private ConcurrentHashMap allVars = new ConcurrentHashMap(32);
private Object lock = new Object();
@Override
public void setSource(Object source){
assert (source != null);
this.source = source;
}
@Override
public void afterPropertiesSet() throws Exception{
// if(this.source == null){
// throw new ArgumentsException("parameter: source is not found!");
// }
List groupList = null;
try{
groupList = load(source);
} catch(Exception ex){
throw new ArgumentsException("业务加载配置参数失败:" + ex.getMessage(), ex);
}
initGroup(groupList);
if(logger.isDebugEnabled()){
logger.debug("~~~~~~~~~~~~~~~~~ 系统加载所有配置参数-start ~~~~~~~~~~~~~~~~~");
for(Variable v : allVars.values()){
logger.debug(v.toString());
}
logger.debug("~~~~~~~~~~~~~~~~~ 系统加载所有配置参数-end ~~~~~~~~~~~~~~~~~");
}
}
private void initGroup(List groupList){
if(groupList != null){
Collections.sort(groupList);
for(Group g : groupList){
groupMap.put(g.getId(), g);
initVariableInGroup(g);
}
}
}
private void initVariableInGroup(Group group){
List varList = group.getChildren();
if(varList != null && varList.size() > 0){
List varIds = new ArrayList(8);
for(Variable v : varList){
allVars.put(v.getId(), v);
varIds.add(v.getId());
}
variableMap.put(group.getId(), varIds);
} else
variableMap.put(group.getId(), null);
}
/**
* 加载具体的参数数据,并返回分组集合信息,分组中包含了可变参数数据。
* 子类实现具体加载过程。
* @param source 输入参数,由业务设置加载数据的原始参数,如:xml文件、数据源等。
* @return
*/
protected abstract List load(Object source) throws Exception;
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 以下为系统提供的标准API
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@Override
public Variable getVariable(String id) {
assert (StringUtils.isNotEmpty(id));
Variable var = allVars.get(id);
if(var == null)
throw new ElementNotFoundException("variable not found: " + id);
return var;
}
@Override
public void persist(String variableId, Object value) {
if(groupMap.size() > 1)
throw new ArgumentsException("存在多个分组,请调用方法: persist(String groupId, String variableId, Object value)");
String groupId = groupMap.keySet().iterator().next();
persist(groupId, variableId, value);
}
@Override
public void persist(String groupId, String variableId, Object value) {
assert (StringUtils.isNotEmpty(groupId));
assert (value != null);
/* 更新实体中的数据 */
try {
saveVariable(groupId, variableId, value);
} catch (Exception e) {
throw new ArgumentsException("更新可变参数到业务中出现错误: " + e.getMessage(), e);
}
synchronized (lock) {
updateCache(groupId, variableId, value);
}
}
private void updateCache(String groupId, String variableId, Object value){
if(groupMap.get(groupId) == null)
throw new IllegalArgumentException("not found group id: " + groupId);
List existVarSet = variableMap.get(groupId);
if(existVarSet == null)
throw new ArgumentsException("not found variable in cache: " + variableId);
if(existVarSet.contains(variableId)){
// 暂时先强制转换成具体对象,调用更改值的方法,后续扩充时需要注意,可能会不合适。
DefaultVariable current = (DefaultVariable)allVars.get(variableId);
current.setValue(value);
} else
throw new ElementNotFoundException("var id: " + variableId);
}
@Override
public void persist(List