use of alluxio.shell.command.ShellCommand in project alluxio by Alluxio.
the class AlluxioShell method loadCommands.
/**
* Uses reflection to get all the {@link ShellCommand} classes and store them in a map.
*/
private void loadCommands() {
String pkgName = ShellCommand.class.getPackage().getName();
Reflections reflections = new Reflections(pkgName);
for (Class<? extends ShellCommand> cls : reflections.getSubTypesOf(ShellCommand.class)) {
// Only instantiate a concrete class
if (!Modifier.isAbstract(cls.getModifiers())) {
ShellCommand cmd;
try {
cmd = CommonUtils.createNewClassInstance(cls, new Class[] { FileSystem.class }, new Object[] { mFileSystem });
} catch (Exception e) {
throw Throwables.propagate(e);
}
mCommands.put(cmd.getCommandName(), cmd);
}
}
}
use of alluxio.shell.command.ShellCommand in project alluxio by Alluxio.
the class AlluxioShell method run.
/**
* Handles the specified shell command request, displaying usage if the command format is invalid.
*
* @param argv [] Array of arguments given by the user's input from the terminal
* @return 0 if command is successful, -1 if an error occurred
*/
public int run(String... argv) {
if (argv.length == 0) {
printUsage();
return -1;
}
// Sanity check on the number of arguments
String cmd = argv[0];
ShellCommand command = mCommands.get(cmd);
if (command == null) {
// Unknown command (we didn't find the cmd in our dict)
String[] replacementCmd = getReplacementCmd(cmd);
if (replacementCmd == null) {
System.out.println(cmd + " is an unknown command.\n");
printUsage();
return -1;
}
// Handle command alias, and print out WARNING message for deprecated cmd.
String deprecatedMsg = "WARNING: " + cmd + " is deprecated. Please use " + StringUtils.join(replacementCmd, " ") + " instead.";
System.out.println(deprecatedMsg);
LOG.warn(deprecatedMsg);
String[] replacementArgv = (String[]) ArrayUtils.addAll(replacementCmd, ArrayUtils.subarray(argv, 1, argv.length));
return run(replacementArgv);
}
String[] args = Arrays.copyOfRange(argv, 1, argv.length);
CommandLine cmdline = command.parseAndValidateArgs(args);
if (cmdline == null) {
printUsage();
return -1;
}
// Handle the command
try {
command.run(cmdline);
return 0;
} catch (Exception e) {
System.out.println(e.getMessage());
LOG.error("Error running " + StringUtils.join(argv, " "), e);
return -1;
}
}
Aggregations