forked from NationalSecurityAgency/emissary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmissary.java
211 lines (186 loc) · 7.47 KB
/
Emissary.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package emissary;
import emissary.command.AgentsCommand;
import emissary.command.Banner;
import emissary.command.ConfigCommand;
import emissary.command.DirectoryCommand;
import emissary.command.EmissaryCommand;
import emissary.command.EnvCommand;
import emissary.command.FeedCommand;
import emissary.command.HelpCommand;
import emissary.command.PeersCommand;
import emissary.command.PoolCommand;
import emissary.command.RunCommand;
import emissary.command.ServerCommand;
import emissary.command.StopCommand;
import emissary.command.TopologyCommand;
import emissary.command.VersionCommand;
import emissary.command.WhatCommand;
import emissary.util.GitRepositoryState;
import emissary.util.io.LoggingPrintStream;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.MissingCommandException;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
/**
* Main entry point of the jar file
*
* Parses command line arguments and delegates commands
*/
public class Emissary {
private static final Logger LOG = LoggerFactory.getLogger(Emissary.class);
private final JCommander jc = new JCommander();
private final Map<String, EmissaryCommand> commands;
public static Map<String, EmissaryCommand> EMISSARY_COMMANDS = new HashMap<>();
private boolean bannerDumped = false;
static {
List<Class<? extends EmissaryCommand>> cmds =
Arrays.asList(ServerCommand.class, HelpCommand.class, WhatCommand.class, TopologyCommand.class, FeedCommand.class,
AgentsCommand.class, PoolCommand.class, VersionCommand.class, RunCommand.class, EnvCommand.class, StopCommand.class,
PeersCommand.class, ConfigCommand.class, DirectoryCommand.class);
Map<String, EmissaryCommand> staticCopy = new HashMap<>();
for (Class<? extends EmissaryCommand> clz : cmds) {
EmissaryCommand cmd;
try {
cmd = clz.getDeclaredConstructor().newInstance();
String name = cmd.getCommandName();
staticCopy.put(name, cmd);
} catch (ReflectiveOperationException e) {
LOG.error("Couldn't make EMISSARY_COMMANDS", e);
System.exit(1);
}
}
EMISSARY_COMMANDS = Collections.unmodifiableMap(staticCopy);
}
@VisibleForTesting
protected JCommander getJCommander() {
return jc;
}
protected Emissary() {
this(EMISSARY_COMMANDS);
}
protected Emissary(Map<String, EmissaryCommand> cmds) {
commands = Collections.unmodifiableMap(cmds);
// sort by command name and then add to jCommander
for (String key : new TreeSet<>(commands.keySet())) {
jc.addCommand(key, commands.get(key));
}
}
protected void execute(String[] args) {
reconfigureLogHook(); // so we can capture everything for test, like the verbose output
String shouldSetVerbose = System.getProperty("set.jcommander.debug");
if (shouldSetVerbose != null && shouldSetVerbose.equals("true")) {
// could also set system property JCommander.DEBUG
// if that was set before adding commands to the jc object though, you would get logs
// for adding parameter descriptions etc
jc.setVerbose(1);
}
try {
jc.parse(args);
String commandName = jc.getParsedCommand();
if (commandName == null) {
dumpBanner();
LOG.error("One command is required");
HelpCommand.dumpCommands(jc);
exit(1);
}
EmissaryCommand cmd = commands.get(commandName);
dumpBanner(cmd);
if (Arrays.asList(args).contains(ServerCommand.COMMAND_NAME)) {
dumpVersionInfo();
}
cmd.run(jc);
// don't exit(0) here or things like server will not continue to run
} catch (MissingCommandException e) {
dumpBanner();
LOG.error("Undefined command: {}", Arrays.toString(args));
HelpCommand.dumpCommands(jc);
exit(1);
} catch (Exception e) {
dumpBanner();
LOG.error("Command threw an exception: {}", Arrays.toString(args), e);
exit(1);
}
}
private void dumpBanner(@Nullable EmissaryCommand cmd) {
if (!bannerDumped) {
bannerDumped = true;
if (cmd == null) {
new Banner().dump();
} else {
cmd.outputBanner();
}
setupLogging();
}
}
private void dumpBanner() {
dumpBanner(null);
}
protected void dumpVersionInfo() {
LOG.info(GitRepositoryState.dumpVersionInfo(GitRepositoryState.getRepositoryState(), "Emissary"));
}
@VisibleForTesting
protected void reconfigureLogHook() {
// overridden in EmissaryTest
}
@VisibleForTesting
// so we can stop exiting long enough to look at the return code
protected void exit(int retCode) {
System.exit(retCode);
}
public static void main(String[] args) {
new Emissary().execute(args);
}
protected void setupLogging() {
redirectStdOutStdErr();
setupLogbackForConsole();
// hook so we can capture stuff in tests
reconfigureLogHook();
}
/*
* Modify the logback stuff, about to run a command
*
* Reinit with a config file if running something like a server where you want the expanded format,
*/
public static LoggerContext setupLogbackForConsole() {
// So it looks better when commands are run
ch.qos.logback.classic.Logger root =
(ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
root.detachAndStopAllAppenders();
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.reset();
PatternLayoutEncoder ple = new PatternLayoutEncoder();
ple.setPattern("%msg%n");
ple.setContext(lc);
ple.start();
ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
consoleAppender.setEncoder(ple);
consoleAppender.setContext(lc);
consoleAppender.start();
root.addAppender(consoleAppender);
root.setLevel(Level.INFO);
root.setAdditive(false);
return lc;
}
static void redirectStdOutStdErr() {
// no need for sysout-over-slf4j anymore, which as need for any calls, like jni, which only
// output to stdout/stderr Last none logback message
LOG.trace("Redefining stdout so logback and capture the output");
System.setOut(new LoggingPrintStream(System.out, "STDOUT", LOG, org.slf4j.event.Level.INFO, 30, TimeUnit.SECONDS));
LOG.trace("Redefining stderr so logback and capture the output");
System.setErr(new LoggingPrintStream(System.err, "STDERR", LOG, org.slf4j.event.Level.ERROR, 30, TimeUnit.SECONDS));
}
}