ServerCmd sCmd = new ServerCmd();
// Execute the requested command.
String[] arguments = cmd.getArgs();
String cmdName = arguments[0];
CmdType cmdType = null;
try {
cmdType = CmdType.lookup(cmdName);
if (CmdType.ADDUSER.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.addUser(arguments[1], arguments[2]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.REMOVEUSER.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.removeUser(arguments[1]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.LISTUSERS.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
sCmd.print(probe.listUsers(), System.out);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.ADDDOMAIN.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.addDomain(arguments[1]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.REMOVEDOMAIN.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.removeDomain(arguments[1]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.CONTAINSDOMAIN.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.containsDomain(arguments[1]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.LISTDOMAINS.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
sCmd.print(probe.listDomains(), System.out);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.LISTMAPPINGS.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
sCmd.print(probe.listMappings(), System.out);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.LISTUSERDOMAINMAPPINGS.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
Collection<String> userDomainMappings = probe.listUserDomainMappings(arguments[1], arguments[2]);
sCmd.print(userDomainMappings.toArray(new String[userDomainMappings.size()]), System.out);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.ADDADDRESSMAPPING.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.addAddressMapping(arguments[1], arguments[2], arguments[3]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.REMOVEADDRESSMAPPING.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.removeAddressMapping(arguments[1], arguments[2], arguments[3]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.ADDREGEXMAPPING.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.addRegexMapping(arguments[1], arguments[2], arguments[3]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.REMOVEREGEXMAPPING.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.removeRegexMapping(arguments[1], arguments[2], arguments[3]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.SETPASSWORD.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.setPassword(arguments[1], arguments[2]);
} else {
printUsage();
System.exit(1);
}
} else if (CmdType.COPYMAILBOX.equals(cmdType)) {
if (cmdType.hasCorrectArguments(arguments.length)) {
probe.copyMailbox(arguments[1], arguments[2]);
} else {
printUsage();
System.exit(1);
}
} else {
System.err.println("Unrecognized command: " + cmdName + ".");
printUsage();
System.exit(1);
}
} catch (Exception e) {
sCmd.onException(e, System.err);
System.exit(1);
}
sCmd.print(new String[]{cmdType.getCommand() + " command executed sucessfully in "
+ (Calendar.getInstance().getTimeInMillis() - start) + " ms."}, System.out);
System.exit(0);
}