View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.maven.plugin;
20  
21  import java.io.IOException;
22  
23  /**
24   * ConsoleScanner
25   *
26   * Read input from stdin
27   */
28  public class ConsoleScanner extends Thread 
29  {
30      private final AbstractJettyMojo mojo;
31      
32      public ConsoleScanner(AbstractJettyMojo mojo) 
33      {
34          this.mojo = mojo;
35          setName("Console scanner");
36          setDaemon(true);
37      }
38      
39      public void run() 
40      {  
41          try 
42          {
43              while (true) 
44              {
45                  checkSystemInput();
46                  getSomeSleep();
47              }
48          } 
49          catch (IOException e) 
50          {
51              mojo.getLog().warn(e);
52          }
53      }
54      
55      private void getSomeSleep() 
56      {
57          try 
58          {
59              Thread.sleep(500);
60          } 
61          catch (InterruptedException e) 
62          {
63              mojo.getLog().debug(e);
64          }
65      }
66      
67      private void checkSystemInput() throws IOException 
68      {     
69          while (System.in.available() > 0) {
70              int inputByte = System.in.read();
71              if (inputByte >= 0) 
72              {
73                  char c = (char)inputByte;
74                  if (c == '\n') {
75                      restartWebApp();
76                  }
77              }
78          }
79      }
80      
81      /**
82       * Skip buffered bytes of system console.
83       */
84      private void clearInputBuffer() 
85      {
86          try
87          {
88              while (System.in.available() > 0)
89              {
90                  // System.in.skip doesn't work properly. I don't know why
91                  long available = System.in.available();
92                  for (int i = 0; i < available; i++)
93                  {
94                      if (System.in.read() == -1)
95                      {
96                          break;
97                      }
98                  }
99              }
100         }
101         catch (IOException e)
102         {
103             mojo.getLog().warn("Error discarding console input buffer", e);
104         }      
105     }
106     
107     private void restartWebApp()
108     {
109         try
110         {
111             mojo.restartWebApp(false);
112             // Clear input buffer to discard anything entered on the console
113             // while the application was being restarted.
114             clearInputBuffer();
115         }
116         catch (Exception e)
117         {
118             mojo.getLog().error(
119                             "Error reconfiguring/restarting webapp after a new line on the console",
120                             e);
121         }
122     }
123 }