Issue #1655 ServerConnector configured with channel

converted setter to open method
added test harness
This commit is contained in:
Greg Wilkins 2017-07-03 17:48:07 +02:00 committed by Joakim Erdfelt
parent 7dc2559c8b
commit 2202d333fe
2 changed files with 60 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
* used to access pages on the Jetty instance is the same as the port used to
* launch Jetty.</p>
*
* @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.

View File

@ -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,20 @@ 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.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class ServerConnectorTest
{
public static class ReuseInfoHandler extends AbstractHandler
@ -265,5 +279,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());
}
}