package com.eactive.eai.agent.jms;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.flowcontrol.jms.QueueMonitorManager;
import com.eactive.eai.flowcontrol.jms.QueueMonitorVO;

public class AddQueueMonitorCommand extends Command {
	private static final long serialVersionUID = 1L;

	public AddQueueMonitorCommand(String name) {
		super(name);
	}

	public AddQueueMonitorCommand() {
		super("AddQueueMonitorCommand");
	}

	public Object execute() throws CommandException {

		Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
		if (logger.isInfo())
			logger.info(this.name + " is executed");

		if (!(args instanceof QueueMonitorVO)) {
			String rspErrorCode = "RECEAIMCM001";
			String msg = makeException(rspErrorCode, null);
			if (logger.isError())
				logger.error(msg);
			throw new CommandException(msg);
		}

		QueueMonitorVO vo = (QueueMonitorVO) args;

		try {
			String localServerName = EAIServerManager.getInstance().getLocalServerName();
			// 추가시에는 인스턴스 정보를 확인한다.
			if (localServerName != null && localServerName.equals(vo.getSevrInstncName())) {
				QueueMonitorManager manager = QueueMonitorManager.getInstance();
				manager.setQueueMonitor(vo);
			}
		} catch (Exception e) {
			String rspErrorCode = "RECEAIMCM002";
			String msg = makeException(rspErrorCode, e);
			if (logger.isError())
				logger.error(msg, e);
			throw new CommandException(msg);
		}
		return "success";
	}

}
