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.util; import java.text.DateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.accesscontrol.bean.PrivilegeConstant.ObjectPrivilegeMode; import com.clustercontrol.commons.util.HinemosEntityManager; import com.clustercontrol.commons.util.JpaTransactionManager; import com.clustercontrol.commons.util.ObjectValidator; import com.clustercontrol.fault.FacilityNotFound; import com.clustercontrol.fault.HinemosUnknown; import com.clustercontrol.fault.InvalidRole; import com.clustercontrol.fault.JobInfoNotFound; import com.clustercontrol.jobmanagement.bean.JobTriggerInfo; import com.clustercontrol.jobmanagement.bean.JobTriggerTypeConstant; import com.clustercontrol.jobmanagement.bean.SystemParameterConstant; import com.clustercontrol.jobmanagement.model.JobParamInfoEntity; import com.clustercontrol.jobmanagement.model.JobSessionEntity; import com.clustercontrol.jobmanagement.model.JobSessionNodeEntity; import com.clustercontrol.maintenance.util.HinemosPropertyUtil; import com.clustercontrol.notify.bean.OutputBasicInfo; import com.clustercontrol.notify.util.NotifyUtil; import com.clustercontrol.repository.factory.FacilitySelector; import com.clustercontrol.repository.model.NodeInfo; import com.clustercontrol.repository.session.RepositoryControllerBean; import com.clustercontrol.repository.util.RepositoryUtil; import com.clustercontrol.util.HinemosMessage; import com.clustercontrol.util.HinemosTime; import com.clustercontrol.util.Messages; /** * <BR> * * @version 3.0.0 * @since 2.1.0 */ public class ParameterUtil { /** ? */ private static Log m_log = LogFactory.getLog(ParameterUtil.class); /** ?? */ private static final String PARAM_JOB_PARAM_DISABLE = "job.param.disable"; /** * ??????? * ????????????? * * @param info * @return */ public static Map<String, String> createParamInfo(OutputBasicInfo info) { // Map<String, String> params = new HashMap<String, String>(); // ? String[] disableStrAry = null; // ??????? if (info == null) { return params; } // ID if (!ObjectValidator.isEmptyString(info.getFacilityId()) && !disableParam(SystemParameterConstant.FACILITY_ID, disableStrAry)) { params.put(SystemParameterConstant.FACILITY_ID, info.getFacilityId()); } // ID if (!ObjectValidator.isEmptyString(info.getPluginId()) && !disableParam(SystemParameterConstant.PLUGIN_ID, disableStrAry)) { params.put(SystemParameterConstant.PLUGIN_ID, info.getPluginId()); } // ID if (!ObjectValidator.isEmptyString(info.getMonitorId()) && !disableParam(SystemParameterConstant.MONITOR_ID, disableStrAry)) { params.put(SystemParameterConstant.MONITOR_ID, info.getMonitorId()); } // if (!ObjectValidator.isEmptyString(info.getSubKey()) && !disableParam(SystemParameterConstant.MONITOR_DETAIL_ID, disableStrAry)) { params.put(SystemParameterConstant.MONITOR_DETAIL_ID, info.getSubKey()); } // if (!ObjectValidator.isEmptyString(info.getApplication()) && !disableParam(SystemParameterConstant.APPLICATION, disableStrAry)) { params.put(SystemParameterConstant.APPLICATION, info.getApplication()); } // ?? if (!disableParam(SystemParameterConstant.PRIORITY, disableStrAry)) { params.put(SystemParameterConstant.PRIORITY, String.valueOf(info.getPriority())); } // if (!ObjectValidator.isEmptyString(info.getMessage()) && !disableParam(SystemParameterConstant.MESSAGE, disableStrAry)) { Locale locale = NotifyUtil.getNotifyLocale(); params.put(SystemParameterConstant.MESSAGE, HinemosMessage.replace(info.getMessage(), locale)); } // if (!ObjectValidator.isEmptyString(info.getMessageOrg()) && !disableParam(SystemParameterConstant.ORG_MESSAGE, disableStrAry)) { params.put(SystemParameterConstant.ORG_MESSAGE, info.getMessageOrg()); } return params; } /** * ???? * ????????????? * * @param info * @return */ public static Map<String, String> createParamInfo(JobTriggerInfo info) { // Map<String, String> params = new HashMap<String, String>(); // ? String[] disableStrAry = null; // ???????? if (info == null) { return params; } // ?? if (!ObjectValidator.isEmptyString(info.getFilename()) && !disableParam(SystemParameterConstant.FILENAME, disableStrAry)) { params.put(SystemParameterConstant.FILENAME, info.getFilename()); } // if (!ObjectValidator.isEmptyString(info.getDirectory()) && !disableParam(SystemParameterConstant.DIRECTORY, disableStrAry)) { params.put(SystemParameterConstant.DIRECTORY, info.getDirectory()); } return params; } /** * ????? * * @param paramId ID * @param sessionId ID * @param jobSessionParams * @return */ public static String getJobSessionParamValue(String paramId, String sessionId, Map<String, String> jobSessionParams) throws JobInfoNotFound { m_log.debug("getJobSessionParamValue() start paramId=" + paramId + ",sessionId=" + sessionId); String ret = null; m_log.debug("getting parameters of job session... (session_id = " + sessionId + ")"); if (jobSessionParams == null) { jobSessionParams = new HashMap<String, String>(); HinemosEntityManager em = new JpaTransactionManager().getEntityManager(); Collection<JobParamInfoEntity> collection = em .createNamedQuery("JobParamInfoEntity.findBySessionId", JobParamInfoEntity.class) .setParameter("sessionId", sessionId).getResultList(); if (collection == null) { JobInfoNotFound je = new JobInfoNotFound( "JobParamInfoEntity.findBySessionId" + ", sessionId = " + sessionId); m_log.info("getJobSessionParamValue() : " + je.getClass().getSimpleName() + ", " + je.getMessage()); throw je; } if (collection.size() > 0) { Iterator<JobParamInfoEntity> itr = collection.iterator(); while (itr.hasNext()) { JobParamInfoEntity param = itr.next(); jobSessionParams.put(param.getId().getParamId(), param.getValue()); } } } // ?? if (jobSessionParams.containsKey(paramId)) { ret = jobSessionParams.get(paramId) == null ? "" : jobSessionParams.get(paramId); } m_log.debug( "getJobSessionParamValue() end paramId=" + paramId + ",sessionId=" + sessionId + ",value=" + ret); return ret; } /** * * ????? * * @param paramId ID * @param sessionId ID * @param jobSessionEntity * @return * @throws JobInfoNotFound */ private static String getJobSessionValue(String paramId, String sessionId, JobSessionEntity jobSessionEntity) throws JobInfoNotFound { m_log.debug("getJobSessionValue() start paramId=" + paramId + ",sessionId=" + sessionId); String ret = null; if (paramId.equals(SystemParameterConstant.SESSION_ID)) { // ID ret = sessionId; } else { if (jobSessionEntity == null) { HinemosEntityManager em = new JpaTransactionManager().getEntityManager(); // ?? jobSessionEntity = em.find(JobSessionEntity.class, sessionId, ObjectPrivilegeMode.READ); if (jobSessionEntity == null) { JobInfoNotFound je = new JobInfoNotFound( "JobSessionEntity.findByPrimaryKey" + ", sessionId = " + sessionId); m_log.info( "getJobParameterValue() : " + je.getClass().getSimpleName() + ", " + je.getMessage()); je.setSessionId(sessionId); throw je; } } if (paramId.equals(SystemParameterConstant.START_DATE)) { // DateFormat df = DateFormat.getDateTimeInstance(); df.setTimeZone(HinemosTime.getTimeZone()); ret = df.format(jobSessionEntity.getScheduleDate()); } else if (paramId.equals(SystemParameterConstant.TRIGGER_TYPE)) { // ? Locale locale = NotifyUtil.getNotifyLocale(); ret = Messages.getString( JobTriggerTypeConstant.typeToMessageCode(jobSessionEntity.getTriggerType()), locale); } else if (paramId.equals(SystemParameterConstant.TRIGGER_INFO)) { // ? ret = jobSessionEntity.getTriggerInfo(); } } m_log.debug("getJobSessionValue() end paramId=" + paramId + ",sessionId=" + sessionId + ",value=" + ret); return ret; } /** * * ?ID?????? * * @param paramId ID * @param facilityId ID * @param nodeParams * @return * @throws HinemosUnknown * @throws FacilityNotFound */ private static String getNodeValue(String paramId, String facilityId, Map<String, String> nodeParams) throws HinemosUnknown, FacilityNotFound, InvalidRole { m_log.debug("getNodeValue() start paramId=" + paramId + ",facilityId=" + facilityId); String ret = null; if (facilityId != null && !facilityId.isEmpty()) { if (paramId.equals(SystemParameterConstant.FACILITY_ID)) { // ID ret = facilityId; } else { if (new RepositoryControllerBean().isNode(facilityId)) { if (nodeParams == null) { // ? NodeInfo nodeInfo = new RepositoryControllerBean().getNode(facilityId); nodeParams = RepositoryUtil.createNodeParameter(nodeInfo); } if (nodeParams.get(paramId) != null) { ret = nodeParams.get(paramId); } } } } m_log.debug("getNodeValue() end paramId=" + paramId + ",facilityId=" + facilityId + ",value=" + ret); return ret; } /** * HinemosProperty?????? * * @param paramId ID * @param disableStrAry ?? * @return true:?false: */ private static boolean disableParam(String paramId, String[] disableStrAry) { // boolean rtn = false; // HinemosProperty?? if (disableStrAry == null) { String disableStr = HinemosPropertyUtil.getHinemosPropertyStr(PARAM_JOB_PARAM_DISABLE, ""); if (disableStr != null && !"".equals(disableStr)) { disableStrAry = disableStr.split(","); } } if (disableStrAry != null && disableStrAry.length > 0) { // ???? if (Arrays.asList(disableStrAry).contains(paramId)) { rtn = true; } } return rtn; } /** * ????ID???<BR> * ?ID??????<BR> * ????ID??????? * * @param sessionId ID * @param source ???? * @return ????? * @throws JobInfoNotFound * @throws HinemosUnknown * @throws FacilityNotFound */ public static String replaceSessionParameterValue(String sessionId, String facilityId, String source) throws JobInfoNotFound, HinemosUnknown, FacilityNotFound, InvalidRole { // Local Variables String commandOrig = source; // ?? String commandConv = source; // ? // Main if (commandOrig == null) { m_log.info("registed command is invalid. (session_id = " + sessionId + ", facility_id = " + facilityId + ", command_orig = null)"); } else { m_log.debug("generating command string... (session_id = " + sessionId + ", facility_id = " + facilityId + ", command_orig = " + commandOrig + ")"); } // ?ID? List<String> list = new ArrayList<String>(); String regexp = "#\\[[a-zA-Z0-9-_:]+\\]"; Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(commandOrig); while (matcher.find()) { list.add(SystemParameterConstant.getParamId(matcher.group())); } // ID?????????? if (list.size() == 0) { return commandOrig; } // ? String[] disableStrAry = null; // JobSessionEntity jobSessionEntity = null; // Map<String, String> nodeParams = null; // Map<String, String> jobSessionParams = null; // ??? for (String paramId : list) { String paramValue = null; // ? paramValue = getJobSessionParamValue(paramId, sessionId, jobSessionParams); // ?????ID?ID?????? if (paramValue == null) { paramValue = getJobSessionParamValue(paramId + ":" + facilityId, sessionId, jobSessionParams); if (paramValue == null) { ArrayList<String> facilityIdList = FacilitySelector.getNodeFacilityIdList(true); for (String str : facilityIdList) { // ?????? if (str.equals(facilityId)) continue; paramValue = getJobSessionParamValue(paramId + ":" + str, sessionId, jobSessionParams); if (paramValue != null) break; } } } // if (paramValue == null) { if (!disableParam(paramId, disableStrAry)) { if (Arrays.asList(SystemParameterConstant.SYSTEM_ID_LIST_JOB_SESSION).contains(paramId)) { // paramValue = getJobSessionValue(paramId, sessionId, jobSessionEntity); } else { // paramValue = getNodeValue(paramId, facilityId, nodeParams); } } } if (paramValue != null) { // ?null?????? commandConv = commandConv.replace(SystemParameterConstant.getParamText(paramId), paramValue); } } m_log.debug("successful in generating command string... (session_id = " + sessionId + ", facility_id = " + facilityId + ", command_orig = " + commandOrig + ", command_conv = " + commandConv + ")"); return commandConv; } /** * #[RETURN:jobId:facilityId]?? * @param sessionId * @param jobunitId * @param jobId * @param source * @return */ public static String replaceReturnCodeParameter(String sessionId, String jobunitId, String source) { String regex = "#\\[RETURN:([^:]*):([^:]*)\\]"; Pattern pattern = Pattern.compile(regex); String ret = source; for (int i = 0; i < 100; i++) { //?????????? Matcher matcher = pattern.matcher(ret); if (matcher.find()) { String rJobId = matcher.group(1); String rFacilityId = matcher.group(2); try { JobSessionNodeEntity node = QueryUtil.getJobSessionNodePK(sessionId, jobunitId, rJobId, rFacilityId); Integer endValue = node.getEndValue(); if (endValue != null) { ret = ret.replaceFirst(regex, endValue.toString()); } else { // ?????? ret = ret.replaceFirst(regex, "null"); } } catch (JobInfoNotFound e) { m_log.warn("replaceReturnCodeParameter : jobId=" + rJobId + ", facilityId=" + rFacilityId); // ?ID???? ret = ret.replaceFirst(regex, "null"); } /* * for test * ?(??main?)?? */ // ret = ret.replaceFirst(regex, "12345"); } else { break; } } return ret; } public static void main(String args[]) { String source = ""; // source = "ls #[RETURN:jobId1:facilityId1]a"; source = "ls #[RETURN:jobId1:facilityId1]a -l#[RETURN:jobId2:facilityId2]a"; System.out.println("source=" + source); System.out.println("replace=" + replaceReturnCodeParameter(null, null, source)); } }