@Override
    public void run() {

      BufferedReader in = null;
      PrintWriter out = null;

      try {
        in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
        OutputStream outputStream = socket.getOutputStream();
        out = new PrintWriter(outputStream);

        String line = in.readLine();
        CommandRequest request = CommandRequest.parse(line);

        out.print("HTTP/1.1 200 OK\r\n\r\n");
        out.flush();
        if (StringUtils.isEmpty(request.getCommand())) {
          out.println("Command is blank");
          out.flush();
          return;
        }

        CommandProcessor commandProcessor = getCommandProcessor(request.getCommand());
        if (commandProcessor != null) {
          commandProcessor.execute(outputStream, request);
        } else {
          out.println("Can not find the command:[" + request.getCommand() + "]");
        }
        out.flush();

      } catch (Throwable t) {
        LOGGER.error("EventRunnable error", t);

        try {
          if (out != null) {
            out.println("CommandCenter error, message is " + t.getMessage());
            out.flush();
          }
        } catch (Exception e) {
          LOGGER.error("EventRunnable error", t);
        }

      } finally {
        try {
          if (out != null) {
            out.close();
          }
          if (in != null) {
            in.close();
          }
          socket.close();
        } catch (Exception e) {
          LOGGER.error("EventRunnable close resource error", e);
        }
      }
    }
Пример #2
0
 public void execute() {
   boolean continueExecution = true;
   while (continueExecution) {
     this.console.prompt("contacts> ");
     String commandLine = this.console.readLine();
     if (commandLine.trim().isEmpty()) {
       continue;
     }
     try {
       CommandContext context = createCommandContext(commandLine);
       CommandProcessor processor = ShellUtil.createProcessor(context.commandName());
       processor.setConsole(this.console);
       processor.setRepository(this.repository);
       processor.setContentManager(this.manager);
       if (processor.canExecute(context)) {
         continueExecution = processor.execute(context);
       }
     } catch (ParseException e) {
       this.console.error("ERROR: Could not process command (%s)%n", e.getMessage());
     }
   }
 }