This is an automated email from the ASF dual-hosted git repository. billblough pushed a commit to branch 1_5 in repository https://gitbox.apache.org/repos/asf/axis-axis2-java-core.git
commit 15d9a12627204f122010d6b43315529cca52831c Author: Andreas Veithen <veit...@apache.org> AuthorDate: Wed Dec 21 10:03:09 2011 +0000 Merged r1220630 to the 1.5 branch for use in the Sandesha2 build. --- .../org/apache/axis2/testutils/PortAllocator.java | 42 ++++++++++++++++++++++ .../http/server/DefaultConnectionListener.java | 23 ++++++++++-- .../transport/http/server/SimpleHttpServer.java | 11 ++++-- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java b/modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java new file mode 100644 index 0000000..d8fdf8e --- /dev/null +++ b/modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java @@ -0,0 +1,42 @@ +/* + * 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.axis2.testutils; + +import java.io.IOException; +import java.net.ServerSocket; + +public final class PortAllocator { + private PortAllocator() {} + + /** + * Allocate a TCP port. + * + * @return the allocated port + */ + public static int allocatePort() { + try { + ServerSocket ss = new ServerSocket(0); + int port = ss.getLocalPort(); + ss.close(); + return port; + } catch (IOException ex) { + throw new Error("Unable to allocate TCP port", ex); + } + } +} diff --git a/modules/transport/http/src/org/apache/axis2/transport/http/server/DefaultConnectionListener.java b/modules/transport/http/src/org/apache/axis2/transport/http/server/DefaultConnectionListener.java index d931c8b..2c39991 100644 --- a/modules/transport/http/src/org/apache/axis2/transport/http/server/DefaultConnectionListener.java +++ b/modules/transport/http/src/org/apache/axis2/transport/http/server/DefaultConnectionListener.java @@ -72,8 +72,11 @@ public class DefaultConnectionListener implements IOProcessor { if (LOG.isInfoEnabled()) { LOG.info("Listening on port " + port); } - serversocket = new ServerSocket(port); - serversocket.setReuseAddress(true); + synchronized (this) { + serversocket = new ServerSocket(port); + serversocket.setReuseAddress(true); + notifyAll(); + } } LOG.debug("Waiting for incoming HTTP connection"); Socket socket = this.serversocket.accept(); @@ -96,12 +99,26 @@ public class DefaultConnectionListener implements IOProcessor { } } finally { destroy(); + synchronized (this) { + notifyAll(); + } } } - public void close() throws IOException { + public synchronized void awaitSocketOpen() throws InterruptedException { + while (serversocket == null) { + wait(); + } + } + + public synchronized int getPort() { + return serversocket.getLocalPort(); + } + + public synchronized void close() throws IOException { if (this.serversocket != null) { this.serversocket.close(); + this.serversocket = null; } } diff --git a/modules/transport/http/src/org/apache/axis2/transport/http/server/SimpleHttpServer.java b/modules/transport/http/src/org/apache/axis2/transport/http/server/SimpleHttpServer.java index b2d6a97..7debb56 100644 --- a/modules/transport/http/src/org/apache/axis2/transport/http/server/SimpleHttpServer.java +++ b/modules/transport/http/src/org/apache/axis2/transport/http/server/SimpleHttpServer.java @@ -38,7 +38,7 @@ public class SimpleHttpServer { private static final int SHUTDOWN_GRACE_PERIOD = 3000; // ms private HttpFactory httpFactory; - private final int port; + private int port; private final HttpParams params; private final WorkerFactory workerFactory; @@ -96,7 +96,14 @@ public class SimpleHttpServer { } public void start() { - this.listenerExecutor.execute(this.listener); + DefaultConnectionListener listener = (DefaultConnectionListener)this.listener; + this.listenerExecutor.execute(listener); + try { + listener.awaitSocketOpen(); + port = listener.getPort(); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } } public boolean isRunning() {