You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by ve...@apache.org on 2011/12/19 08:00:14 UTC

svn commit: r1220630 - in /axis/axis2/java/core/trunk/modules: testutils/src/main/java/org/apache/axis2/testutils/ transport/http/src/org/apache/axis2/transport/http/server/

Author: veithen
Date: Mon Dec 19 07:00:14 2011
New Revision: 1220630

URL: http://svn.apache.org/viewvc?rev=1220630&view=rev
Log:
Added some code that allows to construct test cases that use HTTP with port numbers allocated by the OS. That is useful if one wants to run builds concurrently.

Added:
    axis/axis2/java/core/trunk/modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java   (with props)
Modified:
    axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/DefaultConnectionListener.java
    axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/SimpleHttpServer.java

Added: axis/axis2/java/core/trunk/modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java?rev=1220630&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java (added)
+++ axis/axis2/java/core/trunk/modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java Mon Dec 19 07:00:14 2011
@@ -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);
+        }
+    }
+}

Propchange: axis/axis2/java/core/trunk/modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/DefaultConnectionListener.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/DefaultConnectionListener.java?rev=1220630&r1=1220629&r2=1220630&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/DefaultConnectionListener.java (original)
+++ axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/DefaultConnectionListener.java Mon Dec 19 07:00:14 2011
@@ -77,8 +77,11 @@ public class DefaultConnectionListener i
                         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();
@@ -106,12 +109,26 @@ public class DefaultConnectionListener i
             }
         } 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;
         }
     }
 

Modified: axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/SimpleHttpServer.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/SimpleHttpServer.java?rev=1220630&r1=1220629&r2=1220630&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/SimpleHttpServer.java (original)
+++ axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/server/SimpleHttpServer.java Mon Dec 19 07:00:14 2011
@@ -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() {