From 005ac7b66744912657826d896b69023096ac2ea9 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Thu, 3 Dec 2020 08:16:08 -0500 Subject: [PATCH] move jetty/webapps to RestWebappServerBase, makes it easier to remote jetty dependency if webapps are not used --- .../wizard/server/RestServerBase.java | 39 ++----------- .../wizard/server/RestWebappServerBase.java | 55 +++++++++++++++++++ 2 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 wizard-server/src/main/java/org/cobbzilla/wizard/server/RestWebappServerBase.java diff --git a/wizard-server/src/main/java/org/cobbzilla/wizard/server/RestServerBase.java b/wizard-server/src/main/java/org/cobbzilla/wizard/server/RestServerBase.java index a4e559e..3662549 100644 --- a/wizard-server/src/main/java/org/cobbzilla/wizard/server/RestServerBase.java +++ b/wizard-server/src/main/java/org/cobbzilla/wizard/server/RestServerBase.java @@ -19,9 +19,6 @@ import org.cobbzilla.wizard.server.config.factory.ConfigurationSource; import org.cobbzilla.wizard.server.config.factory.StreamConfigurationSource; import org.cobbzilla.wizard.server.handler.StaticAssetHandler; import org.cobbzilla.wizard.validation.Validator; -import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.glassfish.grizzly.http.server.*; import org.glassfish.grizzly.nio.transport.TCPNIOTransport; import org.glassfish.grizzly.threadpool.ThreadPoolConfig; @@ -164,14 +161,9 @@ public abstract class RestServerBase implemen @Override public boolean isRunning() { return httpServer != null && httpServer.isStarted(); } - @Getter(lazy=true) private final Server jetty = initJetty(); - private Server initJetty() { - final HttpConfiguration httpConfiguration = getConfiguration().getHttp(); - if (!httpConfiguration.hasWebPort()) { - httpConfiguration.setWebPort(PortPicker.pickOrDie()); - } - return new Server(httpConfiguration.getWebPort()); - } + // RestWebappServerBase overrides these to enable support for Servlets/Webapps via Jetty + protected void initWebapps(C configuration, ConfigurableApplicationContext applicationContext) { notSupported("initWebapps"); } + protected void stopWebapps() { notSupported("stopWebapps"); } public HttpServer buildServer(String serverName) throws IOException { @@ -219,22 +211,7 @@ public abstract class RestServerBase implemen final ServerConfiguration serverConfig = httpServer.getServerConfiguration(); // add handlers -- first the optional webapps - if (configuration.hasWebapps()) { - final ContextHandlerCollection contexts = new ContextHandlerCollection(); - final List handlers = new ArrayList<>(); - for (WebappConfiguration config : configuration.getWebapps()) { - handlers.add(config.deploy(applicationContext)); - } - try { - contexts.setHandlers((Handler[]) handlers.toArray()); - final Server jetty = getJetty(); - jetty.setHandler(contexts); - jetty.setStopAtShutdown(true); - jetty.start(); - } catch (Exception e) { - return die("buildServer: error starting Jetty to run webapps: "+e, e); - } - } + if (configuration.hasWebapps()) initWebapps(configuration, applicationContext); // then the REST/Jersey handler final HttpHandler processor = ContainerFactory.createContainer(GrizzlyHttpContainer.class, rc); @@ -344,13 +321,7 @@ public abstract class RestServerBase implemen public synchronized void stopServer() { // stop jetty first, if running - if (configuration.hasWebapps()) { - try { - getJetty().stop(); - } catch (Exception e) { - log.warn("stopServer: error stopping jetty: "+e, e); - } - } + if (configuration.hasWebapps()) stopWebapps(); if (httpServer != null && httpServer.isStarted()) { log.info("stopServer: stopping " + configuration.getServerName() + "..."); for (RestServerLifecycleListener listener : listeners.values()) listener.beforeStop(this); diff --git a/wizard-server/src/main/java/org/cobbzilla/wizard/server/RestWebappServerBase.java b/wizard-server/src/main/java/org/cobbzilla/wizard/server/RestWebappServerBase.java new file mode 100644 index 0000000..d08a0c8 --- /dev/null +++ b/wizard-server/src/main/java/org/cobbzilla/wizard/server/RestWebappServerBase.java @@ -0,0 +1,55 @@ +package org.cobbzilla.wizard.server; + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.cobbzilla.util.network.PortPicker; +import org.cobbzilla.wizard.server.config.HttpConfiguration; +import org.cobbzilla.wizard.server.config.RestServerConfiguration; +import org.cobbzilla.wizard.server.config.WebappConfiguration; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.springframework.context.ConfigurableApplicationContext; + +import java.util.ArrayList; +import java.util.List; + +import static org.cobbzilla.util.daemon.ZillaRuntime.die; + +@Slf4j +public class RestWebappServerBase extends RestServerBase { + + @Getter(lazy=true) private final Server jetty = initJetty(); + private Server initJetty() { + final HttpConfiguration httpConfiguration = getConfiguration().getHttp(); + if (!httpConfiguration.hasWebPort()) { + httpConfiguration.setWebPort(PortPicker.pickOrDie()); + } + return new Server(httpConfiguration.getWebPort()); + } + + @Override protected void initWebapps(C configuration, ConfigurableApplicationContext applicationContext) { + final ContextHandlerCollection contexts = new ContextHandlerCollection(); + final List handlers = new ArrayList<>(); + for (WebappConfiguration config : configuration.getWebapps()) { + handlers.add(config.deploy(applicationContext)); + } + try { + contexts.setHandlers((Handler[]) handlers.toArray()); + final Server jetty = getJetty(); + jetty.setHandler(contexts); + jetty.setStopAtShutdown(true); + jetty.start(); + } catch (Exception e) { + die("initWebapps: error starting Jetty to run webapps: "+e, e); + } + } + + @Override protected void stopWebapps() { + try { + getJetty().stop(); + } catch (Exception e) { + log.warn("stopWebapps: error stopping jetty: "+e, e); + } + } +}