Skip to content
Permalink
Browse files
adding StepExecutions command and handling required @options
  • Loading branch information
Romain Manni-Bucau committed Dec 2, 2015
1 parent 2d63aa1 commit d24d2ef09358e9c42bb049e820bf271deccd47c8
Showing 7 changed files with 312 additions and 127 deletions.
@@ -17,19 +17,21 @@
package org.apache.batchee.cli;

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.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.StepExecutions;
import org.apache.batchee.cli.command.Stop;
import org.apache.batchee.cli.command.api.CliConfiguration;
import org.apache.batchee.cli.command.api.Command;
import org.apache.batchee.cli.command.api.Exit;
import org.apache.batchee.cli.command.api.UserCommand;
import org.apache.batchee.cli.command.internal.DefaultCliConfiguration;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
@@ -38,86 +40,25 @@
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) {
final Iterator<CliConfiguration> configuration = ServiceLoader.load(CliConfiguration.class).iterator();
final CliConfiguration cliConfiguration = configuration.hasNext() ? configuration.next() : new CliConfiguration() {
@Override
public String name() {
return "batchee";
}

@Override
public String description() {
return "BatchEE CLI";
}

@Override
public boolean addDefaultCommands() {
return true;
}

@Override
public Iterator<Class<? extends UserCommand>> userCommands() {
final Collection<Class<? extends UserCommand>> classes = new ArrayList<Class<? extends UserCommand>>();
try { // read manually cause we dont want to instantiate them there, so no ServiceLoader
final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
final ClassLoader loader = tccl != null ? tccl : getSystemClassLoader();
final Enumeration<URL> uc = loader.getResources("META-INF/services/org.apache.batchee.cli.command.UserCommand");
while (uc.hasMoreElements()) {
final URL url = uc.nextElement();
BufferedReader r = null;
try {
r = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = r.readLine()) != null) {
if (line.startsWith("#") || line.trim().isEmpty()) {
continue;
}
classes.add(Class.class.cast(loader.loadClass(line.trim())));
}
} catch (final IOException ioe) {
throw new IllegalStateException(ioe);
} catch (final ClassNotFoundException cnfe) {
throw new IllegalArgumentException(cnfe);
} finally {
if (r != null) {
r.close();
}
}
}
} catch (final IOException e) {
throw new IllegalStateException(e);
}
return classes.iterator();
}

@Override
public Runnable decorate(final Runnable task) {
return task;
}
};
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()) {
@@ -127,7 +68,7 @@ public Runnable decorate(final Runnable task) {
Status.class, Running.class,
Stop.class, Abandon.class,
Instances.class, Executions.class,
Eviction.class)) {
StepExecutions.class, Eviction.class)) {
addCommand(commands, type);
}
}
@@ -174,58 +115,7 @@ public Runnable decorate(final Runnable task) {
final CommandLineParser parser = new DefaultParser();
try {
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();
cliConfiguration.decorate(instantiate(cmd, cliConfiguration, fields, !newArgs.isEmpty(), line)).run();
} catch (final ParseException e) {
printHelp(command, options);
} catch (final RuntimeException e) {
@@ -245,6 +135,44 @@ public Runnable decorate(final Runnable task) {
}
}

private static Runnable instantiate(final Class<? extends Runnable> cmd,
final CliConfiguration configuration,
final Map<String, Field> fields,
final boolean hasArgs,
final CommandLine line) throws InstantiationException, IllegalAccessException {
final Runnable commandInstance = cmd.newInstance();
if (hasArgs) { // 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();
field.set(commandInstance, configuration.coerce(value, field.getGenericType()));
}
}
}
}
return commandInstance;
}

private static void printHelp(final Command command, final Options options) {
new HelpFormatter().printHelp(command.name(), '\n' + command.description() + "\n\n", options, null, true);
}
@@ -262,7 +190,11 @@ private static Options buildOptions(final Class<? extends Runnable> cmd, Map<Str

if (option != null) {
final String name = option.name();
options.addOption(Option.builder(name).desc(option.description()).hasArg().build());
final Option.Builder builder = Option.builder(name).desc(option.description()).hasArg();
if (option.required()) {
builder.required();
}
options.addOption(builder.build());
fields.put(name, f);
f.setAccessible(true);
} else if (arguments != null) {
@@ -21,6 +21,7 @@
import org.apache.batchee.container.impl.JobInstanceImpl;
import org.apache.commons.lang3.StringUtils;

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

@@ -29,9 +30,13 @@ public class Executions extends JobOperatorCommand {
@Option(name = "id", description = "instance id", required = true)
private long id;

@Option(name = "showSteps", description = "if steps should be dumped as well")
private boolean steps;

@Override
public void doRun() {
final List<JobExecution> executions = operator().getJobExecutions(new JobInstanceImpl(id));
final JobOperator operator = operator();
final List<JobExecution> executions = operator.getJobExecutions(new JobInstanceImpl(id));
if (!executions.isEmpty()) {
info("Executions of " + executions.iterator().next().getJobName() + " for instance " + id);
}
@@ -43,5 +48,9 @@ public void doRun() {
StringUtils.leftPad(exec.getBatchStatus() != null ? exec.getBatchStatus().toString() : "null", 12),
StringUtils.leftPad(exec.getExitStatus(), 11), exec.getStartTime(), exec.getEndTime()));
}

if (steps) {
new StepExecutions().withOperator(operator).withId(id).run();
}
}
}
@@ -113,9 +113,15 @@ public abstract class JobOperatorCommand implements Runnable {
@Option(name = "addFolderToLoader", description = "force shared lib and libs folders to be added to the classloader")
private boolean addFolderToLoader = false;

protected JobOperator operator;

protected JobOperator operator() {
if (operator != null) {
return operator;
}

if (baseUrl == null) {
return BatchRuntime.getJobOperator();
return operator = BatchRuntime.getJobOperator();
}

final ClientConfiguration configuration = new ClientConfiguration();
@@ -140,7 +146,7 @@ protected JobOperator operator() {
security.setPassword(password);
security.setType(type);

return BatchEEJAXRSClientFactory.newClient(configuration);
return operator = BatchEEJAXRSClientFactory.newClient(configuration);
}

protected void info(final String text) {
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.batchee.cli.command;

import org.apache.batchee.cli.command.api.Command;
import org.apache.batchee.cli.command.api.Option;
import org.apache.commons.lang3.StringUtils;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.Metric;
import javax.batch.runtime.StepExecution;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Command(name = "stepExecutions", description = "list step executions for a particular execution")
public class StepExecutions extends JobOperatorCommand {
@Option(name = "id", description = "execution id", required = true)
private long id;

public StepExecutions withId(final long id) {
this.id = id;
return this;
}
public StepExecutions withOperator(final JobOperator operator) {
this.operator = operator;
return this;
}

@Override
public void doRun() {
final JobOperator operator = operator();
final List<StepExecution> executions = operator.getStepExecutions(id);
if (executions == null || executions.isEmpty()) {
info("Executions of " + id + " not found");
return;
}

info("Step executions of " + id);

final List<Metric.MetricType> metricsOrder = new ArrayList<Metric.MetricType>();
final StringBuilder metrics = new StringBuilder();
for (final Metric.MetricType type : Metric.MetricType.values()) {
metrics.append("\t|\t").append(type.name());
metricsOrder.add(type);
}

final DateFormat format = new SimpleDateFormat("YYYYMMdd hh:mm:ss");
info(" step id\t|\t step name\t|\t start time \t|\t end time \t|\texit status\t|\tbatch status" + metrics.toString());
for (final StepExecution exec : executions) {
final StringBuilder builder = new StringBuilder(String.format("%10d\t|\t%s\t|\t%s\t|\t%s\t|\t%s\t|\t%s",
exec.getStepExecutionId(),
StringUtils.center(exec.getStepName(), 10),
format.format(exec.getStartTime()),
exec.getEndTime() != null ? format.format(exec.getEndTime()) : "-",
StringUtils.center(exec.getExitStatus() == null ? "-" : exec.getExitStatus(), 11),
StringUtils.center(String.valueOf(exec.getBatchStatus()), 12)));
final Map<Metric.MetricType, Long> stepMetrics = new HashMap<Metric.MetricType, Long>();
if (exec.getMetrics() != null) {
for (final Metric m : exec.getMetrics()) {
stepMetrics.put(m.getType(), m.getValue());
}
}
for (final Metric.MetricType type : metricsOrder) {
final Long value = stepMetrics.get(type);
builder.append("\t|\t");
if (value != null) {
builder.append(StringUtils.center(Long.toString(value), type.name().length()));
} else {
builder.append("-");
}
}
info(builder.toString());
}
}
}

0 comments on commit d24d2ef

Please sign in to comment.