001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006
007package org.fcrepo.camel.toolbox.app;
008
009import org.slf4j.Logger;
010import org.springframework.context.annotation.AnnotationConfigApplicationContext;
011import picocli.CommandLine;
012
013import java.nio.file.Path;
014import java.util.concurrent.Callable;
015
016import static org.fcrepo.camel.common.config.BasePropsConfig.FCREPO_CAMEL_CONFIG_FILE_PROPERTY;
017import static org.slf4j.LoggerFactory.getLogger;
018
019//TODO pull in version and git revision from generated property file
020
021/**
022 * The command line tool entry point and parameter definitions
023 *
024 * @author dbernstein
025 */
026@CommandLine.Command(name = "fcrepo-camel-toolbox",
027        mixinStandardHelpOptions = true, sortOptions = false,
028        versionProvider = AppVersionProvider.class)
029public class Driver implements Callable<Integer> {
030
031    private static final Logger LOGGER = getLogger(Driver.class);
032
033
034    @CommandLine.Option(names = {"--config", "-c"}, required = false, order = 1,
035            description = "The path to the configuration file")
036    private Path configurationFilePath;
037
038    @Override
039    public Integer call() {
040
041        if (configurationFilePath != null) {
042            System.setProperty(FCREPO_CAMEL_CONFIG_FILE_PROPERTY, configurationFilePath.toFile().getAbsolutePath());
043        }
044        final var appContext = new AnnotationConfigApplicationContext("org.fcrepo.camel");
045        appContext.registerShutdownHook();
046        appContext.start();
047        LOGGER.info("fcrepo-camel-toolbox started.");
048
049        while (appContext.isRunning()) {
050            Thread.onSpinWait();
051        }
052        return 0;
053    }
054
055    /**
056     * @param args Command line arguments
057     */
058    public static void main(final String[] args) {
059        final Driver driver = new Driver();
060        final CommandLine cmd = new CommandLine(driver);
061        cmd.setExecutionExceptionHandler(new AppExceptionHandler(driver));
062        cmd.execute(args);
063    }
064
065    private static class AppExceptionHandler implements CommandLine.IExecutionExceptionHandler {
066
067        private final Driver driver;
068
069        AppExceptionHandler(final Driver driver) {
070            this.driver = driver;
071        }
072
073        @Override
074        public int handleExecutionException(
075                final Exception ex,
076                final CommandLine commandLine,
077                final CommandLine.ParseResult parseResult) {
078            commandLine.getErr().println(ex.getMessage());
079            ex.printStackTrace(commandLine.getErr());
080            commandLine.usage(commandLine.getErr());
081            return commandLine.getCommandSpec().exitCodeOnExecutionException();
082        }
083    }
084
085}