View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.conn;
29  
30  import java.io.IOException;
31  import java.util.concurrent.TimeUnit;
32  
33  import org.apache.http.annotation.GuardedBy;
34  import org.apache.http.annotation.ThreadSafe;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.apache.http.conn.ClientConnectionManager;
39  import org.apache.http.conn.ClientConnectionOperator;
40  import org.apache.http.conn.ClientConnectionRequest;
41  import org.apache.http.conn.ManagedClientConnection;
42  import org.apache.http.conn.routing.HttpRoute;
43  import org.apache.http.conn.routing.RouteTracker;
44  import org.apache.http.conn.scheme.SchemeRegistry;
45  import org.apache.http.params.HttpParams;
46  
47  /**
48   * A connection manager for a single connection. This connection manager
49   * maintains only one active connection at a time. Even though this class
50   * is thread-safe it ought to be used by one execution thread only.
51   * <p>
52   * SingleClientConnManager will make an effort to reuse the connection
53   * for subsequent requests with the same {@link HttpRoute route}.
54   * It will, however, close the existing connection and open it
55   * for the given route, if the route of the persistent connection does
56   * not match that of the connection request. If the connection has been
57   * already been allocated {@link IllegalStateException} is thrown.
58   *
59   * @since 4.0
60   *
61   * @deprecated (4.2)  use {@link BasicClientConnectionManager}
62   */
63  @ThreadSafe
64  @Deprecated 
65  public class SingleClientConnManager implements ClientConnectionManager {
66  
67      private final Log log = LogFactory.getLog(getClass());
68  
69      /** The message to be logged on multiple allocation. */
70      public final static String MISUSE_MESSAGE =
71      "Invalid use of SingleClientConnManager: connection still allocated.\n" +
72      "Make sure to release the connection before allocating another one.";
73  
74      /** The schemes supported by this connection manager. */
75      protected final SchemeRegistry schemeRegistry;
76  
77      /** The operator for opening and updating connections. */
78      protected final ClientConnectionOperator connOperator;
79  
80      /** Whether the connection should be shut down  on release. */
81      protected final boolean alwaysShutDown;
82  
83      /** The one and only entry in this pool. */
84      @GuardedBy("this")
85      protected volatile PoolEntry uniquePoolEntry;
86  
87      /** The currently issued managed connection, if any. */
88      @GuardedBy("this")
89      protected volatile ConnAdapter managedConn;
90  
91      /** The time of the last connection release, or -1. */
92      @GuardedBy("this")
93      protected volatile long lastReleaseTime;
94  
95      /** The time the last released connection expires and shouldn't be reused. */
96      @GuardedBy("this")
97      protected volatile long connectionExpiresTime;
98  
99      /** Indicates whether this connection manager is shut down. */
100     protected volatile boolean isShutDown;
101 
102     /**
103      * Creates a new simple connection manager.
104      *
105      * @param params    the parameters for this manager
106      * @param schreg    the scheme registry
107      *
108      * @deprecated (4.1)  use {@link SingleClientConnManager#SingleClientConnManager(SchemeRegistry)}
109      */
110     public SingleClientConnManager(HttpParams params,
111                                    SchemeRegistry schreg) {
112         this(schreg);
113     }
114     /**
115      * Creates a new simple connection manager.
116      *
117      * @param schreg    the scheme registry
118      */
119     public SingleClientConnManager(final SchemeRegistry schreg) {
120         if (schreg == null) {
121             throw new IllegalArgumentException
122                 ("Scheme registry must not be null.");
123         }
124         this.schemeRegistry  = schreg;
125         this.connOperator    = createConnectionOperator(schreg);
126         this.uniquePoolEntry = new PoolEntry();
127         this.managedConn     = null;
128         this.lastReleaseTime = -1L;
129         this.alwaysShutDown  = false; //@@@ from params? as argument?
130         this.isShutDown      = false;
131     }
132 
133     /**
134      * @since 4.1
135      */
136     public SingleClientConnManager() {
137         this(SchemeRegistryFactory.createDefault());
138     }
139 
140     @Override
141     protected void finalize() throws Throwable {
142         try {
143             shutdown();
144         } finally { // Make sure we call overridden method even if shutdown barfs
145             super.finalize();
146         }
147     }
148 
149     public SchemeRegistry getSchemeRegistry() {
150         return this.schemeRegistry;
151     }
152 
153     /**
154      * Hook for creating the connection operator.
155      * It is called by the constructor.
156      * Derived classes can override this method to change the
157      * instantiation of the operator.
158      * The default implementation here instantiates
159      * {@link DefaultClientConnectionOperator DefaultClientConnectionOperator}.
160      *
161      * @param schreg    the scheme registry to use, or <code>null</code>
162      *
163      * @return  the connection operator to use
164      */
165     protected ClientConnectionOperator
166         createConnectionOperator(SchemeRegistry schreg) {
167         return new DefaultClientConnectionOperator(schreg);
168     }
169 
170     /**
171      * Asserts that this manager is not shut down.
172      *
173      * @throws IllegalStateException    if this manager is shut down
174      */
175     protected final void assertStillUp() throws IllegalStateException {
176         if (this.isShutDown)
177             throw new IllegalStateException("Manager is shut down.");
178     }
179 
180     public final ClientConnectionRequest requestConnection(
181             final HttpRoute route,
182             final Object state) {
183 
184         return new ClientConnectionRequest() {
185 
186             public void abortRequest() {
187                 // Nothing to abort, since requests are immediate.
188             }
189 
190             public ManagedClientConnection getConnection(
191                     long timeout, TimeUnit tunit) {
192                 return SingleClientConnManager.this.getConnection(
193                         route, state);
194             }
195 
196         };
197     }
198 
199     /**
200      * Obtains a connection.
201      *
202      * @param route     where the connection should point to
203      *
204      * @return  a connection that can be used to communicate
205      *          along the given route
206      */
207     public ManagedClientConnection getConnection(HttpRoute route, Object state) {
208         if (route == null) {
209             throw new IllegalArgumentException("Route may not be null.");
210         }
211         assertStillUp();
212 
213         if (log.isDebugEnabled()) {
214             log.debug("Get connection for route " + route);
215         }
216 
217         synchronized (this) {
218             if (managedConn != null)
219                 throw new IllegalStateException(MISUSE_MESSAGE);
220 
221             // check re-usability of the connection
222             boolean recreate = false;
223             boolean shutdown = false;
224 
225             // Kill the connection if it expired.
226             closeExpiredConnections();
227 
228             if (uniquePoolEntry.connection.isOpen()) {
229                 RouteTracker tracker = uniquePoolEntry.tracker;
230                 shutdown = (tracker == null || // can happen if method is aborted
231                             !tracker.toRoute().equals(route));
232             } else {
233                 // If the connection is not open, create a new PoolEntry,
234                 // as the connection may have been marked not reusable,
235                 // due to aborts -- and the PoolEntry should not be reused
236                 // either.  There's no harm in recreating an entry if
237                 // the connection is closed.
238                 recreate = true;
239             }
240 
241             if (shutdown) {
242                 recreate = true;
243                 try {
244                     uniquePoolEntry.shutdown();
245                 } catch (IOException iox) {
246                     log.debug("Problem shutting down connection.", iox);
247                 }
248             }
249 
250             if (recreate)
251                 uniquePoolEntry = new PoolEntry();
252 
253             managedConn = new ConnAdapter(uniquePoolEntry, route);
254 
255             return managedConn;
256         }
257     }
258 
259     public void releaseConnection(
260             ManagedClientConnection conn,
261             long validDuration, TimeUnit timeUnit) {
262         assertStillUp();
263 
264         if (!(conn instanceof ConnAdapter)) {
265             throw new IllegalArgumentException
266                 ("Connection class mismatch, " +
267                  "connection not obtained from this manager.");
268         }
269 
270         if (log.isDebugEnabled()) {
271             log.debug("Releasing connection " + conn);
272         }
273 
274         ConnAdapter sca = (ConnAdapter) conn;
275         synchronized (sca) {
276             if (sca.poolEntry == null)
277                 return; // already released
278             ClientConnectionManager manager = sca.getManager();
279             if (manager != null && manager != this) {
280                 throw new IllegalArgumentException
281                     ("Connection not obtained from this manager.");
282             }
283             try {
284                 // make sure that the response has been read completely
285                 if (sca.isOpen() && (this.alwaysShutDown ||
286                                      !sca.isMarkedReusable())
287                     ) {
288                     if (log.isDebugEnabled()) {
289                         log.debug
290                             ("Released connection open but not reusable.");
291                     }
292 
293                     // make sure this connection will not be re-used
294                     // we might have gotten here because of a shutdown trigger
295                     // shutdown of the adapter also clears the tracked route
296                     sca.shutdown();
297                 }
298             } catch (IOException iox) {
299                 if (log.isDebugEnabled())
300                     log.debug("Exception shutting down released connection.",
301                               iox);
302             } finally {
303                 sca.detach();
304                 synchronized (this) {
305                     managedConn = null;
306                     lastReleaseTime = System.currentTimeMillis();
307                     if(validDuration > 0)
308                         connectionExpiresTime = timeUnit.toMillis(validDuration) + lastReleaseTime;
309                     else
310                         connectionExpiresTime = Long.MAX_VALUE;
311                 }
312             }
313         }
314     }
315 
316     public void closeExpiredConnections() {
317         long time = connectionExpiresTime;
318         if (System.currentTimeMillis() >= time) {
319             closeIdleConnections(0, TimeUnit.MILLISECONDS);
320         }
321     }
322 
323     public void closeIdleConnections(long idletime, TimeUnit tunit) {
324         assertStillUp();
325 
326         // idletime can be 0 or negative, no problem there
327         if (tunit == null) {
328             throw new IllegalArgumentException("Time unit must not be null.");
329         }
330 
331         synchronized (this) {
332             if ((managedConn == null) && uniquePoolEntry.connection.isOpen()) {
333                 final long cutoff =
334                     System.currentTimeMillis() - tunit.toMillis(idletime);
335                 if (lastReleaseTime <= cutoff) {
336                     try {
337                         uniquePoolEntry.close();
338                     } catch (IOException iox) {
339                         // ignore
340                         log.debug("Problem closing idle connection.", iox);
341                     }
342                 }
343             }
344         }
345     }
346 
347     public void shutdown() {
348         this.isShutDown = true;
349         synchronized (this) {
350             try {
351                 if (uniquePoolEntry != null) // and connection open?
352                     uniquePoolEntry.shutdown();
353             } catch (IOException iox) {
354                 // ignore
355                 log.debug("Problem while shutting down manager.", iox);
356             } finally {
357                 uniquePoolEntry = null;
358                 managedConn = null;
359             }
360         }
361     }
362 
363     protected void revokeConnection() {
364         ConnAdapter conn = managedConn;
365         if (conn == null)
366             return;
367         conn.detach();
368 
369         synchronized (this) {
370             try {
371                 uniquePoolEntry.shutdown();
372             } catch (IOException iox) {
373                 // ignore
374                 log.debug("Problem while shutting down connection.", iox);
375             }
376         }
377     }
378 
379     /**
380      * The pool entry for this connection manager.
381      */
382     protected class PoolEntry extends AbstractPoolEntry {
383 
384         /**
385          * Creates a new pool entry.
386          *
387          */
388         protected PoolEntry() {
389             super(SingleClientConnManager.this.connOperator, null);
390         }
391 
392         /**
393          * Closes the connection in this pool entry.
394          */
395         protected void close() throws IOException {
396             shutdownEntry();
397             if (connection.isOpen())
398                 connection.close();
399         }
400 
401         /**
402          * Shuts down the connection in this pool entry.
403          */
404         protected void shutdown() throws IOException {
405             shutdownEntry();
406             if (connection.isOpen())
407                 connection.shutdown();
408         }
409 
410     }
411 
412     /**
413      * The connection adapter used by this manager.
414      */
415     protected class ConnAdapter extends AbstractPooledConnAdapter {
416 
417         /**
418          * Creates a new connection adapter.
419          *
420          * @param entry   the pool entry for the connection being wrapped
421          * @param route   the planned route for this connection
422          */
423         protected ConnAdapter(PoolEntry entry, HttpRoute route) {
424             super(SingleClientConnManager.this, entry);
425             markReusable();
426             entry.route = route;
427         }
428 
429     }
430 
431 }