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.monitor.run.factory; import java.util.Calendar; import java.util.List; import java.util.Random; import javax.persistence.EntityExistsException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.accesscontrol.bean.PrivilegeConstant.ObjectPrivilegeMode; import com.clustercontrol.commons.scheduler.TriggerSchedulerException; import com.clustercontrol.commons.util.HinemosEntityManager; import com.clustercontrol.commons.util.JpaTransactionManager; import com.clustercontrol.commons.util.NotifyGroupIdGenerator; import com.clustercontrol.fault.HinemosUnknown; import com.clustercontrol.fault.InvalidRole; import com.clustercontrol.fault.MonitorDuplicate; import com.clustercontrol.fault.MonitorNotFound; import com.clustercontrol.fault.NotifyNotFound; import com.clustercontrol.monitor.run.model.MonitorInfo; import com.clustercontrol.monitor.run.util.QueryUtil; import com.clustercontrol.notify.factory.ModifyNotifyRelation; import com.clustercontrol.notify.model.MonitorStatusEntity; import com.clustercontrol.notify.model.NotifyHistoryEntity; import com.clustercontrol.notify.model.NotifyRelationInfo; import com.clustercontrol.notify.session.NotifyControllerBean; import com.clustercontrol.notify.util.MonitorStatusCache; import com.clustercontrol.plugin.impl.SchedulerPlugin.TriggerType; import com.clustercontrol.util.HinemosTime; /** * ??? * <p> * ??????????? * * @version 4.0.0 * @since 2.0.0 */ abstract public class ModifyMonitor { /** ? */ private static Log m_log = LogFactory.getLog(ModifyMonitor.class); /** ? */ protected MonitorInfo m_monitor; /** */ protected MonitorInfo m_monitorInfo; /** */ protected boolean m_isModifyFacilityId = false; /** */ private boolean m_isModifyRunInterval = false; /** ?? */ private boolean m_isModifyEnableFlg = false; /** ID */ private String m_monitorTypeId; /** ID */ private String m_monitorId; /** * ??? */ protected abstract TriggerType getTriggerType(); /** * ????? */ protected abstract int getDelayTime(); /** * ????????? * * @param info * @param user ?? * @return ???????</code> true </code> * @throws MonitorNotFound * @throws MonitorDuplicate * @throws TriggerSchedulerException * @throws HinemosUnknown * @throws InvalidRole * * @see #addMonitorInfo(String) */ public boolean add(MonitorInfo info, String user) throws MonitorNotFound, MonitorDuplicate, TriggerSchedulerException, HinemosUnknown, InvalidRole { m_monitorInfo = info; boolean result = false; try { // result = addMonitorInfo(user); } catch (EntityExistsException e) { throw new MonitorDuplicate(e.getMessage(), e); } catch (MonitorNotFound e) { throw e; } catch (TriggerSchedulerException e) { throw e; } catch (HinemosUnknown e) { throw e; } catch (InvalidRole e) { throw e; } return result; } /** * ???? * <p> * <ol> * <li>?????????</li> * <li>??????????????{@link #addJudgementInfo()}</li> * <li>????????????????{@link #addCheckInfo()}</li> * <li>Quartz????/???</li> * </ol> * * @param user ?? * @return ???????</code> true </code> * @throws MonitorNotFound * @throws TriggerSchedulerException * @throws EntityExistsException * @throws HinemosUnknown * @throws InvalidRole * * @see com.clustercontrol.monitor.run.ejb.entity.MonitorInfoBean * @see #addJudgementInfo() * @see #addCheckInfo() * @see com.clustercontrol.monitor.run.factory.ModifySchedule#addSchedule(MonitorInfo, String, Calendar) */ protected boolean addMonitorInfo(String user) throws MonitorNotFound, TriggerSchedulerException, EntityExistsException, HinemosUnknown, InvalidRole { long now = HinemosTime.currentTimeMillis(); JpaTransactionManager jtm = new JpaTransactionManager(); try { // ?? jtm.checkEntityExists(MonitorInfo.class, m_monitorInfo.getMonitorId()); m_monitorInfo.setDelayTime(getDelayTime()); m_monitorInfo.setNotifyGroupId(NotifyGroupIdGenerator.generate(m_monitorInfo)); m_monitorInfo.setRegDate(now); m_monitorInfo.setRegUser(user); m_monitorInfo.setTriggerType(getTriggerType().name()); m_monitorInfo.setUpdateDate(now); m_monitorInfo.setUpdateUser(user); jtm.getEntityManager().persist(m_monitorInfo); // ? String notifyGroupId = NotifyGroupIdGenerator.generate(m_monitorInfo); m_monitorInfo.setNotifyGroupId(notifyGroupId); if (m_monitorInfo.getNotifyRelationList() != null && m_monitorInfo.getNotifyRelationList().size() > 0) { for (NotifyRelationInfo notifyRelationInfo : m_monitorInfo.getNotifyRelationList()) { notifyRelationInfo.setNotifyGroupId(notifyGroupId); } // new ModifyNotifyRelation().add(m_monitorInfo.getNotifyRelationList()); } // if (addJudgementInfo()) { // ?? if (addCheckInfo()) { // Quartz?(runInterval = 0 -> ???) if (m_monitorInfo.getRunInterval() > 0) { ModifySchedule quartz = new ModifySchedule(); quartz.updateSchedule(m_monitorInfo.getMonitorId()); } return true; } } return false; } catch (EntityExistsException e) { m_log.info("addMonitorInfo() : " + e.getClass().getSimpleName() + ", " + e.getMessage()); throw e; } catch (MonitorNotFound e) { throw e; } catch (HinemosUnknown e) { throw e; } catch (InvalidRole e) { throw e; } } /** * ??????? * <p> * ??????? * * @return ???????</code> true </code> * @throws MonitorNotFound * @throws InvalidRole */ protected abstract boolean addJudgementInfo() throws MonitorNotFound, InvalidRole; /** * ????????? * <p> * ??????? * * @return ???????</code> true </code> * @throws MonitorNotFound * @throws HinemosUnknown * @throws InvalidRole */ protected abstract boolean addCheckInfo() throws MonitorNotFound, HinemosUnknown, InvalidRole; /** * ???????? * * @param info * @param user * @return ??????</code> true </code> * @throws MonitorNotFound * @throws TriggerSchedulerException * @throws HinemosUnknown * @throws InvalidRole * */ public boolean modify(MonitorInfo info, String user) throws MonitorNotFound, TriggerSchedulerException, HinemosUnknown, InvalidRole { m_monitorInfo = info; boolean result = false; // result = modifyMonitorInfo(user); return result; } /** * ?????? * <p> * ??????? * * @return ??????</code> true </code> * @throws MonitorNotFound * @throws EntityExistsException * @throws InvalidRole */ protected abstract boolean modifyJudgementInfo() throws MonitorNotFound, EntityExistsException, InvalidRole; /** * ???????? * <p> * ??????? * * @return ??????</code> true </code> * @throws MonitorNotFound * @throws InvalidRole * @throws HinemosUnknown */ protected abstract boolean modifyCheckInfo() throws MonitorNotFound, InvalidRole, HinemosUnknown; /** * ???? * <p> * <ol> * <li>ID?ID?????</li> * <li>????????</li> * <li>?????????????{@link #modifyJudgementInfo()}</li> * <li>???????????????{@link #modifyCheckInfo()}</li> * <li> ???? /???????Quartz????</li> * </ol> * * @param user * @return ??????true * @throws MonitorNotFound * @throws TriggerSchedulerException * @throws HinemosUnknown * @throws InvalidRole */ protected boolean modifyMonitorInfo(String user) throws MonitorNotFound, TriggerSchedulerException, HinemosUnknown, InvalidRole { long now = HinemosTime.currentTimeMillis(); HinemosEntityManager em = new JpaTransactionManager().getEntityManager(); try { // m_monitor = QueryUtil.getMonitorInfoPK(m_monitorInfo.getMonitorId(), ObjectPrivilegeMode.MODIFY); // ID????? if (!m_monitorInfo.getFacilityId().equals(m_monitor.getFacilityId())) { m_isModifyFacilityId = true; } // /?????? if (m_monitorInfo.getRunInterval() != m_monitor.getRunInterval().intValue()) { m_isModifyRunInterval = true; } // /??(or?????1??)????? if (!m_monitor.getMonitorFlg() && !m_monitor.getCollectorFlg() && (m_monitorInfo.getMonitorFlg() || m_monitorInfo.getCollectorFlg())) { m_isModifyEnableFlg = true; } m_log.debug("modifyMonitorInfo() m_isModifyFacilityId = " + m_isModifyFacilityId + ", m_isModifyRunInterval = " + m_isModifyRunInterval + ", m_isModifyEnableFlg = " + m_isModifyEnableFlg); m_monitor.setDescription(m_monitorInfo.getDescription()); if (m_isModifyFacilityId) m_monitor.setFacilityId(m_monitorInfo.getFacilityId()); if (m_isModifyRunInterval) m_monitor.setRunInterval(m_monitorInfo.getRunInterval()); m_monitor.setDelayTime(getDelayTime()); m_monitor.setTriggerType(getTriggerType().name()); m_monitor.setCalendarId(m_monitorInfo.getCalendarId()); m_monitor.setFailurePriority(m_monitorInfo.getFailurePriority()); m_monitor.setApplication(m_monitorInfo.getApplication()); // if (m_monitorInfo.getNotifyRelationList() != null && m_monitorInfo.getNotifyRelationList().size() > 0) { for (NotifyRelationInfo notifyRelationInfo : m_monitorInfo.getNotifyRelationList()) { notifyRelationInfo.setNotifyGroupId(m_monitor.getNotifyGroupId()); } } new NotifyControllerBean().modifyNotifyRelation(m_monitorInfo.getNotifyRelationList(), m_monitor.getNotifyGroupId()); m_monitor.setMonitorFlg(m_monitorInfo.getMonitorFlg()); m_monitor.setCollectorFlg(m_monitorInfo.getCollectorFlg()); m_monitor.setLogFormatId(m_monitorInfo.getLogFormatId()); m_monitor.setItemName(m_monitorInfo.getItemName()); m_monitor.setMeasure(m_monitorInfo.getMeasure()); m_monitor.setOwnerRoleId(m_monitorInfo.getOwnerRoleId()); m_monitor.setUpdateDate(now); m_monitor.setUpdateUser(user); // if (modifyJudgementInfo()) { // ?? if (modifyCheckInfo()) { // Quartz? new ModifySchedule().updateSchedule(m_monitorInfo.getMonitorId()); // ????? List<MonitorStatusEntity> statusList = MonitorStatusCache .getByPluginIdAndMonitorId(m_monitor.getMonitorTypeId(), m_monitor.getMonitorId()); for (MonitorStatusEntity status : statusList) { MonitorStatusCache.remove(status); } // ?????????? List<NotifyHistoryEntity> historyList = com.clustercontrol.notify.util.QueryUtil .getNotifyHistoryByPluginIdAndMonitorId(m_monitor.getMonitorTypeId(), m_monitor.getMonitorId()); for (NotifyHistoryEntity history : historyList) { em.remove(history); } return true; } } return false; } catch (NotifyNotFound e) { throw new MonitorNotFound(e.getMessage(), e); } catch (MonitorNotFound e) { throw e; } catch (InvalidRole e) { throw e; } } /** * ????? * * @param monitorTypeId ID * @param monitorId ID * @return ??????</code> true </code> * @throws MonitorNotFound * @throws TriggerSchedulerException * @throws HinemosUnknown * @throws InvalidRole * * @see #deleteMonitorInfo() */ public boolean delete(String monitorTypeId, String monitorId) throws MonitorNotFound, TriggerSchedulerException, HinemosUnknown, InvalidRole { m_monitorTypeId = monitorTypeId; m_monitorId = monitorId; boolean result = false; try { // result = deleteMonitorInfo(); } catch (MonitorNotFound e) { throw e; } catch (TriggerSchedulerException e) { throw e; } catch (HinemosUnknown e) { throw e; } catch (InvalidRole e) { throw e; } return result; } /** * ??? * <p> * <ol> * <li>ID?ID?????</li> * <li>????????????{@link #deleteCheckInfo()}</li> * <li>??????????{@link #deleteJudgementInfo()}</li> * <li>???</li> * <li>Quartz????</li> * </ol> * * @return ??????</code> true </code> * @throws MonitorNotFound * @throws TriggerSchedulerException * @throws HinemosUnknown * @throws InvalidRole * * @see com.clustercontrol.monitor.run.ejb.entity.MonitorInfoBean * @see #deleteCheckInfo() * @see com.clustercontrol.monitor.run.factory.ModifySchedule#deleteSchedule(String, String) */ private boolean deleteMonitorInfo() throws MonitorNotFound, TriggerSchedulerException, HinemosUnknown, InvalidRole { HinemosEntityManager em = new JpaTransactionManager().getEntityManager(); try { // ? m_monitor = QueryUtil.getMonitorInfoPK(m_monitorId, ObjectPrivilegeMode.MODIFY); // new NotifyControllerBean().deleteNotifyRelation(m_monitor.getNotifyGroupId()); // ?? if (deleteCheckInfo()) { // Quartz? deleteSchedule(); // em.remove(m_monitor); // ????? List<MonitorStatusEntity> statusList = MonitorStatusCache.getByPluginIdAndMonitorId(m_monitorTypeId, m_monitorId); for (MonitorStatusEntity status : statusList) { MonitorStatusCache.remove(status); } // ?????????? List<NotifyHistoryEntity> historyList = com.clustercontrol.notify.util.QueryUtil .getNotifyHistoryByPluginIdAndMonitorId(m_monitorTypeId, m_monitorId); for (NotifyHistoryEntity history : historyList) { em.remove(history); } return true; } } catch (MonitorNotFound e) { throw e; } catch (InvalidRole e) { throw e; } catch (HinemosUnknown e) { throw e; } return false; } /** * ?? * @throws TriggerSchedulerException */ protected void deleteSchedule() throws HinemosUnknown { // Quartz?(runInterval = 0 -> ???) if (m_monitor.getRunInterval() > 0) { new ModifySchedule().deleteSchedule(m_monitorTypeId, m_monitorId); } } /** * ????? * <p> * ??????? * * @return ??????</code> true </code> */ protected boolean deleteCheckInfo() { return true; } /** * ID????????? */ public static int getDelayTimeBasic(MonitorInfo monitorInfo) { // ?????Quartz?Trigger???????ID??DelayTime?? // ???ID?????hash? int hashCode = (monitorInfo.getMonitorId() + monitorInfo.getMonitorType()).hashCode(); // hash?????????????0(monitorInfo-1)?? int offsetSecond = new Random(hashCode).nextInt(monitorInfo.getRunInterval()); m_log.debug("MonitorID : " + monitorInfo.getMonitorId() + ", MonitorType : " + monitorInfo.getMonitorType() + ", offset : " + offsetSecond); return offsetSecond; } }