in tools/cli/src/main/java/org/apache/batchee/cli/BatchEECLI.java [64:184]
public static void main(final String[] args) {
final Iterator<CliConfiguration> configuration = ServiceLoader.load(CliConfiguration.class).iterator();
final CliConfiguration cliConfiguration = configuration.hasNext() ? configuration.next() : new DefaultCliConfiguration();
final Map<String, Class<? extends Runnable>> commands = new TreeMap<String, Class<? extends Runnable>>();
if (cliConfiguration.addDefaultCommands()) {
for (final Class<? extends Runnable> type :
Arrays.asList(Names.class,
Start.class, Restart.class,
Status.class, Running.class,
Stop.class, Abandon.class,
Instances.class, Executions.class,
StepExecutions.class, Eviction.class)) {
addCommand(commands, type);
}
}
final Iterator<Class<? extends UserCommand>> userCommands = cliConfiguration.userCommands();
if (userCommands != null) {
while (userCommands.hasNext()) {
addCommand(commands, userCommands.next());
}
}
if (args == null || args.length == 0) {
System.err.print(help(commands));
return;
}
final Class<? extends Runnable> cmd = commands.get(args[0]);
if (cmd == null) {
if (args[0].equals("help")) {
if (args.length > 1) {
final Class<? extends Runnable> helpCmd = commands.get(args[1]);
if (helpCmd != null) {
printHelp(helpCmd.getAnnotation(Command.class), buildOptions(helpCmd, new HashMap<String, Field>()));
return;
}
} // else let use the default help
}
System.err.print(help(commands));
return;
}
// build the command now
final Command command = cmd.getAnnotation(Command.class);
if (command == null) {
System.err.print(help(commands));
return;
}
final Map<String, Field> fields = new HashMap<String, Field>();
final Options options = buildOptions(cmd, fields);
final Collection<String> newArgs = new ArrayList<String>(asList(args));
newArgs.remove(newArgs.iterator().next());
final File cliConf;
String home = System.getProperty("batchee.home");
if (home == null) {
final String conf = System.getProperty("batchee.cli.configuration");
if (conf == null) {
cliConf = null;
} else {
cliConf = new File(conf);
}
} else {
cliConf = new File(home, "conf/batchee-cli.properties");
}
if (cliConf != null && cliConf.exists()) {
final Properties properties = new Properties() {{
Reader reader = null;
try {
reader = new FileReader(cliConf);
load(reader);
} catch (IOException e) {
throw new IllegalArgumentException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
// no-op
}
}
}
}};
for (final String key : properties.stringPropertyNames()) {
if (key.startsWith("_arguments.")) { // /!\ added whatever passed values are
newArgs.add(properties.getProperty(key));
} else {
final String opt = "-" + key;
if (!newArgs.contains(opt)) {
newArgs.add(opt);
newArgs.add(properties.getProperty(key));
}
}
}
}
final CommandLineParser parser = new DefaultParser();
try {
final CommandLine line = parser.parse(options, newArgs.toArray(new String[newArgs.size()]));
cliConfiguration.decorate(instantiate(cmd, cliConfiguration, fields, !newArgs.isEmpty(), line)).run();
} catch (final ParseException e) {
printHelp(command, options);
} catch (final RuntimeException e) {
Class<?> current = e.getClass();
while (current != null) {
final Exit annotation = current.getAnnotation(Exit.class);
if (annotation != null) {
System.exit(annotation.value());
}
current = current.getSuperclass();
}
throw e;
} catch (final InstantiationException e) {
throw new IllegalStateException(e);
} catch (final IllegalAccessException e) {
throw new IllegalStateException(e);
}
}