From d0a6a34fcc272d6b2de446d5b238278105b272a5 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 30 Oct 2014 17:04:03 +1100 Subject: [PATCH] 448675 Impossible to set own Threadpool when using jetty-maven-plugin --- .../jetty/maven/plugin/AbstractJettyMojo.java | 75 ++++---- .../maven/plugin/JettyEffectiveWebXml.java | 5 +- .../maven/plugin/JettyRunForkedMojo.java | 7 +- .../maven/plugin/MavenServerConnector.java | 2 + .../{JettyServer.java => ServerSupport.java} | 170 ++++++++++++------ .../eclipse/jetty/maven/plugin/Starter.java | 42 ++--- 6 files changed, 166 insertions(+), 135 deletions(-) rename jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/{JettyServer.java => ServerSupport.java} (50%) diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java index df6a4788b3b..b64af83d808 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java @@ -42,6 +42,7 @@ import org.codehaus.plexus.util.FileUtils; import org.eclipse.jetty.security.LoginService; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.RequestLog; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ShutdownMonitor; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.ContextHandlerCollection; @@ -61,11 +62,7 @@ import org.eclipse.jetty.xml.XmlConfiguration; */ public abstract class AbstractJettyMojo extends AbstractMojo { - /** - * - */ - public String PORT_SYSPROPERTY = "jetty.port"; - + /** * Whether or not to include dependencies on the plugin's classpath with <scope>provided</scope> @@ -277,8 +274,9 @@ public abstract class AbstractJettyMojo extends AbstractMojo /** * A wrapper for the Server object + * @parameter */ - protected JettyServer server = new JettyServer(); + protected Server server; /** @@ -304,6 +302,10 @@ public abstract class AbstractJettyMojo extends AbstractMojo */ protected Thread consoleScanner; + protected ServerSupport serverSupport; + + + /** *

@@ -451,11 +453,13 @@ public abstract class AbstractJettyMojo extends AbstractMojo * @throws Exception */ public void applyJettyXml() throws Exception - { - if (getJettyXmlFiles() == null) - return; - - this.server.applyXmlConfigurations(getJettyXmlFiles()); + { + Server tmp = ServerSupport.applyXmlConfigurations(server, getJettyXmlFiles()); + if (server == null) + server = tmp; + + if (server == null) + server = new Server(); } @@ -469,6 +473,9 @@ public abstract class AbstractJettyMojo extends AbstractMojo try { getLog().debug("Starting Jetty Server ..."); + + //make sure Jetty does not use URLConnection caches with the plugin + Resource.setDefaultUseCaches(false); configureMonitor(); @@ -477,7 +484,7 @@ public abstract class AbstractJettyMojo extends AbstractMojo //apply any config from a jetty.xml file first which is able to //be overwritten by config in the pom.xml applyJettyXml (); - + // if a was specified in the pom, use it if (httpConnector != null) { @@ -485,46 +492,21 @@ public abstract class AbstractJettyMojo extends AbstractMojo if (httpConnector.getPort() <= 0) { //use any jetty.port settings provided - String tmp = System.getProperty(PORT_SYSPROPERTY, MavenServerConnector.DEFAULT_PORT_STR); + String tmp = System.getProperty(MavenServerConnector.PORT_SYSPROPERTY, MavenServerConnector.DEFAULT_PORT_STR); httpConnector.setPort(Integer.parseInt(tmp.trim())); } - if (httpConnector.getServer() == null) - httpConnector.setServer(this.server); - this.server.addConnector(httpConnector); + httpConnector.setServer(server); } - // if the user hasn't configured the connectors in a jetty.xml file so use a default one - Connector[] connectors = this.server.getConnectors(); - if (connectors == null|| connectors.length == 0) - { - //if not configured in the pom, create one - if (httpConnector == null) - { - httpConnector = new MavenServerConnector(); - //use any jetty.port settings provided - String tmp = System.getProperty(PORT_SYSPROPERTY, MavenServerConnector.DEFAULT_PORT_STR); - httpConnector.setPort(Integer.parseInt(tmp.trim())); - } - if (httpConnector.getServer() == null) - httpConnector.setServer(this.server); - this.server.setConnectors(new Connector[] {httpConnector}); - } + ServerSupport.configureConnectors(server, httpConnector); - //set up a RequestLog if one is provided - if (this.requestLog != null) - this.server.setRequestLog(this.requestLog); - - //set up the webapp and any context provided - this.server.configureHandlers(); + //set up a RequestLog if one is provided and the handle structure + ServerSupport.configureHandlers(server, this.requestLog); configureWebApplication(); - this.server.addWebApplication(webApp); + ServerSupport.addWebApplication(server, webApp); // set up security realms - for (int i = 0; (this.loginServices != null) && i < this.loginServices.length; i++) - { - getLog().debug(this.loginServices[i].getClass().getName() + ": "+ this.loginServices[i].toString()); - this.server.addBean(this.loginServices[i]); - } + ServerSupport.configureLoginServices(server, loginServices); //do any other configuration required by the //particular Jetty version @@ -534,7 +516,6 @@ public abstract class AbstractJettyMojo extends AbstractMojo this.server.start(); getLog().info("Started Jetty Server"); - if ( dumpOnStart ) { @@ -580,6 +561,10 @@ public abstract class AbstractJettyMojo extends AbstractMojo } + + + + /** * Subclasses should invoke this to setup basic info * on the webapp diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java index 6c607c85df5..90a426835a7 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java @@ -94,9 +94,8 @@ public class JettyEffectiveWebXml extends JettyRunMojo //apply any config from a jetty.xml file first to our "fake" server instance //TODO probably not necessary applyJettyXml (); - - server.configureHandlers(); + ServerSupport.configureHandlers(server, null); //ensure config of the webapp based on settings in plugin configureWebApplication(); @@ -114,7 +113,7 @@ public class JettyEffectiveWebXml extends JettyRunMojo webApp.setQuickStartWebDescriptor(descriptor); - server.addWebApplication(webApp); + ServerSupport.addWebApplication(server, webApp); //if our server has a thread pool associated we can do any annotation scanning multithreaded, //otherwise scanning will be single threaded diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java index cc2d67f723a..ad4d5b949de 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java @@ -46,6 +46,7 @@ import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.eclipse.jetty.annotations.AnnotationConfiguration; import org.eclipse.jetty.quickstart.QuickStartDescriptorGenerator; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceCollection; @@ -257,9 +258,11 @@ public class JettyRunForkedMojo extends JettyRunMojo printSystemProperties(); //do NOT apply the jettyXml configuration - as the jvmArgs may be needed for it to work + if (server == null) + server = new Server(); //ensure handler structure enabled - server.configureHandlers(); + ServerSupport.configureHandlers(server, null); //ensure config of the webapp based on settings in plugin configureWebApplication(); @@ -280,7 +283,7 @@ public class JettyRunForkedMojo extends JettyRunMojo webApp.setQuickStartWebDescriptor(Resource.newResource(forkWebXml)); //add webapp to our fake server instance - server.addWebApplication(webApp); + ServerSupport.addWebApplication(server, webApp); //if our server has a thread pool associated we can do annotation scanning multithreaded, //otherwise scanning will be single threaded diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/MavenServerConnector.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/MavenServerConnector.java index ac57d4f475b..b2bb1a8e8fe 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/MavenServerConnector.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/MavenServerConnector.java @@ -47,6 +47,8 @@ import org.eclipse.jetty.util.thread.Scheduler; */ public class MavenServerConnector extends AbstractLifeCycle implements Connector { + public static String PORT_SYSPROPERTY = "jetty.port"; + public static final int DEFAULT_PORT = 8080; public static final String DEFAULT_PORT_STR = String.valueOf(DEFAULT_PORT); public static final int DEFAULT_MAX_IDLE_TIME = 30000; diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyServer.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/ServerSupport.java similarity index 50% rename from jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyServer.java rename to jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/ServerSupport.java index cd7b3c3cae3..b1bdf9e0086 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyServer.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/ServerSupport.java @@ -16,17 +16,20 @@ // ======================================================================== // + package org.eclipse.jetty.maven.plugin; - import java.io.File; -import java.util.Collections; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; import java.util.Map; +import org.eclipse.jetty.security.LoginService; +import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.RequestLog; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; @@ -35,54 +38,16 @@ import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.xml.XmlConfiguration; - /** - * JettyServer - * - * Maven jetty plugin version of a wrapper for the Server class. + * ServerSupport + * + * Helps configure the Server instance. * */ -public class JettyServer extends org.eclipse.jetty.server.Server -{ - private RequestLog requestLog; - private ContextHandlerCollection contexts; - - - - /** - * - */ - public JettyServer() - { - super(); - //make sure Jetty does not use URLConnection caches with the plugin - Resource.setDefaultUseCaches(false); - } +public class ServerSupport +{ - - public void setRequestLog (RequestLog requestLog) - { - this.requestLog = requestLog; - } - /** - * @see org.eclipse.jetty.server.Server#doStart() - */ - public void doStart() throws Exception - { - super.doStart(); - } - - - /** - * @see org.eclipse.jetty.server.handler.HandlerCollection#addHandler(org.eclipse.jetty.server.Handler) - */ - public void addWebApplication(WebAppContext webapp) throws Exception - { - contexts.addHandler (webapp); - } - - /** * Set up the handler structure to receive a webapp. * Also put in a DefaultHandler so we get a nice page @@ -90,22 +55,25 @@ public class JettyServer extends org.eclipse.jetty.server.Server * context isn't at root. * @throws Exception */ - public void configureHandlers () throws Exception + public static void configureHandlers (Server server, RequestLog requestLog) throws Exception { + if (server == null) + throw new IllegalArgumentException ("Server is null"); + DefaultHandler defaultHandler = new DefaultHandler(); RequestLogHandler requestLogHandler = new RequestLogHandler(); - if (this.requestLog != null) - requestLogHandler.setRequestLog(this.requestLog); - - contexts = (ContextHandlerCollection)super.getChildHandlerByClass(ContextHandlerCollection.class); - if (contexts==null) + if (requestLog != null) + requestLogHandler.setRequestLog(requestLog); + + ContextHandlerCollection contexts = findContextHandlerCollection(server); + if (contexts == null) { contexts = new ContextHandlerCollection(); - HandlerCollection handlers = (HandlerCollection)super.getChildHandlerByClass(HandlerCollection.class); - if (handlers==null) + HandlerCollection handlers = (HandlerCollection)server.getChildHandlerByClass(HandlerCollection.class); + if (handlers == null) { handlers = new HandlerCollection(); - super.setHandler(handlers); + server.setHandler(handlers); handlers.setHandlers(new Handler[]{contexts, defaultHandler, requestLogHandler}); } else @@ -114,6 +82,87 @@ public class JettyServer extends org.eclipse.jetty.server.Server } } } + + + /** + * Configure at least one connector for the server + * + * @param server + * @param connector + */ + public static void configureConnectors (Server server, Connector connector) + { + if (server == null) + throw new IllegalArgumentException("Server is null"); + + //if a connector is provided, use it + if (connector != null) + { + server.addConnector(connector); + return; + } + + + + // if the user hasn't configured the connectors in a jetty.xml file so use a default one + Connector[] connectors = server.getConnectors(); + if (connectors == null || connectors.length == 0) + { + //Make a new default connector + MavenServerConnector tmp = new MavenServerConnector(); + //use any jetty.port settings provided + String port = System.getProperty(MavenServerConnector.PORT_SYSPROPERTY, MavenServerConnector.DEFAULT_PORT_STR); + tmp.setPort(Integer.parseInt(port.trim())); + tmp.setServer(server); + server.setConnectors(new Connector[] {tmp}); + } + } + + + /** + * Set up any security LoginServices provided. + * + * @param server + * @param loginServices + */ + public static void configureLoginServices (Server server, LoginService[] loginServices) + { + if (server == null) + throw new IllegalArgumentException ("Server is null"); + + if (loginServices != null) + { + for (LoginService loginService:loginServices) + { + PluginLog.getLog().debug(loginService.getClass().getName() + ": "+ loginService.toString()); + server.addBean(loginService); + } + } + } + + + /** + * @see org.eclipse.jetty.server.handler.HandlerCollection#addHandler(org.eclipse.jetty.server.Handler) + */ + public static void addWebApplication(Server server, WebAppContext webapp) throws Exception + { + if (server == null) + throw new IllegalArgumentException ("Server is null"); + ContextHandlerCollection contexts = findContextHandlerCollection(server); + if (contexts == null) + throw new IllegalStateException("ContextHandlerCollection is null"); + contexts.addHandler (webapp); + } + + + public static ContextHandlerCollection findContextHandlerCollection (Server server) + { + if (server == null) + return null; + + return (ContextHandlerCollection)server.getChildHandlerByClass(ContextHandlerCollection.class); + } + /** * Apply xml files to server startup, passing in ourselves as the @@ -122,13 +171,17 @@ public class JettyServer extends org.eclipse.jetty.server.Server * @param files * @throws Exception */ - public void applyXmlConfigurations (List files) + public static Server applyXmlConfigurations (Server server, List files) throws Exception { if (files == null || files.isEmpty()) - return; + return server; - Map lastMap = Collections.singletonMap("Server", (Object)this); + Map lastMap = new HashMap(); + + if (server != null) + lastMap.put("Server", server); + for ( File xmlFile : files ) { @@ -152,5 +205,8 @@ public class JettyServer extends org.eclipse.jetty.server.Server xmlConfiguration.configure(); lastMap = xmlConfiguration.getIdMap(); } + + return (Server)lastMap.get("Server"); } + } diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/Starter.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/Starter.java index 63e49c97342..cd95f00532f 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/Starter.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/Starter.java @@ -33,6 +33,7 @@ import java.util.TreeMap; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ShutdownMonitor; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.util.log.Log; @@ -57,7 +58,7 @@ public class Starter private List jettyXmls; // list of jetty.xml config files to apply - Mandatory private File contextXml; //name of context xml file to configure the webapp - Mandatory - private JettyServer server = new JettyServer(); + private Server server; private JettyWebAppContext webApp; @@ -121,34 +122,16 @@ public class Starter public void configureJetty () throws Exception { LOG.debug("Starting Jetty Server ..."); - + Resource.setDefaultUseCaches(false); + //apply any configs from jetty.xml files first applyJettyXml (); - // if the user hasn't configured a connector in the jetty.xml - //then use a default - Connector[] connectors = this.server.getConnectors(); - if (connectors == null|| connectors.length == 0) - { - //if a SystemProperty -Djetty.port= has been supplied, use that as the default port - MavenServerConnector httpConnector = new MavenServerConnector(); - httpConnector.setServer(this.server); - String tmp = System.getProperty(PORT_SYSPROPERTY, MavenServerConnector.DEFAULT_PORT_STR); - httpConnector.setPort(Integer.parseInt(tmp.trim())); - connectors = new Connector[] {httpConnector}; - this.server.setConnectors(connectors); - } - - //check that everything got configured, and if not, make the handlers - HandlerCollection handlers = (HandlerCollection) server.getChildHandlerByClass(HandlerCollection.class); - if (handlers == null) - { - handlers = new HandlerCollection(); - server.setHandler(handlers); - } + //ensure there's a connector + ServerSupport.configureConnectors(server, null); //check if contexts already configured, create if not - this.server.configureHandlers(); + ServerSupport.configureHandlers(server, null); webApp = new JettyWebAppContext(); @@ -176,7 +159,7 @@ public class Starter xmlConfiguration.configure(webApp); } - this.server.addWebApplication(webApp); + ServerSupport.addWebApplication(server, webApp); if(stopPort>0 && stopKey!=null) { @@ -431,9 +414,12 @@ public class Starter */ public void applyJettyXml() throws Exception { - if (jettyXmls == null) - return; - this.server.applyXmlConfigurations(jettyXmls); + Server tmp = ServerSupport.applyXmlConfigurations(server, jettyXmls); + if (server == null) + server = tmp; + + if (server == null) + server = new Server(); }