Skip to content
Permalink
Browse files
BATCHEE-81 getting rid of airline
  • Loading branch information
Romain Manni-Bucau committed Dec 2, 2015
1 parent 7110542 commit a5191447fe8958949e31b4d948c9995a7e1bc132
Show file tree
Hide file tree
Showing 25 changed files with 372 additions and 91 deletions.
@@ -48,22 +48,16 @@
<artifactId>batchee-jaxrs-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.airlift</groupId>
<artifactId>airline</artifactId>
<version>0.7</version>
<exclusions>
<exclusion>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@@ -16,34 +16,48 @@
*/
package org.apache.batchee.cli;

import io.airlift.airline.Cli;
import io.airlift.airline.Help;
import io.airlift.airline.ParseException;
import org.apache.batchee.cli.command.Abandon;
import org.apache.batchee.cli.command.CliConfiguration;
import org.apache.batchee.cli.command.Eviction;
import org.apache.batchee.cli.command.Executions;
import org.apache.batchee.cli.command.Exit;
import org.apache.batchee.cli.command.api.Exit;
import org.apache.batchee.cli.command.Instances;
import org.apache.batchee.cli.command.Names;
import org.apache.batchee.cli.command.Restart;
import org.apache.batchee.cli.command.Running;
import org.apache.batchee.cli.command.Start;
import org.apache.batchee.cli.command.Status;
import org.apache.batchee.cli.command.Stop;
import org.apache.batchee.cli.command.UserCommand;
import org.apache.batchee.cli.command.api.Command;
import org.apache.batchee.cli.command.api.UserCommand;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.TreeMap;

import static java.lang.ClassLoader.getSystemClassLoader;
import static java.util.Arrays.asList;

public class BatchEECLI {
public static void main(final String[] args) {
@@ -105,31 +119,115 @@ public Runnable decorate(final Runnable task) {
}
};

final Cli.CliBuilder<Runnable> builder = Cli.<Runnable>builder(cliConfiguration.name())
.withDescription(cliConfiguration.description())
.withDefaultCommand(Help.class);
final Map<String, Class<? extends Runnable>> commands = new TreeMap<String, Class<? extends Runnable>>();
if (cliConfiguration.addDefaultCommands()) {
builder.withCommands(Help.class,
Names.class,
Start.class, Restart.class,
Status.class, Running.class,
Stop.class, Abandon.class,
Instances.class, Executions.class,
Eviction.class);
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,
Eviction.class)) {
addCommand(commands, type);
}
}
final Iterator<Class<? extends UserCommand>> userCommands = cliConfiguration.userCommands();
if (userCommands != null) {
while (userCommands.hasNext()) {
builder.withCommand(userCommands.next());
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;
}

final Cli<Runnable> parser = builder.build();
// 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 CommandLineParser parser = new DefaultParser();
try {
cliConfiguration.decorate(parser.parse(args)).run();
final CommandLine line = parser.parse(options, newArgs.toArray(new String[newArgs.size()]));

final Runnable commandInstance = cmd.newInstance();
if (!newArgs.isEmpty()) { // we have few commands we can execute without args even if we have a bunch of config
for (final Map.Entry<String, Field> option : fields.entrySet()) {
final String key = option.getKey();
if (key.isEmpty()) { // arguments, not an option
final List<String> list = line.getArgList();
if (list != null) {
final Field field = option.getValue();
final Type expectedType = field.getGenericType();
if (ParameterizedType.class.isInstance(expectedType)) {
final ParameterizedType pt = ParameterizedType.class.cast(expectedType);
if ((pt.getRawType() == List.class || pt.getRawType() == Collection.class)
&& pt.getActualTypeArguments().length == 1 && pt.getActualTypeArguments()[0] == String.class) {
field.set(commandInstance, list);
} else {
throw new IllegalArgumentException("@Arguments only supports List<String>");
}
} else {
throw new IllegalArgumentException("@Arguments only supports List<String>");
}
}
} else {
final String value = line.getOptionValue(key);
if (value != null) {
final Field field = option.getValue();
final Class<?> expectedType = field.getType();
if (String.class == expectedType) {
field.set(commandInstance, value);
} else if (long.class == expectedType) {
field.set(commandInstance, Long.parseLong(value));
} else if (int.class == expectedType) {
field.set(commandInstance, Integer.parseInt(value));
} else if (boolean.class == expectedType) {
field.set(commandInstance, Boolean.parseBoolean(value));
} else if (short.class == expectedType) {
field.set(commandInstance, Short.parseShort(value));
} else if (byte.class == expectedType) {
field.set(commandInstance, Byte.parseByte(value));
} else {
try {
field.set(commandInstance, expectedType.getMethod("fromString", String.class)
.invoke(null, value));
} catch (final Exception e) {
throw new IllegalArgumentException(expectedType + " not supported as option with value '" + value + "'");
}
}
}
}
}
}
cliConfiguration.decorate(commandInstance).run();
} catch (final ParseException e) {
parser.parse("help").run();
printHelp(command, options);
} catch (final RuntimeException e) {
Class<?> current = e.getClass();
while (current != null) {
@@ -140,9 +238,71 @@ public Runnable decorate(final Runnable task) {
current = current.getSuperclass();
}
throw e;
} catch (final InstantiationException e) {
throw new IllegalStateException(e);
} catch (final IllegalAccessException e) {
throw new IllegalStateException(e);
}
}

private static void printHelp(final Command command, final Options options) {
new HelpFormatter().printHelp(command.name(), command.description(), options, null, true);
}

private static Options buildOptions(final Class<? extends Runnable> cmd, Map<String, Field> fields) {
final Options options = new Options();
Class<?> it = cmd;
while (it != null) {
for (final Field f : it.getDeclaredFields()) {
final org.apache.batchee.cli.command.api.Option option = f.getAnnotation(org.apache.batchee.cli.command.api.Option.class);
final org.apache.batchee.cli.command.api.Arguments arguments = f.getAnnotation(org.apache.batchee.cli.command.api.Arguments.class);
if (option != null && arguments != null) {
throw new IllegalArgumentException("An @Option can't get @Arguments: " + f);
}

if (option != null) {
final String name = option.name();
options.addOption(Option.builder(name).desc(option.description()).hasArg().build());
fields.put(name, f);
f.setAccessible(true);
} else if (arguments != null) {
if (fields.put("", f) != null) {
throw new IllegalArgumentException("A command can only have a single @Arguments");
}
f.setAccessible(true);
}
}
it = it.getSuperclass();
}
return options;
}

private static String help(final Map<String, Class<? extends Runnable>> commands) {
final StringBuilder builder = new StringBuilder();
for (final Map.Entry<String, Class<? extends Runnable>> cmd : commands.entrySet()) {
final Command c = cmd.getValue().getAnnotation(Command.class);
if (c == null) {
throw new IllegalArgumentException("Missing @Command on " + cmd.getValue());
}
builder.append(c.name());
if (!c.description().isEmpty()) {
builder.append(": ").append(c.description());
}
builder.append(System.getProperty("line.separator"));
}
return builder.toString();
}

private static void addCommand(final Map<String, Class<? extends Runnable>> commands, final Class<? extends Runnable> type) {
final Command command = type.getAnnotation(Command.class);
if (command == null) {
throw new IllegalArgumentException(type + " is not a command, missing @Command");
}

final String name = command.name();
commands.put(name, type);
}

private BatchEECLI() {
// no-op
}
@@ -16,12 +16,12 @@
*/
package org.apache.batchee.cli.command;

import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.apache.batchee.cli.command.api.Command;
import org.apache.batchee.cli.command.api.Option;

@Command(name = "abandon", description = "abandon a batch from its id")
public class Abandon extends SocketCommand {
@Option(name = "-id", description = "id of the batch to abandon", required = true)
@Option(name = "id", description = "id of the batch to abandon", required = true)
private long id;

@Override
@@ -16,6 +16,8 @@
*/
package org.apache.batchee.cli.command;

import org.apache.batchee.cli.command.api.UserCommand;

import java.util.Iterator;

public interface CliConfiguration {
@@ -24,4 +26,5 @@ public interface CliConfiguration {
boolean addDefaultCommands();
Iterator<Class<? extends UserCommand>> userCommands();
Runnable decorate(Runnable task);

}
@@ -16,8 +16,8 @@
*/
package org.apache.batchee.cli.command;

import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.apache.batchee.cli.command.api.Command;
import org.apache.batchee.cli.command.api.Option;
import org.apache.batchee.container.services.ServicesManager;
import org.apache.batchee.spi.PersistenceManagerService;

@@ -27,7 +27,7 @@

@Command(name = "evict", description = "remove old data, uses embedded configuration (no JAXRS support yet)")
public class Eviction implements Runnable {
@Option(name = "-until", description = "date until when the eviction will occur (excluded), YYYYMMDD format", required = true)
@Option(name = "until", description = "date until when the eviction will occur (excluded), YYYYMMDD format", required = true)
private String date;

@Override
@@ -16,8 +16,8 @@
*/
package org.apache.batchee.cli.command;

import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.apache.batchee.cli.command.api.Command;
import org.apache.batchee.cli.command.api.Option;
import org.apache.batchee.container.impl.JobInstanceImpl;
import org.apache.commons.lang3.StringUtils;

@@ -26,7 +26,7 @@

@Command(name = "executions", description = "list executions")
public class Executions extends JobOperatorCommand {
@Option(name = "-id", description = "instance id", required = true)
@Option(name = "id", description = "instance id", required = true)
private long id;

@Override
@@ -16,22 +16,22 @@
*/
package org.apache.batchee.cli.command;

import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.apache.batchee.cli.command.api.Command;
import org.apache.batchee.cli.command.api.Option;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.JobInstance;
import java.util.List;

@Command(name = "instances", description = "list instances")
public class Instances extends JobOperatorCommand {
@Option(name = "-name", description = "name of the batch to start", required = true)
@Option(name = "name", description = "name of the batch to start", required = true)
private String name;

@Option(name = "-start", description = "start of the list of job instance to query")
@Option(name = "start", description = "start of the list of job instance to query")
private int start = 0;

@Option(name = "-count", description = "number of instance to query")
@Option(name = "count", description = "number of instance to query")
private int count = 100;

@Override

0 comments on commit a519144

Please sign in to comment.