Skip to content
This repository was archived by the owner on Jun 7, 2022. It is now read-only.

Commit c8f4879

Browse files
authored
Merge pull request #187 from McRuben/localwrapper-console
Use ExecutorService for the local wrappers console
2 parents da771a5 + 0793923 commit c8f4879

File tree

1 file changed

+67
-65
lines changed

1 file changed

+67
-65
lines changed

cloudnet-core/src/main/java/de/dytanic/cloudnetcore/wrapper/local/LocalCloudWrapper.java

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.nio.file.Paths;
2929
import java.nio.file.StandardCopyOption;
3030
import java.util.Collections;
31+
import java.util.concurrent.ExecutorService;
32+
import java.util.concurrent.Executors;
3133
import java.util.concurrent.TimeUnit;
3234
import java.util.function.Consumer;
3335

@@ -39,9 +41,7 @@ public class LocalCloudWrapper implements Runnabled<OptionSet>, Closeable {
3941
private static final String WRAPPER_URL = "https://ci.cloudnetservice.eu/job/CloudNetService/job/CloudNet/job/master/lastSuccessfulBuild/artifact/cloudnet-wrapper/target/CloudNet-Wrapper.jar";
4042

4143
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);
4545
private boolean shutdown = false;
4646
private boolean enabled;
4747
private boolean showConsoleOutput = !Boolean.getBoolean("cloudnet.localwrapper.disableConsole");
@@ -68,8 +68,8 @@ public Configuration loadWrapperConfiguration() {
6868
if (this.config == null || this.config.isOutdated()) {
6969
try (InputStream inputStream = Files.newInputStream(Paths.get("wrapper/config.yml"))) {
7070
this.config = new LocalWrapperConfig(ConfigurationProvider.getProvider(YamlConfiguration.class).load(inputStream));
71-
} catch (IOException e) {
72-
e.printStackTrace();
71+
} catch (IOException exception) {
72+
exception.printStackTrace();
7373
}
7474
}
7575
return this.config != null ? this.config.getConfiguration() : null;
@@ -78,7 +78,24 @@ public Configuration loadWrapperConfiguration() {
7878
public void installUpdate(WebClient webClient) {
7979
Path path = Paths.get("wrapper/CloudNet-Wrapper.jar");
8080
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+
}
8191
webClient.updateLocalCloudWrapper(path);
92+
if (runningBeforeUpdate) {
93+
try {
94+
this.startProcess();
95+
} catch (IOException exception) {
96+
exception.printStackTrace();
97+
}
98+
}
8299
}
83100
}
84101

@@ -95,8 +112,8 @@ public void run(OptionSet obj) {
95112
this.setupWrapperKey();
96113
this.setupSpigot(obj);
97114

98-
} catch (IOException e) {
99-
e.printStackTrace();
115+
} catch (IOException exception) {
116+
exception.printStackTrace();
100117
}
101118

102119
this.startup();
@@ -117,8 +134,8 @@ private void setupWrapperJar() {
117134
urlConnection.connect();
118135
Files.copy(urlConnection.getInputStream(), path);
119136
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());
122139
return;
123140
}
124141
}
@@ -179,17 +196,17 @@ private void setupConfig() {
179196

180197
try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(path), StandardCharsets.UTF_8)) {
181198
ConfigurationProvider.getProvider(YamlConfiguration.class).save(configuration, outputStreamWriter);
182-
} catch (IOException e) {
183-
e.printStackTrace();
199+
} catch (IOException exception) {
200+
exception.printStackTrace();
184201
}
185202
}
186203
}
187204

188205
private void setupWrapperKey() {
189206
try {
190207
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();
193210
}
194211
}
195212

@@ -198,8 +215,8 @@ private void setupSpigot(OptionSet obj) {
198215
if (!obj.has("disallow_bukkit_download") && !Files.exists(path)) {
199216
try {
200217
Files.createDirectories(path.getParent());
201-
} catch (IOException e) {
202-
e.printStackTrace();
218+
} catch (IOException exception) {
219+
exception.printStackTrace();
203220
}
204221
SetupSpigotVersion setup = new SetupSpigotVersion();
205222
setup.setTarget(path);
@@ -215,12 +232,11 @@ private void startup() {
215232
System.out.println("Starting local wrapper...");
216233
try {
217234
this.startProcess();
218-
this.initConsoleThread();
219235

220236
System.out.println("Successfully started the local wrapper!");
221-
} catch (IOException e) {
237+
} catch (IOException exception) {
222238
System.err.println("Failed to start the local wrapper!");
223-
e.printStackTrace();
239+
exception.printStackTrace();
224240
}
225241
}
226242

@@ -232,62 +248,48 @@ private void startProcess() throws IOException {
232248
"-Dcloudnet.logging.prompt.disabled=true",
233249
"-jar",
234250
"CloudNet-Wrapper.jar").directory(new File("wrapper")).start();
251+
this.initConsoleThread();
235252
System.out.println("Successfully started the wrapper process!");
236253
}
237254

238255
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);
261261
}
262+
});
263+
if (!this.shutdown) {
264+
try {
265+
this.startProcess();
266+
} catch (IOException e) {
267+
e.printStackTrace();
268+
}
269+
} else {
270+
this.enabled = false;
262271
}
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+
});
265281
}
266282

267283
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);
284289
}
285290
}
286-
287-
this.stringBuffer.setLength(0);
288-
289-
} catch (Exception ignored) {
290-
this.stringBuffer.setLength(0);
291+
} catch (IOException exception) {
292+
exception.printStackTrace();
291293
}
292294
}
293295

@@ -308,8 +310,8 @@ private void stop() throws IOException {
308310
this.process.destroy();
309311
}
310312
System.out.println("Successfully stopped the local wrapper!");
311-
} catch (InterruptedException e) {
312-
e.printStackTrace();
313+
} catch (InterruptedException exception) {
314+
exception.printStackTrace();
313315
}
314316
}
315317

0 commit comments

Comments
 (0)