package com.ext.eai.agent.submessage;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.submessage.SubMessageManager;
import com.eactive.eai.common.util.Logger;

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

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

	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);
		}

		try {
			SubMessageManager manager = SubMessageManager.getInstance();
			String keyName = (String) args;
			if (keyName != null) {
				if ("ALL".equals(keyName)) {
					manager.reload();
					if (logger.isWarn())
						logger.warn(this.name + "] 모든 정보가 Reload 되었습니다.");
				} else {
					manager.reload(keyName);
					if (logger.isWarn())
						logger.warn(this.name + "] " + keyName + " 에 대한 정보가 Reload 되었습니다.");
				}
			}
		} catch (Exception e) {
			String rspErrorCode = "RECEAIMCM002";
			String msg = makeException(rspErrorCode, e);
			if (logger.isError())
				logger.error(msg);
			throw new CommandException(msg);
		}
		return "success";
	}

//	   public static void main(String args[]) throws Exception {
//	   test();
//}

//    private static void test() throws Exception {
//        Command command = new ReloadSubMessageCommand();
//        command.setArgs("ALL");
//        AgentUtil.broadcast(command);
//    }
}
