View Javadoc

    1   /**
    2    *
    3    */
    4   package org.mortbay.jetty.plugin.util;
    5
    6   import java.io.IOException;
    7
    8   import org.mortbay.jetty.plugin.AbstractJettyMojo;
    9
    10  public class ConsoleScanner extends Thread
    11  {
    12
    13      private final AbstractJettyMojo mojo;
    14
    15      public ConsoleScanner(AbstractJettyMojo mojo)
    16      {
    17          this.mojo = mojo;
    18          setName("Console scanner");
    19          setDaemon(true);
    20      }
    21
    22      public void run()
    23      {
    24          try
    25          {
    26              while (true)
    27              {
    28                  checkSystemInput();
    29                  getSomeSleep();
    30              }
    31          }
    32          catch (IOException e)
    33          {
    34              mojo.getLog().warn(e);
    35          }
    36      }
    37
    38      private void getSomeSleep()
    39      {
    40          try
    41          {
    42              Thread.sleep(500);
    43          }
    44          catch (InterruptedException e)
    45          {
    46              mojo.getLog().debug(e);
    47          }
    48      }
    49
    50      private void checkSystemInput() throws IOException
    51      {
    52          while (System.in.available() > 0) {
    53              int inputByte = System.in.read();
    54              if (inputByte >= 0)
    55              {
    56                  char c = (char)inputByte;
    57                  if (c == '\n') {
    58                      restartWebApp();
    59                  }
    60              }
    61          }
    62      }
    63
    64
    65      /**
    66       * Skip buffered bytes of system console.
    67       */
    68      private void clearInputBuffer()
    69      {
    70          try
    71          {
    72              while (System.in.available() > 0)
    73              {
    74                  // System.in.skip doesn't work properly. I don't know why
    75                  long available = System.in.available();
    76                  for (int i = 0; i < available; i++)
    77                  {
    78                      if (System.in.read() == -1)
    79                      {
    80                          break;
    81                      }
    82                  }
    83              }
    84          }
    85          catch (IOException e)
    86          {
    87              mojo.getLog().warn("Error discarding console input buffer", e);
    88          }
    89      }
    90
    91      private void restartWebApp()
    92      {
    93          try
    94          {
    95              mojo.restartWebApp(false);
    96              // Clear input buffer to discard anything entered on the console
    97              // while the application was being restarted.
    98              clearInputBuffer();
    99          }
    100         catch (Exception e)
    101         {
    102             mojo.getLog().error(
    103                             "Error reconfiguring/restarting webapp after a new line on the console",
    104                             e);
    105         }
    106     }
    107 }