Exemple #1
0
  private void repl() {
    Ansi.setEnabled(false);
    while (true) {
      Object error = null;
      try {
        String str = _reader.readLine();
        try {
          if (str == null) {
            throw new CommandExitException();
          }
          Object result = _userAdminShell.executeCommand(str);
          String s = Strings.toString(result);
          if (!s.isEmpty()) {
            _writer.println(s);
          }
          _writer.flush();
        } catch (IllegalArgumentException e) {
          error = e.toString();
        } catch (SerializationException e) {
          error = "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) {
          error = e;
        } catch (CommandEvaluationException | CommandAclException e) {
          error = e.getMessage();
        } catch (CommandExitException e) {
          break;
        } catch (CommandPanicException e) {
          error =
              "Command '"
                  + str
                  + "' triggered a bug ("
                  + e.getTargetException()
                  + "); the service log file contains additional information. Please "
                  + "contact [email protected].";
        } catch (CommandThrowableException e) {
          Throwable cause = e.getTargetException();
          if (cause instanceof CacheException) {
            error = cause.getMessage();
          } else {
            error = cause.toString();
          }
        } catch (CommandException e) {
          error = "There is a bug here, please report to [email protected]: " + e.getMessage();
          _logger.warn("Unexpected exception, please report this " + "bug to [email protected]");
        } catch (NoRouteToCellException e) {
          error = "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) {
          error =
              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) error, e);
        }
      } catch (InterruptedException e) {
        error = null;
      } catch (Exception e) {
        error = e.getMessage();
        if (error == null) {
          error = e.getClass().getSimpleName() + ": (null)";
        }
      }

      if (error != null) {
        if (error instanceof CommandSyntaxException) {
          CommandSyntaxException e = (CommandSyntaxException) error;
          _error.append("Syntax error: ").println(e.getMessage());
        } else {
          _error.println(error);
        }
        _error.flush();
      }
    }
  }
  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();
    }
  }