diff options
Diffstat (limited to '')
8 files changed, 132 insertions, 259 deletions
diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/DefaultHttpPortAllocatorExtensionPoint.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/DefaultHttpPortAllocatorExtensionPoint.java deleted file mode 100644 index 5c5847f77b..0000000000 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/DefaultHttpPortAllocatorExtensionPoint.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.host.http.extensibility; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.extensibility.ServiceDeclaration; -import org.apache.tuscany.sca.extensibility.ServiceHelper; - -public class DefaultHttpPortAllocatorExtensionPoint implements HttpPortAllocatorExtensionPoint { - private final static Logger logger = Logger.getLogger(DefaultHttpPortAllocatorExtensionPoint.class.getName()); - - private ExtensionPointRegistry registry; - private List<HttpPortAllocator> portAllocators = new ArrayList<HttpPortAllocator>(); - private boolean loaded; - - public DefaultHttpPortAllocatorExtensionPoint(ExtensionPointRegistry registry) { - this.registry = registry; - } - public void addPortAllocators(HttpPortAllocator httpPortAllocator) { - this.portAllocators.add(httpPortAllocator); - } - - public void removePortAllocators(HttpPortAllocator httpPortAllocator) { - this.portAllocators.remove(httpPortAllocator); - } - - public List<HttpPortAllocator> getPortAllocators() { - loadServletHosts(); - return this.portAllocators; - } - - private synchronized void loadServletHosts() { - if (loaded) - return; - - // Get the activator service declarations - Collection<ServiceDeclaration> activatorDeclarations; - try { - // Load the port allocators by ranking - activatorDeclarations = registry.getServiceDiscovery().getServiceDeclarations(HttpPortAllocator.class.getName(), true); - } catch (IOException e) { - throw new IllegalStateException(e); - } - - // Load and instantiate http port allocators - for (ServiceDeclaration allocatorDeclaration : activatorDeclarations) { - if (logger.isLoggable(Level.FINE)) { - logger.fine("Loading " + allocatorDeclaration.getClassName()); - } - HttpPortAllocator allocator = null; - try { - Class<HttpPortAllocator> allocatorClass = (Class<HttpPortAllocator>)allocatorDeclaration.loadClass(); - try { - allocator = ServiceHelper.newInstance(allocatorClass, ExtensionPointRegistry.class, registry); - } catch (NoSuchMethodException e) { - try { - allocator = - ServiceHelper.newInstance(allocatorClass, - new Class<?>[] {ExtensionPointRegistry.class, Map.class}, - registry, - allocatorDeclaration.getAttributes()); - - } catch (NoSuchMethodException e1) { - allocator = ServiceHelper.newInstance(allocatorClass); - - } - } - } catch (Throwable e) { - String optional = allocatorDeclaration.getAttributes().get("optional"); - if ("true".equalsIgnoreCase(optional)) { - // If the optional flag is true, just log the error - logger.log(Level.SEVERE, e.getMessage(), e); - continue; - } else { - throw new IllegalArgumentException(e); - } - } - addPortAllocators(allocator); - } - - loaded = true; - } -} diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/ExtensibleHttpPortAllocator.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/ExtensibleHttpPortAllocator.java deleted file mode 100644 index c1f3083cf8..0000000000 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/ExtensibleHttpPortAllocator.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.host.http.extensibility; - -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.UtilityExtensionPoint; -import org.apache.tuscany.sca.host.http.HttpScheme; - -public class ExtensibleHttpPortAllocator implements HttpPortAllocator { - HttpPortAllocatorExtensionPoint httpPortAllocators; - - public ExtensibleHttpPortAllocator(ExtensionPointRegistry registry) { - this.httpPortAllocators = registry.getExtensionPoint(HttpPortAllocatorExtensionPoint.class); - } - - public static ExtensibleHttpPortAllocator getInstance(ExtensionPointRegistry registry) { - UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); - return utilityExtensionPoint.getUtility(ExtensibleHttpPortAllocator.class); - } - - public int getDefaultPort(HttpScheme scheme) { - if(this.httpPortAllocators.getPortAllocators().isEmpty()) { - throw new RuntimeException("No port allocators registered"); - } - - return this.httpPortAllocators.getPortAllocators().get(0).getDefaultPort(scheme); - } -} diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/HttpPortAllocatorExtensionPoint.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/HttpPortAllocatorExtensionPoint.java deleted file mode 100644 index b7e748f217..0000000000 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/HttpPortAllocatorExtensionPoint.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.host.http.extensibility; - -import java.util.List; - -/** - * Extension Point to allow registration of different port allocators - * @version $Rev$ $Date$ - */ -public interface HttpPortAllocatorExtensionPoint { - - /** - * Register a new http port allocator - * @param httpPortAllocator the http port allocator - */ - void addPortAllocators(HttpPortAllocator httpPortAllocator); - - /** - * Unregister a http port allocator - * @param httpPortAllocator the http port allocator - */ - void removePortAllocators(HttpPortAllocator httpPortAllocator); - - /** - * Get a list of all registered http port allocators - * @return the list of http port allocators - */ - List<HttpPortAllocator> getPortAllocators(); -} diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java index b345402d8b..af41e85213 100644 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java +++ b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java @@ -19,6 +19,11 @@ package org.apache.tuscany.sca.host.http.extensibility.impl; +import java.io.IOException; +import java.net.ServerSocket; +import java.security.AccessController; +import java.security.PrivilegedAction; + import org.apache.tuscany.sca.host.http.HttpScheme; import org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator; @@ -27,15 +32,68 @@ public class DefaultHttpPortAllocatorImpl implements HttpPortAllocator { public int getDefaultPort(HttpScheme scheme) { int port = 0; - if(scheme == HttpScheme.HTTP) { - port = 8080; - } else if(scheme == HttpScheme.HTTPS) { - port = 8443; - } else { - throw new IllegalArgumentException("Scheme not support : " + scheme.toString()); + if (scheme == null || scheme == HttpScheme.HTTP) { + try { + port = Integer.parseInt(getVariable("HTTP_PORT", "8080")); + if (port == 0) { + port = findFreePort(8080, 9080); + } + } catch (NumberFormatException e) { + port = 8080; + } + } else if (scheme == HttpScheme.HTTPS) { + try { + port = Integer.parseInt(getVariable("HTTPS_PORT", "8443")); + if (port == 0) { + port = findFreePort(8443, 9443); + } + } catch (NumberFormatException e) { + port = 8443; + } } return port; + + } + + private static String getVariable(final String variableName, final String defaultValue) { + return AccessController.doPrivileged(new PrivilegedAction<String>() { + public String run() { + String value = System.getProperty(variableName); + if (value == null || value.length() == 0) { + value = System.getenv(variableName); + if (value == null || value.length() == 0) { + value = defaultValue; + } + } + return value; + } + }); + } + + private int findFreePort(final int start, final int end) { + return AccessController.doPrivileged(new PrivilegedAction<Integer>() { + public Integer run() { + for (int p = start; p <= end; p++) { + ServerSocket socket = null; + try { + socket = new ServerSocket(p); + return p; + } catch (IOException e) { + // Ignore + } finally { + if (socket != null) { + try { + socket.close(); + } catch (IOException e) { + // Ignore + } + } + } + } + return -1; + } + }); } } diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator b/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator index 87ef89118d..70bc948ffc 100644 --- a/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator +++ b/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator @@ -15,5 +15,5 @@ # specific language governing permissions and limitations # under the License. -org.apache.tuscany.sca.host.http.extensibility.impl.DefaultHttpPortAllocatorImpl,ranking=900 +org.apache.tuscany.sca.host.http.extensibility.impl.DefaultHttpPortAllocatorImpl diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocatorExtensionPoint b/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocatorExtensionPoint deleted file mode 100644 index 06d49c5ea3..0000000000 --- a/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocatorExtensionPoint +++ /dev/null @@ -1,19 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -org.apache.tuscany.sca.host.http.extensibility.DefaultHttpPortAllocatorExtensionPoint - diff --git a/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java b/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java index a7bfcfea2a..f62f24adc8 100644 --- a/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java +++ b/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java @@ -49,7 +49,6 @@ import org.apache.tuscany.sca.host.http.HttpScheme; import org.apache.tuscany.sca.host.http.SecurityContext; import org.apache.tuscany.sca.host.http.ServletHost; import org.apache.tuscany.sca.host.http.ServletMappingException; -import org.apache.tuscany.sca.host.http.extensibility.ExtensibleHttpPortAllocator; import org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator; import org.apache.tuscany.sca.work.WorkScheduler; import org.mortbay.jetty.Connector; @@ -120,19 +119,25 @@ public class JettyServer implements ServletHost, LifeCycleListener { private org.mortbay.log.Logger jettyLogger; public JettyServer(ExtensionPointRegistry registry) { - this(registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(WorkScheduler.class), ExtensibleHttpPortAllocator.getInstance(registry)); + UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.workScheduler = utilityExtensionPoint.getUtility(WorkScheduler.class); + this.httpPortAllocator = utilityExtensionPoint.getUtility(HttpPortAllocator.class); + init(); } protected JettyServer(WorkScheduler workScheduler, HttpPortAllocator httpPortAllocator) { this.httpPortAllocator = httpPortAllocator; this.workScheduler = workScheduler; + init(); + } - this.defaultPort = httpPortAllocator.getDefaultPort(HttpScheme.HTTP); + private void init() { + this.defaultPort = this.httpPortAllocator.getDefaultPort(HttpScheme.HTTP); //handle backdoor to set specific default port in tests if(portDefault > 0) { this.defaultPort = portDefault; } - this.defaultSSLPort = httpPortAllocator.getDefaultPort(HttpScheme.HTTPS); + this.defaultSSLPort = this.httpPortAllocator.getDefaultPort(HttpScheme.HTTPS); AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { trustStore = System.getProperty("javax.net.ssl.trustStore"); diff --git a/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java b/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java index ee1de9b624..80666db8e3 100644 --- a/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java +++ b/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java @@ -25,6 +25,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ConnectException; import java.net.InetAddress; +import java.net.ServerSocket; import java.net.Socket; import java.net.URL; import java.util.concurrent.ExecutorService; @@ -42,8 +43,8 @@ import javax.servlet.http.HttpServletResponse; import junit.framework.TestCase; import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.host.http.DefaultResourceServlet; -import org.apache.tuscany.sca.host.http.extensibility.ExtensibleHttpPortAllocator; import org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator; import org.apache.tuscany.sca.work.NotificationListener; import org.apache.tuscany.sca.work.WorkScheduler; @@ -54,23 +55,23 @@ import org.junit.Assert; */ public class JettyServerTestCase extends TestCase { - private static final String REQUEST1_HEADER = - "GET / HTTP/1.0\n" + "Host: localhost\n" - + "Content-Type: text/xml\n" - + "Connection: close\n" - + "Content-Length: "; + private static final String REQUEST1_HEADER = "GET / HTTP/1.0\n" + "Host: localhost\n" + + "Content-Type: text/xml\n" + + "Connection: close\n" + + "Content-Length: "; private static final String REQUEST1_CONTENT = ""; - private static final String REQUEST1 = - REQUEST1_HEADER + REQUEST1_CONTENT.getBytes().length + "\n\n" + REQUEST1_CONTENT; - - private static final String REQUEST2_HEADER = - "GET /webcontent/test.html HTTP/1.0\n" + "Host: localhost\n" - + "Content-Type: text/xml\n" - + "Connection: close\n" - + "Content-Length: "; + private static final String REQUEST1 = REQUEST1_HEADER + REQUEST1_CONTENT.getBytes().length + + "\n\n" + + REQUEST1_CONTENT; + + private static final String REQUEST2_HEADER = "GET /webcontent/test.html HTTP/1.0\n" + "Host: localhost\n" + + "Content-Type: text/xml\n" + + "Connection: close\n" + + "Content-Length: "; private static final String REQUEST2_CONTENT = ""; - private static final String REQUEST2 = - REQUEST2_HEADER + REQUEST2_CONTENT.getBytes().length + "\n\n" + REQUEST2_CONTENT; + private static final String REQUEST2 = REQUEST2_HEADER + REQUEST2_CONTENT.getBytes().length + + "\n\n" + + REQUEST2_CONTENT; private static final int HTTP_PORT = 8085; @@ -90,7 +91,8 @@ public class JettyServerTestCase extends TestCase { } }; - private HttpPortAllocator httpPortAllocator = new ExtensibleHttpPortAllocator(new DefaultExtensionPointRegistry()); + private HttpPortAllocator httpPortAllocator = new DefaultExtensionPointRegistry() + .getExtensionPoint(UtilityExtensionPoint.class).getUtility(HttpPortAllocator.class); /** * Verifies requests are properly routed according to the Servlet mapping @@ -113,7 +115,7 @@ public class JettyServerTestCase extends TestCase { * Verifies requests are properly routed according to the Servlet mapping */ public void testDeployedURI() throws Exception { - JettyServer service = new JettyServer(workScheduler,httpPortAllocator); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.setDefaultPort(8085); service.start(); TestServlet servlet = new TestServlet(); @@ -139,7 +141,7 @@ public class JettyServerTestCase extends TestCase { System.setProperty("javax.net.ssl.keyStore", "target/test-classes/tuscany.keyStore"); System.setProperty("javax.net.ssl.keyStorePassword", "apache"); System.setProperty("jetty.ssl.password", "apache"); - JettyServer service = new JettyServer(workScheduler,httpPortAllocator); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.start(); TestServlet servlet = new TestServlet(); try { @@ -152,12 +154,12 @@ public class JettyServerTestCase extends TestCase { System.setProperty("javax.net.ssl.trustStore", "target/test-classes/tuscany.keyStore"); System.setProperty("javax.net.ssl.trustStorePassword", "apache"); URL url = new URL("https://127.0.0.1:8085/foo"); - HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); + HttpsURLConnection conn = (HttpsURLConnection)url.openConnection(); conn.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; - }} - ); + } + }); conn.connect(); read(conn.getInputStream()); @@ -171,7 +173,7 @@ public class JettyServerTestCase extends TestCase { * Verifies that Servlets can be registered with multiple ports */ public void testRegisterMultiplePorts() throws Exception { - JettyServer service = new JettyServer(workScheduler,httpPortAllocator); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.start(); TestServlet servlet = new TestServlet(); service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet); @@ -198,7 +200,7 @@ public class JettyServerTestCase extends TestCase { } public void testUnregisterMapping() throws Exception { - JettyServer service = new JettyServer(workScheduler,httpPortAllocator); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.start(); TestServlet servlet = new TestServlet(); String uri = "http://127.0.0.1:" + HTTP_PORT + "/foo"; @@ -218,7 +220,7 @@ public class JettyServerTestCase extends TestCase { } public void testRequestSession() throws Exception { - JettyServer service = new JettyServer(workScheduler,httpPortAllocator); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.start(); TestServlet servlet = new TestServlet(); service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet); @@ -233,14 +235,14 @@ public class JettyServerTestCase extends TestCase { } public void testRestart() throws Exception { - JettyServer service = new JettyServer(workScheduler,httpPortAllocator); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.start(); service.stop(); service.stop(); } public void testNoMappings() throws Exception { - JettyServer service = new JettyServer(workScheduler,httpPortAllocator); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.start(); Exception ex = null; try { @@ -253,7 +255,7 @@ public class JettyServerTestCase extends TestCase { } public void testResourceServlet() throws Exception { - JettyServer service = new JettyServer(workScheduler,httpPortAllocator); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.start(); String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString(); @@ -274,7 +276,7 @@ public class JettyServerTestCase extends TestCase { } public void testDefaultServlet() throws Exception { - JettyServer service = new JettyServer(workScheduler,httpPortAllocator); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.start(); String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString(); @@ -293,6 +295,33 @@ public class JettyServerTestCase extends TestCase { service.stop(); } + public void testDefaultPort() throws IOException { + try { + // Open 9085 + System.setProperty("HTTP_PORT", "9085"); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); + assertEquals(9085, service.getDefaultPort()); + + // Try to find a free port + System.setProperty("HTTP_PORT", "0"); + service = new JettyServer(workScheduler, httpPortAllocator); + int port = service.getDefaultPort(); + assertNotSame(0, port); + + // Try to find the next free port + ServerSocket socket = null; + try { + socket = new ServerSocket(port); + service = new JettyServer(workScheduler, httpPortAllocator); + assertNotSame(port, service.getDefaultPort()); + } finally { + socket.close(); + } + } finally { + System.clearProperty("HTTP_PORT"); + } + } + private static String read(Socket socket) throws IOException { InputStream is = socket.getInputStream(); return read(is); |