Esempio n. 1
0
  public static void main(String... args) {

    ConsoleReader console;
    try {
      console = new ConsoleReader();
      String pattern;
      String iterationsString;
      if (!DEBUG) {
        pattern = console.readLine("Ant Pattern: ");
        iterationsString = console.readLine("Iterations: ");
      } else {
        pattern = DEBUG_PATTERN;
        iterationsString = DEBUG_ITERATIONS;
      }
      verifyPattern(pattern);

      long iterations = verifyIterations(iterationsString);
      LangtonsAnt app = new LangtonsAnt(pattern, iterations);
      app.initColours();
      app.initAnt();
      app.simulate();
      app.render();
    } catch (IOException e) {
      System.out.println("Fatal IO Exception: " + e.getMessage());
      System.exit(1);
    }
  }
Esempio n. 2
0
 public void run() {
   ConsoleReader console = null;
   try {
     console = new ConsoleReader(in, out);
     console.println(WELCOME_MESSAGE);
     String statement = null;
     while ((statement = console.readLine(PROMPT)) != null) {
       if ("exit".equals(statement.trim())) {
         return;
       } else {
         try {
           dynJS.eval(context, statement);
         } catch (DynJSException e) {
           console.println(e.getClass().getSimpleName());
           console.println(e.getLocalizedMessage());
           console.println("Error parsing statement: " + statement.toString());
         } catch (Exception e) {
           e.printStackTrace(new PrintWriter(out));
         }
       }
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Esempio n. 3
0
 @Override
 public String promptPassword() throws IOException {
   String prompt = this.console.getPrompt();
   String password = console.readLine("Password: ", new Character('\0'));
   console.setPrompt(prompt);
   return password;
 }
Esempio n. 4
0
  private AccessToken getNewAccessToken(
      ConnectionConfig connectionInfo, PrintStream output, boolean debug) throws IOException {

    AuthenticationClient authenticationClient = getAuthenticationClient(connectionInfo);

    Properties properties = new Properties();
    properties.put(
        BasicAuthenticationClient.VERIFY_SSL_CERT_PROP_NAME,
        String.valueOf(clientConfig.isVerifySSLCert()));

    // obtain new access token via manual user input
    output.printf(
        "Authentication is enabled in the CDAP instance: %s.\n", connectionInfo.getHostname());
    ConsoleReader reader = new ConsoleReader();
    for (Credential credential : authenticationClient.getRequiredCredentials()) {
      String prompt = "Please, specify " + credential.getDescription() + "> ";
      String credentialValue;
      if (credential.isSecret()) {
        credentialValue = reader.readLine(prompt, '*');
      } else {
        credentialValue = reader.readLine(prompt);
      }
      properties.put(credential.getName(), credentialValue);
    }

    authenticationClient.configure(properties);
    AccessToken accessToken = authenticationClient.getAccessToken();

    if (accessToken != null) {
      if (saveAccessToken(accessToken, connectionInfo.getHostname()) && debug) {
        output.printf(
            "Saved access token to %s\n",
            getAccessTokenFile(connectionInfo.getHostname()).getAbsolutePath());
      }
    }

    return accessToken;
  }
Esempio n. 5
0
  public void run() {

    //
    String welcome = shell.getWelcome();
    writer.println(welcome);
    writer.flush();

    //
    while (true) {
      String prompt = getPrompt();
      String line;
      try {
        writer.println();
        writer.flush();
        if ((line = reader.readLine(prompt)) == null) {
          break;
        }
      } catch (IOException e) {
        // What should we do other than that ?
        break;
      }

      //
      ShellProcess process = shell.createProcess(line);
      JLineProcessContext context = new JLineProcessContext(this);
      current.set(process);
      try {
        process.execute(context);
        try {
          context.latch.await();
        } catch (InterruptedException ignore) {
          // At the moment
        }
      } finally {
        current.set(null);
      }
      ShellResponse response = context.resp.get();

      //
      if (response instanceof ShellResponse.Cancelled) {
        // Do nothing
      } else if (response instanceof ShellResponse.Close) {
        break;
      } else {
        response.getReader().writeAnsiTo(writer);
        writer.flush();
      }
    }
  }
Esempio n. 6
0
 @Override
 public void start(Application app) throws IOException {
   super.start(app);
   updatePrompt();
   // register completors
   console.addCompleter(new CompositeCompletor(this, app.getCommandRegistry()));
   String line = console.readLine();
   while (line != null) {
     line = line.trim();
     try {
       if (line.length() > 0) {
         if (!execute(line)) {
           break;
         }
         // println();
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
     line = console.readLine();
   }
   console.print("Bye");
   console.println();
 }
Esempio n. 7
0
  private void runAsciiMode() throws IOException {
    Ansi.setEnabled(_useColors);
    while (true) {
      String prompt = Ansi.ansi().bold().a(_userAdminShell.getPrompt()).boldOff().toString();
      Object result;
      try {
        String str = _console.readLine(prompt);
        try {
          if (str == null) {
            throw new CommandExitException();
          }
          result = _userAdminShell.executeCommand(str);
        } catch (IllegalArgumentException e) {
          result = e.toString();
        } catch (SerializationException e) {
          result = "There is a bug here, please report to [email protected]";
          _logger.error("This must be a bug, please report to [email protected].", e);
        } catch (CommandSyntaxException e) {
          result = e;
        } catch (CommandExitException e) {
          break;
        } catch (CommandPanicException e) {
          result =
              "Command '"
                  + str
                  + "' triggered a bug ("
                  + e.getTargetException()
                  + "); the service log file contains additional information. Please "
                  + "contact [email protected].";
        } catch (CommandException e) {
          result = e.getMessage();
        } catch (NoRouteToCellException e) {
          result = "Cell name does not exist or cell is not started: " + e.getMessage();
          _logger.warn(
              "The cell the command was sent to is no " + "longer there: {}", e.getMessage());
        } catch (RuntimeException e) {
          result =
              String.format(
                  "Command '%s' triggered a bug (%s); please"
                      + " locate this message in the log file of the admin service and"
                      + " send an email to [email protected] with this line and the"
                      + " following stack-trace",
                  str, e);
          _logger.error((String) result, e);
        }
      } catch (InterruptedIOException e) {
        _console.getCursorBuffer().clear();
        _console.println();
        result = null;
      } catch (InterruptedException e) {
        _console.println("^C");
        _console.flush();
        _console.getCursorBuffer().clear();
        result = null;
      } catch (IOException e) {
        throw e;
      } catch (Exception e) {
        result = e.getMessage();
        if (result == null) {
          result = e.getClass().getSimpleName() + ": (null)";
        }
      }

      if (result != null) {
        if (result instanceof CommandSyntaxException) {
          CommandSyntaxException e = (CommandSyntaxException) result;
          Ansi sb = Ansi.ansi();
          sb.fg(RED).a("Syntax error: ").a(e.getMessage()).newline();
          String help = e.getHelpText();
          if (help != null) {
            sb.fg(CYAN);
            sb.a("Help : ").newline();
            sb.a(help);
          }
          _console.println(sb.reset().toString());
        } else {
          String s;
          s = Strings.toMultilineString(result);
          if (!s.isEmpty()) {
            _console.println(s);
            _console.flush();
          }
        }
      }
      _console.flush();
    }
  }
Esempio n. 8
0
  public void run() throws IOException {
    CHIAState chiaState = CHIAState.AUTOMATAMODE;

    CandidateListCompletionHandler ch = new CandidateListCompletionHandler();
    console.setCompletionHandler(ch);
    ch.setPrintSpaceAfterFullCompletion(false);

    CHIAAutomataConsole chiaAutomataConsole = new CHIAAutomataConsole();
    CHIAReplacementConsole chiaReplacementConsole = new CHIAReplacementConsole();

    List<Completer> replacementCompleter =
        chiaReplacementConsole.getCompleter().computeCompleters();
    replacementCompleter.add(new StringsCompleter(AUTOMATA_MODE));
    Completer replacementCom = new AggregateCompleter(replacementCompleter);

    List<Completer> automataCompletor = this.automataConsole.getCompleter().computeCompleters();
    automataCompletor.add(new StringsCompleter(REPLACEMENT_MODE));
    Completer automataCom = new AggregateCompleter(automataCompletor);

    console.addCompleter(new AggregateCompleter(automataCompletor));

    String line;

    while ((line = console.readLine()) != null) {
      try {
        if (line.equals(REPLACEMENT_MODE)) {

          chiaState = CHIAState.REPLACEMENTMODE;
          this.removeCompleter();
          console.removeCompleter(automataCom);
          console.addCompleter(replacementCom);
          out.write("replacement mode enabled" + "\n");
          out.flush();

        } else {
          if (line.equals(AUTOMATA_MODE)) {
            chiaState = CHIAState.AUTOMATAMODE;
            this.removeCompleter();

            console.removeCompleter(replacementCom);
            console.addCompleter(automataCom);

            out.write("automata mode enabled" + "\n");
            out.flush();

          } else {
            if (QUIT.equalsIgnoreCase(line) || EXIT.equalsIgnoreCase(line)) {
              System.exit(0);
            } else {
              if (line.equalsIgnoreCase(CLS)) {
                console.clearScreen();
              } else {
                if (chiaState.equals(CHIAState.REPLACEMENTMODE)) {
                  ReplacementAction action =
                      chiaReplacementConsole
                          .getCompleter()
                          .parse(line, chiaReplacementConsole, out);
                  if (chiaReplacementConsole.getChiaState().isPerformable(action)) {
                    action.perform(chiaReplacementConsole);
                    chiaReplacementConsole.setChiaState(
                        chiaReplacementConsole.getChiaState().next(action));
                  } else {
                    out.write(
                        "Action: "
                            + line
                            + " not performable into the state "
                            + chiaReplacementConsole.getChiaState().getClass()
                            + "\n");
                    out.flush();
                  }

                } else {
                  if (chiaState.equals(CHIAState.AUTOMATAMODE)) {
                    AutomataAction action =
                        this.automataConsole.getCompleter().parse(line, chiaAutomataConsole, out);
                    if (this.automataConsole.getChiaState().isPerformable(action)) {
                      action.perform(chiaAutomataConsole);
                      this.automataConsole.setChiaState(
                          this.automataConsole.getChiaState().next(action));
                    } else {
                      out.write(
                          "Action: "
                              + line
                              + " not performable into the state "
                              + this.automataConsole.getChiaState().getClass()
                              + "\n");
                      out.flush();
                    }
                  }
                }
              }
            }
          }
        }
      } catch (Exception e) {
        out.write(e.getMessage());
        out.flush();
      }
    }
  }
Esempio n. 9
0
 @Override
 public String readLine() throws IOException {
   return reader.readLine();
 }