ShellExecutorUtil.java 3.61 KB
package com.sitech.ismp.check.mbean;

import com.sitech.ismp.messageObject.ScheduleLog;
import com.sitech.ismp.messageObject.cc.CommandParameters;
import com.sitech.ismp.messageObject.cc.CommandResults;
import com.sitech.util.DateFormater;
import com.sitech.util.JSONUtil;
import com.sitech.util.mq.MQConstants;
import com.sitech.util.mq.TunnelFactory;
import org.apache.log4j.Logger;

import java.io.File;
import java.text.ParseException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 * User: Linxc
 * Date: 14-3-13
 * Time: 上午10:28
 * To change this template use File | Settings | File Templates.
 */
public class ShellExecutorUtil {
    private static Logger logger = Logger.getLogger(ShellExecutorUtil.class);

    public static final String Exec_Flag_Success = "1";
    public static final String Exec_Flag_Failure = "-1";

    public static Map<String, CommandParameters> cache = new ConcurrentHashMap<String, CommandParameters>();

    public void parseResult(ScheduleLog schLog, ScheduleLog befLog) {
        try {
            CommandParameters req = cache.get(schLog.getRequestId());

            CommandResults rsp = new CommandResults();


            rsp.setTASK_ID(schLog.getSchId());
            rsp.setTYPE(req.getTYPE());
            rsp.setBEGIN_TIME(DateFormater.stringToDateTime((String)schLog.getExtInfo().get("SH_EXEC_TIME")));
            rsp.setEND_TIME(DateFormater.stringToDateTime(schLog.getEndTime()));
            rsp.setEXEC_LOG("执行指令结束\n");
            rsp.setEXEC_RESULT(getExecResult(schLog, befLog));
            rsp.setEXEC_FLAG(getExecFlag(schLog, befLog));
            rsp.setFAILURE_REASION(getFailureReason(schLog, befLog));
            rsp.setCUSTOMPROP(req.getCUSTOMPROP());

            logger.info("Send CCCP Exec Result:" + JSONUtil.toJSON(rsp));
            TunnelFactory.getTunnel(MQConstants.Q_CCAGENT_RESULT).writeData(rsp);
        } catch (ParseException e) {
            logger.error("Exception while parseResult:\n log1=" + JSONUtil.toJSON(schLog) + "\n log2=" + JSONUtil.toJSON(befLog), e);
        }finally {
            cache.remove(schLog.getRequestId());

            File tmpScriptFile = new File(ShellExecutorThread.SCRIPT_FILE_DIR, schLog.getRequestId() + ".sh");
            if(tmpScriptFile.exists() && tmpScriptFile.canWrite()){
                logger.info("Delete Temp Script File " + tmpScriptFile.getAbsolutePath());
                tmpScriptFile.delete();
            }
        }

    }

    private String getFailureReason(ScheduleLog schLog, ScheduleLog befLog) {
        if ("err_out".equals(schLog.getType())) {
            return schLog.getLogInfo();
        } else if ("err_out".equals(befLog.getType())) {
            return befLog.getLogInfo();
        } else {
            return "";
        }
    }

    private String getExecResult(ScheduleLog schLog, ScheduleLog befLog) {
        if ("std_out".equals(schLog.getType())) {
            return schLog.getLogInfo();
        } else if ("std_out".equals(befLog.getType())) {
            return befLog.getLogInfo();
        } else {
            return "";
        }
    }


    /**
     * 脚本执行状态
     * @param schLog
     * @param befLog
     * @return -1:失败 1:成功
     */
    private String getExecFlag(ScheduleLog schLog, ScheduleLog befLog) {
        if ("err_out".equals(schLog.getType()) && !"".equals(schLog.getLogInfo())) {
            return Exec_Flag_Failure;
        } else if ("err_out".equals(befLog.getType()) && !"".equals(befLog.getLogInfo())) {
            return Exec_Flag_Failure;
        } else {
            return Exec_Flag_Success;
        }
    }

}