package com.eactive.eai.agent.encryption;

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

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

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

	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 = "EAIINIT001";
			String msg = makeException(rspErrorCode, null);
			if (logger.isError())
				logger.error(msg);
			throw new CommandException(msg);
		}

		try {
			EncryptionManager manager = EncryptionManager.getInstance();

			String keyName = (String) args;

			if (keyName != null) {
				if ("ENC".equals(keyName)) {
					manager.reload();
					if (logger.isWarn())
						logger.warn(this.name + "] 암호화 여부가 Reload  되었습니다.");
				} else {
//                    String rspErrorCode = "EAIINIT002";
					String msg = "EAIINIT002  암호화 여부 동기화중 keyName [" + keyName + "] 오류가 발생하였습니다. ";
					if (logger.isError())
						logger.error(msg);
					throw new CommandException(msg);
				}
			}
		} catch (Exception e) {
			String rspErrorCode = "EAIINIT003";
			String msg = makeException(rspErrorCode, e);
			if (logger.isError())
				logger.error(msg, e);
			throw new CommandException(msg);
		}
		return "success";
	}

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

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