package com.eactive.eai.agent.command; import java.io.Serializable; import java.util.Map; import lombok.Getter; import lombok.NonNull; import lombok.Setter; import lombok.experimental.Accessors; import lombok.experimental.SuperBuilder; @SuperBuilder @Accessors(chain = true) public abstract class Command implements Serializable { private static final long serialVersionUID = 1L; @Getter @Setter @NonNull protected String name; @Setter protected Object args; private Map errorCode; protected Command() { this.name = this.getClass().getName(); } protected Command(String name) { this.name = name; } public abstract Object execute() throws CommandException; protected String makeException(String rspErrorCode, Throwable cause) { String[] msgArgs = new String[2]; msgArgs[0] = name; if (cause == null) { msgArgs[1] = args.toString(); } else { msgArgs[1] = cause.getMessage(); } return makeMessage(errorCode.get(rspErrorCode), msgArgs); } private static String makeMessage(String msg, String[] args) { String keyword = null; String head = null; String tail = null; int idx = -1; for (int i = 0; i < args.length; i++) { keyword = "{" + (i + 1) + "}"; idx = msg.indexOf(keyword); if (idx == -1) { continue; } head = msg.substring(0, idx); tail = msg.substring(idx + keyword.length()); msg = head + args[i] + tail; } return msg; } }