[Bug 394232] add jetty-ant into jetty9

This commit is contained in:
Jesse McConnell 2012-11-15 13:38:53 -06:00
parent 73ecfd6755
commit 7975b25ae8
17 changed files with 462 additions and 48 deletions

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-project</artifactId>
@ -8,7 +9,29 @@
<artifactId>jetty-ant</artifactId>
<packaging>jar</packaging>
<name>Jetty :: Ant Plugin</name>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-lib-deps</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>org.eclipse.jetty</includeGroupIds>
<excludeGroupIds>org.eclipse.jetty.orbit,org.eclipse.jetty.spdy,org.eclipse.jetty.websocket,org.eclipse.jetty.drafts</excludeGroupIds>
<excludeArtifactIds>jetty-all,jetty-start,jetty-monitor,jetty-jsp</excludeArtifactIds>
<includeTypes>jar</includeTypes>
<outputDirectory>${project.build.directory}/test-lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
@ -21,6 +44,11 @@
<artifactId>ant</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-launcher</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
@ -35,7 +63,7 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${project.version}</version>
</dependency>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>

View File

@ -35,9 +35,6 @@ import org.eclipse.jetty.webapp.WebXmlConfiguration;
* properties into the configured web application. The list of classpath files,
* the application base directory and web.xml file could be specified in this
* way.
*
* @author Jakub Pawlowicz
* @author Athena Yao
*/
public class AntWebXmlConfiguration extends WebXmlConfiguration
{

View File

@ -37,8 +37,6 @@ import org.eclipse.jetty.util.Scanner;
/**
* Ant task for running a Jetty server.
*
* @author Jakub Pawlowicz
*/
public class JettyRunTask extends Task
{

View File

@ -27,7 +27,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.jetty.ant.types.Connectors.Connector;
import org.eclipse.jetty.ant.types.Connector;
import org.eclipse.jetty.ant.utils.ServerProxy;
import org.eclipse.jetty.ant.utils.TaskLog;
import org.eclipse.jetty.ant.utils.WebApplicationProxy;
@ -47,8 +47,6 @@ import org.xml.sax.SAXException;
/**
* A proxy class for interaction with Jetty server object. Used to have some
* level of abstraction over standard Jetty classes.
*
* @author Jakub Pawlowicz
*/
public class ServerProxyImpl implements ServerProxy
{
@ -128,7 +126,10 @@ public class ServerProxyImpl implements ServerProxy
jc.setPort(jettyConnector.getPort());
jc.setIdleTimeout(jettyConnector.getMaxIdleTime());
server.addConnector(jc);
}
// Configures login services
@ -217,6 +218,8 @@ public class ServerProxyImpl implements ServerProxy
try
{
server.start();
TaskLog.log("" + server.getConnectors()[0]);
startScanners();
server.join();

View File

@ -57,7 +57,6 @@ import org.eclipse.jetty.webapp.WebAppContext;
/**
* An abstraction layer over Jetty WebAppContext.
*
* @author Jakub Pawlowicz
*/
public class WebApplicationProxyImpl implements WebApplicationProxy
{

View File

@ -0,0 +1,57 @@
//
// ========================================================================
// 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.ant.types;
public class Connector
{
private int port;
private int maxIdleTime;
public Connector()
{
}
public Connector(int port, int maxIdleTime)
{
this.port = port;
this.maxIdleTime = maxIdleTime;
}
public int getPort()
{
return port;
}
public void setPort(int port)
{
this.port = port;
}
public int getMaxIdleTime()
{
return maxIdleTime;
}
public void setMaxIdleTime(int maxIdleTime)
{
this.maxIdleTime = maxIdleTime;
}
}

View File

@ -25,7 +25,6 @@ import java.util.List;
/**
* Specifies a jetty configuration <connectors/> element for Ant build file.
*
* @author Jakub Pawlowicz
*/
public class Connectors
{
@ -81,33 +80,4 @@ public class Connectors
return defaultConnectors;
}
public class Connector
{
private int port;
private int maxIdleTime;
public Connector(int port, int maxIdleTime)
{
this.port = port;
this.maxIdleTime = maxIdleTime;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getMaxIdleTime() {
return maxIdleTime;
}
public void setMaxIdleTime(int maxIdleTime) {
this.maxIdleTime = maxIdleTime;
}
}
}

View File

@ -27,7 +27,6 @@ import org.eclipse.jetty.server.handler.ContextHandler;
/**
* Specifies <contextHandlers/> element in web app configuration.
*
* @author Jakub Pawlowicz
*/
public class ContextHandlers
{

View File

@ -31,7 +31,6 @@ import org.apache.tools.ant.DirectoryScanner;
* file. It is used to group application classes, libraries, and scannedTargets
* elements.
*
* @author Jakub Pawlowicz
*/
public class FileMatchingConfiguration
{

View File

@ -27,7 +27,6 @@ import org.eclipse.jetty.security.LoginService;
/**
* Specifies a jetty configuration <loginServices/> element for Ant build file.
*
* @author Jakub Pawlowicz
*/
public class LoginServices
{

View File

@ -28,7 +28,6 @@ import org.eclipse.jetty.ant.utils.TaskLog;
/**
* Ant <systemProperties/> tag definition.
*
* @author Jakub Pawlowicz
*/
public class SystemProperties
{

View File

@ -31,7 +31,6 @@ import org.apache.tools.ant.types.FileSet;
/**
* Ant's WebApp object definition.
*
* @author Jakub Pawlowicz
*/
public class WebApp
{

View File

@ -27,7 +27,6 @@ import org.apache.tools.ant.Task;
* Provides logging functionality for classes without access to the Ant project
* variable.
*
* @author Jakub Pawlowicz
*/
public class TaskLog
{

View File

@ -0,0 +1,297 @@
//
// ========================================================================
// 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.ant;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.eclipse.jetty.toolchain.test.IO;
public class AntBuild
{
private Thread _process;
private String _ant;
private int _port;
private String _host;
public AntBuild(String ant)
{
_ant = ant;
}
private class AntBuildProcess implements Runnable
{
List<String[]> connList;
@Override
public void run()
{
File buildFile = new File(_ant);
Project antProject = new Project();
try
{
antProject.setUserProperty("ant.file",buildFile.getAbsolutePath());
DefaultLogger logger = new DefaultLogger();
ConsoleParser parser = new ConsoleParser();
connList = parser.newPattern(".*([0-9]+\\.[0-9]*\\.[0-9]*\\.[0-9]*):([0-9]*)",1);
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
PipedOutputStream pose = new PipedOutputStream();
PipedInputStream pise = new PipedInputStream(pose);
startPump("STDOUT",parser,pis);
startPump("STDERR",parser,pise);
logger.setErrorPrintStream(new PrintStream(pos));
logger.setOutputPrintStream(new PrintStream(pose));
logger.setMessageOutputLevel(Project.MSG_VERBOSE);
antProject.addBuildListener(logger);
antProject.fireBuildStarted();
antProject.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
antProject.addReference("ant.projectHelper",helper);
helper.parse(antProject,buildFile);
antProject.executeTarget("jetty.run");
parser.waitForDone(10000,TimeUnit.MILLISECONDS);
}
catch (Exception e)
{
antProject.fireBuildFinished(e);
}
}
public void waitForStarted() throws Exception
{
while (connList == null || connList.isEmpty())
{
Thread.sleep(10);
}
}
public List<String[]> getConnectionList()
{
return connList;
}
}
public void start() throws Exception
{
System.out.println("Starting Ant Build ...");
AntBuildProcess abp = new AntBuildProcess();
_process = new Thread(abp);
_process.start();
abp.waitForStarted();
// once this has returned we should have the connection info we need
_host = abp.getConnectionList().get(0)[0];
_port = Integer.parseInt(abp.getConnectionList().get(0)[1]);
}
public int getJettyPort()
{
return _port;
}
public String getJettyHost()
{
return _host;
}
/**
* Stop the jetty server
*/
public void stop()
{
System.out.println("Stopping Ant Build ...");
_process.interrupt();
}
private static class ConsoleParser
{
private List<ConsolePattern> patterns = new ArrayList<ConsolePattern>();
private CountDownLatch latch;
private int count;
public List<String[]> newPattern(String exp, int cnt)
{
ConsolePattern pat = new ConsolePattern(exp,cnt);
patterns.add(pat);
count += cnt;
return pat.getMatches();
}
public void parse(String line)
{
for (ConsolePattern pat : patterns)
{
Matcher mat = pat.getMatcher(line);
if (mat.find())
{
int num = 0, count = mat.groupCount();
String[] match = new String[count];
while (num++ < count)
{
match[num - 1] = mat.group(num);
}
pat.getMatches().add(match);
if (pat.getCount() > 0)
{
getLatch().countDown();
}
}
}
}
public void waitForDone(long timeout, TimeUnit unit) throws InterruptedException
{
getLatch().await(timeout,unit);
}
private CountDownLatch getLatch()
{
synchronized (this)
{
if (latch == null)
{
latch = new CountDownLatch(count);
}
}
return latch;
}
}
private static class ConsolePattern
{
private Pattern pattern;
private List<String[]> matches;
private int count;
ConsolePattern(String exp, int cnt)
{
pattern = Pattern.compile(exp);
matches = new ArrayList<String[]>();
count = cnt;
}
public Matcher getMatcher(String line)
{
return pattern.matcher(line);
}
public List<String[]> getMatches()
{
return matches;
}
public int getCount()
{
return count;
}
}
private void startPump(String mode, ConsoleParser parser, InputStream inputStream)
{
ConsoleStreamer pump = new ConsoleStreamer(mode,inputStream);
pump.setParser(parser);
Thread thread = new Thread(pump,"ConsoleStreamer/" + mode);
thread.start();
}
/**
* Simple streamer for the console output from a Process
*/
private static class ConsoleStreamer implements Runnable
{
private String mode;
private BufferedReader reader;
private ConsoleParser parser;
public ConsoleStreamer(String mode, InputStream is)
{
this.mode = mode;
this.reader = new BufferedReader(new InputStreamReader(is));
}
public void setParser(ConsoleParser connector)
{
this.parser = connector;
}
public void run()
{
String line;
//System.out.printf("ConsoleStreamer/%s initiated%n",mode);
try
{
while ((line = reader.readLine()) != (null))
{
if (parser != null)
{
parser.parse(line);
}
System.out.println("[" + mode + "] " + line);
}
}
catch (IOException ignore)
{
/* ignore */
}
finally
{
IO.close(reader);
}
//System.out.printf("ConsoleStreamer/%s finished%n",mode);
}
}
}

View File

@ -0,0 +1,51 @@
//
// ========================================================================
// 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.ant;
import java.net.HttpURLConnection;
import java.net.URI;
import junit.framework.Assert;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.junit.Test;
public class JettyAntTaskTest
{
@Test
public void testJettyTask() throws Exception
{
AntBuild build = new AntBuild(MavenTestingUtils.getTestResourceFile("test.xml").getAbsolutePath());
build.start();
URI uri = new URI("http://" + build.getJettyHost() + ":" + build.getJettyPort());
HttpURLConnection connection = (HttpURLConnection)uri.toURL().openConnection();
connection.connect();
Assert.assertEquals(404,connection.getResponseCode());
build.stop();
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<project name="Jetty-Ant integration test" basedir=".">
<path id="jetty.plugin.classpath">
<fileset dir="../../../target/test-lib" includes="*.jar"/>
</path>
<taskdef classpathref="jetty.plugin.classpath" resource="tasks.properties" loaderref="jetty.loader" />
<typedef name="connector" classname="org.eclipse.jetty.ant.types.Connector"
classpathref="jetty.plugin.classpath" loaderref="jetty.loader" />
<target name="jetty.run">
<jetty>
<connectors>
<connector port="0"/>
</connectors>
</jetty>
</target>
</project>

View File

@ -377,7 +377,7 @@
</repository>
</repositories>
<modules>
<!--module>jetty-ant</module-->
<module>jetty-ant</module>
<module>jetty-util</module>
<module>jetty-jmx</module>
<module>jetty-io</module>