|
|
@ -579,46 +579,56 @@ public class SpringApplication {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Static helper that can be used to exit a {@link SpringApplication} and obtain a
|
|
|
|
* Static helper that can be used to exit a {@link SpringApplication} and obtain a
|
|
|
|
* code indicating success (0) or otherwise. Does not throw exceptions but should
|
|
|
|
* code indicating success (0) or otherwise. Does not throw exceptions but should
|
|
|
|
* print stack traces of any encountered.
|
|
|
|
* print stack traces of any encountered. Applies the specified
|
|
|
|
|
|
|
|
* {@link ExitCodeGenerator} in addition to any Spring beans that implement
|
|
|
|
|
|
|
|
* {@link ExitCodeGenerator}. In the case of multiple exit codes the highest value
|
|
|
|
|
|
|
|
* will be used (or if all values are negative, the lowest value will be used)
|
|
|
|
* @param context the context to close if possible
|
|
|
|
* @param context the context to close if possible
|
|
|
|
|
|
|
|
* @param exitCodeGenerators exist code generators
|
|
|
|
* @return the outcome (0 if successful)
|
|
|
|
* @return the outcome (0 if successful)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static int exit(ApplicationContext context,
|
|
|
|
public static int exit(ApplicationContext context,
|
|
|
|
ExitCodeGenerator... exitCodeGenerators) {
|
|
|
|
ExitCodeGenerator... exitCodeGenerators) {
|
|
|
|
|
|
|
|
int exitCode = 0;
|
|
|
|
int code = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<ExitCodeGenerator> exiters = new ArrayList<ExitCodeGenerator>(
|
|
|
|
|
|
|
|
Arrays.asList(exitCodeGenerators));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
try {
|
|
|
|
exiters.addAll(context.getBeansOfType(ExitCodeGenerator.class).values());
|
|
|
|
List<ExitCodeGenerator> generators = new ArrayList<ExitCodeGenerator>();
|
|
|
|
|
|
|
|
generators.addAll(Arrays.asList(exitCodeGenerators));
|
|
|
|
for (ExitCodeGenerator exiter : exiters) {
|
|
|
|
generators.addAll(context.getBeansOfType(ExitCodeGenerator.class)
|
|
|
|
try {
|
|
|
|
.values());
|
|
|
|
int value = exiter.getExitCode();
|
|
|
|
exitCode = getExitCode(generators);
|
|
|
|
if (value > code || value < 0 && value < code) {
|
|
|
|
} finally {
|
|
|
|
code = value;
|
|
|
|
close(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
code = code == 0 ? 1 : code;
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (context instanceof ConfigurableApplicationContext) {
|
|
|
|
|
|
|
|
ConfigurableApplicationContext closable = (ConfigurableApplicationContext) context;
|
|
|
|
|
|
|
|
closable.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
code = code == 0 ? 1 : code;
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
exitCode = (exitCode == 0 ? 1 : exitCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
private static int getExitCode(List<ExitCodeGenerator> exitCodeGenerators) {
|
|
|
|
|
|
|
|
int exitCode = 0;
|
|
|
|
|
|
|
|
for (ExitCodeGenerator exitCodeGenerator : exitCodeGenerators) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int value = exitCodeGenerator.getExitCode();
|
|
|
|
|
|
|
|
if (value > 0 && value > exitCode || value < 0 && value < exitCode) {
|
|
|
|
|
|
|
|
exitCode = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
exitCode = (exitCode == 0 ? 1 : exitCode);
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void close(ApplicationContext context) {
|
|
|
|
|
|
|
|
if (context instanceof ConfigurableApplicationContext) {
|
|
|
|
|
|
|
|
ConfigurableApplicationContext closable = (ConfigurableApplicationContext) context;
|
|
|
|
|
|
|
|
closable.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|