Search in sources :

Example 1 with ConsoleCommand

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");
}
Also used : Connect(edu.umass.cs.gnsclient.console.commands.Connect) ConsoleCommand(edu.umass.cs.gnsclient.console.commands.ConsoleCommand) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ConsoleCommand

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());
}
Also used : FileNameCompletor(jline.FileNameCompletor) ArgumentCompletor(jline.ArgumentCompletor) ConsoleCommand(edu.umass.cs.gnsclient.console.commands.ConsoleCommand) LinkedList(java.util.LinkedList) TreeSet(java.util.TreeSet) SimpleCompletor(jline.SimpleCompletor) SimpleCompletor(jline.SimpleCompletor) ArgumentCompletor(jline.ArgumentCompletor) FileNameCompletor(jline.FileNameCompletor) Completor(jline.Completor)

Example 3 with ConsoleCommand

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");
    }
}
Also used : ConsoleCommand(edu.umass.cs.gnsclient.console.commands.ConsoleCommand)

Example 4 with ConsoleCommand

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
        }
    }
}
Also used : ConsoleCommand(edu.umass.cs.gnsclient.console.commands.ConsoleCommand) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(org.json.JSONObject)

Example 5 with ConsoleCommand

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");
}
Also used : ConsoleCommand(edu.umass.cs.gnsclient.console.commands.ConsoleCommand)

Aggregations

ConsoleCommand (edu.umass.cs.gnsclient.console.commands.ConsoleCommand)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Connect (edu.umass.cs.gnsclient.console.commands.Connect)1 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 ArgumentCompletor (jline.ArgumentCompletor)1 Completor (jline.Completor)1 FileNameCompletor (jline.FileNameCompletor)1 SimpleCompletor (jline.SimpleCompletor)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1