1   //========================================================================
2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at 
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  /**
16   * 
17   */
18  package org.mortbay.jetty.plugin.util;
19  
20  import java.io.IOException;
21  
22  import org.mortbay.jetty.plugin.AbstractJettyMojo;
23  
24  public class ConsoleScanner extends Thread {
25      
26      private final AbstractJettyMojo mojo;
27      
28      public ConsoleScanner(AbstractJettyMojo mojo) {
29          this.mojo = mojo;
30          setName("Console scanner");
31          setDaemon(true);
32      }
33      
34      public void run() {
35          
36          try 
37          {
38              while (true) 
39              {
40                  checkSystemInput();
41                  getSomeSleep();
42              }
43          } 
44          catch (IOException e) 
45          {
46              mojo.getLog().warn(e);
47          }
48      }
49      
50      private void getSomeSleep() {
51          try 
52          {
53              Thread.sleep(500);
54          } 
55          catch (InterruptedException e) 
56          {
57              mojo.getLog().debug(e);
58          }
59      }
60      
61      private void checkSystemInput() throws IOException {
62          
63          while (System.in.available() > 0) {
64              int inputByte = System.in.read();
65              if (inputByte >= 0) 
66              {
67                  char c = (char)inputByte;
68                  if (c == '\n') {
69                      restartWebApp();
70                  }
71              }
72          }
73      }
74      
75      
76      /**
77       * Skip buffered bytes of system console.
78       */
79      private void clearInputBuffer() {
80  
81          try {
82              while (System.in.available() > 0) 
83              {
84                  // System.in.skip doesn't work properly. I don't know why
85                  long available = System.in.available();
86                  for (int i = 0; i < available; i++) {
87                      if (System.in.read() == -1) 
88                      {
89                          break;
90                      }
91                  }
92              }
93          } catch (IOException e) 
94          {
95              mojo.getLog().warn("Error discarding console input buffer", e);
96          }
97          
98      }
99      
100     private void restartWebApp() {
101         try 
102         {
103             mojo.restartWebApp(false);
104             // Clear input buffer to discard anything entered on the console
105             // while the application was being restarted.
106             clearInputBuffer();
107         } 
108         catch (Exception e) 
109         {
110             mojo.getLog().error("Error reconfiguring/restarting webapp after a new line on the console", e);
111         }
112     }
113 }