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 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
83
84 private void clearInputBuffer()
85 {
86 try
87 {
88 while (System.in.available() > 0)
89 {
90
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
113
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 }