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" );
@@ -59,6 +59,11 @@ public void setShowConsoleOutput(boolean showConsoleOutput) {
59
59
this .showConsoleOutput = showConsoleOutput ;
60
60
}
61
61
62
+ public Wrapper getWrapper () {
63
+ String wrapperId = this .loadWrapperConfiguration ().getString ("general.wrapperId" );
64
+ return CloudNet .getInstance ().getWrappers ().get (wrapperId );
65
+ }
66
+
62
67
public Configuration loadWrapperConfiguration () {
63
68
if (this .config == null || this .config .isOutdated ()) {
64
69
try (InputStream inputStream = Files .newInputStream (Paths .get ("wrapper/config.yml" ))) {
@@ -70,24 +75,37 @@ public Configuration loadWrapperConfiguration() {
70
75
return this .config != null ? this .config .getConfiguration () : null ;
71
76
}
72
77
73
- public Wrapper getWrapper () {
74
- String wrapperId = this .loadWrapperConfiguration ().getString ("general.wrapperId" );
75
- return CloudNet .getInstance ().getWrappers ().get (wrapperId );
76
- }
77
-
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 e ) {
88
+ e .printStackTrace ();
89
+ }
90
+ }
81
91
webClient .updateLocalCloudWrapper (path );
92
+ if (runningBeforeUpdate ) {
93
+ try {
94
+ this .startProcess ();
95
+ } catch (IOException e ) {
96
+ e .printStackTrace ();
97
+ }
98
+ }
82
99
}
83
100
}
84
101
85
102
@ Override
86
103
public void run (OptionSet obj ) {
87
104
if (obj .has ("installWrapper" )) {
88
105
try {
89
- if (!Files .exists (Paths .get ("wrapper" )))
106
+ if (!Files .exists (Paths .get ("wrapper" ))) {
90
107
Files .createDirectories (Paths .get ("wrapper" ));
108
+ }
91
109
92
110
this .setupWrapperJar ();
93
111
this .setupConfig ();
@@ -105,35 +123,14 @@ public void run(OptionSet obj) {
105
123
106
124
// -------------------- SETUP --------------------
107
125
108
- private void setupSpigot (OptionSet obj ) {
109
- Path path = Paths .get ("wrapper/local/spigot.jar" );
110
- if (!obj .has ("disallow_bukkit_download" ) && !Files .exists (path )) {
111
- try {
112
- Files .createDirectories (path .getParent ());
113
- } catch (IOException e ) {
114
- e .printStackTrace ();
115
- }
116
- SetupSpigotVersion setup = new SetupSpigotVersion ();
117
- setup .setTarget (path );
118
- setup .run (CloudNet .getLogger ().getReader ());
119
- }
120
- }
121
-
122
- private void setupWrapperKey () {
123
- try {
124
- Files .copy (Paths .get ("WRAPPER_KEY.cnd" ), Paths .get ("wrapper/WRAPPER_KEY.cnd" ), StandardCopyOption .REPLACE_EXISTING );
125
- } catch (IOException e ) {
126
- e .printStackTrace ();
127
- }
128
- }
129
-
130
126
private void setupWrapperJar () {
131
127
Path path = Paths .get ("wrapper/CloudNet-Wrapper.jar" );
132
128
if (!Files .exists (path )) {
133
129
try {
134
130
System .out .println ("Downloading wrapper..." );
135
131
URLConnection urlConnection = new URL (WRAPPER_URL ).openConnection ();
136
- urlConnection .setRequestProperty ("User-Agent" , "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11" );
132
+ urlConnection .setRequestProperty ("User-Agent" ,
133
+ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11" );
137
134
urlConnection .connect ();
138
135
Files .copy (urlConnection .getInputStream (), path );
139
136
System .out .println ("Download completed!" );
@@ -158,13 +155,20 @@ private void setupConfig() {
158
155
}
159
156
160
157
final User finalUser = user ;
161
- WrapperMeta wrapperMeta = CloudNet .getInstance ().getConfig ().getWrappers ().stream ()
162
- .filter (meta -> meta .getId ().equals ("Wrapper-1" ))
163
- .findFirst ().orElseGet (() -> {
164
- WrapperMeta newMeta = new WrapperMeta ("Wrapper-1" , address .getHostName (), finalUser .getName ());
165
- CloudNet .getInstance ().getConfig ().createWrapper (newMeta );
166
- return newMeta ;
167
- });
158
+ WrapperMeta wrapperMeta = CloudNet .getInstance ()
159
+ .getConfig ()
160
+ .getWrappers ()
161
+ .stream ()
162
+ .filter (meta -> meta .getId ()
163
+ .equals ("Wrapper-1" ))
164
+ .findFirst ()
165
+ .orElseGet (() -> {
166
+ WrapperMeta newMeta = new WrapperMeta ("Wrapper-1" ,
167
+ address .getHostName (),
168
+ finalUser .getName ());
169
+ CloudNet .getInstance ().getConfig ().createWrapper (newMeta );
170
+ return newMeta ;
171
+ });
168
172
169
173
long memory = ((NetworkUtils .systemMemory () / 1048576 ) - 2048 );
170
174
if (memory < 1024 ) {
@@ -198,6 +202,28 @@ private void setupConfig() {
198
202
}
199
203
}
200
204
205
+ private void setupWrapperKey () {
206
+ try {
207
+ Files .copy (Paths .get ("WRAPPER_KEY.cnd" ), Paths .get ("wrapper/WRAPPER_KEY.cnd" ), StandardCopyOption .REPLACE_EXISTING );
208
+ } catch (IOException e ) {
209
+ e .printStackTrace ();
210
+ }
211
+ }
212
+
213
+ private void setupSpigot (OptionSet obj ) {
214
+ Path path = Paths .get ("wrapper/local/spigot.jar" );
215
+ if (!obj .has ("disallow_bukkit_download" ) && !Files .exists (path )) {
216
+ try {
217
+ Files .createDirectories (path .getParent ());
218
+ } catch (IOException e ) {
219
+ e .printStackTrace ();
220
+ }
221
+ SetupSpigotVersion setup = new SetupSpigotVersion ();
222
+ setup .setTarget (path );
223
+ setup .accept (CloudNet .getLogger ().getReader ());
224
+ }
225
+ }
226
+
201
227
// -------------------- SETUP --------------------
202
228
203
229
// -------------------- PROCESS --------------------
@@ -206,7 +232,6 @@ private void startup() {
206
232
System .out .println ("Starting local wrapper..." );
207
233
try {
208
234
this .startProcess ();
209
- this .initConsoleThread ();
210
235
211
236
System .out .println ("Successfully started the local wrapper!" );
212
237
} catch (IOException e ) {
@@ -217,12 +242,57 @@ private void startup() {
217
242
218
243
private void startProcess () throws IOException {
219
244
System .out .println ("Starting wrapper process..." );
220
- this .process = new ProcessBuilder ("java" , "-Xmx256M" , "-Djline.terminal=jline.UnsupportedTerminal" , "-Dcloudnet.logging.prompt.disabled=true" , "-jar" , "CloudNet-Wrapper.jar" )
221
- .directory (new File ("wrapper" ))
222
- .start ();
245
+ this .process = new ProcessBuilder ("java" ,
246
+ "-Xmx256M" ,
247
+ "-Djline.terminal=jline.UnsupportedTerminal" ,
248
+ "-Dcloudnet.logging.prompt.disabled=true" ,
249
+ "-jar" ,
250
+ "CloudNet-Wrapper.jar" ).directory (new File ("wrapper" )).start ();
251
+ this .initConsoleThread ();
223
252
System .out .println ("Successfully started the wrapper process!" );
224
253
}
225
254
255
+ private void initConsoleThread () {
256
+ this .executorService .execute (() -> {
257
+ InputStream inputStream = this .process .getInputStream ();
258
+ this .readStream (inputStream , s -> {
259
+ if (this .showConsoleOutput ) {
260
+ System .out .println ("LocalWrapper | " + s );
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 ;
271
+ }
272
+ });
273
+ this .executorService .execute (() -> {
274
+ InputStream inputStream = this .process .getErrorStream ();
275
+ this .readStream (inputStream , s -> {
276
+ if (this .showConsoleOutput ) {
277
+ System .err .println ("LocalWrapper | " + s );
278
+ }
279
+ });
280
+ });
281
+ }
282
+
283
+ private void readStream (InputStream inputStream , Consumer <String > consumer ) {
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 );
289
+ }
290
+ }
291
+ } catch (IOException e ) {
292
+ e .printStackTrace ();
293
+ }
294
+ }
295
+
226
296
@ Override
227
297
public void close () throws IOException {
228
298
this .enabled = false ;
@@ -232,10 +302,6 @@ public void close() throws IOException {
232
302
}
233
303
}
234
304
235
- public void restart () throws IOException {
236
- this .stop ();
237
- }
238
-
239
305
private void stop () throws IOException {
240
306
System .out .println ("Stopping the local wrapper..." );
241
307
this .executeCommand ("stop" );
@@ -250,60 +316,14 @@ private void stop() throws IOException {
250
316
}
251
317
252
318
public void executeCommand (String command ) throws IOException {
253
- this .process .getOutputStream ().write ((command + " \n " ).getBytes (StandardCharsets .UTF_8 ));
319
+ this .process .getOutputStream ().write ((command + '\n' ).getBytes (StandardCharsets .UTF_8 ));
254
320
this .process .getOutputStream ().flush ();
255
321
}
256
322
257
- private void initConsoleThread () {
258
- this .consoleThread = new Thread (() -> {
259
- while (!Thread .interrupted ()) {
260
- if (this .process .isAlive ()) {
261
- this .readStream (this .process .getInputStream (), s -> {
262
- if (this .showConsoleOutput ) {
263
- System .out .println ("LocalWrapper | " + s );
264
- }
265
- });
266
- this .readStream (this .process .getErrorStream (), s -> {
267
- if (this .showConsoleOutput ) {
268
- System .err .println ("LocalWrapper | " + s );
269
- }
270
- });
271
- } else if (!shutdown ) {
272
- try {
273
- this .startProcess ();
274
- } catch (IOException e ) {
275
- e .printStackTrace ();
276
- }
277
- } else {
278
- this .enabled = false ;
279
- Thread .currentThread ().interrupt ();
280
- }
281
- }
282
- }, "LocalWrapper-Console" );
283
- this .consoleThread .start ();
284
- }
285
-
286
- private void readStream (InputStream inputStream , Consumer <String > consumer ) {
287
- try {
288
- int len ;
289
- while (inputStream .available () > 0 && (len = inputStream .read (this .buffer )) != -1 )
290
- this .stringBuffer .append (new String (this .buffer , 0 , len , StandardCharsets .UTF_8 ));
291
-
292
- String stringText = this .stringBuffer .toString ();
293
- if (!stringText .contains ("\n " ) && !stringText .contains ("\r " )) return ;
294
-
295
- for (String input : stringText .split ("\r " ))
296
- for (String text : input .split ("\n " ))
297
- if (!text .trim ().isEmpty ())
298
- consumer .accept (text );
299
-
300
- this .stringBuffer .setLength (0 );
301
-
302
- } catch (Exception ignored ) {
303
- this .stringBuffer .setLength (0 );
304
- }
323
+ public void restart () throws IOException {
324
+ this .stop ();
305
325
}
306
326
307
327
// -------------------- PROCESS --------------------
308
328
309
- }
329
+ }
0 commit comments