Package org.foo.shell.commands

Source Code of org.foo.shell.commands.ExecuteCommand

package org.foo.shell.commands;

import java.io.PrintStream;
import java.util.Map;

import org.foo.shell.Command;

public class ExecuteCommand implements Command {
  private final Map<String, Command> m_commands;

  public ExecuteCommand(Map<String, Command> commands) {
    m_commands = commands;
  }

  public void exec(String args, PrintStream out, PrintStream err) {
    int idx = args.indexOf(' ');

    boolean found = false;

    Command command = m_commands.get((idx > 0) ? args.substring(0, idx) : args);

    if (command != null) {
      found = true;

      try {
        command.exec((idx > 0) ? args.substring(idx) : null, out, err);
      } catch (Exception ex) {
        ex.printStackTrace(err);
        out.println("Unable to execute: " + args);
      }
    }

    if (!found && !(args.trim().length() == 0)) {
      out.println("Unknown command: " + args);
    }
  }
}
TOP

Related Classes of org.foo.shell.commands.ExecuteCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.