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.execchain;
29  
30  import java.io.Closeable;
31  import java.io.IOException;
32  import java.util.concurrent.TimeUnit;
33  import java.util.concurrent.atomic.AtomicBoolean;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.http.HttpClientConnection;
37  import org.apache.http.annotation.ThreadSafe;
38  import org.apache.http.concurrent.Cancellable;
39  import org.apache.http.conn.ConnectionReleaseTrigger;
40  import org.apache.http.conn.HttpClientConnectionManager;
41  
42  /**
43   * Internal connection holder.
44   *
45   * @since 4.3
46   */
47  @ThreadSafe
48  class ConnectionHolder implements ConnectionReleaseTrigger, Cancellable, Closeable {
49  
50      private final Log log;
51  
52      private final HttpClientConnectionManager manager;
53      private final HttpClientConnection managedConn;
54      private final AtomicBoolean released;
55      private volatile boolean reusable;
56      private volatile Object state;
57      private volatile long validDuration;
58      private volatile TimeUnit tunit;
59  
60      public ConnectionHolder(
61              final Log log,
62              final HttpClientConnectionManager manager,
63              final HttpClientConnection managedConn) {
64          super();
65          this.log = log;
66          this.manager = manager;
67          this.managedConn = managedConn;
68          this.released = new AtomicBoolean(false);
69      }
70  
71      public boolean isReusable() {
72          return this.reusable;
73      }
74  
75      public void markReusable() {
76          this.reusable = true;
77      }
78  
79      public void markNonReusable() {
80          this.reusable = false;
81      }
82  
83      public void setState(final Object state) {
84          this.state = state;
85      }
86  
87      public void setValidFor(final long duration, final TimeUnit tunit) {
88          synchronized (this.managedConn) {
89              this.validDuration = duration;
90              this.tunit = tunit;
91          }
92      }
93  
94      private void releaseConnection(final boolean reusable) {
95          if (this.released.compareAndSet(false, true)) {
96              synchronized (this.managedConn) {
97                  if (reusable) {
98                      this.manager.releaseConnection(this.managedConn,
99                              this.state, this.validDuration, this.tunit);
100                 } else {
101                     try {
102                         this.managedConn.close();
103                         log.debug("Connection discarded");
104                     } catch (final IOException ex) {
105                         if (this.log.isDebugEnabled()) {
106                             this.log.debug(ex.getMessage(), ex);
107                         }
108                     } finally {
109                         this.manager.releaseConnection(
110                                 this.managedConn, null, 0, TimeUnit.MILLISECONDS);
111                     }
112                 }
113             }
114         }
115     }
116 
117     @Override
118     public void releaseConnection() {
119         releaseConnection(this.reusable);
120     }
121 
122     @Override
123     public void abortConnection() {
124         if (this.released.compareAndSet(false, true)) {
125             synchronized (this.managedConn) {
126                 try {
127                     this.managedConn.shutdown();
128                     log.debug("Connection discarded");
129                 } catch (final IOException ex) {
130                     if (this.log.isDebugEnabled()) {
131                         this.log.debug(ex.getMessage(), ex);
132                     }
133                 } finally {
134                     this.manager.releaseConnection(
135                             this.managedConn, null, 0, TimeUnit.MILLISECONDS);
136                 }
137             }
138         }
139     }
140 
141     @Override
142     public boolean cancel() {
143         final boolean alreadyReleased = this.released.get();
144         log.debug("Cancelling request execution");
145         abortConnection();
146         return !alreadyReleased;
147     }
148 
149     public boolean isReleased() {
150         return this.released.get();
151     }
152 
153     @Override
154     public void close() throws IOException {
155         releaseConnection(false);
156     }
157 
158 }