28
28
import java .nio .file .Paths ;
29
29
import java .nio .file .StandardCopyOption ;
30
30
import java .util .Collections ;
31
+ import java .util .concurrent .ExecutorService ;
32
+ import java .util .concurrent .Executors ;
31
33
import java .util .concurrent .TimeUnit ;
32
34
import java .util .function .Consumer ;
33
35
@@ -39,9 +41,7 @@ public class LocalCloudWrapper implements Runnabled<OptionSet>, Closeable {
39
41
private static final String WRAPPER_URL = "https://ci.cloudnetservice.eu/job/CloudNetService/job/CloudNet/job/master/lastSuccessfulBuild/artifact/cloudnet-wrapper/target/CloudNet-Wrapper.jar" ;
40
42
41
43
private Process process ;
42
- private Thread consoleThread ;
43
- private StringBuffer stringBuffer = new StringBuffer ();
44
- private byte [] buffer = new byte [1024 ];
44
+ private ExecutorService executorService = Executors .newFixedThreadPool (2 );
45
45
private boolean shutdown = false ;
46
46
private boolean enabled ;
47
47
private boolean showConsoleOutput = !Boolean .getBoolean ("cloudnet.localwrapper.disableConsole" );
@@ -68,8 +68,8 @@ public Configuration loadWrapperConfiguration() {
68
68
if (this .config == null || this .config .isOutdated ()) {
69
69
try (InputStream inputStream = Files .newInputStream (Paths .get ("wrapper/config.yml" ))) {
70
70
this .config = new LocalWrapperConfig (ConfigurationProvider .getProvider (YamlConfiguration .class ).load (inputStream ));
71
- } catch (IOException e ) {
72
- e .printStackTrace ();
71
+ } catch (IOException exception ) {
72
+ exception .printStackTrace ();
73
73
}
74
74
}
75
75
return this .config != null ? this .config .getConfiguration () : null ;
@@ -78,7 +78,24 @@ public Configuration loadWrapperConfiguration() {
78
78
public void installUpdate (WebClient webClient ) {
79
79
Path path = Paths .get ("wrapper/CloudNet-Wrapper.jar" );
80
80
if (Files .exists (path )) {
81
+ boolean runningBeforeUpdate = false ;
82
+ if (this .process != null && this .process .isAlive ()) {
83
+ this .shutdown = true ;
84
+ try {
85
+ this .stop ();
86
+ runningBeforeUpdate = true ;
87
+ } catch (IOException exception ) {
88
+ exception .printStackTrace ();
89
+ }
90
+ }
81
91
webClient .updateLocalCloudWrapper (path );
92
+ if (runningBeforeUpdate ) {
93
+ try {
94
+ this .startProcess ();
95
+ } catch (IOException exception ) {
96
+ exception .printStackTrace ();
97
+ }
98
+ }
82
99
}
83
100
}
84
101
@@ -95,8 +112,8 @@ public void run(OptionSet obj) {
95
112
this .setupWrapperKey ();
96
113
this .setupSpigot (obj );
97
114
98
- } catch (IOException e ) {
99
- e .printStackTrace ();
115
+ } catch (IOException exception ) {
116
+ exception .printStackTrace ();
100
117
}
101
118
102
119
this .startup ();
@@ -117,8 +134,8 @@ private void setupWrapperJar() {
117
134
urlConnection .connect ();
118
135
Files .copy (urlConnection .getInputStream (), path );
119
136
System .out .println ("Download completed!" );
120
- } catch (Exception ex ) {
121
- System .err .println ("Error on setting up wrapper: " + ex .getMessage ());
137
+ } catch (Exception exception ) {
138
+ System .err .println ("Error on setting up wrapper: " + exception .getMessage ());
122
139
return ;
123
140
}
124
141
}
@@ -179,17 +196,17 @@ private void setupConfig() {
179
196
180
197
try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter (Files .newOutputStream (path ), StandardCharsets .UTF_8 )) {
181
198
ConfigurationProvider .getProvider (YamlConfiguration .class ).save (configuration , outputStreamWriter );
182
- } catch (IOException e ) {
183
- e .printStackTrace ();
199
+ } catch (IOException exception ) {
200
+ exception .printStackTrace ();
184
201
}
185
202
}
186
203
}
187
204
188
205
private void setupWrapperKey () {
189
206
try {
190
207
Files .copy (Paths .get ("WRAPPER_KEY.cnd" ), Paths .get ("wrapper/WRAPPER_KEY.cnd" ), StandardCopyOption .REPLACE_EXISTING );
191
- } catch (IOException e ) {
192
- e .printStackTrace ();
208
+ } catch (IOException exception ) {
209
+ exception .printStackTrace ();
193
210
}
194
211
}
195
212
@@ -198,8 +215,8 @@ private void setupSpigot(OptionSet obj) {
198
215
if (!obj .has ("disallow_bukkit_download" ) && !Files .exists (path )) {
199
216
try {
200
217
Files .createDirectories (path .getParent ());
201
- } catch (IOException e ) {
202
- e .printStackTrace ();
218
+ } catch (IOException exception ) {
219
+ exception .printStackTrace ();
203
220
}
204
221
SetupSpigotVersion setup = new SetupSpigotVersion ();
205
222
setup .setTarget (path );
@@ -215,12 +232,11 @@ private void startup() {
215
232
System .out .println ("Starting local wrapper..." );
216
233
try {
217
234
this .startProcess ();
218
- this .initConsoleThread ();
219
235
220
236
System .out .println ("Successfully started the local wrapper!" );
221
- } catch (IOException e ) {
237
+ } catch (IOException exception ) {
222
238
System .err .println ("Failed to start the local wrapper!" );
223
- e .printStackTrace ();
239
+ exception .printStackTrace ();
224
240
}
225
241
}
226
242
@@ -232,62 +248,48 @@ private void startProcess() throws IOException {
232
248
"-Dcloudnet.logging.prompt.disabled=true" ,
233
249
"-jar" ,
234
250
"CloudNet-Wrapper.jar" ).directory (new File ("wrapper" )).start ();
251
+ this .initConsoleThread ();
235
252
System .out .println ("Successfully started the wrapper process!" );
236
253
}
237
254
238
255
private void initConsoleThread () {
239
- this .consoleThread = new Thread (() -> {
240
- while (!Thread .interrupted ()) {
241
- if (this .process .isAlive ()) {
242
- this .readStream (this .process .getInputStream (), s -> {
243
- if (this .showConsoleOutput ) {
244
- System .out .println ("LocalWrapper | " + s );
245
- }
246
- });
247
- this .readStream (this .process .getErrorStream (), s -> {
248
- if (this .showConsoleOutput ) {
249
- System .err .println ("LocalWrapper | " + s );
250
- }
251
- });
252
- } else if (!shutdown ) {
253
- try {
254
- this .startProcess ();
255
- } catch (IOException e ) {
256
- e .printStackTrace ();
257
- }
258
- } else {
259
- this .enabled = false ;
260
- Thread .currentThread ().interrupt ();
256
+ this .executorService .execute (() -> {
257
+ InputStream inputStream = this .process .getInputStream ();
258
+ this .readStream (inputStream , line -> {
259
+ if (this .showConsoleOutput ) {
260
+ System .out .println ("LocalWrapper | " + line );
261
261
}
262
+ });
263
+ if (!this .shutdown ) {
264
+ try {
265
+ this .startProcess ();
266
+ } catch (IOException e ) {
267
+ e .printStackTrace ();
268
+ }
269
+ } else {
270
+ this .enabled = false ;
262
271
}
263
- }, "LocalWrapper-Console" );
264
- this .consoleThread .start ();
272
+ });
273
+ this .executorService .execute (() -> {
274
+ InputStream inputStream = this .process .getErrorStream ();
275
+ this .readStream (inputStream , line -> {
276
+ if (this .showConsoleOutput ) {
277
+ System .err .println ("LocalWrapper | " + line );
278
+ }
279
+ });
280
+ });
265
281
}
266
282
267
283
private void readStream (InputStream inputStream , Consumer <String > consumer ) {
268
- try {
269
- int len ;
270
- while (inputStream .available () > 0 && (len = inputStream .read (this .buffer )) != -1 ) {
271
- this .stringBuffer .append (new String (this .buffer , 0 , len , StandardCharsets .UTF_8 ));
272
- }
273
-
274
- String stringText = this .stringBuffer .toString ();
275
- if (!stringText .contains ("\n " ) && !stringText .contains ("\r " )) {
276
- return ;
277
- }
278
-
279
- for (String input : stringText .split ("\r " )) {
280
- for (String text : input .split ("\n " )) {
281
- if (!text .trim ().isEmpty ()) {
282
- consumer .accept (text );
283
- }
284
+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream , StandardCharsets .UTF_8 ))){
285
+ String line ;
286
+ while ((line = reader .readLine ()) != null ) {
287
+ if (!line .isEmpty ()) {
288
+ consumer .accept (line );
284
289
}
285
290
}
286
-
287
- this .stringBuffer .setLength (0 );
288
-
289
- } catch (Exception ignored ) {
290
- this .stringBuffer .setLength (0 );
291
+ } catch (IOException exception ) {
292
+ exception .printStackTrace ();
291
293
}
292
294
}
293
295
@@ -308,8 +310,8 @@ private void stop() throws IOException {
308
310
this .process .destroy ();
309
311
}
310
312
System .out .println ("Successfully stopped the local wrapper!" );
311
- } catch (InterruptedException e ) {
312
- e .printStackTrace ();
313
+ } catch (InterruptedException exception ) {
314
+ exception .printStackTrace ();
313
315
}
314
316
}
315
317
0 commit comments