diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java index 399bff510fe..1d296fb82f9 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java @@ -266,7 +266,7 @@ public class ServerConnector extends AbstractNetworkConnector *
Use it with xinetd/inetd, to launch an instance of Jetty on demand. The port * used to access pages on the Jetty instance is the same as the port used to * launch Jetty.
- * + * @see ServerConnector#openAcceptChannel() * @param inheritChannel whether this connector uses a channel inherited from the JVM. */ public void setInheritChannel(boolean inheritChannel) @@ -274,18 +274,22 @@ public class ServerConnector extends AbstractNetworkConnector _inheritChannel = inheritChannel; } - - public ServerSocketChannel getAcceptChannel() - { - return _acceptChannel; - } - - public void setAcceptChannel(ServerSocketChannel acceptChannel) + /** + * Open the connector using the passed ServerSocketChannel. + * This open method can be called before starting the connector to pass it a ServerSocketChannel + * that will be used instead of one returned from {@link #openAcceptChannel()} + * @param acceptChannel the channel to use + * @throws IOException + */ + public void open(ServerSocketChannel acceptChannel) throws IOException { if (isStarted()) throw new IllegalStateException(getState()); updateBean(_acceptChannel,acceptChannel); _acceptChannel = acceptChannel; + _localPort = _acceptChannel.socket().getLocalPort(); + if (_localPort <= 0) + throw new IOException("Server channel not bound"); } @Override @@ -302,7 +306,6 @@ public class ServerConnector extends AbstractNetworkConnector } } - /** * Called by {@link #open()} to obtain the accepting channel. * @return ServerSocketChannel used to accept connections. diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ServerConnectorTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ServerConnectorTest.java index 832ca51f836..10370b20d08 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ServerConnectorTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ServerConnectorTest.java @@ -32,9 +32,11 @@ import java.io.InputStream; import java.io.PrintWriter; import java.lang.reflect.Field; import java.net.HttpURLConnection; +import java.net.InetSocketAddress; import java.net.Socket; import java.net.URI; import java.net.URISyntaxException; +import java.nio.channels.ServerSocketChannel; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.concurrent.atomic.AtomicLong; @@ -52,8 +54,11 @@ import org.eclipse.jetty.toolchain.test.OS; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.log.StacklessLogging; import org.hamcrest.Matchers; +import org.junit.Assert; import org.junit.Test; +import static org.hamcrest.Matchers.greaterThan; +import static org.junit.Assert.assertTrue; public class ServerConnectorTest { public static class ReuseInfoHandler extends AbstractHandler @@ -265,5 +270,39 @@ public class ServerConnectorTest { server.stop(); } + } + + @Test + public void testOpenWithServerSocketChannel() throws Exception + { + Server server = new Server(); + ServerConnector connector = new ServerConnector(server); + server.addConnector(connector); + + ServerSocketChannel channel = ServerSocketChannel.open(); + channel.bind(new InetSocketAddress(0)); + + assertTrue(channel.isOpen()); + int port = channel.socket().getLocalPort(); + assertThat(port,greaterThan(0)); + + connector.open(channel); + + assertThat(connector.getLocalPort(),is(port)); + + server.start(); + + assertThat(connector.getLocalPort(),is(port)); + assertThat(connector.getTransport(),is(channel)); + + server.stop(); + + assertThat(connector.getTransport(),Matchers.nullValue()); + + + + + + } }