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.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.http.annotation.GuardedBy;
36  import org.apache.http.annotation.ThreadSafe;
37  import org.apache.http.conn.ClientConnectionManager;
38  import org.apache.http.conn.ClientConnectionOperator;
39  import org.apache.http.conn.ClientConnectionRequest;
40  import org.apache.http.conn.ManagedClientConnection;
41  import org.apache.http.conn.routing.HttpRoute;
42  import org.apache.http.conn.routing.RouteTracker;
43  import org.apache.http.conn.scheme.SchemeRegistry;
44  import org.apache.http.params.HttpParams;
45  import org.apache.http.util.Args;
46  import org.apache.http.util.Asserts;
47  
48  /**
49   * A connection manager for a single connection. This connection manager
50   * maintains only one active connection at a time. Even though this class
51   * is thread-safe it ought to be used by one execution thread only.
52   * <p>
53   * SingleClientConnManager will make an effort to reuse the connection
54   * for subsequent requests with the same {@link HttpRoute route}.
55   * It will, however, close the existing connection and open it
56   * for the given route, if the route of the persistent connection does
57   * not match that of the connection request. If the connection has been
58   * already been allocated {@link IllegalStateException} is thrown.
59   *
60   * @since 4.0
61   *
62   * @deprecated (4.2)  use {@link BasicClientConnectionManager}
63   */
64  @ThreadSafe
65  @Deprecated
66  public class SingleClientConnManager implements ClientConnectionManager {
67  
68      private final Log log = LogFactory.getLog(getClass());
69  
70      /** The message to be logged on multiple allocation. */
71      public final static String MISUSE_MESSAGE =
72      "Invalid use of SingleClientConnManager: connection still allocated.\n" +
73      "Make sure to release the connection before allocating another one.";
74  
75      /** The schemes supported by this connection manager. */
76      protected final SchemeRegistry schemeRegistry;
77  
78      /** The operator for opening and updating connections. */
79      protected final ClientConnectionOperator connOperator;
80  
81      /** Whether the connection should be shut down  on release. */
82      protected final boolean alwaysShutDown;
83  
84      /** The one and only entry in this pool. */
85      @GuardedBy("this")
86      protected volatile PoolEntry uniquePoolEntry;
87  
88      /** The currently issued managed connection, if any. */
89      @GuardedBy("this")
90      protected volatile ConnAdapter managedConn;
91  
92      /** The time of the last connection release, or -1. */
93      @GuardedBy("this")
94      protected volatile long lastReleaseTime;
95  
96      /** The time the last released connection expires and shouldn't be reused. */
97      @GuardedBy("this")
98      protected volatile long connectionExpiresTime;
99  
100     /** Indicates whether this connection manager is shut down. */
101     protected volatile boolean isShutDown;
102 
103     /**
104      * Creates a new simple connection manager.
105      *
106      * @param params    the parameters for this manager
107      * @param schreg    the scheme registry
108      *
109      * @deprecated (4.1)  use {@link SingleClientConnManager#SingleClientConnManager(SchemeRegistry)}
110      */
111     @Deprecated
112     public SingleClientConnManager(final HttpParams params,
113                                    final SchemeRegistry schreg) {
114         this(schreg);
115     }
116     /**
117      * Creates a new simple connection manager.
118      *
119      * @param schreg    the scheme registry
120      */
121     public SingleClientConnManager(final SchemeRegistry schreg) {
122         Args.notNull(schreg, "Scheme registry");
123         this.schemeRegistry  = schreg;
124         this.connOperator    = createConnectionOperator(schreg);
125         this.uniquePoolEntry = new PoolEntry();
126         this.managedConn     = null;
127         this.lastReleaseTime = -1L;
128         this.alwaysShutDown  = false; //@@@ from params? as argument?
129         this.isShutDown      = false;
130     }
131 
132     /**
133      * @since 4.1
134      */
135     public SingleClientConnManager() {
136         this(SchemeRegistryFactory.createDefault());
137     }
138 
139     @Override
140     protected void finalize() throws Throwable {
141         try {
142             shutdown();
143         } finally { // Make sure we call overridden method even if shutdown barfs
144             super.finalize();
145         }
146     }
147 
148     @Override
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}
162      *
163      * @return  the connection operator to use
164      */
165     protected ClientConnectionOperator
166         createConnectionOperator(final 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         Asserts.check(!this.isShutDown, "Manager is shut down");
177     }
178 
179     @Override
180     public final ClientConnectionRequest requestConnection(
181             final HttpRoute route,
182             final Object state) {
183 
184         return new ClientConnectionRequest() {
185 
186             @Override
187             public void abortRequest() {
188                 // Nothing to abort, since requests are immediate.
189             }
190 
191             @Override
192             public ManagedClientConnection getConnection(
193                     final long timeout, final TimeUnit tunit) {
194                 return SingleClientConnManager.this.getConnection(
195                         route, state);
196             }
197 
198         };
199     }
200 
201     /**
202      * Obtains a connection.
203      *
204      * @param route     where the connection should point to
205      *
206      * @return  a connection that can be used to communicate
207      *          along the given route
208      */
209     public ManagedClientConnection getConnection(final HttpRoute route, final Object state) {
210         Args.notNull(route, "Route");
211         assertStillUp();
212 
213         if (log.isDebugEnabled()) {
214             log.debug("Get connection for route " + route);
215         }
216 
217         synchronized (this) {
218 
219             Asserts.check(managedConn == null, 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                 final 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 (final IOException iox) {
246                     log.debug("Problem shutting down connection.", iox);
247                 }
248             }
249 
250             if (recreate) {
251                 uniquePoolEntry = new PoolEntry();
252             }
253 
254             managedConn = new ConnAdapter(uniquePoolEntry, route);
255 
256             return managedConn;
257         }
258     }
259 
260     @Override
261     public void releaseConnection(
262             final ManagedClientConnection conn,
263             final long validDuration, final TimeUnit timeUnit) {
264         Args.check(conn instanceof ConnAdapter, "Connection class mismatch, " +
265             "connection not obtained from this manager");
266         assertStillUp();
267 
268         if (log.isDebugEnabled()) {
269             log.debug("Releasing connection " + conn);
270         }
271 
272         final ConnAdapter sca = (ConnAdapter) conn;
273         synchronized (sca) {
274             if (sca.poolEntry == null)
275              {
276                 return; // already released
277             }
278             final ClientConnectionManager manager = sca.getManager();
279             Asserts.check(manager == this, "Connection not obtained from this manager");
280             try {
281                 // make sure that the response has been read completely
282                 if (sca.isOpen() && (this.alwaysShutDown ||
283                                      !sca.isMarkedReusable())
284                     ) {
285                     if (log.isDebugEnabled()) {
286                         log.debug
287                             ("Released connection open but not reusable.");
288                     }
289 
290                     // make sure this connection will not be re-used
291                     // we might have gotten here because of a shutdown trigger
292                     // shutdown of the adapter also clears the tracked route
293                     sca.shutdown();
294                 }
295             } catch (final IOException iox) {
296                 if (log.isDebugEnabled()) {
297                     log.debug("Exception shutting down released connection.",
298                               iox);
299                 }
300             } finally {
301                 sca.detach();
302                 synchronized (this) {
303                     managedConn = null;
304                     lastReleaseTime = System.currentTimeMillis();
305                     if(validDuration > 0) {
306                         connectionExpiresTime = timeUnit.toMillis(validDuration) + lastReleaseTime;
307                     } else {
308                         connectionExpiresTime = Long.MAX_VALUE;
309                     }
310                 }
311             }
312         }
313     }
314 
315     @Override
316     public void closeExpiredConnections() {
317         final long time = connectionExpiresTime;
318         if (System.currentTimeMillis() >= time) {
319             closeIdleConnections(0, TimeUnit.MILLISECONDS);
320         }
321     }
322 
323     @Override
324     public void closeIdleConnections(final long idletime, final TimeUnit tunit) {
325         assertStillUp();
326 
327         // idletime can be 0 or negative, no problem there
328         Args.notNull(tunit, "Time unit");
329 
330         synchronized (this) {
331             if ((managedConn == null) && uniquePoolEntry.connection.isOpen()) {
332                 final long cutoff =
333                     System.currentTimeMillis() - tunit.toMillis(idletime);
334                 if (lastReleaseTime <= cutoff) {
335                     try {
336                         uniquePoolEntry.close();
337                     } catch (final IOException iox) {
338                         // ignore
339                         log.debug("Problem closing idle connection.", iox);
340                     }
341                 }
342             }
343         }
344     }
345 
346     @Override
347     public void shutdown() {
348         this.isShutDown = true;
349         synchronized (this) {
350             try {
351                 if (uniquePoolEntry != null) {
352                     uniquePoolEntry.shutdown();
353                 }
354             } catch (final IOException iox) {
355                 // ignore
356                 log.debug("Problem while shutting down manager.", iox);
357             } finally {
358                 uniquePoolEntry = null;
359                 managedConn = null;
360             }
361         }
362     }
363 
364     protected void revokeConnection() {
365         final ConnAdapter conn = managedConn;
366         if (conn == null) {
367             return;
368         }
369         conn.detach();
370 
371         synchronized (this) {
372             try {
373                 uniquePoolEntry.shutdown();
374             } catch (final IOException iox) {
375                 // ignore
376                 log.debug("Problem while shutting down connection.", iox);
377             }
378         }
379     }
380 
381     /**
382      * The pool entry for this connection manager.
383      */
384     protected class PoolEntry extends AbstractPoolEntry {
385 
386         /**
387          * Creates a new pool entry.
388          *
389          */
390         protected PoolEntry() {
391             super(SingleClientConnManager.this.connOperator, null);
392         }
393 
394         /**
395          * Closes the connection in this pool entry.
396          */
397         protected void close() throws IOException {
398             shutdownEntry();
399             if (connection.isOpen()) {
400                 connection.close();
401             }
402         }
403 
404         /**
405          * Shuts down the connection in this pool entry.
406          */
407         protected void shutdown() throws IOException {
408             shutdownEntry();
409             if (connection.isOpen()) {
410                 connection.shutdown();
411             }
412         }
413 
414     }
415 
416     /**
417      * The connection adapter used by this manager.
418      */
419     protected class ConnAdapter extends AbstractPooledConnAdapter {
420 
421         /**
422          * Creates a new connection adapter.
423          *
424          * @param entry   the pool entry for the connection being wrapped
425          * @param route   the planned route for this connection
426          */
427         protected ConnAdapter(final PoolEntry entry, final HttpRoute route) {
428             super(SingleClientConnManager.this, entry);
429             markReusable();
430             entry.route = route;
431         }
432 
433     }
434 
435 }