You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ro...@apache.org on 2007/02/25 11:41:56 UTC

svn commit: r511477 - /jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/

Author: rolandw
Date: Sun Feb 25 02:41:55 2007
New Revision: 511477

URL: http://svn.apache.org/viewvc?view=rev&rev=511477
Log:
new pooled connection adapter, simplified SingleCCM

Added:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java
      - copied, changed from r511460, jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnectionAdapter.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java   (with props)
Removed:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnectionAdapter.java
Modified:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/SingleClientConnManager.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/ThreadSafeClientConnManager.java

Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java (from r511460, jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnectionAdapter.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java?view=diff&rev=511477&p1=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnectionAdapter.java&r1=511460&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java&r2=511477
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnectionAdapter.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java Sun Feb 25 02:41:55 2007
@@ -72,7 +72,7 @@
  *
  * @since 4.0
  */
-public abstract class AbstractClientConnectionAdapter
+public abstract class AbstractClientConnAdapter
     implements ManagedClientConnection {
 
     /** The connection manager, if any. */
@@ -93,8 +93,8 @@
      * @param mgr       the connection manager, or <code>null</code>
      * @param conn      the connection to wrap, or <code>null</code>
      */
-    protected AbstractClientConnectionAdapter(ClientConnectionManager mgr,
-                                              OperatedClientConnection conn) {
+    protected AbstractClientConnAdapter(ClientConnectionManager mgr,
+                                        OperatedClientConnection conn) {
 
         connManager = mgr;
         wrappedConnection = conn;
@@ -272,4 +272,4 @@
         connManager.releaseConnection(this);
     }
 
-} // class AbstractClientConnectionAdapter
+} // class AbstractClientConnAdapter

Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java?view=auto&rev=511477
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java Sun Feb 25 02:41:55 2007
@@ -0,0 +1,168 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.conn;
+
+
+import java.io.IOException;
+
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.conn.HttpRoute;
+import org.apache.http.conn.ClientConnectionManager;
+
+
+
+/**
+ * Abstract adapter from pool {@link AbstractPoolEntry entries} to
+ * {@link ManagedClientConnection managed} client connections.
+ * The connection in the pool entry is used to initialize the base class.
+ * In addition, methods to establish a route are delegated to the
+ * pool entry. {@link #shutdown shutdown} and {@link #close close}
+ * will clear the tracked route in the pool entry and call the
+ * respective method of the wrapped connection.
+ *
+ * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
+ *
+ *
+ * <!-- empty lines to avoid svn diff problems -->
+ * @version   $Revision$ $Date$
+ *
+ * @since 4.0
+ */
+public abstract class AbstractPooledConnAdapter
+    extends AbstractClientConnAdapter {
+
+    /** The wrapped pool entry. */
+    protected AbstractPoolEntry poolEntry;
+
+
+    /**
+     * Creates a new connection adapter.
+     *
+     * @param manager   the connection manager
+     * @param entry     the pool entry for the connection being wrapped
+     * <!--
+     * @ param plan      the planned route, which will be used
+     *                  to update the pool entry
+     * -->
+     */
+    protected AbstractPooledConnAdapter(ClientConnectionManager manager,
+                                        AbstractPoolEntry entry
+                                        //, HttpRoute plan
+                                        ) {
+        super(manager, entry.connection);
+
+        //entry.plannedRoute = plan;
+        this.poolEntry = entry;
+    }
+
+
+    /**
+     * Asserts that this adapter is still attached.
+     *
+     * @throws IllegalStateException
+     *      if it is {@link #detach detach}ed
+     */
+    protected final void assertAttached() {
+        if (poolEntry == null) {
+            throw new IllegalStateException("Adapter is detached.");
+        }
+    }
+
+    /**
+     * Detaches this adapter from the wrapped connection.
+     * This adapter becomes useless.
+     */
+    protected void detach() {
+        wrappedConnection = null;
+        poolEntry = null;
+    }
+
+
+    // non-javadoc, see interface ManagedHttpConnection
+    public HttpRoute getRoute() {
+
+        assertAttached();
+        return (poolEntry.tracker == null) ?
+            null : poolEntry.tracker.toRoute();
+    }
+
+    // non-javadoc, see interface ManagedHttpConnection
+    public void open(HttpRoute route,
+                     HttpContext context, HttpParams params)
+        throws IOException {
+
+        assertAttached();
+        poolEntry.open(route, context, params);
+    }
+
+
+    // non-javadoc, see interface ManagedHttpConnection
+    public void tunnelCreated(boolean secure, HttpParams params)
+        throws IOException {
+
+        assertAttached();
+        poolEntry.tunnelCreated(secure, params);
+    }
+
+
+    // non-javadoc, see interface ManagedHttpConnection
+    public void layerProtocol(HttpContext context, HttpParams params)
+        throws IOException {
+
+        assertAttached();
+        poolEntry.layerProtocol(context, params);
+    }
+
+
+
+    // non-javadoc, see interface HttpConnection        
+    public void close() throws IOException {
+        if (poolEntry != null)
+            poolEntry.closing();
+
+        if (wrappedConnection != null) {
+            wrappedConnection.close();
+        }
+    }
+
+    // non-javadoc, see interface HttpConnection        
+    public void shutdown() throws IOException {
+        if (poolEntry != null)
+            poolEntry.closing();
+
+        if (wrappedConnection != null) {
+            wrappedConnection.shutdown();
+        }
+    }
+
+
+} // class AbstractPooledConnAdapter

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/SingleClientConnManager.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/SingleClientConnManager.java?view=diff&rev=511477&r1=511476&r2=511477
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/SingleClientConnManager.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/SingleClientConnManager.java Sun Feb 25 02:41:55 2007
@@ -215,13 +215,12 @@
         // check re-usability of the connection
         if (uniquePoolEntry.connection.isOpen()) {
             final boolean shutdown =
-                (uniquePoolEntry.tracker == null) || // how could that happen?
-                !uniquePoolEntry.tracker.toRoute().equals(route);
+                ((uniquePoolEntry.tracker == null) || // how could that happen?
+                 !uniquePoolEntry.tracker.toRoute().equals(route));
 
             if (shutdown) {
-                uniquePoolEntry.closing();
                 try {
-                    uniquePoolEntry.connection.shutdown();
+                    uniquePoolEntry.shutdown();
                 } catch (IOException iox) {
                     LOG.debug("Problem shutting down connection.", iox);
                     // create a new connection, just to be sure
@@ -251,7 +250,7 @@
             throw new IllegalArgumentException
                 ("Connection not obtained from this manager.");
         }
-        if (sca.wrappedConnection == null)
+        if (sca.poolEntry == null)
             return; // already released
 
         try {
@@ -265,9 +264,6 @@
                 }
 
                 // make sure this connection will not be re-used
-                //@@@ Can we set some kind of flag before shutting down?
-                //@@@ If shutdown throws an exception, we can't be sure
-                //@@@ that the connection will consider itself closed.
                 // we might have gotten here because of a shutdown trigger
                 // shutdown of the adapter also clears the tracked route
                 sca.shutdown();
@@ -292,9 +288,8 @@
         if ((managedConn == null) && uniquePoolEntry.connection.isOpen()) {
             final long cutoff = System.currentTimeMillis() - idletime;
             if (lastReleaseTime <= cutoff) {
-                uniquePoolEntry.closing();
                 try {
-                    uniquePoolEntry.connection.close();
+                    uniquePoolEntry.close();
                 } catch (IOException iox) {
                     // ignore
                     LOG.debug("Problem closing idle connection.", iox);
@@ -314,7 +309,7 @@
 
         try {
             if (uniquePoolEntry != null) // and connection open?
-                uniquePoolEntry.connection.shutdown();
+                uniquePoolEntry.shutdown();
         } catch (IOException iox) {
             // ignore
             LOG.debug("Problem while shutting down manager.", iox);
@@ -342,11 +337,7 @@
             managedConn.detach();
 
         try {
-            
-            if (uniquePoolEntry.connection.isOpen()) {
-                uniquePoolEntry.closing();
-                uniquePoolEntry.connection.shutdown();
-            }
+            uniquePoolEntry.shutdown();
         } catch (IOException iox) {
             // ignore
             LOG.debug("Problem while shutting down connection.", iox);
@@ -361,7 +352,7 @@
 
         //@@@ move to base class when TSCCM is cleaned up
         /** The route for which this entry gets allocated. */
-        private HttpRoute plannedRoute;
+        protected HttpRoute plannedRoute;
 
             
         /**
@@ -369,7 +360,7 @@
          *
          * @param occ   the underlying connection for this entry
          */
-        private PoolEntry(OperatedClientConnection occ) {
+        protected PoolEntry(OperatedClientConnection occ) {
             super(occ);
         }
 
@@ -379,133 +370,51 @@
             return SingleClientConnManager.this.connOperator;
         }
 
-    } // class PoolEntry
-
-
-
-    /**
-     * The connection adapter used by this manager.
-     * <p><i>Refactoring pending!</i>
-     * This has a lot of common code with private classes
-     * <code>TrackingPoolEntry</code> and <code>HttpConnectionAdapter</code>
-     * in {@link ThreadSafeClientConnManager ThreadSafeClientConnManager}.
-     */
-    protected class ConnAdapter
-        extends AbstractClientConnectionAdapter {
-
-        /** The wrapped pool entry. */
-        private PoolEntry poolEntry;
-
 
         /**
-         * Creates a new connection adapter.
-         *
-         * @param entry   the pool entry for the connection being wrapped
+         * Closes the connection in this pool entry.
          */
-        protected ConnAdapter(PoolEntry entry, HttpRoute plan) {
-            super(SingleClientConnManager.this, entry.connection);
-            super.markedReusable = true;
-
-            entry.plannedRoute = plan;
-            this.poolEntry = entry;
-        }
-
+        protected void close()
+            throws IOException {
 
-        /**
-         * Asserts that this adapter is still attached.
-         *
-         * @throws IllegalStateException
-         *      if it is {@link #detach detach}ed
-         */
-        protected final void assertAttached() {
-            if (wrappedConnection == null) {
-                throw new IllegalStateException("Adapter is detached.");
-            }
+            closing();
+            if (connection.isOpen())
+                connection.close();
         }
 
-        /**
-         * Checks if the wrapped connection is still available.
-         *
-         * @return <code>true</code> if still available,
-         *         <code>false</code> if {@link #detach detached}
-         */
-        protected boolean hasConnection() {
-            return wrappedConnection != null;
-        }
 
         /**
-         * Detaches this adapter from the wrapped connection.
-         * This adapter becomes useless.
+         * Shuts down the connection in this pool entry.
          */
-        protected void detach() {
-            this.wrappedConnection = null;
-        }
-
-
-        // non-javadoc, see interface ManagedHttpConnection
-        public HttpRoute getRoute() {
-
-            assertAttached();
-            return (poolEntry.tracker == null) ?
-                null : poolEntry.tracker.toRoute();
-        }
-
-
-
-        // non-javadoc, see interface ManagedHttpConnection
-        public void open(HttpRoute route,
-                         HttpContext context, HttpParams params)
-            throws IOException {
-
-            assertAttached();
-            poolEntry.open(route, context, params);
-        }
-
-
-        // non-javadoc, see interface ManagedHttpConnection
-        public void tunnelCreated(boolean secure, HttpParams params)
-            throws IOException {
-
-            assertAttached();
-            poolEntry.tunnelCreated(secure, params);
-        }
-
-
-        // non-javadoc, see interface ManagedHttpConnection
-        public void layerProtocol(HttpContext context, HttpParams params)
+        protected void shutdown()
             throws IOException {
 
-            assertAttached();
-            poolEntry.layerProtocol(context, params);
+            closing();
+            if (connection.isOpen())
+                connection.shutdown();
         }
 
+    } // class PoolEntry
 
 
-        // non-javadoc, see interface HttpConnection        
-        public void close() throws IOException {
-            if (poolEntry != null)
-                poolEntry.closing();
 
-            if (hasConnection()) {
-                wrappedConnection.close();
-            } else {
-                // do nothing
-            }
-        }
+    /**
+     * The connection adapter used by this manager.
+     */
+    protected class ConnAdapter extends AbstractPooledConnAdapter {
 
-        // non-javadoc, see interface HttpConnection        
-        public void shutdown() throws IOException {
-            if (poolEntry != null)
-                poolEntry.closing();
-
-            if (hasConnection()) {
-                wrappedConnection.shutdown();
-            } else {
-                // do nothing
-            }
+        /**
+         * Creates a new connection adapter.
+         *
+         * @param entry   the pool entry for the connection being wrapped
+         * @param plan    the planned route for this connection
+         */
+        protected ConnAdapter(PoolEntry entry, HttpRoute plan) {
+            super(SingleClientConnManager.this, entry);
+            super.markedReusable = true;
+            entry.plannedRoute = plan;
         }
-
-    } // class ConnAdapter
+    }
 
 
 } // class SingleClientConnManager

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/ThreadSafeClientConnManager.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/ThreadSafeClientConnManager.java?view=diff&rev=511477&r1=511476&r2=511477
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/ThreadSafeClientConnManager.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/conn/ThreadSafeClientConnManager.java Sun Feb 25 02:41:55 2007
@@ -1146,7 +1146,7 @@
      * can be {@link #detach detach}ed to prevent further use on release.
      */
     private class HttpConnectionAdapter
-        extends AbstractClientConnectionAdapter {
+        extends AbstractClientConnAdapter {
 
         /** The wrapped pool entry. */
         private TrackingPoolEntry poolEntry;