package com.eactive.eai.agent.transformer;

import com.eactive.eai.agent.AgentUtil;
import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.transformer.layout.LayoutManager;

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

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

	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 {
			LayoutManager manager = LayoutManager.getManager();

			String keyName = (String) args;

			if (keyName != null) {
				if ("ALL".equals(keyName)) {
					manager.permanentReload();
					if (logger.isWarn()) {
						logger.warn(this.name + "] Permanent Cache가  Reload  되었습니다.");
					}
				} else {
					throw new Exception("잘못된 파라미터입니다. : " + keyName);
				}
			}
		} catch (Exception e) {
			String rspErrorCode = "EAIINIT003";
			String msg = makeException(rspErrorCode, e);
			if (logger.isError())
				logger.error(msg);
			// e.printStackTrace();
			throw new CommandException(msg);
		}
		return "success";
	}

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

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