forked from javaee-samples/javaee7-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliCommands.java
More file actions
130 lines (104 loc) · 3.92 KB
/
CliCommands.java
File metadata and controls
130 lines (104 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.javaee7;
import static java.lang.Runtime.getRuntime;
import static java.lang.Thread.currentThread;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Logger;
/**
* Methods to execute "cli commands" against various servers.
*
* @author Arjan Tijms
*
*/
public class CliCommands {
private static final Logger logger = Logger.getLogger(CliCommands.class.getName());
private static final String OS = System.getProperty("os.name").toLowerCase();
public static int payaraGlassFish(List<String> cliCommands) {
String gfHome = System.getProperty("glassfishRemote_gfHome");
if (gfHome == null) {
logger.info("glassfishRemote_gfHome not specified");
return -1;
}
Path gfHomePath = Paths.get(gfHome);
if (!gfHomePath.toFile().exists()) {
logger.severe("glassfishRemote_gfHome at " + gfHome + " does not exists");
return -1;
}
if (!gfHomePath.toFile().isDirectory()) {
logger.severe("glassfishRemote_gfHome at " + gfHome + " is not a directory");
return -1;
}
Path asadminPath = gfHomePath.resolve(isWindows()? "bin/asadmin.bat" : "bin/asadmin");
if (!asadminPath.toFile().exists()) {
logger.severe("asadmin command at " + asadminPath.toAbsolutePath() + " does not exists");
return -1;
}
List<String> cmd = new ArrayList<>();
cmd.add(asadminPath.toAbsolutePath().toString());
cmd.addAll(cliCommands);
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
processBuilder.redirectErrorStream(true);
try {
return
waitToFinish(
readAllInput(
destroyAtShutDown(
processBuilder.start())));
} catch (IOException e) {
return -1;
}
}
public static Process destroyAtShutDown(final Process process) {
getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
if (process != null) {
process.destroy();
try {
process.waitFor();
} catch (InterruptedException e) {
currentThread().interrupt();
throw new RuntimeException(e);
}
}
}
}));
return process;
}
public static Process readAllInput(Process process) {
// Read any output from the process
try (Scanner scanner = new Scanner(process.getInputStream())) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
}
return process;
}
public static int waitToFinish(Process process) {
// Wait up to 30s for the process to finish
int startupTimeout = 30 * 1000;
while (startupTimeout > 0) {
startupTimeout -= 200;
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
// Ignore
}
try {
int exitValue = process.exitValue();
System.out.println("Asadmin process exited with status " + exitValue);
return exitValue;
} catch (IllegalThreadStateException e) {
// process is still running
}
}
throw new IllegalStateException("Asadmin process seems stuck after waiting for 30 seconds");
}
public static boolean isWindows() {
return OS.contains("win");
}
}