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