use of org.openksavi.sponge.core.engine.LoggingExceptionHandler 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;
}
Aggregations