in meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveTask.java [381:465]
private void doRun() {
final Thread thread = Thread.currentThread();
final ClassLoader loader = thread.getContextClassLoader();
final AtomicBoolean running = new AtomicBoolean(false);
Thread hook = null;
AutoCloseable container;
try {
final Class<?> containerClass = loader.loadClass("org.apache.meecrowave.Meecrowave");
final Class<?> configClass = loader.loadClass("org.apache.meecrowave.Meecrowave$Builder");
final Object config = getConfig(configClass);
running.set(true);
container = AutoCloseable.class.cast(containerClass.getConstructor(configClass).newInstance(config));
final AutoCloseable finalContainer = container;
hook = new Thread() {
@Override
public void run() {
if (running.compareAndSet(true, false)) {
final Thread thread = Thread.currentThread();
final ClassLoader old = thread.getContextClassLoader();
thread.setContextClassLoader(loader);
try {
finalContainer.close();
} catch (final NoClassDefFoundError noClassDefFoundError) {
// debug cause it is too late to shutdown properly so don't pollute logs
getLogger().debug("can't stop Meecrowave", noClassDefFoundError);
} catch (final Exception e) {
getLogger().error("can't stop Meecrowave", e);
} finally {
thread.setContextClassLoader(old);
}
}
}
};
hook.setName("Meecrowave-Embedded-ShutdownHook");
Runtime.getRuntime().addShutdownHook(hook);
containerClass.getMethod("start").invoke(container);
final String fixedContext = ofNullable(context).orElse("");
if (webapp == null) {
containerClass.getMethod("deployClasspath", String.class).invoke(container, fixedContext);
} else {
containerClass.getMethod("deployWebapp", String.class, File.class).invoke(container, fixedContext, webapp);
}
getLogger().info("Meecrowave started on " + configClass.getMethod("getHost").invoke(config) + ":" + configClass.getMethod("getHttpPort").invoke(config));
} catch (final Exception e) {
ofNullable(hook).ifPresent(h -> {
try {
h.run();
} finally {
Runtime.getRuntime().removeShutdownHook(h);
}
});
throw new GradleException(e.getMessage(), e);
}
try {
String line;
final Scanner scanner = new Scanner(System.in);
while ((line = scanner.nextLine()) != null) {
final String cmd = line.trim().toLowerCase(Locale.ENGLISH);
switch (cmd) {
case "exit":
case "quit":
running.set(false);
try {
hook.run();
} finally {
Runtime.getRuntime().removeShutdownHook(hook);
}
return;
default:
getLogger().warn("Unknown: '" + cmd + "', use 'exit' or 'quit'");
}
}
} catch (final Exception e) {
Thread.interrupted();
} finally {
thread.setContextClassLoader(loader);
}
}