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  package org.apache.http.impl.conn;
28  
29  import java.io.IOException;
30  
31  import org.apache.http.HttpHost;
32  import org.apache.http.conn.ClientConnectionManager;
33  import org.apache.http.conn.OperatedClientConnection;
34  import org.apache.http.conn.routing.HttpRoute;
35  import org.apache.http.params.HttpParams;
36  import org.apache.http.protocol.HttpContext;
37  
38  /**
39   * Abstract adapter from pool {@link AbstractPoolEntry entries} to
40   * {@link org.apache.http.conn.ManagedClientConnection managed}
41   * client connections.
42   * The connection in the pool entry is used to initialize the base class.
43   * In addition, methods to establish a route are delegated to the
44   * pool entry. {@link #shutdown shutdown} and {@link #close close}
45   * will clear the tracked route in the pool entry and call the
46   * respective method of the wrapped connection.
47   *
48   * @since 4.0
49   *
50   * @deprecated (4.2)  do not use
51   */
52  @Deprecated
53  public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapter {
54  
55      /** The wrapped pool entry. */
56      protected volatile AbstractPoolEntry poolEntry;
57  
58      /**
59       * Creates a new connection adapter.
60       *
61       * @param manager   the connection manager
62       * @param entry     the pool entry for the connection being wrapped
63       */
64      protected AbstractPooledConnAdapter(final ClientConnectionManager manager,
65                                          final AbstractPoolEntry entry) {
66          super(manager, entry.connection);
67          this.poolEntry = entry;
68      }
69  
70      @Override
71      public String getId() {
72          return null;
73      }
74  
75      /**
76       * Obtains the pool entry.
77       *
78       * @return  the pool entry, or {@code null} if detached
79       *
80       * @deprecated (4.0.1)
81       */
82      @Deprecated
83      protected AbstractPoolEntry getPoolEntry() {
84          return this.poolEntry;
85      }
86  
87      /**
88       * Asserts that there is a valid pool entry.
89       *
90       * @throws ConnectionShutdownException if there is no pool entry
91       *                                  or connection has been aborted
92       *
93       * @see #assertValid(OperatedClientConnection)
94       */
95      protected void assertValid(final AbstractPoolEntry entry) {
96          if (isReleased() || entry == null) {
97              throw new ConnectionShutdownException();
98          }
99      }
100 
101     /**
102      * @deprecated (4.1)  use {@link #assertValid(AbstractPoolEntry)}
103      */
104     @Deprecated
105     protected final void assertAttached() {
106         if (poolEntry == null) {
107             throw new ConnectionShutdownException();
108         }
109     }
110 
111     /**
112      * Detaches this adapter from the wrapped connection.
113      * This adapter becomes useless.
114      */
115     @Override
116     protected synchronized void detach() {
117         poolEntry = null;
118         super.detach();
119     }
120 
121     @Override
122     public HttpRoute getRoute() {
123         final AbstractPoolEntry entry = getPoolEntry();
124         assertValid(entry);
125         return (entry.tracker == null) ? null : entry.tracker.toRoute();
126     }
127 
128     @Override
129     public void open(final HttpRoute route,
130                      final HttpContext context, final HttpParams params)
131         throws IOException {
132         final AbstractPoolEntry entry = getPoolEntry();
133         assertValid(entry);
134         entry.open(route, context, params);
135     }
136 
137     @Override
138     public void tunnelTarget(final boolean secure, final HttpParams params)
139         throws IOException {
140         final AbstractPoolEntry entry = getPoolEntry();
141         assertValid(entry);
142         entry.tunnelTarget(secure, params);
143     }
144 
145     @Override
146     public void tunnelProxy(final HttpHost next, final boolean secure, final HttpParams params)
147         throws IOException {
148         final AbstractPoolEntry entry = getPoolEntry();
149         assertValid(entry);
150         entry.tunnelProxy(next, secure, params);
151     }
152 
153     @Override
154     public void layerProtocol(final HttpContext context, final HttpParams params)
155         throws IOException {
156         final AbstractPoolEntry entry = getPoolEntry();
157         assertValid(entry);
158         entry.layerProtocol(context, params);
159     }
160 
161     @Override
162     public void close() throws IOException {
163         final AbstractPoolEntry entry = getPoolEntry();
164         if (entry != null) {
165             entry.shutdownEntry();
166         }
167 
168         final OperatedClientConnection conn = getWrappedConnection();
169         if (conn != null) {
170             conn.close();
171         }
172     }
173 
174     @Override
175     public void shutdown() throws IOException {
176         final AbstractPoolEntry entry = getPoolEntry();
177         if (entry != null) {
178             entry.shutdownEntry();
179         }
180 
181         final OperatedClientConnection conn = getWrappedConnection();
182         if (conn != null) {
183             conn.shutdown();
184         }
185     }
186 
187     @Override
188     public Object getState() {
189         final AbstractPoolEntry entry = getPoolEntry();
190         assertValid(entry);
191         return entry.getState();
192     }
193 
194     @Override
195     public void setState(final Object state) {
196         final AbstractPoolEntry entry = getPoolEntry();
197         assertValid(entry);
198         entry.setState(state);
199     }
200 
201 }