Expose local port in EmbeddedServletContainer

pull/138/head
Dave Syer 11 years ago
parent d1dcb015b6
commit 67cc427d2b

@ -44,6 +44,10 @@ public interface EmbeddedServletContainer {
public void stop() throws EmbeddedServletContainerException {
// Do nothing
}
public int getPort() {
return 0;
}
};
/**
@ -60,4 +64,9 @@ public interface EmbeddedServletContainer {
*/
void stop() throws EmbeddedServletContainerException;
/**
* @return the port this server is listening on (or zero if none)
*/
int getPort();
}

@ -109,6 +109,16 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer {
}
}
@Override
public int getPort() {
Connector[] connectors = this.server.getConnectors();
for (Connector connector : connectors) {
// Probably only one...
return connector.getLocalPort();
}
return 0;
}
/**
* Returns access to the underlying Jetty Server.
*/

@ -126,9 +126,18 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
this.tomcat.destroy();
}
catch (Exception ex) {
throw new EmbeddedServletContainerException(
"Unable to stop embedded Tomcat", ex);
throw new EmbeddedServletContainerException("Unable to stop embedded Tomcat",
ex);
}
}
@Override
public int getPort() {
Connector connector = this.tomcat.getConnector();
if (connector != null) {
return connector.getLocalPort();
}
return 0;
}
/**

@ -47,6 +47,7 @@ import org.springframework.util.StreamUtils;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.inOrder;
@ -172,6 +173,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.start();
assertThat(getResponse("http://localhost:8081/hello"), equalTo("Hello World"));
assertEquals(8081, this.container.getPort());
}
@Test

Loading…
Cancel
Save