1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.maven.plugin;
20
21 import java.io.IOException;
22
23
24
25
26
27
28
29
30
31 public class ConsoleScanner extends Thread
32 {
33
34 private final AbstractJettyMojo mojo;
35
36
37
38
39
40
41
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
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
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
115
116 private void clearInputBuffer()
117 {
118 try
119 {
120 while (System.in.available() > 0)
121 {
122
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
151
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 }