From a579823f2956d4c90dbda77e88b5dc819bb92598 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Tue, 10 Oct 2017 13:48:22 -0700 Subject: [PATCH 1/2] Issue #1885 Fixed rounding error of negative maxInactiveInterval Session.getMaxInactiveInterval should return a negative number if the the session is configured with a negative maxInactiveInterval. It was being rounded up to 0 by mistake. Signed-off-by: Dan Smith --- .../eclipse/jetty/server/session/Session.java | 3 +- ...AbstractModifyMaxInactiveIntervalTest.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/Session.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/Session.java index 84ff111678d..8ac48b02a7a 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/Session.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/Session.java @@ -575,7 +575,8 @@ public class Session implements SessionHandler.SessionIf { try (Lock lock = _lock.lock()) { - return (int)(_sessionData.getMaxInactiveMs()/1000); + long maxInactiveMs = _sessionData.getMaxInactiveMs(); + return (int)(maxInactiveMs < 0 ? -1 : maxInactiveMs/1000); } } diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractModifyMaxInactiveIntervalTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractModifyMaxInactiveIntervalTest.java index f05d1d9c7a5..15fd1e84ca5 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractModifyMaxInactiveIntervalTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractModifyMaxInactiveIntervalTest.java @@ -430,6 +430,53 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest server.stop(); } } + + @Test + public void testGetMaxInactiveIntervalWithNegativeMaxInactiveInterval() throws Exception + { + int maxInactive = -1; + DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory(); + cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT); + SessionDataStoreFactory storeFactory = createSessionDataStoreFactory(); + ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC); + + TestServer server = new TestServer(0, maxInactive, __scavenge, cacheFactory, storeFactory); + ServletContextHandler ctxA = server.addContext("/mod"); + ctxA.addServlet(TestModServlet.class, "/test"); + + server.start(); + int port=server.getPort(); + try + { + HttpClient client = new HttpClient(); + client.start(); + try + { + // Perform a request to create a session + + ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create"); + + assertEquals(HttpServletResponse.SC_OK,response.getStatus()); + String sessionCookie = response.getHeaders().get("Set-Cookie"); + assertTrue(sessionCookie != null); + + //Test that the maxInactiveInterval matches the expected value + Request request= client.newRequest("http://localhost:" + port + "/mod/test?action=test&val="+maxInactive); + response = request.send(); + assertEquals(HttpServletResponse.SC_OK,response.getStatus()); + + + } + finally + { + client.stop(); + } + } + finally + { + server.stop(); + } + } public static class TestModServlet extends HttpServlet From 5656594b4b275fee8bdb89659e25a8f84fd3d274 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Wed, 11 Oct 2017 15:40:02 +1100 Subject: [PATCH 2/2] Issue #1868 --- .../jetty-simple-webapp/pom.xml | 44 +---- .../src/base/etc/test-jetty.xml | 14 ++ .../src/base/modules/testmod.mod | 10 ++ .../TestHelloServlet.java | 46 +++++- .../src/it/jetty-run-distro-mojo-it/pom.xml | 129 +++++++-------- .../jetty-simple-webapp/pom.xml | 42 ++--- .../jetty-simple-webapp/src/config/jetty.xml | 11 +- .../TestHelloServlet.java | 47 +++++- .../src/it/jetty-run-forked-mojo-it/pom.xml | 143 ++++++++-------- .../jetty-simple-webapp/pom.xml | 15 +- .../jetty-simple-webapp/src/config/jetty.xml | 11 +- .../jetty_run_mojo_it/TestHelloServlet.java | 44 ++++- .../src/it/jetty-run-mojo-it/pom.xml | 153 +++++++++--------- .../jetty-simple-webapp/pom.xml | 18 ++- .../jetty-simple-webapp/src/config/jetty.xml | 40 +++++ .../TestHelloServlet.java | 46 +++++- .../it/jetty-run-war-exploded-mojo-it/pom.xml | 143 ++++++++-------- .../jetty-simple-webapp/pom.xml | 20 ++- .../jetty-simple-webapp/src/config/jetty.xml | 40 +++++ .../TestHelloServlet.java | 46 +++++- .../src/it/jetty-run-war-mojo-it/pom.xml | 143 ++++++++-------- .../jetty-simple-webapp/pom.xml | 16 +- .../jetty-simple-webapp/src/config/jetty.xml | 40 +++++ .../jetty_start_mojo_it/TestHelloServlet.java | 46 +++++- .../src/it/jetty-start-mojo-it/pom.xml | 143 ++++++++-------- .../maven/plugin/ServerConnectorListener.java | 116 +++++++++++++ 26 files changed, 999 insertions(+), 567 deletions(-) create mode 100644 jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/base/etc/test-jetty.xml create mode 100644 jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/base/modules/testmod.mod rename jetty-maven-plugin/src/it/{jetty-run-distro-mojo-it => jetty-run-mojo-it}/jetty-simple-webapp/src/config/jetty.xml (76%) create mode 100644 jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/src/config/jetty.xml create mode 100644 jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/src/config/jetty.xml create mode 100644 jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/src/config/jetty.xml create mode 100644 jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/ServerConnectorListener.java diff --git a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/pom.xml b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/pom.xml index f6a2759db6b..c43b9f0e7b1 100644 --- a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/pom.xml @@ -15,7 +15,7 @@ Jetty :: Simple :: Webapp - @jetty.runPort@ + ${project.build.directory}/jetty-run-distro-port.txt @jetty.jvmArgs@ @@ -68,7 +68,7 @@ 2.20 - @jetty.runPort@ + ${jetty.port.file} @@ -87,52 +87,20 @@ run-distro + ${basedir}/src/base jetty.server.dumpAfterStart=true - jetty.http.port=@jetty.runPort@ + jetty.port.file=${jetty.port.file} + jetty.http.port=0 false jsp jstl + testmod - - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.2 - - - copy-resources-jetty - generate-resources - - copy-resources - - - ${project.build.directory}/config - - - ${basedir}/src/config/ - true - - **/** - - - - - diff --git a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/base/etc/test-jetty.xml b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/base/etc/test-jetty.xml new file mode 100644 index 00000000000..fbdc1a01394 --- /dev/null +++ b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/base/etc/test-jetty.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/base/modules/testmod.mod b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/base/modules/testmod.mod new file mode 100644 index 00000000000..e1ee0758792 --- /dev/null +++ b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/base/modules/testmod.mod @@ -0,0 +1,10 @@ +[description] +Enables test setup + +[depend] +http + + +[xml] +etc/test-jetty.xml + diff --git a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_distro_mojo_it/TestHelloServlet.java b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_distro_mojo_it/TestHelloServlet.java index f14c0f2e6cf..c8f126177ce 100644 --- a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_distro_mojo_it/TestHelloServlet.java +++ b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_distro_mojo_it/TestHelloServlet.java @@ -18,6 +18,10 @@ package org.eclipse.jetty.its.jetty_run_distro_mojo_it; +import java.io.File; +import java.io.FileReader; +import java.io.LineNumberReader; + import org.eclipse.jetty.client.HttpClient; import org.junit.Assert; import org.junit.Test; @@ -32,8 +36,8 @@ public class TestHelloServlet public void hello_servlet() throws Exception { - int port = Integer.getInteger( "jetty.runPort" ); - System.out.println( "port used:" + port ); + int port = getPort(); + Assert.assertTrue(port > 0); HttpClient httpClient = new HttpClient(); try { @@ -41,14 +45,10 @@ public class TestHelloServlet String response = httpClient.GET( "http://localhost:" + port + "/hello?name=beer" ).getContentAsString(); - System.out.println( "httpResponse hello annotation servlet:" + response ); - Assert.assertEquals( "hello beer", response.trim() ); response = httpClient.GET( "http://localhost:" + port + "/ping?name=beer" ).getContentAsString(); - System.out.println( "httpResponse ping fragment servlet:" + response ); - Assert.assertEquals( "pong beer", response.trim() ); } finally @@ -56,4 +56,38 @@ public class TestHelloServlet httpClient.stop(); } } + + public int getPort() + throws Exception + { + int attempts = 20; + int port = -1; + String s = System.getProperty("jetty.port.file"); + Assert.assertNotNull(s); + File f = new File(s); + while (true) + { + if (f.exists()) + { + try (FileReader r = new FileReader(f); + LineNumberReader lnr = new LineNumberReader(r); + ) + { + s = lnr.readLine(); + Assert.assertNotNull(s); + port = Integer.parseInt(s.trim()); + } + break; + } + else + { + if (--attempts < 0) + break; + else + Thread.currentThread().sleep(100); + } + } + return port; + } + } diff --git a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/pom.xml b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/pom.xml index 84ebb04a284..02601b40cc2 100644 --- a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/pom.xml @@ -1,27 +1,27 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.eclipse.jetty.its.jetty-run-distro-mojo-it - jetty-simple-project - 0.0.1-SNAPSHOT - pom + org.eclipse.jetty.its.jetty-run-distro-mojo-it + jetty-simple-project + 0.0.1-SNAPSHOT + pom - Jetty :: Simple + Jetty :: Simple - - UTF-8 - UTF-8 - 1.8 - 3.0.0 - @project.version@ - + + UTF-8 + UTF-8 + 1.8 + 3.0.0 + @project.version@ + - - jetty-simple-base - jetty-simple-webapp - + + jetty-simple-base + jetty-simple-webapp + @@ -31,59 +31,54 @@ ${project.version} - - javax.servlet - javax.servlet-api - 3.1.0 - jar - provided - + + javax.servlet + javax.servlet-api + 3.1.0 + jar + provided + - - org.slf4j - slf4j-api - 1.7.21 - + + org.slf4j + slf4j-api + 1.7.21 + - - commons-io - commons-io - 2.5 - + + commons-io + commons-io + 2.5 + - - org.eclipse.jetty - jetty-servlet - ${jetty.version} - + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + - - + + - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.1 - - - org.eclipse.jetty - jetty-maven-plugin - ${jetty.version} - - - - + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + + diff --git a/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/pom.xml b/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/pom.xml index fa400e45aa1..aee069f4651 100644 --- a/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/pom.xml @@ -15,8 +15,8 @@ Jetty :: Simple :: Webapp - @jetty.runPort@ @jetty.jvmArgs@ + ${project.build.directory}/jetty-run-forked-port.txt @@ -39,6 +39,13 @@ test + + org.eclipse.jetty.toolchain + jetty-test-helper + 4.2 + test + + junit junit @@ -68,7 +75,7 @@ 2.20 - @jetty.runPort@ + ${jetty.port.file} @@ -89,8 +96,11 @@ true false - ${project.build.directory}/config/jetty.xml + ${basedir}/src/config/jetty.xml ${jetty.jvmArgs} + + jetty.port.file=${jetty.port.file} + - - org.apache.maven.plugins - maven-resources-plugin - 3.0.2 - - - copy-resources-jetty - generate-resources - - copy-resources - - - ${project.build.directory}/config - - - ${basedir}/src/config/ - true - - **/** - - - - - - - diff --git a/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/src/config/jetty.xml b/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/src/config/jetty.xml index a057f0664eb..0b843c05a99 100644 --- a/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/src/config/jetty.xml +++ b/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/src/config/jetty.xml @@ -24,10 +24,17 @@ + + + + + + + - + 0 30000 - \ No newline at end of file + diff --git a/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_forked_mojo_it/TestHelloServlet.java b/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_forked_mojo_it/TestHelloServlet.java index a2370904be6..86bdd162347 100644 --- a/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_forked_mojo_it/TestHelloServlet.java +++ b/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_forked_mojo_it/TestHelloServlet.java @@ -18,6 +18,10 @@ package org.eclipse.jetty.its.jetty_run_forked_mojo_it; +import java.io.File; +import java.io.FileReader; +import java.io.LineNumberReader; + import org.eclipse.jetty.client.HttpClient; import org.junit.Assert; import org.junit.Test; @@ -31,8 +35,9 @@ public class TestHelloServlet public void hello_servlet() throws Exception { - int port = Integer.getInteger( "jetty.runPort" ); - System.out.println( "port used:" + port ); + + int port = getPort(); + Assert.assertTrue(port > 0); HttpClient httpClient = new HttpClient(); try { @@ -40,14 +45,10 @@ public class TestHelloServlet String response = httpClient.GET( "http://localhost:" + port + "/hello?name=beer" ).getContentAsString(); - System.out.println( "httpResponse hello annotation servlet:" + response ); - Assert.assertEquals( "hello beer", response.trim() ); response = httpClient.GET( "http://localhost:" + port + "/ping?name=beer" ).getContentAsString(); - System.out.println( "httpResponse ping fragment servlet:" + response ); - Assert.assertEquals( "pong beer", response.trim() ); } finally @@ -55,4 +56,38 @@ public class TestHelloServlet httpClient.stop(); } } + + + public int getPort() + throws Exception + { + int attempts = 20; + int port = -1; + String s = System.getProperty("jetty.port.file"); + Assert.assertNotNull(s); + File f = new File(s); + while (true) + { + if (f.exists()) + { + try (FileReader r = new FileReader(f); + LineNumberReader lnr = new LineNumberReader(r); + ) + { + s = lnr.readLine(); + Assert.assertNotNull(s); + port = Integer.parseInt(s.trim()); + } + break; + } + else + { + if (--attempts < 0) + break; + else + Thread.currentThread().sleep(100); + } + } + return port; + } } diff --git a/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/pom.xml b/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/pom.xml index fae3f39a0c6..f67096ab1a8 100644 --- a/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-forked-mojo-it/pom.xml @@ -1,90 +1,85 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.eclipse.jetty.its.jetty-run-forked-mojo-it - jetty-simple-project - 0.0.1-SNAPSHOT - pom + org.eclipse.jetty.its.jetty-run-forked-mojo-it + jetty-simple-project + 0.0.1-SNAPSHOT + pom - Jetty :: Simple + Jetty :: Simple - - UTF-8 - UTF-8 - 1.8 - 3.0.0 - @project.version@ - + + UTF-8 + UTF-8 + 1.8 + 3.0.0 + @project.version@ + - - jetty-simple-base - jetty-simple-webapp - + + jetty-simple-base + jetty-simple-webapp + - - + + - - org.eclipse.jetty.its.jetty-run-forked-mojo-it - jetty-simple-base - ${project.version} - + + org.eclipse.jetty.its.jetty-run-forked-mojo-it + jetty-simple-base + ${project.version} + - - javax.servlet - javax.servlet-api - 3.1.0 - jar - provided - + + javax.servlet + javax.servlet-api + 3.1.0 + jar + provided + - - org.slf4j - slf4j-api - 1.7.21 - + + org.slf4j + slf4j-api + 1.7.21 + - - commons-io - commons-io - 2.5 - + + commons-io + commons-io + 2.5 + - - org.eclipse.jetty - jetty-servlet - ${jetty.version} - + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + - - + + - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.1 - - - org.eclipse.jetty - jetty-maven-plugin - ${jetty.version} - - - - + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + + diff --git a/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/pom.xml b/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/pom.xml index e713998af7c..2d8381239d5 100644 --- a/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/pom.xml @@ -14,6 +14,9 @@ Jetty :: Simple :: Webapp + + ${project.build.directory}/jetty-run-mojo.txt + @@ -68,7 +71,7 @@ 2.20 - @jetty.runPort@ + ${jetty.port.file} @@ -83,10 +86,14 @@ start + + + jetty.port.file + ${jetty.port.file} + + true - - @jetty.runPort@ - + ${basedir}/src/config/jetty.xml diff --git a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/config/jetty.xml b/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/src/config/jetty.xml similarity index 76% rename from jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/config/jetty.xml rename to jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/src/config/jetty.xml index a057f0664eb..0b843c05a99 100644 --- a/jetty-maven-plugin/src/it/jetty-run-distro-mojo-it/jetty-simple-webapp/src/config/jetty.xml +++ b/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/src/config/jetty.xml @@ -24,10 +24,17 @@ + + + + + + + - + 0 30000 - \ No newline at end of file + diff --git a/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_mojo_it/TestHelloServlet.java b/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_mojo_it/TestHelloServlet.java index 6fc5cb06e25..4f7bfcb7e71 100644 --- a/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_mojo_it/TestHelloServlet.java +++ b/jetty-maven-plugin/src/it/jetty-run-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_mojo_it/TestHelloServlet.java @@ -19,6 +19,10 @@ package org.eclipse.jetty.its.jetty_run_mojo_it; +import java.io.File; +import java.io.FileReader; +import java.io.LineNumberReader; + import org.eclipse.jetty.client.HttpClient; import org.junit.Assert; import org.junit.Test; @@ -32,8 +36,7 @@ public class TestHelloServlet public void hello_servlet() throws Exception { - int port = Integer.getInteger( "jetty.runPort" ); - System.out.println( "port used:" + port ); + int port = getPort(); HttpClient httpClient = new HttpClient(); try { @@ -41,14 +44,10 @@ public class TestHelloServlet String response = httpClient.GET( "http://localhost:" + port + "/hello?name=beer" ).getContentAsString(); - System.out.println( "httpResponse hello annotation servlet:" + response ); - Assert.assertEquals( "hello beer", response.trim() ); response = httpClient.GET( "http://localhost:" + port + "/ping?name=beer" ).getContentAsString(); - System.out.println( "httpResponse ping fragment servlet:" + response ); - Assert.assertEquals( "pong beer", response.trim() ); } finally @@ -56,4 +55,37 @@ public class TestHelloServlet httpClient.stop(); } } + + public int getPort() + throws Exception + { + int attempts = 20; + int port = -1; + String s = System.getProperty("jetty.port.file"); + Assert.assertNotNull(s); + File f = new File(s); + while (true) + { + if (f.exists()) + { + try (FileReader r = new FileReader(f); + LineNumberReader lnr = new LineNumberReader(r); + ) + { + s = lnr.readLine(); + Assert.assertNotNull(s); + port = Integer.parseInt(s.trim()); + } + break; + } + else + { + if (--attempts < 0) + break; + else + Thread.currentThread().sleep(100); + } + } + return port; + } } diff --git a/jetty-maven-plugin/src/it/jetty-run-mojo-it/pom.xml b/jetty-maven-plugin/src/it/jetty-run-mojo-it/pom.xml index 313904bfff4..304b54dac53 100644 --- a/jetty-maven-plugin/src/it/jetty-run-mojo-it/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-mojo-it/pom.xml @@ -1,96 +1,91 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.eclipse.jetty.its.jetty-run-mojo-it - jetty-simple-project - 0.0.1-SNAPSHOT - pom + org.eclipse.jetty.its.jetty-run-mojo-it + jetty-simple-project + 0.0.1-SNAPSHOT + pom - Jetty :: Simple + Jetty :: Simple - - UTF-8 - UTF-8 - 1.8 - 3.0.0 - @project.version@ - + + UTF-8 + UTF-8 + 1.8 + 3.0.0 + @project.version@ + - - jetty-simple-base - jetty-simple-webapp - + + jetty-simple-base + jetty-simple-webapp + - - + + - - org.eclipse.jetty.its.jetty-run-mojo-it - jetty-simple-base - ${project.version} - + + org.eclipse.jetty.its.jetty-run-mojo-it + jetty-simple-base + ${project.version} + - - javax.servlet - javax.servlet-api - 3.1.0 - jar - provided - + + javax.servlet + javax.servlet-api + 3.1.0 + jar + provided + - - org.slf4j - slf4j-api - 1.7.21 - + + org.slf4j + slf4j-api + 1.7.21 + - - org.slf4j - slf4j-simple - 1.7.21 - + + org.slf4j + slf4j-simple + 1.7.21 + - - commons-io - commons-io - 2.5 - + + commons-io + commons-io + 2.5 + - - org.eclipse.jetty - jetty-servlet - ${jetty.version} - + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + - - + + - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.1 - - - org.eclipse.jetty - jetty-maven-plugin - ${jetty.version} - - - - + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + + diff --git a/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/pom.xml b/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/pom.xml index 6cab86275c0..2cb58793d49 100644 --- a/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/pom.xml @@ -14,6 +14,10 @@ Jetty :: Simple :: Webapp + + ${project.build.directory}/jetty-run-war-exploded-port.txt + + @@ -64,7 +68,7 @@ true - @jetty.runPort@ + ${jetty.port.file} @@ -74,7 +78,7 @@ 2.20 - @jetty.runPort@ + ${jetty.port.file} **/*TestHelloServlet* @@ -107,9 +111,13 @@ true - - @jetty.runPort@ - + + + jetty.port.file + ${jetty.port.file} + + + ${basedir}/src/config/jetty.xml diff --git a/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/src/config/jetty.xml b/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/src/config/jetty.xml new file mode 100644 index 00000000000..0b843c05a99 --- /dev/null +++ b/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/src/config/jetty.xml @@ -0,0 +1,40 @@ + + + + + + https + + 32768 + 8192 + 8192 + 512 + + + + + + + + + + + + + + + + + + + + + + + + 0 + 30000 + + + + diff --git a/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_war_exploded_mojo_it/TestHelloServlet.java b/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_war_exploded_mojo_it/TestHelloServlet.java index be94a057063..9af276a3c13 100644 --- a/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_war_exploded_mojo_it/TestHelloServlet.java +++ b/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_war_exploded_mojo_it/TestHelloServlet.java @@ -19,6 +19,10 @@ package org.eclipse.jetty.its.jetty_run_war_exploded_mojo_it; +import java.io.File; +import java.io.FileReader; +import java.io.LineNumberReader; + import org.eclipse.jetty.client.HttpClient; import org.junit.Assert; import org.junit.Test; @@ -32,8 +36,8 @@ public class TestHelloServlet public void hello_servlet() throws Exception { - int port = Integer.getInteger( "jetty.runPort" ); - System.out.println( "port used:" + port ); + int port = getPort(); + Assert.assertTrue(port > 0); HttpClient httpClient = new HttpClient(); try { @@ -41,14 +45,10 @@ public class TestHelloServlet String response = httpClient.GET( "http://localhost:" + port + "/hello?name=beer" ).getContentAsString(); - System.out.println( "httpResponse:" + response ); - Assert.assertEquals( "hello beer", response.trim() ); response = httpClient.GET( "http://localhost:" + port + "/ping?name=beer" ).getContentAsString(); - System.out.println( "httpResponse:" + response ); - Assert.assertEquals( "pong beer", response.trim() ); } finally @@ -56,4 +56,38 @@ public class TestHelloServlet httpClient.stop(); } } + + public int getPort() + throws Exception + { + int attempts = 20; + int port = -1; + String s = System.getProperty("jetty.port.file"); + Assert.assertNotNull(s); + File f = new File(s); + while (true) + { + if (f.exists()) + { + try (FileReader r = new FileReader(f); + LineNumberReader lnr = new LineNumberReader(r); + ) + { + s = lnr.readLine(); + Assert.assertNotNull(s); + port = Integer.parseInt(s.trim()); + } + break; + } + else + { + if (--attempts < 0) + break; + else + Thread.currentThread().sleep(100); + } + } + return port; + } + } diff --git a/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/pom.xml b/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/pom.xml index a6ef2713d27..ef8e9da91be 100644 --- a/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-war-exploded-mojo-it/pom.xml @@ -1,90 +1,85 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.eclipse.jetty.its.jetty-run-war-exploded-mojo-it - jetty-simple-project - 0.0.1-SNAPSHOT - pom + org.eclipse.jetty.its.jetty-run-war-exploded-mojo-it + jetty-simple-project + 0.0.1-SNAPSHOT + pom - Jetty :: Simple + Jetty :: Simple - - UTF-8 - UTF-8 - 1.8 - 3.0.0 - @project.version@ - + + UTF-8 + UTF-8 + 1.8 + 3.0.0 + @project.version@ + - - jetty-simple-base - jetty-simple-webapp - + + jetty-simple-base + jetty-simple-webapp + - - + + - - org.eclipse.jetty.its.jetty-run-war-exploded-mojo-it - jetty-simple-base - ${project.version} - + + org.eclipse.jetty.its.jetty-run-war-exploded-mojo-it + jetty-simple-base + ${project.version} + - - javax.servlet - javax.servlet-api - 3.1.0 - jar - provided - + + javax.servlet + javax.servlet-api + 3.1.0 + jar + provided + - - org.slf4j - slf4j-api - 1.7.21 - + + org.slf4j + slf4j-api + 1.7.21 + - - commons-io - commons-io - 2.5 - + + commons-io + commons-io + 2.5 + - - org.eclipse.jetty - jetty-servlet - ${jetty.version} - + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + - - + + - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.1 - - - org.eclipse.jetty - jetty-maven-plugin - ${jetty.version} - - - - + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + + diff --git a/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/pom.xml b/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/pom.xml index f9ba41b965d..ea1c1b8f7e8 100644 --- a/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/pom.xml @@ -14,6 +14,12 @@ Jetty :: Simple :: Webapp + + + ${project.build.directory}/jetty-run-war-port.txt + + + @@ -64,7 +70,7 @@ true - @jetty.runPort@ + ${jetty.port.file} @@ -74,7 +80,7 @@ 2.20 - @jetty.runPort@ + ${jetty.port.file} **/*TestHelloServlet* @@ -107,9 +113,13 @@ true - - @jetty.runPort@ - + + + jetty.port.file + ${jetty.port.file} + + + ${basedir}/src/config/jetty.xml diff --git a/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/src/config/jetty.xml b/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/src/config/jetty.xml new file mode 100644 index 00000000000..0b843c05a99 --- /dev/null +++ b/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/src/config/jetty.xml @@ -0,0 +1,40 @@ + + + + + + https + + 32768 + 8192 + 8192 + 512 + + + + + + + + + + + + + + + + + + + + + + + + 0 + 30000 + + + + diff --git a/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_war_mojo_it/TestHelloServlet.java b/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_war_mojo_it/TestHelloServlet.java index c3ea0cf6e89..9ccb140688f 100644 --- a/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_war_mojo_it/TestHelloServlet.java +++ b/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_run_war_mojo_it/TestHelloServlet.java @@ -19,6 +19,10 @@ package org.eclipse.jetty.its.jetty_run_war_mojo_it; +import java.io.File; +import java.io.FileReader; +import java.io.LineNumberReader; + import org.eclipse.jetty.client.HttpClient; import org.junit.Assert; import org.junit.Test; @@ -32,8 +36,8 @@ public class TestHelloServlet public void hello_servlet() throws Exception { - int port = Integer.getInteger( "jetty.runPort" ); - System.out.println( "port used:" + port ); + int port = getPort(); + Assert.assertTrue(port > 0); HttpClient httpClient = new HttpClient(); try { @@ -41,14 +45,10 @@ public class TestHelloServlet String response = httpClient.GET( "http://localhost:" + port + "/hello?name=beer" ).getContentAsString(); - System.out.println( "httpResponse:" + response ); - Assert.assertEquals( "hello beer", response.trim() ); response = httpClient.GET( "http://localhost:" + port + "/ping?name=beer" ).getContentAsString(); - System.out.println( "httpResponse:" + response ); - Assert.assertEquals( "pong beer", response.trim() ); } finally @@ -56,4 +56,38 @@ public class TestHelloServlet httpClient.stop(); } } + + public int getPort() + throws Exception + { + int attempts = 20; + int port = -1; + String s = System.getProperty("jetty.port.file"); + Assert.assertNotNull(s); + File f = new File(s); + while (true) + { + if (f.exists()) + { + try (FileReader r = new FileReader(f); + LineNumberReader lnr = new LineNumberReader(r); + ) + { + s = lnr.readLine(); + Assert.assertNotNull(s); + port = Integer.parseInt(s.trim()); + } + break; + } + else + { + if (--attempts < 0) + break; + else + Thread.currentThread().sleep(100); + } + } + return port; + } + } diff --git a/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/pom.xml b/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/pom.xml index a48c4d51db1..7907e80cb52 100644 --- a/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-run-war-mojo-it/pom.xml @@ -1,90 +1,85 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.eclipse.jetty.its.jetty-run-war-mojo-it - jetty-simple-project - 0.0.1-SNAPSHOT - pom + org.eclipse.jetty.its.jetty-run-war-mojo-it + jetty-simple-project + 0.0.1-SNAPSHOT + pom - Jetty :: Simple + Jetty :: Simple - - UTF-8 - UTF-8 - 1.8 - 3.0.0 - @project.version@ - + + UTF-8 + UTF-8 + 1.8 + 3.0.0 + @project.version@ + - - jetty-simple-base - jetty-simple-webapp - + + jetty-simple-base + jetty-simple-webapp + - - + + - - org.eclipse.jetty.its.jetty-run-war-mojo-it - jetty-simple-base - ${project.version} - + + org.eclipse.jetty.its.jetty-run-war-mojo-it + jetty-simple-base + ${project.version} + - - javax.servlet - javax.servlet-api - 3.1.0 - jar - provided - + + javax.servlet + javax.servlet-api + 3.1.0 + jar + provided + - - org.slf4j - slf4j-api - 1.7.21 - + + org.slf4j + slf4j-api + 1.7.21 + - - commons-io - commons-io - 2.5 - + + commons-io + commons-io + 2.5 + - - org.eclipse.jetty - jetty-servlet - ${jetty.version} - + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + - - + + - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.1 - - - org.eclipse.jetty - jetty-maven-plugin - ${jetty.version} - - - - + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + + diff --git a/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/pom.xml b/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/pom.xml index fdb6bf16cff..14bb7e933ef 100644 --- a/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/pom.xml @@ -14,6 +14,10 @@ Jetty :: Simple :: Webapp + + ${project.build.directory}/jetty-start-port.txt + + @@ -63,7 +67,7 @@ 2.20 - @jetty.runPort@ + ${jetty.port.file} @@ -78,9 +82,13 @@ start - - @jetty.runPort@ - + + + jetty.port.file + ${jetty.port.file} + + + ${basedir}/src/config/jetty.xml diff --git a/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/src/config/jetty.xml b/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/src/config/jetty.xml new file mode 100644 index 00000000000..0b843c05a99 --- /dev/null +++ b/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/src/config/jetty.xml @@ -0,0 +1,40 @@ + + + + + + https + + 32768 + 8192 + 8192 + 512 + + + + + + + + + + + + + + + + + + + + + + + + 0 + 30000 + + + + diff --git a/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_start_mojo_it/TestHelloServlet.java b/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_start_mojo_it/TestHelloServlet.java index 677be7512ab..d53213991ab 100644 --- a/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_start_mojo_it/TestHelloServlet.java +++ b/jetty-maven-plugin/src/it/jetty-start-mojo-it/jetty-simple-webapp/src/test/java/org/eclipse/jetty/its/jetty_start_mojo_it/TestHelloServlet.java @@ -19,6 +19,11 @@ package org.eclipse.jetty.its.jetty_start_mojo_it; + +import java.io.File; +import java.io.FileReader; +import java.io.LineNumberReader; + import org.eclipse.jetty.client.HttpClient; import org.junit.Assert; import org.junit.Test; @@ -32,8 +37,8 @@ public class TestHelloServlet public void hello_servlet() throws Exception { - int port = Integer.getInteger( "jetty.runPort" ); - System.out.println( "port used:" + port ); + int port = getPort(); + Assert.assertTrue(port > 0); HttpClient httpClient = new HttpClient(); try { @@ -41,14 +46,10 @@ public class TestHelloServlet String response = httpClient.GET( "http://localhost:" + port + "/hello?name=beer" ).getContentAsString(); - System.out.println( "httpResponse:" + response ); - Assert.assertEquals( "hello beer", response.trim() ); response = httpClient.GET( "http://localhost:" + port + "/ping?name=beer" ).getContentAsString(); - System.out.println( "httpResponse:" + response ); - Assert.assertEquals( "pong beer", response.trim() ); } finally @@ -56,4 +57,37 @@ public class TestHelloServlet httpClient.stop(); } } + public int getPort() + throws Exception + { + int attempts = 20; + int port = -1; + String s = System.getProperty("jetty.port.file"); + Assert.assertNotNull(s); + File f = new File(s); + while (true) + { + if (f.exists()) + { + try (FileReader r = new FileReader(f); + LineNumberReader lnr = new LineNumberReader(r); + ) + { + s = lnr.readLine(); + Assert.assertNotNull(s); + port = Integer.parseInt(s.trim()); + } + break; + } + else + { + if (--attempts < 0) + break; + else + Thread.currentThread().sleep(100); + } + } + return port; + } + } diff --git a/jetty-maven-plugin/src/it/jetty-start-mojo-it/pom.xml b/jetty-maven-plugin/src/it/jetty-start-mojo-it/pom.xml index 58add42c68c..0371ce2e327 100644 --- a/jetty-maven-plugin/src/it/jetty-start-mojo-it/pom.xml +++ b/jetty-maven-plugin/src/it/jetty-start-mojo-it/pom.xml @@ -1,90 +1,85 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.eclipse.jetty.its.jetty-start-mojo-it - jetty-simple-project - 0.0.1-SNAPSHOT - pom + org.eclipse.jetty.its.jetty-start-mojo-it + jetty-simple-project + 0.0.1-SNAPSHOT + pom - Jetty :: Simple + Jetty :: Simple - - UTF-8 - UTF-8 - 1.8 - 3.0.0 - @project.version@ - + + UTF-8 + UTF-8 + 1.8 + 3.0.0 + @project.version@ + - - jetty-simple-base - jetty-simple-webapp - + + jetty-simple-base + jetty-simple-webapp + - - + + - - org.eclipse.jetty.its.jetty-start-mojo-it - jetty-simple-base - ${project.version} - + + org.eclipse.jetty.its.jetty-start-mojo-it + jetty-simple-base + ${project.version} + - - javax.servlet - javax.servlet-api - 3.1.0 - jar - provided - + + javax.servlet + javax.servlet-api + 3.1.0 + jar + provided + - - org.slf4j - slf4j-api - 1.7.21 - + + org.slf4j + slf4j-api + 1.7.21 + - - commons-io - commons-io - 2.5 - + + commons-io + commons-io + 2.5 + - - org.eclipse.jetty - jetty-servlet - ${jetty.version} - + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + - - + + - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.1 - - - org.eclipse.jetty - jetty-maven-plugin - ${jetty.version} - - - - + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + + diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/ServerConnectorListener.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/ServerConnectorListener.java new file mode 100644 index 00000000000..f6f7e536940 --- /dev/null +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/ServerConnectorListener.java @@ -0,0 +1,116 @@ +// +// ======================================================================== +// Copyright (c) 1995-2017 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 java.io.File; +import java.io.FileWriter; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener; +import org.eclipse.jetty.util.component.LifeCycle; + +/** + * ServerConnectorListener + * + * This is for test support, where we need jetty to run on a random port, and we need + * a client to be able to find out which port was picked. + */ +public class ServerConnectorListener extends AbstractLifeCycleListener +{ + + private String _fileName; + private String _sysPropertyName; + + + + /** + * @see org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener#lifeCycleStarted(org.eclipse.jetty.util.component.LifeCycle) + */ + @Override + public void lifeCycleStarted(LifeCycle event) + { + if (getFileName() != null) + { + File file = new File(getFileName()); + try + { + Files.deleteIfExists(file.toPath()); + } + catch (Exception e) + { + throw new RuntimeException (e); + } + + try (FileWriter writer = new FileWriter(file)) + { + writer.write(String.valueOf(((ServerConnector)event).getLocalPort())); + writer.close(); + } + catch (Exception e) + { + throw new RuntimeException (e); + } + } + + if (getSysPropertyName() != null) + { + System.setProperty(_sysPropertyName,String.valueOf(((ServerConnector)event).getLocalPort())); + } + super.lifeCycleStarted(event); + } + + /** + * @return the file name + */ + public String getFileName() + { + return _fileName; + } + + /** + * @param filePath the filePath to set + */ + public void setFileName(String name) + { + +System.err.println("FILE NAME="+name); + _fileName = name; + } + + /** + * @return the sysPropertyName + */ + public String getSysPropertyName() + { + return _sysPropertyName; + } + + /** + * @param sysPropertyName the sysPropertyName to set + */ + public void setSysPropertyName(String sysPropertyName) + { + _sysPropertyName = sysPropertyName; + } + + +}