Fix server command

This commit is contained in:
James Agnew 2018-05-16 16:13:16 -04:00
parent 0b6a146945
commit e187d72a3b
4 changed files with 72 additions and 9 deletions

View File

@ -33,6 +33,7 @@ import org.fusesource.jansi.AnsiConsole;
import org.slf4j.LoggerFactory;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -52,14 +53,18 @@ public abstract class BaseApp {
loggingConfigOff();
// We don't use qualified names for loggers in CLI
ourLog = LoggerFactory.getLogger(App.class.getSimpleName());
ourLog = LoggerFactory.getLogger(App.class);
}
private MyShutdownHook myShutdownHook;
private boolean myShutdownHookHasNotRun;
private void logAppHeader() {
System.out.flush();
System.out.println("------------------------------------------------------------");
System.out.println("\ud83d\udd25 " + ansi().bold() + " " + provideProductName() + ansi().boldOff() + " " + provideProductVersion() + " - Command Line Tool");
System.out.println("------------------------------------------------------------");
System.out.println("Process ID : " + ManagementFactory.getRuntimeMXBean().getName());
System.out.println("Max configured JVM memory (Xmx) : " + FileHelper.getFileSizeDisplay(Runtime.getRuntime().maxMemory(), 1));
System.out.println("Detected Java version : " + System.getProperty("java.version"));
System.out.println("------------------------------------------------------------");
@ -197,6 +202,9 @@ public abstract class BaseApp {
return;
}
myShutdownHook = new MyShutdownHook(command);
Runtime.getRuntime().addShutdownHook(myShutdownHook);
Options options = command.getOptions();
DefaultParser parser = new DefaultParser();
CommandLine parsedOptions;
@ -215,6 +223,9 @@ public abstract class BaseApp {
// Actually execute the command
command.run(parsedOptions);
myShutdownHookHasNotRun = true;
runCleanupHookAndUnregister();
if (!"true".equals(System.getProperty("test"))) {
System.exit(0);
}
@ -225,9 +236,11 @@ public abstract class BaseApp {
System.err.println(" " + ansi().fg(Ansi.Color.RED).bold() + e.getMessage());
System.err.println("" + ansi().fg(Ansi.Color.WHITE).boldOff());
logCommandUsageNoHeader(command);
runCleanupHookAndUnregister();
System.exit(1);
} catch (CommandFailureException e) {
ourLog.error(e.getMessage());
runCleanupHookAndUnregister();
if ("true".equals(System.getProperty("test"))) {
throw e;
} else {
@ -235,12 +248,22 @@ public abstract class BaseApp {
}
} catch (Throwable t) {
ourLog.error("Error during execution: ", t);
runCleanupHookAndUnregister();
if ("true".equals(System.getProperty("test"))) {
throw new CommandFailureException("Error: " + t.toString(), t);
} else {
System.exit(1);
}
}
}
private void runCleanupHookAndUnregister() {
if (myShutdownHookHasNotRun) {
Runtime.getRuntime().removeShutdownHook(myShutdownHook);
myShutdownHook.run();
myShutdownHookHasNotRun = false;
}
}
private void validateJavaVersion() {
@ -275,4 +298,17 @@ public abstract class BaseApp {
}
private class MyShutdownHook extends Thread {
private final BaseCommand myFinalCommand;
public MyShutdownHook(BaseCommand theFinalCommand) {
myFinalCommand = theFinalCommand;
}
@Override
public void run() {
ourLog.info(provideProductName() + " is shutting down...");
myFinalCommand.cleanup();
}
}
}

View File

@ -152,6 +152,13 @@ public abstract class BaseCommand implements Comparable<BaseCommand> {
addOptionalOption(theOptions, VERBOSE_LOGGING_PARAM, VERBOSE_LOGGING_PARAM_LONGOPT, false, VERBOSE_LOGGING_PARAM_DESC);
}
/**
* Subclasses may override if they want, to do any cleanup they need to do.
*/
public void cleanup() {
// nothing
}
@Override
public int compareTo(BaseCommand theO) {
return getCommandName().compareTo(theO.getCommandName());

View File

@ -29,6 +29,7 @@ import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.springframework.web.context.ContextLoader;
@ -193,9 +194,22 @@ public class RunServerCommand extends BaseCommand {
ourLog.info("Web Testing UI : http://localhost:{}/", myPort);
ourLog.info("Server Base URL: http://localhost:{}{}", myPort, path);
// Never quit.. We'll let the user ctrl-C their way out.
loopForever();
}
@SuppressWarnings("InfiniteLoopStatement")
private void loopForever() {
while (true) {
try {
Thread.sleep(DateUtils.MILLIS_PER_MINUTE);
} catch (InterruptedException theE) {
// ignore
}
}
}
public static void main(String[] theArgs) {

View File

@ -23,6 +23,7 @@ package ca.uhn.fhir.cli;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.igpacks.parser.IgPackParserDstu2;
import ca.uhn.fhir.igpacks.parser.IgPackParserDstu3;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.parser.LenientErrorHandler;
import ca.uhn.fhir.validation.FhirValidator;
import ca.uhn.fhir.validation.SingleValidationMessage;
@ -103,7 +104,7 @@ public class ValidateCommand extends BaseCommand {
parseFhirContext(theCommandLine);
String fileName = theCommandLine.getOptionValue("n");
String contents = theCommandLine.getOptionValue("c");
String contents = theCommandLine.getOptionValue("d");
if (isNotBlank(fileName) && isNotBlank(contents)) {
throw new ParseException("Can not supply both a file (-n) and data (-d)");
}
@ -199,7 +200,12 @@ public class ValidateCommand extends BaseCommand {
val.setValidateAgainstStandardSchema(theCommandLine.hasOption("x"));
val.setValidateAgainstStandardSchematron(theCommandLine.hasOption("s"));
ValidationResult results = val.validateWithResult(contents);
ValidationResult results;
try {
results = val.validateWithResult(contents);
} catch (DataFormatException e) {
throw new CommandFailureException(e.getMessage());
}
StringBuilder b = new StringBuilder("Validation results:" + ansi().boldOff());
int count = 0;