hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1597550 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/impl/bootstrap/Server.java main/java/org/apache/http/impl/bootstrap/ServerBootstrap.java test/java/org/apache/http/testserver/HttpServer.java
Date Mon, 26 May 2014 10:13:46 GMT
Author: olegk
Date: Mon May 26 10:13:46 2014
New Revision: 1597550

URL: http://svn.apache.org/r1597550
Log:
Use new embedded server for integration tests

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/Server.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/ServerBootstrap.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpServer.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/Server.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/Server.java?rev=1597550&r1=1597549&r2=1597550&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/Server.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/Server.java
Mon May 26 10:13:46 2014
@@ -62,6 +62,7 @@ public class Server {
     private final ExecutorService workerExecutorService;
     private final AtomicReference<Status> status;
 
+    private volatile ServerSocket serverSocket;
     private volatile RequestListener requestListener;
 
     Server(
@@ -87,17 +88,35 @@ public class Server {
         this.status = new AtomicReference<Status>(Status.READY);
     }
 
+    public InetAddress getInetAddress() {
+        final ServerSocket localSocket = this.serverSocket;
+        if (localSocket != null) {
+            return localSocket.getInetAddress();
+        } else {
+            return null;
+        }
+    }
+
+    public int getLocalPort() {
+        final ServerSocket localSocket = this.serverSocket;
+        if (localSocket != null) {
+            return localSocket.getLocalPort();
+        } else {
+            return -1;
+        }
+    }
+
     public void start() throws IOException {
         if (this.status.compareAndSet(Status.READY, Status.ACTIVE)) {
-            final ServerSocket serverSocket = this.serverSocketFactory.createServerSocket(
+            this.serverSocket = this.serverSocketFactory.createServerSocket(
                     this.port, this.socketConfig.getBacklogSize(), this.ifAddress);
-            serverSocket.setReuseAddress(this.socketConfig.isSoReuseAddress());
+            this.serverSocket.setReuseAddress(this.socketConfig.isSoReuseAddress());
             if (this.socketConfig.getRcvBufSize() > 0) {
-                serverSocket.setReceiveBufferSize(this.socketConfig.getRcvBufSize());
+                this.serverSocket.setReceiveBufferSize(this.socketConfig.getRcvBufSize());
             }
             this.requestListener = new RequestListener(
                     this.socketConfig,
-                    serverSocket,
+                    this.serverSocket,
                     this.httpService,
                     this.connectionFactory,
                     this.exceptionLogger,

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/ServerBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/ServerBootstrap.java?rev=1597550&r1=1597549&r2=1597550&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/ServerBootstrap.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/bootstrap/ServerBootstrap.java
Mon May 26 10:13:46 2014
@@ -376,7 +376,7 @@ public class ServerBootstrap {
         }
 
         return new Server(
-                this.listenerPort > 0 ? this.listenerPort : 8080,
+                this.listenerPort > 0 ? this.listenerPort : 0,
                 this.localAddress,
                 this.socketConfig != null ? this.socketConfig : SocketConfig.DEFAULT,
                 serverSocketFactory,

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpServer.java?rev=1597550&r1=1597549&r2=1597550&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpServer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpServer.java
Mon May 26 10:13:46 2014
@@ -28,59 +28,35 @@
 package org.apache.http.testserver;
 
 import java.io.IOException;
-import java.io.InterruptedIOException;
 import java.net.InetAddress;
-import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.SocketException;
+import java.util.concurrent.TimeUnit;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.http.ConnectionClosedException;
-import org.apache.http.ConnectionReuseStrategy;
-import org.apache.http.HttpException;
-import org.apache.http.HttpResponseFactory;
-import org.apache.http.HttpResponseInterceptor;
-import org.apache.http.HttpServerConnection;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.HttpContext;
+import org.apache.http.ExceptionLogger;
+import org.apache.http.HttpConnectionFactory;
+import org.apache.http.config.SocketConfig;
+import org.apache.http.impl.bootstrap.Server;
+import org.apache.http.impl.bootstrap.ServerBootstrap;
 import org.apache.http.protocol.HttpExpectationVerifier;
-import org.apache.http.protocol.HttpProcessor;
 import org.apache.http.protocol.HttpRequestHandler;
-import org.apache.http.protocol.HttpService;
-import org.apache.http.protocol.ImmutableHttpProcessor;
-import org.apache.http.protocol.ResponseConnControl;
-import org.apache.http.protocol.ResponseContent;
-import org.apache.http.protocol.ResponseDate;
-import org.apache.http.protocol.ResponseServer;
 import org.apache.http.protocol.UriHttpRequestHandlerMapper;
 import org.apache.http.util.Asserts;
 
 public class HttpServer {
 
-    private final HttpProcessor httpproc;
-    private final ConnectionReuseStrategy connStrategy;
-    private final HttpResponseFactory responseFactory;
     private final UriHttpRequestHandlerMapper reqistry;
-    private final ServerSocket serversocket;
-
     private volatile HttpExpectationVerifier expectationVerifier;
-    private volatile Thread listener;
-    private volatile boolean shutdown;
     private volatile int timeout;
 
+    private volatile Server server;
+
     public HttpServer() throws IOException {
         super();
-        this.httpproc = new ImmutableHttpProcessor(
-                new HttpResponseInterceptor[] {
-                        new ResponseDate(),
-                        new ResponseServer("TEST-SERVER/1.1"),
-                        new ResponseContent(),
-                        new ResponseConnControl()
-                });
-        this.connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
-        this.responseFactory = DefaultHttpResponseFactory.INSTANCE;
         this.reqistry = new UriHttpRequestHandlerMapper();
-        this.serversocket = new ServerSocket(0);
     }
 
     public int getTimeout() {
@@ -101,101 +77,71 @@ public class HttpServer {
         this.expectationVerifier = expectationVerifier;
     }
 
-    private HttpServerConnection acceptConnection() throws IOException {
-        final Socket socket = this.serversocket.accept();
-        final LoggingBHttpServerConnection conn = new LoggingBHttpServerConnection(8 * 1024);
-        conn.bind(socket);
-        conn.setSocketTimeout(this.timeout);
-        return conn;
-    }
-
     public int getPort() {
-        return this.serversocket.getLocalPort();
+        final Server local = this.server;
+        if (local != null) {
+            return this.server.getLocalPort();
+        } else {
+            throw new IllegalStateException("Server not running");
+        }
     }
 
     public InetAddress getInetAddress() {
-        return this.serversocket.getInetAddress();
+        final Server local = this.server;
+        if (local != null) {
+            return local.getInetAddress();
+        } else {
+            throw new IllegalStateException("Server not running");
+        }
     }
 
-    public void start() {
-        Asserts.check(this.listener == null, "Listener already running");
-        this.listener = new Thread(new Runnable() {
-
-            @Override
-            public void run() {
-                while (!shutdown && !Thread.interrupted()) {
-                    try {
-                        // Set up HTTP connection
-                        final HttpServerConnection conn = acceptConnection();
-                        // Set up the HTTP service
-                        final HttpService httpService = new HttpService(
-                                httpproc,
-                                connStrategy,
-                                responseFactory,
-                                reqistry,
-                                expectationVerifier);
-                        // Start worker thread
-                        final Thread t = new WorkerThread(httpService, conn);
-                        t.setDaemon(true);
-                        t.start();
-                    } catch (final InterruptedIOException ex) {
-                        break;
-                    } catch (final IOException e) {
-                        break;
-                    }
-                }
-            }
-
-        });
-        this.listener.start();
+    public void start() throws IOException {
+        Asserts.check(this.server == null, "Server already running");
+        this.server = ServerBootstrap.bootstrap()
+                .setSocketConfig(SocketConfig.custom()
+                        .setSoTimeout(this.timeout)
+                        .build())
+                .setServerInfo("TEST-SERVER/1.1")
+                .setConnectionFactory(new LoggingConnFactory())
+                .setExceptionLogger(new SimpleExceptionLogger())
+                .setExpectationVerifier(this.expectationVerifier)
+                .setHandlerMapper(this.reqistry)
+                .create();
+        this.server.start();
     }
 
     public void shutdown() {
-        if (this.shutdown) {
-            return;
+        final Server local = this.server;
+        this.server = null;
+        if (local != null) {
+            local.shutdown(5, TimeUnit.SECONDS);
         }
-        this.shutdown = true;
-        try {
-            this.serversocket.close();
-        } catch (final IOException ignore) {}
-        this.listener.interrupt();
-        try {
-            this.listener.join(1000);
-        } catch (final InterruptedException ignore) {}
-    }
-
-    static class WorkerThread extends Thread {
-
-        private final HttpService httpservice;
-        private final HttpServerConnection conn;
-
-        public WorkerThread(
-                final HttpService httpservice,
-                final HttpServerConnection conn) {
-            super();
-            this.httpservice = httpservice;
-            this.conn = conn;
+    }
+
+    static class LoggingConnFactory implements HttpConnectionFactory<LoggingBHttpServerConnection>
{
+
+        @Override
+        public LoggingBHttpServerConnection createConnection(final Socket socket) throws
IOException {
+            final LoggingBHttpServerConnection conn = new LoggingBHttpServerConnection(8
* 1024);
+            conn.bind(socket);
+            return conn;
         }
+    }
+
+    static class SimpleExceptionLogger implements ExceptionLogger {
+
+        private final Log log = LogFactory.getLog(HttpServer.class);
 
         @Override
-        public void run() {
-            final HttpContext context = new BasicHttpContext(null);
-            try {
-                while (!Thread.interrupted() && this.conn.isOpen()) {
-                    this.httpservice.handleRequest(this.conn, context);
-                }
-            } catch (final ConnectionClosedException ex) {
-            } catch (final IOException ex) {
-                System.err.println("I/O error: " + ex.getMessage());
-            } catch (final HttpException ex) {
-                System.err.println("Unrecoverable HTTP protocol violation: " + ex.getMessage());
-            } finally {
-                try {
-                    this.conn.shutdown();
-                } catch (final IOException ignore) {}
+        public void log(final Exception ex) {
+            if (ex instanceof ConnectionClosedException) {
+                this.log.debug(ex.getMessage());
+            } else if (ex instanceof SocketException) {
+                this.log.debug(ex.getMessage());
+            } else {
+                this.log.error(ex.getMessage(), ex);
             }
         }
-
     }
 
 }



Mime
View raw message