-
Notifications
You must be signed in to change notification settings - Fork 8
/
Xapi.java
executable file
·190 lines (155 loc) · 6.17 KB
/
Xapi.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//bin/cat &>/dev/null <<'EOF'
/* This mess is a trick to start a shell script inside a valid java comment.
EOF
mkdir -p /tmp/xapi # grab a safe place to work
yes | cp -u $0 /tmp/xapi # copy this file there
# suppress annoying log messages
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
pushd /tmp/xapi # go to tmp dir
#rotate logs
rm -f logOut.1 logErr.1
mv logOut logOut.1 &>/dev/null
mv logErr logErr.1 &>/dev/null
touch logOut logErr
#compile self;
javac `basename $0`
# let's stream both files to the executing shell
tail -f logOut &
tail -f logErr &
# run the java command, allowing it to use System.out to execute commands in executed shell
java Xapi $@ | sh 1>out 2>err
# once the program terminates, kill everything we've started
(jobs -p | xargs kill -9) &>/dev/null
# go back to wherever we came from
popd
exit
*/
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
class Xapi {
// grab references to the real, system out/err streams, so we can replace them
private static final PrintStream run = System.out;
private static final PrintStream log = System.err;
private static final String XAPI_VERSION = "0.5.1";
private static final Path out = Paths.get("out");
private static final Path err = Paths.get("err");
interface Print<T> { void out(T t); }
public static void main(String ... args) throws Throwable{
// std out runs shell commands, per the unix shell hack at the top of this file
// our log, which is streamed back to executing cli (yes, it's dirty, but it works...)
try {
// We'll try to use mvn to download xapi jars (for now, no fallback... in the future, hardcode uberjar url)
run.println("which mvn");
String mvn = read(out);
log.print("Using maven: " + mvn);
// Replace the real stdOut and stdErr with printstreams that log to files
PrintStream o = new PrintStream("logOut");
System.setOut(log);
o = new PrintStream("logErr");
System.setErr(o);
// extract arguments we want to pass along to the demo
List<String> progArg = new ArrayList<>();
Map<String, String> sysArg = new LinkedHashMap<>();
for (String arg : args) {
if (arg.startsWith("-D")) {
// system prop
int ind = arg.indexOf('=');
String key = arg.substring(2, ind);
sysArg.put(key, arg.substring(ind+1));
} else {
// plain arguments
progArg.add(arg);
}
}
// Generate a pom for us to tell mvn what to do;
String nl = "\\n";
// for ease on the eyes, we'll use printf
run.print("printf \"");
// to create nicely formatted output that is readable, we'll make indent/outdent
String[] depth = {""};
Print<String> output = s->run.print(depth[0] + s + nl);
Print<String> indent = s->{
run.print(depth[0] + s + nl);
depth[0] = depth[0] + " ";
};
Print<String> outdent = s->{
assert !depth[0].isEmpty() : "Called outdent too many times";
run.print(
// in case assertions disabled, silently ignore
(depth[0] = depth[0].isEmpty() ? "" : depth[0].substring(2))
+ s + nl);
};
indent.out("<project>");
output.out("<modelVersion>4.0.0</modelVersion>");
output.out("<groupId>net.wti</groupId>");
output.out("<artifactId>demo</artifactId>");
output.out("<version>0.1</version>");
indent.out("<dependencies>");
indent.out("<dependency>");
output.out("<groupId>net.wetheinter</groupId>");
output.out("<artifactId>xapi-demo</artifactId>");
output.out("<version>" + XAPI_VERSION + "</version>");
outdent.out("</dependency>");
outdent.out("</dependencies>");
indent.out("<build>");
indent.out("<plugins>");
indent.out("<plugin>");
output.out("<groupId>org.codehaus.mojo</groupId>");
output.out("<artifactId>exec-maven-plugin</artifactId>");
output.out("<version>1.2.1</version>");
indent.out("<configuration>");
output.out("<mainClass>xapi.demo.jre.XapiLangAdmin</mainClass>");
if (!sysArg.isEmpty()) {
indent.out("<systemProperties>");
sysArg.entrySet().forEach(e->{
output.out("<key>" + e.getKey() + "</key>");
output.out("<value>" + e.getValue() + "</value>");
});
indent.out("<systemProperty>");
outdent.out("</systemProperty>");
outdent.out("</systemProperties>");
}
if (!progArg.isEmpty()) {
indent.out("<arguments>");
progArg.forEach(arg->
output.out("<argument>" + arg + "</argument>")
);
outdent.out("</arguments>");
}
outdent.out("</configuration>");
outdent.out("</plugin>");
outdent.out("</plugins>");
outdent.out("</build>");
outdent.out("</project>");
run.println("\" > pom.xml");
String res = read(Paths.get("pom.xml"));
log.println("maven pom:\n" + res);
// Now, execute a maven command that will download and run the xapi demo app;
// we'll use run to append to logOut so our message appears in the server log (output to log.print() is ephemeral)
run.println("echo \"Executing XApi\" >> logOut");
// This will cause the sh command we are being piped to to block, keeping this process alive.
run.println(mvn.trim() + " exec:java -q 1>>logOut 2>>logErr");
run.println("echo \"Finished Executing XApi\" >> logOut");
} catch (Throwable t){
t.printStackTrace(log);
throw t;
} finally {
run.println("exit");
}
}
private static String read(Path source) throws Exception {
byte[] bytes = Files.readAllBytes(source);
return new String(bytes);
}
}