396460 Make ServerConnector configurable with jetty-maven-plugin

This commit is contained in:
Jan Bartel 2012-12-13 19:48:00 +11:00
parent 0c16c774ca
commit d9b34930d5
4 changed files with 96 additions and 49 deletions

View File

@ -85,16 +85,6 @@ public abstract class AbstractJettyMojo extends AbstractMojo
protected String[] excludedGoals; protected String[] excludedGoals;
/**
* List of connectors to use. If none are configured
* then the default is a single SelectChannelConnector at port 8080. You can
* override this default port number by using the system property jetty.port
* on the command line, eg: mvn -Djetty.port=9999 jetty:run. Consider using instead
* the <jettyXml> element to specify external jetty xml config file.
*
* @parameter
*/
protected Connector[] connectors;
/** /**
@ -283,10 +273,18 @@ public abstract class AbstractJettyMojo extends AbstractMojo
protected List pluginArtifacts; protected List pluginArtifacts;
/**
* A ServerConnector to use.
*
* @parameter
*/
protected MavenServerConnector httpConnector;
/** /**
* A wrapper for the Server object * A wrapper for the Server object
*/ */
protected JettyServer server; protected JettyServer server = JettyServer.getInstance();
/** /**
@ -473,28 +471,38 @@ public abstract class AbstractJettyMojo extends AbstractMojo
getLog().debug("Starting Jetty Server ..."); getLog().debug("Starting Jetty Server ...");
printSystemProperties(); printSystemProperties();
this.server = new JettyServer();
//apply any config from a jetty.xml file first which is able to //apply any config from a jetty.xml file first which is able to
//be overwritten by config in the pom.xml //be overwritten by config in the pom.xml
applyJettyXml (); applyJettyXml ();
// if the user hasn't configured their project's pom to use a // if a <httpConnector> was specified in the pom, use it
// different set of connectors, if (httpConnector != null)
// use the default {
// check that its port was set
if (httpConnector.getPort() <= 0)
{
//use any jetty.port settings provided
String tmp = System.getProperty(PORT_SYSPROPERTY, MavenServerConnector.DEFAULT_PORT_STR);
httpConnector.setPort(Integer.parseInt(tmp.trim()));
}
this.server.addConnector(httpConnector);
}
// if the user hasn't configured the connectors in a jetty.xml file so use a default one
Connector[] connectors = this.server.getConnectors(); Connector[] connectors = this.server.getConnectors();
if (connectors == null|| connectors.length == 0) if (connectors == null|| connectors.length == 0)
{ {
//try using ones configured in pom //if <httpConnector> not configured in the pom, create one
this.server.setConnectors(this.connectors); if (httpConnector == null)
connectors = this.server.getConnectors();
if (connectors == null || connectors.length == 0)
{ {
//if a SystemProperty -Djetty.port=<portnum> has been supplied, use that as the default port httpConnector = new MavenServerConnector();
this.connectors = new Connector[] { this.server.createDefaultConnector(System.getProperty(PORT_SYSPROPERTY, null)) }; //use any jetty.port settings provided
this.server.setConnectors(this.connectors); String tmp = System.getProperty(PORT_SYSPROPERTY, MavenServerConnector.DEFAULT_PORT_STR);
httpConnector.setPort(Integer.parseInt(tmp.trim()));
} }
this.server.setConnectors(new Connector[] {httpConnector});
} }
//set up a RequestLog if one is provided //set up a RequestLog if one is provided
@ -565,12 +573,12 @@ public abstract class AbstractJettyMojo extends AbstractMojo
*/ */
public void configureWebApplication () throws Exception public void configureWebApplication () throws Exception
{ {
//As of jetty-7, you must use a <webAppConfig> element //As of jetty-7, you must use a <webApp> element
if (webApp == null) if (webApp == null)
webApp = new JettyWebAppContext(); webApp = new JettyWebAppContext();
//Apply any context xml file to set up the webapp //Apply any context xml file to set up the webapp
//CAUTION: if you've defined a <webAppConfig> element then the //CAUTION: if you've defined a <webApp> element then the
//context xml file can OVERRIDE those settings //context xml file can OVERRIDE those settings
if (contextXml != null) if (contextXml != null)
{ {

View File

@ -38,15 +38,27 @@ import org.eclipse.jetty.webapp.WebAppContext;
*/ */
public class JettyServer extends org.eclipse.jetty.server.Server public class JettyServer extends org.eclipse.jetty.server.Server
{ {
public static int DEFAULT_PORT = 8080; public static final JettyServer __instance = new JettyServer();
public static int DEFAULT_MAX_IDLE_TIME = 30000;
/**
* Singleton instance
* @return
*/
public static JettyServer getInstance()
{
return __instance;
}
private RequestLog requestLog; private RequestLog requestLog;
private ContextHandlerCollection contexts; private ContextHandlerCollection contexts;
public JettyServer()
/**
*
*/
private JettyServer()
{ {
super(); super();
setStopAtShutdown(true); setStopAtShutdown(true);
@ -109,19 +121,4 @@ public class JettyServer extends org.eclipse.jetty.server.Server
} }
} }
} }
public Connector createDefaultConnector(String portnum) throws Exception
{
ServerConnector connector = new ServerConnector(this);
int port = ((portnum==null||portnum.equals(""))?DEFAULT_PORT:Integer.parseInt(portnum.trim()));
connector.setPort(port);
// connector.setMaxIdleTime(DEFAULT_MAX_IDLE_TIME);
return connector;
}
} }

View File

@ -0,0 +1,39 @@
//
// ========================================================================
// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.maven.plugin;
import org.eclipse.jetty.server.ServerConnector;
/**
* MavenServerConnector
*
*
*/
public class MavenServerConnector extends ServerConnector
{
public static int DEFAULT_PORT = 8080;
public static String DEFAULT_PORT_STR = String.valueOf(DEFAULT_PORT);
public static int DEFAULT_MAX_IDLE_TIME = 30000;
public MavenServerConnector()
{
super(JettyServer.getInstance());
}
}

View File

@ -120,7 +120,7 @@ public class Starter
{ {
LOG.debug("Starting Jetty Server ..."); LOG.debug("Starting Jetty Server ...");
this.server = new JettyServer(); this.server = JettyServer.getInstance();
//apply any configs from jetty.xml files first //apply any configs from jetty.xml files first
applyJettyXml (); applyJettyXml ();
@ -131,7 +131,10 @@ public class Starter
if (connectors == null|| connectors.length == 0) if (connectors == null|| connectors.length == 0)
{ {
//if a SystemProperty -Djetty.port=<portnum> has been supplied, use that as the default port //if a SystemProperty -Djetty.port=<portnum> has been supplied, use that as the default port
connectors = new Connector[] { this.server.createDefaultConnector(System.getProperty(PORT_SYSPROPERTY, null)) }; MavenServerConnector httpConnector = new MavenServerConnector();
String tmp = System.getProperty(PORT_SYSPROPERTY, MavenServerConnector.DEFAULT_PORT_STR);
httpConnector.setPort(Integer.parseInt(tmp.trim()));
connectors = new Connector[] {httpConnector};
this.server.setConnectors(connectors); this.server.setConnectors(connectors);
} }