package com.vci.server.omd.versionrule.cache; import java.io.IOException; import java.sql.SQLException; import org.apache.commons.lang3.StringUtils; import org.dom4j.DocumentException; import com.alibaba.fastjson.JSONObject; import com.vci.common.exception.VciExceptionTool; import com.vci.corba.common.VCIError; import com.vci.corba.omd.vrm.VersionRule; 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.versionrule.service.VRService; public class VRServerCacheUtil { //extends BaseCacheTimer{ private static volatile VRServerCacheUtil instance = null; // private VersionRule[] versionRules = null; // private Map versionRuleMap = null; // // public static String CACHENAME = "versionrule"; public static void initCache() { try { clearCache(); getInstance().initVerRules(); } catch (VCIError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void clearCache() { RedisUtil.getInstance().del(CacheNames.VERRULES); } public static void setVersionRule(VersionRule item) { if (item == null) return; String jsonObj = JSONObject.toJSONString(item); RedisUtil.getInstance().hset(CacheNames.VERRULES, item.name.toLowerCase(), jsonObj); } public static void delVersionRule(String name) { if (StringUtils.isBlank(name)) return; RedisUtil.getInstance().hdel(CacheNames.VERRULES, name.toLowerCase()); } private VRServerCacheUtil() { // super(CACHENAME); // try { // this.setCurrentTime(System.currentTimeMillis()); // versionRuleMap = new HashMap(); // initAttribute(); // } catch (Throwable e) { // e.printStackTrace(); // } } private static VRServerCacheUtil getInstance() { if (instance == null) { instance = new VRServerCacheUtil(); } return instance; } private void initVerRules() throws VCIError, SQLException, IOException, DocumentException { VersionRule[] vrs = VRService.getInstance().getVersionRules(); //versionRules = vrs; for (int i = 0; i < vrs.length; i++) { //versionRuleMap.put(vrs[i].name, vrs[i]); setVersionRule(vrs[i]); } } // public void reloadCache() throws VCIError { // try { // this.setCurrentTime(System.currentTimeMillis()); // versionRuleMap = new HashMap(); // initAttribute(); // } catch (Throwable e) { // e.printStackTrace(); // throw getLocalVciError("P0010VERSION-00008", e); // } // } // public List needUpdateCache(String objType) throws VCIError { // if (!CacheConfUtil.getEveryTimeRefresh()) { // return null; // } // List list = VciCacheServerDelegate // .getInstance().getPLCacheTempEntityByObjType(objType, this.getCurrentTime()); // return list; // } // // public void arraySort() { // List list = Arrays.asList(versionRules); // Collections.sort(list, new Comparator(){ // @Override // public int compare(VersionRule v1, VersionRule v2) { // return new String(v1.name).compareTo(new String(v2.name)); // } // }); // versionRules = list.toArray(new VersionRule[list.size()]); // } // // public VersionRule[] getVersionRules() throws VCIError { // refreshCache(); // return versionRules; // } // private synchronized void refreshCache() throws VCIError { // String objType = "versionrule"; // long cTime = System.currentTimeMillis(); // //List list = needUpdateCache(objType); // PLCacheRecordEnt[] list = getUpdatedCacheData(objType); // if (list != null) { // if (list.length > 0) { // updateCache(objType, list, cTime); // } // this.setCurrentTime(cTime); // } // } // public VersionRule getVersionRule(String name) throws VCIError { // refreshCache(); // if (versionRuleMap.containsKey(name)) { // return versionRuleMap.get(name); // } // return new VersionRule(); // } // // public boolean updateCache(String objType, PLCacheRecordEnt[] cacheTempList, long cTime) throws VCIError { // boolean flag = false; // try { // List list = new ArrayList(); // for (int i = 0 ; i < versionRules.length ; i++) { // list.add(versionRules[i]); // } // List idList = new ArrayList(); // for (PLCacheRecordEnt cache : cacheTempList) { // idList.add(cache.getId()); // VersionRule vr = VRService.getInstance().getVersionRuleByOid(cache.getObjId()); // if ("creat".equals(cache.getOperateType())) { // list.add(vr); // versionRuleMap.put(vr.name, vr); // } else if ("edit".equals(cache.getOperateType())) { // list = removeListItemByOid(cache.getObjId(), list); // versionRuleMap = removeMapItemByOid(cache.getObjId(), versionRuleMap); // list.add(vr); // versionRuleMap.put(vr.name, vr); // } else if ("delete".equals(cache.getOperateType())) { // versionRuleMap = removeMapItemByOid(cache.getObjId(), versionRuleMap); // list = removeListItemByOid(cache.getObjId(), list); // } // } // versionRules = list.toArray(new VersionRule[list.size()]); // this.setCurrentTime(cTime); // arraySort(); // } catch (Throwable e) { // e.printStackTrace(); // throw getLocalVciError("P0010VERSION-00008", e); // } // flag = true; // return flag; // } 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, "PLMVERSION"); return rsError; } // private Map removeMapItemByOid(String oid, Map map) { // for (Iterator it = map.keySet().iterator();it.hasNext();) { // VersionRule vr = map.get(it.next()); // if (oid.equals(vr.oid)) { // map.remove(vr); // } // } // return map; // } // // private List removeListItemByOid(String oid, List list) { // Iterator it = list.iterator(); // for (;it.hasNext();) { // VersionRule vr = it.next(); // if (oid.equals(vr.oid)) { // it.remove(); // } // } // return list; // } }