use of edu.umass.cs.gnsclient.console.commands.ConsoleCommand in project GNS by MobilityFirst.
the class ConsoleModule method handlePrompt.
/**
* Handle a series of commands
*
*/
public void handlePrompt() {
quit = false;
// Try to connect to the GNS
try {
new Connect(this).parse("");
} catch (Exception e) {
printString("Couldn't connect to GNS...\n");
}
if (useGnsDefaults) {
useGnsDefaults();
}
while (!quit) {
Map<String, ConsoleCommand> hashCommands = getHashCommands();
try {
String commandLine;
if (silent) {
console.flushConsole();
commandLine = readLineBypassJLine();
} else {
commandLine = console.readLine(getPromptString());
}
if (commandLine == null) {
quit();
break;
}
if (commandLine.equals("")) {
continue;
}
handleCommandLine(commandLine, hashCommands);
} catch (UnknownCommandException e) {
printString(e.getMessage() + "\n");
} catch (Exception e) {
printString("Error during console command execution: " + e.getMessage() + "\n");
e.printStackTrace();
}
}
GNSClientConfig.getLogger().fine("Quitting");
}
use of edu.umass.cs.gnsclient.console.commands.ConsoleCommand in project GNS by MobilityFirst.
the class ConsoleModule method loadCompletor.
/**
* Loads the commands for this module
*/
protected void loadCompletor() {
List<Completor> completors = new LinkedList<>();
int size = commands.size();
if (size > 0) {
TreeSet<String> set = new TreeSet<>();
Iterator<ConsoleCommand> it = commands.iterator();
while (it.hasNext()) {
set.add(it.next().getCommandName());
}
completors.add(new SimpleCompletor(set.toArray(new String[size])));
}
completors.add(new FileNameCompletor());
Completor[] completorsArray = completors.toArray(new Completor[completors.size()]);
consoleCompletor = new ArgumentCompletor(completorsArray, new CommandDelimiter());
}
use of edu.umass.cs.gnsclient.console.commands.ConsoleCommand in project GNS by MobilityFirst.
the class ConsoleModule method help.
/**
* Display help for this module
*
*/
public void help() {
printString("Commands available for the main menu are:\n");
ConsoleCommand command;
Iterator<ConsoleCommand> it = commands.iterator();
while (it.hasNext()) {
command = it.next();
printString(command.getCommandName() + " " + command.getCommandParameters() + "\n");
printString(" " + command.getCommandDescription() + "\n");
}
}
use of edu.umass.cs.gnsclient.console.commands.ConsoleCommand in project GNS by MobilityFirst.
the class ConsoleModule method addCommands.
/**
* Add commands to this module. Commands instances are created by reflection
* based on the command class names passed in parameter
*
* @param commandClasses a String[] containing the class names of the command
* to instantiate
* @param commands Set where the commands are added
*/
protected void addCommands(String[] commandClasses, Set<ConsoleCommand> commands) {
for (int i = 0; i < commandClasses.length; i++) {
String commandClass = commandClasses[i].trim();
Class<?> clazz;
try {
clazz = Class.forName(commandClass);
Constructor<?> constructor;
try {
constructor = clazz.getConstructor(new Class<?>[] { this.getClass() });
} catch (NoSuchMethodException e) {
constructor = clazz.getConstructor(new Class<?>[] { ConsoleModule.class });
}
ConsoleCommand command = (ConsoleCommand) constructor.newInstance(new Object[] { this });
commands.add(command);
} catch (ClassNotFoundException | SecurityException | NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// fail silently: the command won't be added to the commands list
}
}
}
use of edu.umass.cs.gnsclient.console.commands.ConsoleCommand in project GNS by MobilityFirst.
the class ConsoleModule method handleCommandLine.
/**
* Handle module command
*
* @param commandLine the command line to handle
* @param hashCommands the list of commands available for this module
* @throws Exception if fails *
*/
public final void handleCommandLine(String commandLine, Map<String, ConsoleCommand> hashCommands) throws Exception {
ConsoleCommand command = findConsoleCommand(commandLine, hashCommands);
GNSClientConfig.getLogger().log(Level.FINE, "Command:{0}", command);
if (command != null) {
command.execute(commandLine.substring(command.getCommandName().length()));
return;
}
throw new UnknownCommandException("Command " + commandLine + " is not supported here");
}
Aggregations