Ver a proveniência

move jetty/webapps to RestWebappServerBase, makes it easier to remote jetty dependency if webapps are not used

tags/2.0.1
Jonathan Cobb há 3 anos
ascendente
cometimento
005ac7b667
2 ficheiros alterados com 60 adições e 34 eliminações
  1. +5
    -34
      wizard-server/src/main/java/org/cobbzilla/wizard/server/RestServerBase.java
  2. +55
    -0
      wizard-server/src/main/java/org/cobbzilla/wizard/server/RestWebappServerBase.java

+ 5
- 34
wizard-server/src/main/java/org/cobbzilla/wizard/server/RestServerBase.java Ver ficheiro

@@ -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<C extends RestServerConfiguration> 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<C extends RestServerConfiguration> implemen
final ServerConfiguration serverConfig = httpServer.getServerConfiguration();

// add handlers -- first the optional webapps
if (configuration.hasWebapps()) {
final ContextHandlerCollection contexts = new ContextHandlerCollection();
final List<Handler> 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<C extends RestServerConfiguration> 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<C> listener : listeners.values()) listener.beforeStop(this);


+ 55
- 0
wizard-server/src/main/java/org/cobbzilla/wizard/server/RestWebappServerBase.java Ver ficheiro

@@ -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<C extends RestServerConfiguration> extends RestServerBase<C> {

@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<Handler> 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);
}
}
}

Carregando…
Cancelar
Guardar