| From | Sent On | Attachments |
|---|---|---|
| Olivier Lamy | Oct 9, 2011 7:08 am | |
| Mark Thomas | Oct 9, 2011 9:39 am | |
| Olivier Lamy | Oct 9, 2011 10:37 am | |
| Konstantin Kolinko | Oct 9, 2011 3:58 pm | |
| Olivier Lamy | Oct 14, 2011 1:27 am | |
| Henri Gomez | Oct 14, 2011 1:34 am | |
| Konstantin Kolinko | Oct 14, 2011 4:33 am | |
| Mark Thomas | Oct 14, 2011 4:35 am | |
| Olivier Lamy | Oct 14, 2011 5:40 am | |
| Konstantin Kolinko | Oct 14, 2011 5:57 am |
| Subject: | Embeded Tomcat using a Connector with a random port (port 0) | |
|---|---|---|
| From: | Olivier Lamy (ola...@apache.org) | |
| Date: | Oct 9, 2011 7:08:32 am | |
| List: | org.apache.tomcat.dev | |
Hello,
I'd like to be able to use a random port when using embedded Tomcat in unit tests to test servlets. Currently it's "locked" by a test in Connector#startInternal. Is it intentional ?
I'd like to be able to do simply something like :
Tomcat tomcat = new Tomcat(); tomcat.setBaseDir(System.getProperty("java.io.tmpdir")); tomcat.getConnector().setPort( 0 );
Context context = tomcat.addContext("", System.getProperty("java.io.tmpdir")); Tomcat.addServlet(context, "cxf", new MyServlet()); context.addServletMapping("/" + getRestServicesPath() + "/*", "cxf");
tomcat.start();
int port = tomcat.getConnector().getPort( );
here my tests with the port
tomcat.stop()
At least I'd like to have: tomcat.getConnector().getLocalPort( ) which returns the port on which the socket is listening (most of the time will be same as getPort() except when using port 0 )
Currently I do something which I find a little "hackhish" :-) :
Http11Protocol http11Protocol = ( (Http11Protocol) tomcat.getConnector().getProtocolHandler() );
// ReflectionUtils is a Spring framework class to help Field fieldEndpoint = ReflectionUtils.findField(Http11Protocol.class, "endpoint"); fieldEndpoint.setAccessible(true); JIoEndpoint jIoEndpoint = (JIoEndpoint) fieldEndpoint.get(http11Protocol);
Field serverSocketField = ReflectionUtils.findField(JIoEndpoint.class, "serverSocket"); serverSocketField.setAccessible(true); ServerSocket serverSocket = (ServerSocket) serverSocketField.get(jIoEndpoint);
this.port = serverSocket.getLocalPort();
Makes sense for to add getLocalPort on Connector class and remove the test if (getPort() < 1) ? If yes I can start on providing a patch for review.
Thanks
-- Olivier Lamy Talend : http://talend.com http://twitter.com/olamy | http://linkedin.com/in/olamy





