Merge remote-tracking branch 'origin/jetty-9.3.x' into jetty-9.4.x

This commit is contained in:
Greg Wilkins 2017-07-03 17:56:54 +02:00
commit 578df575e1
2 changed files with 51 additions and 9 deletions

View File

@ -266,7 +266,7 @@ public class ServerConnector extends AbstractNetworkConnector
* <p>Use it with xinetd/inetd, to launch an instance of Jetty on demand. The port * <p>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 * used to access pages on the Jetty instance is the same as the port used to
* launch Jetty.</p> * launch Jetty.</p>
* * @see ServerConnector#openAcceptChannel()
* @param inheritChannel whether this connector uses a channel inherited from the JVM. * @param inheritChannel whether this connector uses a channel inherited from the JVM.
*/ */
public void setInheritChannel(boolean inheritChannel) public void setInheritChannel(boolean inheritChannel)
@ -274,18 +274,22 @@ public class ServerConnector extends AbstractNetworkConnector
_inheritChannel = inheritChannel; _inheritChannel = inheritChannel;
} }
/**
public ServerSocketChannel getAcceptChannel() * Open the connector using the passed ServerSocketChannel.
{ * This open method can be called before starting the connector to pass it a ServerSocketChannel
return _acceptChannel; * that will be used instead of one returned from {@link #openAcceptChannel()}
} * @param acceptChannel the channel to use
* @throws IOException
public void setAcceptChannel(ServerSocketChannel acceptChannel) */
public void open(ServerSocketChannel acceptChannel) throws IOException
{ {
if (isStarted()) if (isStarted())
throw new IllegalStateException(getState()); throw new IllegalStateException(getState());
updateBean(_acceptChannel,acceptChannel); updateBean(_acceptChannel,acceptChannel);
_acceptChannel = acceptChannel; _acceptChannel = acceptChannel;
_localPort = _acceptChannel.socket().getLocalPort();
if (_localPort <= 0)
throw new IOException("Server channel not bound");
} }
@Override @Override
@ -302,7 +306,6 @@ public class ServerConnector extends AbstractNetworkConnector
} }
} }
/** /**
* Called by {@link #open()} to obtain the accepting channel. * Called by {@link #open()} to obtain the accepting channel.
* @return ServerSocketChannel used to accept connections. * @return ServerSocketChannel used to accept connections.

View File

@ -32,9 +32,11 @@ import java.io.InputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.channels.ServerSocketChannel;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.atomic.AtomicLong; 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.IO;
import org.eclipse.jetty.util.log.StacklessLogging; import org.eclipse.jetty.util.log.StacklessLogging;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertTrue;
public class ServerConnectorTest public class ServerConnectorTest
{ {
public static class ReuseInfoHandler extends AbstractHandler public static class ReuseInfoHandler extends AbstractHandler
@ -265,5 +270,39 @@ public class ServerConnectorTest
{ {
server.stop(); 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());
} }
} }