package com.eactive.eai.agent.inflow;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.inflow.AbstractInflowControlManager;
import com.eactive.eai.common.inflow.InflowControlUtil;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.custom.inflow.ClientDualInflowControlManager;

public class RemoveInflowClientControlCommand extends Command {

	private static final long serialVersionUID = 1L;

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

	public Object execute() throws CommandException {
		Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
		if (logger.isInfo())
			logger.info(this.name + " is executed");

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

		String key = (String) args;
		try {
			AbstractInflowControlManager base = InflowControlUtil.getInflowControlManager();
			if (!(base instanceof ClientDualInflowControlManager)) {
				throw new IllegalStateException("ClientDualInflowControlManager 가 활성화되지 않았습니다.");
			}
			ClientDualInflowControlManager manager = (ClientDualInflowControlManager) base;
			manager.removeClient(key);
			if (logger.isWarn())
				logger.warn(this.name + "] " + key + " removed.");
		} catch (Exception e) {
			String rspErrorCode = "RECEAIMCM002";
			String msg = makeException(rspErrorCode, e);
			if (logger.isError())
				logger.error(msg, e);
			throw new CommandException(msg);
		}
		return "success";
	}
}
