1
2
3
4
5
6
7
8
9
10
11
12
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
78
79 private void clearInputBuffer() {
80
81 try {
82 while (System.in.available() > 0)
83 {
84
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
105
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 }