View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  
25  
26  /**
27   * ConsoleScanner
28   *
29   * Read input from stdin
30   */
31  public class ConsoleScanner extends Thread 
32  {
33      
34      private final AbstractJettyMojo mojo;
35      
36      
37      
38      
39      
40      /**
41       * @param mojo
42       */
43      public ConsoleScanner(AbstractJettyMojo mojo) 
44      {
45          this.mojo = mojo;
46          setName("Console scanner");
47          setDaemon(true);
48      }
49      
50      
51      
52      
53      /** 
54       * @see java.lang.Thread#run()
55       */
56      public void run() 
57      {  
58          try 
59          {
60              while (true) 
61              {
62                  checkSystemInput();
63                  getSomeSleep();
64              }
65          } 
66          catch (IOException e) 
67          {
68              mojo.getLog().warn(e);
69          }
70      }
71      
72      
73      
74      
75      /**
76       * 
77       */
78      private void getSomeSleep() 
79      {
80          try 
81          {
82              Thread.sleep(500);
83          } 
84          catch (InterruptedException e) 
85          {
86              mojo.getLog().debug(e);
87          }
88      }
89      
90      
91      
92      
93      /**
94       * @throws IOException
95       */
96      private void checkSystemInput() throws IOException 
97      {     
98          while (System.in.available() > 0) {
99              int inputByte = System.in.read();
100             if (inputByte >= 0) 
101             {
102                 char c = (char)inputByte;
103                 if (c == '\n') {
104                     restartWebApp();
105                 }
106             }
107         }
108     }
109     
110     
111     
112     
113     /**
114      * Skip buffered bytes of system console.
115      */
116     private void clearInputBuffer() 
117     {
118         try
119         {
120             while (System.in.available() > 0)
121             {
122                 // System.in.skip doesn't work properly. I don't know why
123                 long available = System.in.available();
124                 for (int i = 0; i < available; i++)
125                 {
126                     if (System.in.read() == -1)
127                     {
128                         break;
129                     }
130                 }
131             }
132         }
133         catch (IOException e)
134         {
135             mojo.getLog().warn("Error discarding console input buffer", e);
136         }      
137     }
138     
139     
140     
141     
142     /**
143      * 
144      */
145     private void restartWebApp()
146     {
147         try
148         {
149             mojo.restartWebApp(false);
150             // Clear input buffer to discard anything entered on the console
151             // while the application was being restarted.
152             clearInputBuffer();
153         }
154         catch (Exception e)
155         {
156             mojo.getLog().error(
157                             "Error reconfiguring/restarting webapp after a new line on the console",
158                             e);
159         }
160     }
161 }