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,17 +85,7 @@ public abstract class AbstractJettyMojo extends AbstractMojo
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;
/**
* List of other contexts to set up. Consider using instead
@ -282,11 +272,19 @@ public abstract class AbstractJettyMojo extends AbstractMojo
*/
protected List pluginArtifacts;
/**
* A ServerConnector to use.
*
* @parameter
*/
protected MavenServerConnector httpConnector;
/**
* 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 ...");
printSystemProperties();
this.server = new JettyServer();
//apply any config from a jetty.xml file first which is able to
//be overwritten by config in the pom.xml
applyJettyXml ();
// if the user hasn't configured their project's pom to use a
// different set of connectors,
// use the default
// if a <httpConnector> was specified in the pom, use it
if (httpConnector != null)
{
// 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();
if (connectors == null|| connectors.length == 0)
{
//try using ones configured in pom
this.server.setConnectors(this.connectors);
connectors = this.server.getConnectors();
if (connectors == null || connectors.length == 0)
//if <httpConnector> not configured in the pom, create one
if (httpConnector == null)
{
//if a SystemProperty -Djetty.port=<portnum> has been supplied, use that as the default port
this.connectors = new Connector[] { this.server.createDefaultConnector(System.getProperty(PORT_SYSPROPERTY, null)) };
this.server.setConnectors(this.connectors);
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()));
}
this.server.setConnectors(new Connector[] {httpConnector});
}
//set up a RequestLog if one is provided
@ -565,12 +573,12 @@ public abstract class AbstractJettyMojo extends AbstractMojo
*/
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)
webApp = new JettyWebAppContext();
//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
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 static int DEFAULT_PORT = 8080;
public static int DEFAULT_MAX_IDLE_TIME = 30000;
public static final JettyServer __instance = new JettyServer();
/**
* Singleton instance
* @return
*/
public static JettyServer getInstance()
{
return __instance;
}
private RequestLog requestLog;
private ContextHandlerCollection contexts;
public JettyServer()
/**
*
*/
private JettyServer()
{
super();
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 ...");
this.server = new JettyServer();
this.server = JettyServer.getInstance();
//apply any configs from jetty.xml files first
applyJettyXml ();
@ -131,7 +131,10 @@ public class Starter
if (connectors == null|| connectors.length == 0)
{
//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);
}
@ -491,9 +494,9 @@ public class Starter
*/
public static final void main(String[] args)
{
if (args == null)
if (args == null)
System.exit(1);
Starter starter = null;
try
{