Search in sources :

Example 1 with CombinedExceptionHandler

use of org.openksavi.sponge.core.engine.CombinedExceptionHandler in project sponge by softelnet.

the class StandaloneEngineBuilder method build.

/**
 * Returns a new StandaloneEngine or {@code null} if help or version option is specified so the application should exit.
 */
@Override
public StandaloneSpongeEngine build() {
    try {
        if (commandLineArgs == null) {
            return super.build();
        }
        CommandLineParser parser = new DefaultParser();
        CommandLine commandLine = parser.parse(options, commandLineArgs);
        if (commandLine.hasOption(OPTION_HELP)) {
            printHelp();
            return null;
        }
        if (commandLine.hasOption(OPTION_VERSION)) {
            System.out.println(getDescription());
            return null;
        }
        if (commandLine.hasOption(OPTION_CONFIG)) {
            config(commandLine.getOptionValue(OPTION_CONFIG));
        }
        if (Stream.of(commandLine.getOptions()).filter(option -> option.getOpt().equals(OPTION_CONFIG)).count() > 1) {
            throw new StandaloneInitializationException("Only one Sponge XML configuration file may be provided.");
        }
        Stream.of(commandLine.getOptions()).filter(option -> option.getOpt().equals(OPTION_KNOWLEDGE_BASE)).forEachOrdered(option -> {
            String value = option.getValue();
            if (value == null || StringUtils.isBlank(value)) {
                throw new StandaloneInitializationException("Empty knowledge base specification.");
            }
            String[] values = StringUtils.split(value, ARGUMENT_VALUE_SEPARATOR);
            String kbName = values.length == 2 ? values[0] : DEFAULT_KNOWLEDGE_BASE_NAME;
            String kbFilesString = values.length == 2 ? values[1] : values[0];
            if (StringUtils.isBlank(kbName)) {
                throw new StandaloneInitializationException("Empty knowledge base name.");
            }
            List<String> kbFiles = SpongeUtils.split(kbFilesString, KB_FILES_SEPARATOR);
            knowledgeBase(kbName, kbFiles.toArray(new String[kbFiles.size()]));
        });
        if (!commandLine.hasOption(OPTION_CONFIG) && !commandLine.hasOption(OPTION_KNOWLEDGE_BASE)) {
            throw new StandaloneInitializationException("An Sponge XML configuration file or a knowledge base file(s) should be provided.");
        }
        // Apply standard parameters.
        super.build();
        applyDefaultParameters();
        if (commandLine.hasOption(OPTION_INTERACTIVE)) {
            InteractiveMode interactiveMode = new DefaultInteractiveMode(engine, commandLine.getOptionValue(OPTION_INTERACTIVE), interactiveModeConsoleSupplier);
            ExceptionHandler interactiveExceptionHandler = new SystemErrExceptionHandler();
            interactiveMode.setExceptionHandler(interactiveExceptionHandler);
            engine.setInteractiveMode(interactiveMode);
            if (commandLine.hasOption(OPTION_PRINT_ALL_EXCEPTIONS)) {
                engine.setExceptionHandler(new CombinedExceptionHandler(interactiveExceptionHandler, new LoggingExceptionHandler()));
            } else {
                LoggingUtils.logToConsole(false);
            }
        } else {
            if (commandLine.hasOption(OPTION_PRINT_ALL_EXCEPTIONS)) {
                throw new StandaloneInitializationException("'" + OPTION_PRINT_ALL_EXCEPTIONS + "' option may be used only if '" + OPTION_INTERACTIVE + "' is also used.");
            }
        }
        StandaloneEngineListener standaloneListener = new StandaloneEngineListener(engine);
        List<String> springConfigurationFiles = Stream.of(commandLine.getOptions()).filter(option -> option.getOpt().equals(OPTION_SPRING)).map(option -> {
            String[] values = option.getValues();
            if (values == null || values.length == 0) {
                throw new StandaloneInitializationException("No Spring configuration file provided.");
            }
            return option.getValue(0);
        }).collect(Collectors.toList());
        if (!springConfigurationFiles.isEmpty()) {
            standaloneListener.setSpringConfigurations(springConfigurationFiles);
        }
        if (commandLine.hasOption(OPTION_CAMEL)) {
            standaloneListener.setCamel(true);
        }
        engine.addOnStartupListener(standaloneListener);
        engine.addOnShutdownListener(standaloneListener);
    } catch (ParseException e) {
        throw new StandaloneInitializationException(e.getMessage(), e);
    }
    return engine;
}
Also used : VersionInfo(org.openksavi.sponge.core.VersionInfo) LoggingExceptionHandler(org.openksavi.sponge.core.engine.LoggingExceptionHandler) JLineInteractiveModeConsole(org.openksavi.sponge.standalone.interactive.JLineInteractiveModeConsole) Options(org.apache.commons.cli.Options) LoggerFactory(org.slf4j.LoggerFactory) HelpFormatter(org.apache.commons.cli.HelpFormatter) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) DefaultParser(org.apache.commons.cli.DefaultParser) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) InteractiveModeConsole(org.openksavi.sponge.engine.interactive.InteractiveModeConsole) Logger(org.slf4j.Logger) CommandLineParser(org.apache.commons.cli.CommandLineParser) InteractiveModeConstants(org.openksavi.sponge.core.engine.interactive.InteractiveModeConstants) ExceptionHandler(org.openksavi.sponge.engine.ExceptionHandler) CombinedExceptionHandler(org.openksavi.sponge.core.engine.CombinedExceptionHandler) Collectors(java.util.stream.Collectors) InteractiveMode(org.openksavi.sponge.engine.interactive.InteractiveMode) List(java.util.List) Stream(java.util.stream.Stream) ParseException(org.apache.commons.cli.ParseException) StringsCompleter(org.jline.reader.impl.completer.StringsCompleter) SpongeUtils(org.openksavi.sponge.core.util.SpongeUtils) EngineBuilder(org.openksavi.sponge.core.engine.EngineBuilder) LoggingUtils(org.openksavi.sponge.logging.LoggingUtils) DefaultInteractiveMode(org.openksavi.sponge.core.engine.interactive.DefaultInteractiveMode) LoggingExceptionHandler(org.openksavi.sponge.core.engine.LoggingExceptionHandler) LoggingExceptionHandler(org.openksavi.sponge.core.engine.LoggingExceptionHandler) ExceptionHandler(org.openksavi.sponge.engine.ExceptionHandler) CombinedExceptionHandler(org.openksavi.sponge.core.engine.CombinedExceptionHandler) CommandLine(org.apache.commons.cli.CommandLine) CombinedExceptionHandler(org.openksavi.sponge.core.engine.CombinedExceptionHandler) DefaultInteractiveMode(org.openksavi.sponge.core.engine.interactive.DefaultInteractiveMode) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) InteractiveMode(org.openksavi.sponge.engine.interactive.InteractiveMode) DefaultInteractiveMode(org.openksavi.sponge.core.engine.interactive.DefaultInteractiveMode) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

List (java.util.List)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 HelpFormatter (org.apache.commons.cli.HelpFormatter)1 Option (org.apache.commons.cli.Option)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 StringUtils (org.apache.commons.lang3.StringUtils)1 StringsCompleter (org.jline.reader.impl.completer.StringsCompleter)1 VersionInfo (org.openksavi.sponge.core.VersionInfo)1 CombinedExceptionHandler (org.openksavi.sponge.core.engine.CombinedExceptionHandler)1 EngineBuilder (org.openksavi.sponge.core.engine.EngineBuilder)1 LoggingExceptionHandler (org.openksavi.sponge.core.engine.LoggingExceptionHandler)1 DefaultInteractiveMode (org.openksavi.sponge.core.engine.interactive.DefaultInteractiveMode)1 InteractiveModeConstants (org.openksavi.sponge.core.engine.interactive.InteractiveModeConstants)1 SpongeUtils (org.openksavi.sponge.core.util.SpongeUtils)1