Java tutorial
/* Copyright (C) 2006 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.notify.util; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.bean.HinemosModuleConstant; import com.clustercontrol.commons.util.AbstractCacheManager; import com.clustercontrol.commons.util.CacheManagerFactory; import com.clustercontrol.commons.util.ICacheManager; import com.clustercontrol.commons.util.ILock; import com.clustercontrol.commons.util.ILockManager; import com.clustercontrol.commons.util.JpaTransactionManager; import com.clustercontrol.commons.util.LockManagerFactory; import com.clustercontrol.notify.model.NotifyRelationInfo; import com.clustercontrol.util.HinemosTime; /** * ID?ID????? * ???????? */ public class NotifyRelationCache { private static Log m_log = LogFactory.getLog(NotifyRelationCache.class); private static final ILock _lock; static { ILockManager lockManager = LockManagerFactory.instance().create(); _lock = lockManager.create(NotifyRelationCache.class.getName()); try { _lock.writeLock(); HashMap<String, List<NotifyRelationInfo>> cache = getCache(); if (cache == null) { // not null when clustered refresh(); } } finally { _lock.writeUnlock(); } } @SuppressWarnings("unchecked") private static HashMap<String, List<NotifyRelationInfo>> getCache() { ICacheManager cm = CacheManagerFactory.instance().create(); Serializable cache = cm.get(AbstractCacheManager.KEY_NOTIFY_RELATION); if (m_log.isDebugEnabled()) m_log.debug("get cache " + AbstractCacheManager.KEY_NOTIFY_RELATION + " : " + cache); return cache == null ? null : (HashMap<String, List<NotifyRelationInfo>>) cache; } private static void storeCache(HashMap<String, List<NotifyRelationInfo>> newCache) { ICacheManager cm = CacheManagerFactory.instance().create(); if (m_log.isDebugEnabled()) m_log.debug("store cache " + AbstractCacheManager.KEY_NOTIFY_RELATION + " : " + newCache); cm.store(AbstractCacheManager.KEY_NOTIFY_RELATION, newCache); } /** * ID???ID?? * * @param notifyGroupId ID * @return ID???? */ public static List<String> getNotifyIdList(String notifyGroupId) { List<String> list = new ArrayList<String>(); for (NotifyRelationInfo info : getNotifyList(notifyGroupId)) { list.add(info.getNotifyId()); } Collections.sort(list); return list; } /** * ID????? * * @param notifyGroupId ID * @return ???? */ public static List<NotifyRelationInfo> getNotifyList(String notifyGroupId) { try { { HashMap<String, List<NotifyRelationInfo>> cache = getCache(); // ????????????????????????? // (?????????????????????????) List<NotifyRelationInfo> notifyList = cache.get(notifyGroupId); if (notifyList != null) { return notifyList; } // ???????????? if (onCache(notifyGroupId)) { return new ArrayList<NotifyRelationInfo>(); } } m_log.debug("getNotifyIdList() : Job Master or Job Session. " + notifyGroupId); List<NotifyRelationInfo> nriList = QueryUtil.getNotifyRelationInfoByNotifyGroupId(notifyGroupId); return nriList; } catch (Exception e) { m_log.warn("getNotifyList() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); return new ArrayList<NotifyRelationInfo>(); // ?? } } /** * ????true? * @param notifyGroupId * @return */ private static boolean onCache(String notifyGroupId) { return (notifyGroupId.startsWith(HinemosModuleConstant.JOB_SESSION + "-") == false && notifyGroupId.startsWith(HinemosModuleConstant.JOB_MST + "-") == false); } public static void refresh() { JpaTransactionManager jtm = new JpaTransactionManager(); if (!jtm.isNestedEm()) { m_log.warn("refresh() : transactioin has not been begined."); jtm.close(); return; } try { _lock.writeLock(); long start = HinemosTime.currentTimeMillis(); new JpaTransactionManager().getEntityManager().clear(); HashMap<String, List<NotifyRelationInfo>> notifyMap = new HashMap<String, List<NotifyRelationInfo>>(); List<NotifyRelationInfo> nriList = null; try { nriList = QueryUtil.getAllNotifyRelationInfoWithoutJob(); } catch (Exception e) { m_log.warn("refresh() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); return; } for (NotifyRelationInfo nri : nriList) { String notifyGroupId = nri.getId().getNotifyGroupId(); // ??????????? if (onCache(notifyGroupId)) { List<NotifyRelationInfo> notifyList = notifyMap.get(notifyGroupId); if (notifyList == null) { notifyList = new ArrayList<NotifyRelationInfo>(); notifyList.add(nri); notifyMap.put(notifyGroupId, notifyList); } else { notifyList.add(nri); } } } for (List<NotifyRelationInfo> notifyList : notifyMap.values()) { if (notifyList == null) { continue; } Collections.sort(notifyList); } storeCache(notifyMap); m_log.info("refresh NotifyRelationCache. " + (HinemosTime.currentTimeMillis() - start) + "ms. size=" + notifyMap.size()); } finally { _lock.writeUnlock(); } } }