Example usage for java.util ServiceLoader spliterator

List of usage examples for java.util ServiceLoader spliterator

Introduction

In this page you can find the example usage for java.util ServiceLoader spliterator.

Prototype

default Spliterator<T> spliterator() 

Source Link

Document

Creates a Spliterator over the elements described by this Iterable .

Usage

From source file:uk.trainwatch.app.util.Main.java

private static int run(String... args) throws Exception {

    // Load all of the utility implementations
    ServiceLoader<Utility> loader = ServiceLoader.load(Utility.class);
    Map<String, Utility> tools = StreamSupport.stream(loader.spliterator(), false)
            .collect(Collectors.toMap(Utility::getName, Function.identity()));

    Supplier<String> toolNames = () -> tools.keySet().stream().sorted()
            .collect(Collectors.joining(", ", "Available tools: ", ""));

    Consumer<Utility> showHelp = u -> new HelpFormatter().printHelp(u.getName(), u.getOptions());

    if (args.length == 0) {
        LOG.log(Level.INFO, toolNames);
        return 1;
    }/*from   w w w.j a va2 s.co  m*/

    String toolName = args[0];

    if ("-?".equals(toolName) || "--help".equals(toolName)) {
        HelpFormatter hf = new HelpFormatter();
        tools.keySet().stream().sorted().map(tools::get).filter(Objects::nonNull).forEach(showHelp);
    } else {
        Utility util = getUtility(tools, toolNames, toolName);
        if (util != null) {

            String toolArgs[] = Arrays.copyOfRange(args, 1, args.length);

            if (toolArgs.length == 0 || "-?".equals(toolArgs[0]) || "--help".equals(toolArgs[0])) {
                new HelpFormatter().printHelp(util.getName(), util.getOptions());
            } else {
                try {
                    CommandLineParser parser = new BasicParser();
                    CommandLine cmd = parser.parse(util.getOptions(), toolArgs);
                    if (util.parseArgs(cmd)) {
                        // Simple banner for identifying whats being run
                        LoggingUtils.logBanner("Utility: " + util.getName());
                        try {
                            CDIUtils.inject(util);
                            util.call();
                            return 0;
                        } finally {
                            LoggingUtils.logBanner("End run of " + util.getName());
                        }
                    } else {
                        LOG.log(Level.WARNING, () -> "Failed to parse args " + cmd);
                        showHelp.accept(util);
                    }
                } catch (UnrecognizedOptionException ex) {
                    LOG.log(Level.WARNING, ex, ex::getMessage);
                    showHelp.accept(util);
                } catch (UncheckedSQLException ex) {
                    LOG.log(Level.SEVERE, null, ex.getCause());
                } catch (Exception ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    return 1;
}