package com.vci.server.omd.lifecycle.cache;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.vci.common.exception.VciExceptionTool;
|
import com.vci.server.base.exception.ExceptionLocalHandler;
|
import com.vci.server.cache.CacheNames;
|
import com.vci.server.cache.redis.RedisUtil;
|
import com.vci.server.omd.lifecycle.service.LifeCycleService;
|
import com.vci.corba.common.VCIError;
|
import com.vci.corba.omd.lcm.LifeCycle;
|
|
public class LifeCycleCacheUtil { //extends BaseCacheTimer{
|
|
private static volatile LifeCycleCacheUtil instance = null;
|
// private LifeCycle[] lifeCycles = null;
|
private String[] eventKeys = null;
|
// private Map<String, LifeCycle> lifecyleNameMap = null;
|
// private Map<String, String> keyValueMap = null;
|
|
public static String CACHENAME = "lifecycle";
|
|
private static LifeCycleCacheUtil getInstance() {
|
if (instance == null) {
|
synchronized (LifeCycleCacheUtil.class) {
|
if (instance == null) {
|
instance = new LifeCycleCacheUtil();
|
}
|
}
|
}
|
|
return instance;
|
}
|
|
private LifeCycleCacheUtil() {
|
// super(CACHENAME);
|
// try {
|
// this.setCurrentTime(this.getDataBaseCurrtenttime());
|
// lifecyleNameMap = new HashMap<String, LifeCycle>();
|
// keyValueMap = new HashMap<String, String>();
|
// initLifecyle();
|
// initLCEvent();
|
// } catch (Throwable e) {
|
// e.printStackTrace();
|
// }
|
}
|
|
public static void initCache() {
|
try {
|
clearCache();
|
|
getInstance().initLifecyle();
|
getInstance().initLCEvent();
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
|
public static void clearCache() {
|
RedisUtil.getInstance().del(CacheNames.LIFECYCLES);
|
RedisUtil.getInstance().del(CacheNames.LCEVENTS);
|
}
|
|
|
public static void setLifeCycle(LifeCycle item) {
|
if (item == null)
|
return;
|
|
String jsonObj = JSONObject.toJSONString(item);
|
|
RedisUtil.getInstance().hset(CacheNames.LIFECYCLES, item.name.toLowerCase(), jsonObj);
|
}
|
|
|
public static void setLCEvent(String name, String value) {
|
if (StringUtils.isBlank(name) || StringUtils.isBlank(value))
|
return;
|
|
RedisUtil.getInstance().hset(CacheNames.LCEVENTS, name, value);
|
}
|
|
public static void delLifeCycle(String name) {
|
if (StringUtils.isBlank(name))
|
return;
|
|
RedisUtil.getInstance().hdel(CacheNames.LIFECYCLES, name.toLowerCase());
|
}
|
|
|
/**
|
* 初始化event事件
|
*
|
* @throws Throwable
|
*
|
*/
|
private void initLCEvent() throws VCIError {
|
//String lceEventKeys[];
|
try {
|
String[] lceKeys = LifeCycleService.getInstance().getLCEventKeys();
|
//eventKeys = lceEventKeys;
|
//String[] eventValue = new String[eventKeys.length];
|
for (int i = 0; i < lceKeys.length; i++) {
|
String eventValue = LifeCycleService.getInstance().getLCEventValueByKey(lceKeys[i]);
|
//keyValueMap.put(eventKeys[i], eventValue[i]);
|
setLCEvent(lceKeys[i], eventValue);
|
}
|
} catch (Throwable e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
throw getLocalVciError("P0010LIFECYCLE-00015", e);
|
}
|
|
}
|
|
/**
|
* 初始化生命周期
|
*
|
*/
|
private void initLifecyle() throws Exception {
|
LifeCycle[] lcs = LifeCycleService.getInstance().getLifeCyles();
|
//lifeCycles = lcs;
|
for (int i = 0; i < lcs.length; i++) {
|
//lifecyleNameMap.put(lcs[i].name, lcs[i]);
|
|
setLifeCycle(lcs[i]);
|
}
|
}
|
|
/**
|
* 获取事件key
|
*
|
* @throws Throwable
|
*/
|
// public static String[] getLCEventKeys() throws Throwable {
|
// return getInstance().eventKeys
|
//// String[] cEventKeys = eventKeys;
|
//// return cEventKeys;
|
// }
|
|
/**
|
* 获取事件value
|
*
|
* @throws Throwable
|
*/
|
|
// public String getLCEventValueByKey(String key) throws Throwable {
|
// String value = keyValueMap.get(key);
|
// return value;
|
// }
|
|
// public LifeCycle[] getLifeCyles() throws VCIError {
|
// // 在调用具体方法时,先判断数据库中数据是否发生变化,若变化,则将发生变化的数据同步缓存
|
// checkCache();
|
// LifeCycle[] cLifeCycles = lifeCycles;
|
// return cLifeCycles;
|
// }
|
//
|
// public LifeCycle getLifeCycle(String name) throws VCIError {
|
// // 在调用具体方法时,先判断数据库中数据是否发生变化,若变化,则将发生变化的数据同步缓存
|
// checkCache();
|
// LifeCycle lifeCycle = null;
|
// if (lifecyleNameMap.containsKey(name)) {
|
// lifeCycle = lifecyleNameMap.get(name);
|
// } else {
|
// lifeCycle = new LifeCycle();
|
// lifeCycle.bounds = new Bound[0];
|
// lifeCycle.routes = new TransitionVO[0];
|
// }
|
//
|
// return lifeCycle;
|
// }
|
|
/****
|
* 检查缓存和数据库的一致性,若不一致,则刷新缓存
|
*
|
* @param spList
|
* @throws VCIError
|
*/
|
// private synchronized void checkCache() throws VCIError {
|
//// if (!CacheConfUtil.getEveryTimeRefresh()) {
|
//// return;
|
//// }
|
// // 在调用具体方法时,先判断数据库中数据是否发生变化,若变化,则将发生变化的数据同步缓存
|
// long cTime = this.getDataBaseCurrtenttime();
|
// //List<PLCacheTempEntity> pto = getStateLogs("lifecycle");
|
// PLCacheRecordEnt[] records = getUpdatedCacheData("lifecycle");
|
// if (records == null || records.length == 0) {
|
// if (records != null)
|
// this.setCurrentTime(cTime);
|
// return;
|
// }
|
// List<LifeCycle> spList = new ArrayList<LifeCycle>();
|
// for (int i = 0; i < lifeCycles.length; i++) {
|
// spList.add(lifeCycles[i]);
|
// }
|
//
|
// List<String> list = new ArrayList<String>();
|
// List<String> idList = new ArrayList<String>();
|
// for (int i = 0; i < records.length; i++) {
|
// idList.add(records[i].getId());
|
// list.add(records[i].getObjId());
|
// }
|
// list = getSimpleId(list); // 去除重复的id号
|
// Map<String, LifeCycle> lifeCycleIdMap = getChangeLifeCycleById(list);// 获取statepool表中所有被修改的数据
|
// for (String key : lifeCycleIdMap.keySet()) {
|
// LifeCycle lc = lifeCycleIdMap.get(key);
|
// boolean flag = true;
|
// for (int j = 0; j < spList.size(); j++) {
|
// if (spList.get(j).oid.equals(key)) {
|
// if (lc == null) {
|
// spList.remove(j);
|
// flag = false;
|
// break;
|
// } else {
|
// spList.remove(j);
|
// spList.add(lc);
|
// flag = false;
|
// break;
|
// }
|
// }
|
// }
|
// //add by caill 解决增删改后左侧树的刷新问题
|
// if(flag){
|
// if (lc != null) {
|
// spList.add(lc);
|
// }
|
// }
|
// }
|
// LifeCycle[] cLifeCycles = new LifeCycle[spList.size()];
|
// sort(spList);// 将所有的状态按照状态名称的字母排序
|
// for (int i = 0; i < spList.size(); i++) {
|
// cLifeCycles[i] = spList.get(i);
|
// }
|
// // 刷新公共变量lifeCycles和lifecyleNameMap
|
// refresh(cLifeCycles);
|
// this.setCurrentTime(cTime);
|
// }
|
//
|
// // 将所有的状态按照状态名称的字母排序
|
// private void sort(List<LifeCycle> spList) {
|
// Collections.sort(spList, new Comparator<LifeCycle>() {
|
// @Override
|
// public int compare(LifeCycle s1, LifeCycle s2) {
|
// return new String(s1.name).compareTo(new String(s2.name));
|
// }
|
// });
|
// }
|
//
|
// // 获取statepool表中所有被修改的数据
|
// private Map<String, LifeCycle> getChangeLifeCycleById(List<String> list) {
|
// Map<String, LifeCycle> lifeCycleIdMap = new HashMap<String, LifeCycle>();
|
// LifeCycle[] changedLifeCyle = new LifeCycle[list.size()];
|
// for (int i = 0; i < list.size(); i++) {
|
// LifeCycle lc = null;
|
// try {
|
// lc = LifeCycleService.getInstance().getLifeCycleByOid(
|
// list.get(i));
|
// changedLifeCyle[i] = lc;
|
// lifeCycleIdMap.put(list.get(i), lc);
|
// } catch (Exception e) {
|
// // TODO Auto-generated catch block
|
// e.printStackTrace();
|
// }
|
// }
|
// return lifeCycleIdMap;
|
// }
|
//
|
// // 获得中间表中与状态池相关的数据
|
//// private List<PLCacheTempEntity> getStateLogs(String type) throws VCIError {
|
//// List<PLCacheTempEntity> pto = null;
|
//// pto = VciCacheServerDelegate.getInstance()
|
//// .getPLCacheTempEntityByObjType(type, this.getCurrentTime());
|
//// return pto;
|
//// }
|
//
|
// // 去除中间表中重复的id并装入list
|
// private List<String> getSimpleId(List<String> list) {
|
// HashSet<String> h = new HashSet<String>(list);
|
// list.clear();
|
// list.addAll(h);
|
// return list;
|
// }
|
//
|
// // 刷新公共变量
|
// private void refresh(LifeCycle[] cLifeCycles) {
|
// lifeCycles = cLifeCycles;
|
// lifecyleNameMap.clear();// 先将lifecyleNameMap清空
|
// for (int i = 0; i < lifeCycles.length; i++) {
|
// lifecyleNameMap.put(lifeCycles[i].name, lifeCycles[i]);
|
// }
|
// }
|
//
|
//
|
// // 重新加载缓存
|
//
|
// public void reLoad() throws VCIError {
|
// try {
|
// this.setCurrentTime(this.getDataBaseCurrtenttime());
|
// lifecyleNameMap = new HashMap<String, LifeCycle>();
|
// keyValueMap = new HashMap<String, String>();
|
// initLifecyle();
|
// initLCEvent();
|
// } catch (Exception e) {
|
// // TODO Auto-generated catch block
|
// e.printStackTrace();
|
// throw getLocalVciError("P0010LIFECYCLE-00015", e);
|
// }
|
//
|
// }
|
protected VCIError getLocalVciError(String key, Throwable e) {
|
VCIError error = new VCIError(key, new String[]{VciExceptionTool.getExceptionStr(e), VciExceptionTool.getExceptionDetail(e)});
|
VCIError rsError = ExceptionLocalHandler.getInstance().getLocalString(error, "PLMLIFECYCLE");
|
return rsError;
|
}
|
}
|