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.jobmanagement.factory; import java.io.Serializable; import java.util.ArrayList; 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.QuartzUtil; import com.clustercontrol.commons.util.HinemosEntityManager; import com.clustercontrol.commons.util.JpaTransactionManager; import com.clustercontrol.fault.HinemosUnknown; import com.clustercontrol.fault.JobInfoNotFound; import com.clustercontrol.fault.JobKickDuplicate; import com.clustercontrol.fault.ObjectPrivilege_InvalidRole; import com.clustercontrol.jobmanagement.bean.JobFileCheck; import com.clustercontrol.jobmanagement.bean.JobKick; import com.clustercontrol.jobmanagement.bean.JobKickConstant; import com.clustercontrol.jobmanagement.bean.JobRuntimeParam; import com.clustercontrol.jobmanagement.bean.JobRuntimeParamDetail; import com.clustercontrol.jobmanagement.bean.JobSchedule; import com.clustercontrol.jobmanagement.bean.JobTriggerInfo; import com.clustercontrol.jobmanagement.bean.JobTriggerTypeConstant; import com.clustercontrol.jobmanagement.bean.QuartzConstant; import com.clustercontrol.jobmanagement.model.JobKickEntity; import com.clustercontrol.jobmanagement.model.JobRuntimeParamDetailEntity; import com.clustercontrol.jobmanagement.model.JobRuntimeParamDetailEntityPK; import com.clustercontrol.jobmanagement.model.JobRuntimeParamEntity; import com.clustercontrol.jobmanagement.model.JobRuntimeParamEntityPK; import com.clustercontrol.jobmanagement.session.JobControllerBean; import com.clustercontrol.plugin.impl.SchedulerPlugin; import com.clustercontrol.plugin.impl.SchedulerPlugin.SchedulerType; import com.clustercontrol.util.HinemosTime; /** * ???? * * @version 2.4.0 * @since 1.0.0 */ public class ModifyJobKick { /** ? */ private static Log m_log = LogFactory.getLog(ModifyJobKick.class); /** * DB?????? * ???????<BR> * * @param info * @param user ID * @param jobkickType * @throws HinemosUnknown * @throws JobKickDuplicate * * @see com.clustercontrol.jobmanagement.bean.QuartzConstant * @see com.clustercontrol.jobmanagement.bean.JobTriggerInfo * @see com.clustercontrol.jobmanagement.util.QuartzUtil#getQuartzManager() */ public void addJobKick(final JobKick info, String loginUser, Integer jobkickType) throws HinemosUnknown, JobKickDuplicate { m_log.debug("addJobKick() : id=" + info.getId() + ", jobId=" + info.getJobId() + ", jobkickType=" + jobkickType); JpaTransactionManager jtm = new JpaTransactionManager(); // long now = HinemosTime.currentTimeMillis(); // DB?? try { // ID??? String id = info.getId(); jtm.checkEntityExists(JobKickEntity.class, id); // JobKickEntity jobKickEntity = new JobKickEntity(info.getId()); jobKickEntity.setJobkickName(info.getName()); jobKickEntity.setJobkickType(jobkickType); jobKickEntity.setJobunitId(info.getJobunitId()); jobKickEntity.setJobId(info.getJobId()); // if (info.getJobRuntimeParamList() != null && info.getJobRuntimeParamList().size() > 0) { for (JobRuntimeParam jobRuntimeParam : info.getJobRuntimeParamList()) { JobRuntimeParamEntity jobRuntimeParamEntity = new JobRuntimeParamEntity(jobKickEntity, jobRuntimeParam.getParamId()); jobRuntimeParamEntity.setParamType(jobRuntimeParam.getParamType()); jobRuntimeParamEntity.setDefaultValue(jobRuntimeParam.getValue()); jobRuntimeParamEntity.setDescription(jobRuntimeParam.getDescription()); jobRuntimeParamEntity.setRequiredFlg(jobRuntimeParam.getRequiredFlg()); // if (jobRuntimeParam.getJobRuntimeParamDetailList() != null && jobRuntimeParam.getJobRuntimeParamDetailList().size() > 0) { int detailIdx = 0; for (JobRuntimeParamDetail jobRuntimeParamDetail : jobRuntimeParam .getJobRuntimeParamDetailList()) { JobRuntimeParamDetailEntity jobRuntimeParamDetailEntity = new JobRuntimeParamDetailEntity( jobRuntimeParamEntity, detailIdx); jobRuntimeParamDetailEntity.setParamValue(jobRuntimeParamDetail.getParamValue()); jobRuntimeParamDetailEntity.setDescription(jobRuntimeParamDetail.getDescription()); detailIdx++; } } } } if (jobkickType == JobKickConstant.TYPE_SCHEDULE) { // if (!(info instanceof JobSchedule)) { throw new HinemosUnknown("type error : " + info.getClass() + "!=JobSchedule"); } JobSchedule jobSchedule = (JobSchedule) info; if (!"".equals(jobSchedule.getCalendarId())) { jobKickEntity.setCalendarId(jobSchedule.getCalendarId()); } jobKickEntity.setValidFlg(info.isValid()); jobKickEntity.setScheduleType(jobSchedule.getScheduleType()); jobKickEntity.setWeek(jobSchedule.getWeek()); jobKickEntity.setHour(jobSchedule.getHour()); jobKickEntity.setMinute(jobSchedule.getMinute()); jobKickEntity.setFromXMinutes(jobSchedule.getFromXminutes()); jobKickEntity.setEveryXMinutes(jobSchedule.getEveryXminutes()); } else if (jobkickType == JobKickConstant.TYPE_FILECHECK) { // ? if (!(info instanceof JobFileCheck)) { throw new HinemosUnknown("type error : " + info.getClass() + "!=JobFileCheck"); } JobFileCheck jobFileCheck = (JobFileCheck) info; if (!"".equals(jobFileCheck.getCalendarId())) { jobKickEntity.setCalendarId(jobFileCheck.getCalendarId()); } jobKickEntity.setValidFlg(info.isValid()); jobKickEntity.setFacilityId(jobFileCheck.getFacilityId()); jobKickEntity.setFileName(jobFileCheck.getFileName()); jobKickEntity.setDirectory(jobFileCheck.getDirectory()); jobKickEntity.setEventType(jobFileCheck.getEventType()); jobKickEntity.setModifyType(jobFileCheck.getModifyType()); } else if (jobkickType == JobKickConstant.TYPE_MANUAL) { jobKickEntity.setCalendarId(null); jobKickEntity.setValidFlg(true); } jobKickEntity.setOwnerRoleId(info.getOwnerRoleId()); jobKickEntity.setRegDate(now); jobKickEntity.setUpdateDate(now); jobKickEntity.setRegUser(loginUser); jobKickEntity.setUpdateUser(loginUser); } catch (EntityExistsException e) { m_log.info("addJobKick() JobKickEntity.create() : " + e.getClass().getSimpleName() + ", " + e.getMessage()); throw new JobKickDuplicate(e.getMessage(), e); } catch (Exception e) { m_log.warn( "addJobKick() JobKickEntity.create() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); throw new HinemosUnknown(e.getMessage(), e); } if (jobkickType == JobKickConstant.TYPE_SCHEDULE) { // ????? JobTriggerInfo triggerInfo = new JobTriggerInfo(); triggerInfo.setTrigger_type(JobTriggerTypeConstant.TYPE_SCHEDULE); triggerInfo.setTrigger_info(info.getName() + "(" + info.getId() + ")"); //JobDetail???? Serializable[] jdArgs = new Serializable[QuartzConstant.ARGS_NUM]; @SuppressWarnings("unchecked") Class<? extends Serializable>[] jdArgsType = new Class[QuartzConstant.ARGS_NUM]; //ID jdArgs[QuartzConstant.INDEX_JOBUNIT_ID] = info.getJobunitId(); jdArgsType[QuartzConstant.INDEX_JOBUNIT_ID] = String.class; //ID jdArgs[QuartzConstant.INDEX_JOB_ID] = info.getJobId(); jdArgsType[QuartzConstant.INDEX_JOB_ID] = String.class; //ID jdArgs[QuartzConstant.INDEX_CALENDAR_ID] = info.getCalendarId(); jdArgsType[QuartzConstant.INDEX_CALENDAR_ID] = String.class; // jdArgs[QuartzConstant.INDEX_TRIGGER_TYPE] = triggerInfo.getTrigger_type(); jdArgsType[QuartzConstant.INDEX_TRIGGER_TYPE] = Integer.class; // jdArgs[QuartzConstant.INDEX_TRIGGER_INFO] = triggerInfo.getTrigger_info(); jdArgsType[QuartzConstant.INDEX_TRIGGER_INFO] = String.class; //?? jdArgs[QuartzConstant.INDEX_TRIGGER_FILENAME] = triggerInfo.getFilename(); jdArgsType[QuartzConstant.INDEX_TRIGGER_FILENAME] = String.class; //?? jdArgs[QuartzConstant.INDEX_TRIGGER_DIRECTORY] = triggerInfo.getDirectory(); jdArgsType[QuartzConstant.INDEX_TRIGGER_DIRECTORY] = String.class; //???? jdArgs[QuartzConstant.INDEX_TRIGGER_JOB_WAIT_TIME] = triggerInfo.getJobWaitTime(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOB_WAIT_TIME] = Boolean.class; //????? jdArgs[QuartzConstant.INDEX_TRIGGER_JOB_WAIT_MINUTE] = triggerInfo.getJobWaitTime(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOB_WAIT_MINUTE] = Boolean.class; //???? jdArgs[QuartzConstant.INDEX_TRIGGER_JOB_COMMAND] = triggerInfo.getJobCommand(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOB_COMMAND] = Boolean.class; //???? jdArgs[QuartzConstant.INDEX_TRIGGER_JOB_COMMAND_TEXT] = triggerInfo.getJobCommandText(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOB_COMMAND_TEXT] = String.class; //ID jdArgs[QuartzConstant.INDEX_TRIGGER_JOBKICK_ID] = info.getId(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOBKICK_ID] = String.class; //Cron?? String cronString = QuartzUtil.getCronString(((JobSchedule) info).getScheduleType(), ((JobSchedule) info).getWeek(), ((JobSchedule) info).getHour(), ((JobSchedule) info).getMinute(), ((JobSchedule) info).getFromXminutes(), ((JobSchedule) info).getEveryXminutes()); m_log.trace("CronString =" + cronString); // try { if (info.isValid().booleanValue()) { SchedulerPlugin.scheduleCronJob(SchedulerType.DBMS, info.getId(), QuartzConstant.GROUP_NAME, HinemosTime.currentTimeMillis() + 15 * 1000, cronString, true, JobControllerBean.class.getName(), QuartzConstant.METHOD_NAME, jdArgsType, jdArgs); } else { SchedulerPlugin.deleteJob(SchedulerType.DBMS, info.getId(), QuartzConstant.GROUP_NAME); } } catch (HinemosUnknown e) { m_log.error(e); } } } /** * DB????<BR> * @param info * @param loginUser ID * @param jobkickType * @throws HinemosUnknown * @throws JobInfoNotFound * @throws ObjectPrivilege_InvalidRole */ public void modifyJobKick(final JobKick info, String loginUser, Integer jobkickType) throws HinemosUnknown, JobInfoNotFound, ObjectPrivilege_InvalidRole { m_log.debug("modifyJobKick() : id=" + info.getId() + ", jobId=" + info.getJobId() + ", jobkickType=" + jobkickType); JpaTransactionManager jtm = new JpaTransactionManager(); HinemosEntityManager em = jtm.getEntityManager(); // long now = HinemosTime.currentTimeMillis(); // DB?? try { JobKickEntity bean = em.find(JobKickEntity.class, info.getId(), ObjectPrivilegeMode.MODIFY); if (bean == null) { JobInfoNotFound e = new JobInfoNotFound("JobKickEntity.findByPrimaryKey"); m_log.info(e.getClass().getSimpleName() + ", " + e.getMessage()); throw e; } bean.setJobkickId(info.getId()); bean.setJobkickName(info.getName()); bean.setJobunitId(info.getJobunitId()); bean.setJobId(info.getJobId()); // if (info.getJobRuntimeParamList() != null && info.getJobRuntimeParamList().size() > 0) { ArrayList<JobRuntimeParamEntityPK> jobRuntimeParamEntityPKList = new ArrayList<>(); for (JobRuntimeParam jobRuntimeParam : info.getJobRuntimeParamList()) { JobRuntimeParamEntityPK jobRuntimeParamEntityPK = new JobRuntimeParamEntityPK(info.getId(), jobRuntimeParam.getParamId()); JobRuntimeParamEntity jobRuntimeParamEntity = em.find(JobRuntimeParamEntity.class, jobRuntimeParamEntityPK, ObjectPrivilegeMode.MODIFY); if (jobRuntimeParamEntity == null) { // ? jobRuntimeParamEntity = new JobRuntimeParamEntity(jobRuntimeParamEntityPK, bean); } jobRuntimeParamEntity.setParamType(jobRuntimeParam.getParamType()); jobRuntimeParamEntity.setDefaultValue(jobRuntimeParam.getValue()); jobRuntimeParamEntity.setDescription(jobRuntimeParam.getDescription()); jobRuntimeParamEntity.setRequiredFlg(jobRuntimeParam.getRequiredFlg()); jobRuntimeParamEntityPKList.add(jobRuntimeParamEntityPK); // if (jobRuntimeParam.getJobRuntimeParamDetailList() != null && jobRuntimeParam.getJobRuntimeParamDetailList().size() > 0) { ArrayList<JobRuntimeParamDetailEntityPK> jobRuntimeParamDetailEntityPKList = new ArrayList<>(); int detailIdx = 0; for (JobRuntimeParamDetail jobRuntimeParamDetail : jobRuntimeParam .getJobRuntimeParamDetailList()) { JobRuntimeParamDetailEntityPK jobRuntimeParamDetailEntityPK = new JobRuntimeParamDetailEntityPK( info.getId(), jobRuntimeParam.getParamId(), detailIdx); JobRuntimeParamDetailEntity jobRuntimeParamDetailEntity = em.find( JobRuntimeParamDetailEntity.class, jobRuntimeParamDetailEntityPK, ObjectPrivilegeMode.MODIFY); if (jobRuntimeParamDetailEntity == null) { // ? jobRuntimeParamDetailEntity = new JobRuntimeParamDetailEntity( jobRuntimeParamDetailEntityPK, jobRuntimeParamEntity); } jobRuntimeParamDetailEntity.setParamValue(jobRuntimeParamDetail.getParamValue()); jobRuntimeParamDetailEntity.setDescription(jobRuntimeParamDetail.getDescription()); jobRuntimeParamDetailEntityPKList.add(jobRuntimeParamDetailEntityPK); detailIdx++; } // ???JobRuntimeParamDetailEntity jobRuntimeParamEntity .deleteJobRuntimeParamDetailEntities(jobRuntimeParamDetailEntityPKList); } } // ???JobRuntimeParamEntity bean.deleteJobRuntimeParamEntities(jobRuntimeParamEntityPKList); } if (jobkickType == JobKickConstant.TYPE_SCHEDULE) { // if (!(info instanceof JobSchedule)) { throw new HinemosUnknown("type error : " + info.getClass() + "!=JobSchedule"); } JobSchedule jobSchedule = (JobSchedule) info; if ("".equals(jobSchedule.getCalendarId())) { bean.setCalendarId(null); } else { bean.setCalendarId(jobSchedule.getCalendarId()); } bean.setValidFlg(info.isValid()); bean.setScheduleType(jobSchedule.getScheduleType()); bean.setWeek(jobSchedule.getWeek()); bean.setHour(jobSchedule.getHour()); bean.setMinute(jobSchedule.getMinute()); bean.setFromXMinutes(jobSchedule.getFromXminutes()); bean.setEveryXMinutes(jobSchedule.getEveryXminutes()); } else if (jobkickType == JobKickConstant.TYPE_FILECHECK) { // ? if (!(info instanceof JobFileCheck)) { throw new HinemosUnknown("type error : " + info.getClass() + "!=JobFileCheck"); } JobFileCheck jobFileCheck = (JobFileCheck) info; if ("".equals(jobFileCheck.getCalendarId())) { bean.setCalendarId(null); } else { bean.setCalendarId(jobFileCheck.getCalendarId()); } bean.setValidFlg(info.isValid()); bean.setFacilityId(jobFileCheck.getFacilityId()); bean.setDirectory(jobFileCheck.getDirectory()); bean.setFileName(jobFileCheck.getFileName()); bean.setEventType(jobFileCheck.getEventType()); bean.setModifyType(jobFileCheck.getModifyType()); } else if (jobkickType == JobKickConstant.TYPE_MANUAL) { bean.setCalendarId(null); bean.setValidFlg(true); } bean.setOwnerRoleId(info.getOwnerRoleId()); bean.setUpdateDate(now); bean.setUpdateUser(loginUser); } catch (JobInfoNotFound e) { throw e; } catch (ObjectPrivilege_InvalidRole e) { throw e; } catch (Exception e) { m_log.warn("modifyJobKick() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); throw new HinemosUnknown(e.getMessage(), e); } if (jobkickType == JobKickConstant.TYPE_SCHEDULE) { // ????? JobTriggerInfo triggerInfo = new JobTriggerInfo(); triggerInfo.setTrigger_type(JobTriggerTypeConstant.TYPE_SCHEDULE); triggerInfo.setTrigger_info(info.getName() + "(" + info.getId() + ")"); //JobDetail???? Serializable[] jdArgs = new Serializable[QuartzConstant.ARGS_NUM]; @SuppressWarnings("unchecked") Class<? extends Serializable>[] jdArgsType = new Class[QuartzConstant.ARGS_NUM]; //ID jdArgs[QuartzConstant.INDEX_JOBUNIT_ID] = info.getJobunitId(); jdArgsType[QuartzConstant.INDEX_JOBUNIT_ID] = String.class; //ID jdArgs[QuartzConstant.INDEX_JOB_ID] = info.getJobId(); jdArgsType[QuartzConstant.INDEX_JOB_ID] = String.class; //ID jdArgs[QuartzConstant.INDEX_CALENDAR_ID] = info.getCalendarId(); jdArgsType[QuartzConstant.INDEX_CALENDAR_ID] = String.class; // jdArgs[QuartzConstant.INDEX_TRIGGER_TYPE] = triggerInfo.getTrigger_type(); jdArgsType[QuartzConstant.INDEX_TRIGGER_TYPE] = Integer.class; // jdArgs[QuartzConstant.INDEX_TRIGGER_INFO] = triggerInfo.getTrigger_info(); jdArgsType[QuartzConstant.INDEX_TRIGGER_INFO] = String.class; //?? jdArgs[QuartzConstant.INDEX_TRIGGER_FILENAME] = triggerInfo.getFilename(); jdArgsType[QuartzConstant.INDEX_TRIGGER_FILENAME] = String.class; //?? jdArgs[QuartzConstant.INDEX_TRIGGER_DIRECTORY] = triggerInfo.getDirectory(); jdArgsType[QuartzConstant.INDEX_TRIGGER_DIRECTORY] = String.class; //???? jdArgs[QuartzConstant.INDEX_TRIGGER_JOB_WAIT_TIME] = triggerInfo.getJobWaitTime(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOB_WAIT_TIME] = Boolean.class; //????? jdArgs[QuartzConstant.INDEX_TRIGGER_JOB_WAIT_MINUTE] = triggerInfo.getJobWaitMinute(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOB_WAIT_MINUTE] = Boolean.class; //???? jdArgs[QuartzConstant.INDEX_TRIGGER_JOB_COMMAND] = triggerInfo.getJobCommand(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOB_COMMAND] = Boolean.class; //???? jdArgs[QuartzConstant.INDEX_TRIGGER_JOB_COMMAND_TEXT] = triggerInfo.getJobCommandText(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOB_COMMAND_TEXT] = String.class; //ID jdArgs[QuartzConstant.INDEX_TRIGGER_JOBKICK_ID] = info.getId(); jdArgsType[QuartzConstant.INDEX_TRIGGER_JOBKICK_ID] = String.class; //Cron?? String cronString = QuartzUtil.getCronString(((JobSchedule) info).getScheduleType(), ((JobSchedule) info).getWeek(), ((JobSchedule) info).getHour(), ((JobSchedule) info).getMinute(), ((JobSchedule) info).getFromXminutes(), ((JobSchedule) info).getEveryXminutes()); m_log.trace("CronString =" + cronString); // try { if (info.isValid().booleanValue()) { SchedulerPlugin.scheduleCronJob(SchedulerType.DBMS, info.getId(), QuartzConstant.GROUP_NAME, HinemosTime.currentTimeMillis() + 15 * 1000, cronString, true, JobControllerBean.class.getName(), QuartzConstant.METHOD_NAME, jdArgsType, jdArgs); } else { SchedulerPlugin.deleteJob(SchedulerType.DBMS, info.getId(), QuartzConstant.GROUP_NAME); } } catch (HinemosUnknown e) { m_log.error(e); } } } /** * ??? * ?????Quartz?????? * * @param jobkickId ID * @param jobkickType * @throws HinemosUnknown * @throws JobInfoNotFound * @throws ObjectPrivilege_InvalidRole * * @see com.clustercontrol.jobmanagement.bean.QuartzConstant * @see com.clustercontrol.jobmanagement.util.QuartzUtil#getQuartzManager() * @see com.clustercontrol.quartzmanager.ejb.session.QuartzManager#deleteSchedule(java.lang.String, java.lang.String) */ public void deleteJobKick(final String jobkickId, Integer jobkickType) throws HinemosUnknown, JobInfoNotFound, ObjectPrivilege_InvalidRole { // m_log.debug("deleteJobKick() : id=" + jobkickId); JpaTransactionManager jtm = new JpaTransactionManager(); HinemosEntityManager em = jtm.getEntityManager(); // DB? try { // JobKickEntity jobKickEntity = em.find(JobKickEntity.class, jobkickId, ObjectPrivilegeMode.MODIFY); if (jobKickEntity == null) { JobInfoNotFound e = new JobInfoNotFound("JobKickEntity.findByPrimaryKey"); m_log.info(e.getClass().getSimpleName() + ", " + e.getMessage()); throw e; } // em.remove(jobKickEntity); } catch (JobInfoNotFound e) { throw e; } catch (ObjectPrivilege_InvalidRole e) { throw e; } catch (Exception e) { m_log.warn("deleteSchedule() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); throw new HinemosUnknown(e.getMessage(), e); } if (jobkickType == JobKickConstant.TYPE_SCHEDULE) { // ???CronTrigger try { m_log.debug("deleteJob() : id=" + jobkickId); SchedulerPlugin.deleteJob(SchedulerType.DBMS, jobkickId, QuartzConstant.GROUP_NAME); } catch (HinemosUnknown e) { m_log.error(e); } } } }