1 /*
2 * ====================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * 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, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ====================================================================
19 *
20 * This software consists of voluntary contributions made by many
21 * individuals on behalf of the Apache Software Foundation. For more
22 * information on the Apache Software Foundation, please see
23 * <http://www.apache.org/>.
24 *
25 */
26
27 package org.apache.http.impl.conn;
28
29 import java.io.IOException;
30 import java.io.InterruptedIOException;
31
32 import org.apache.http.HttpHost;
33 import org.apache.http.params.HttpParams;
34 import org.apache.http.protocol.HttpContext;
35 import org.apache.http.conn.routing.HttpRoute;
36 import org.apache.http.conn.routing.RouteTracker;
37 import org.apache.http.conn.ClientConnectionOperator;
38 import org.apache.http.conn.OperatedClientConnection;
39
40 /**
41 * A pool entry for use by connection manager implementations.
42 * Pool entries work in conjunction with an
43 * {@link AbstractClientConnAdapter adapter}.
44 * The adapter is handed out to applications that obtain a connection.
45 * The pool entry stores the underlying connection and tracks the
46 * {@link HttpRoute route} established.
47 * The adapter delegates methods for establishing the route to
48 * its pool entry.
49 * <p>
50 * If the managed connections is released or revoked, the adapter
51 * gets disconnected, but the pool entry still contains the
52 * underlying connection and the established route.
53 *
54 * @since 4.0
55 *
56 * @deprecated (4.2) do not use
57 */
58 @Deprecated
59 public abstract class AbstractPoolEntry {
60
61 /** The connection operator. */
62 protected final ClientConnectionOperator connOperator;
63
64 /** The underlying connection being pooled or used. */
65 protected final OperatedClientConnection connection;
66
67 /** The route for which this entry gets allocated. */
68 //@@@ currently accessed from connection manager(s) as attribute
69 //@@@ avoid that, derived classes should decide whether update is allowed
70 //@@@ SCCM: yes, TSCCM: no
71 protected volatile HttpRoute route;
72
73 /** Connection state object */
74 protected volatile Object state;
75
76 /** The tracked route, or <code>null</code> before tracking starts. */
77 protected volatile RouteTracker tracker;
78
79
80 /**
81 * Creates a new pool entry.
82 *
83 * @param connOperator the Connection Operator for this entry
84 * @param route the planned route for the connection,
85 * or <code>null</code>
86 */
87 protected AbstractPoolEntry(ClientConnectionOperator connOperator,
88 HttpRoute route) {
89 super();
90 if (connOperator == null) {
91 throw new IllegalArgumentException("Connection operator may not be null");
92 }
93 this.connOperator = connOperator;
94 this.connection = connOperator.createConnection();
95 this.route = route;
96 this.tracker = null;
97 }
98
99 /**
100 * Returns the state object associated with this pool entry.
101 *
102 * @return The state object
103 */
104 public Object getState() {
105 return state;
106 }
107
108 /**
109 * Assigns a state object to this pool entry.
110 *
111 * @param state The state object
112 */
113 public void setState(final Object state) {
114 this.state = state;
115 }
116
117 /**
118 * Opens the underlying connection.
119 *
120 * @param route the route along which to open the connection
121 * @param context the context for opening the connection
122 * @param params the parameters for opening the connection
123 *
124 * @throws IOException in case of a problem
125 */
126 public void open(HttpRoute route,
127 HttpContext context, HttpParams params)
128 throws IOException {
129
130 if (route == null) {
131 throw new IllegalArgumentException
132 ("Route must not be null.");
133 }
134 if (params == null) {
135 throw new IllegalArgumentException
136 ("Parameters must not be null.");
137 }
138 if ((this.tracker != null) && this.tracker.isConnected()) {
139 throw new IllegalStateException("Connection already open.");
140 }
141
142 // - collect the arguments
143 // - call the operator
144 // - update the tracking data
145 // In this order, we can be sure that only a successful
146 // opening of the connection will be tracked.
147
148 this.tracker = new RouteTracker(route);
149 final HttpHost proxy = route.getProxyHost();
150
151 connOperator.openConnection
152 (this.connection,
153 (proxy != null) ? proxy : route.getTargetHost(),
154 route.getLocalAddress(),
155 context, params);
156
157 RouteTracker localTracker = tracker; // capture volatile
158
159 // If this tracker was reset while connecting,
160 // fail early.
161 if (localTracker == null) {
162 throw new InterruptedIOException("Request aborted");
163 }
164
165 if (proxy == null) {
166 localTracker.connectTarget(this.connection.isSecure());
167 } else {
168 localTracker.connectProxy(proxy, this.connection.isSecure());
169 }
170
171 }
172
173 /**
174 * Tracks tunnelling of the connection to the target.
175 * The tunnel has to be established outside by sending a CONNECT
176 * request to the (last) proxy.
177 *
178 * @param secure <code>true</code> if the tunnel should be
179 * considered secure, <code>false</code> otherwise
180 * @param params the parameters for tunnelling the connection
181 *
182 * @throws IOException in case of a problem
183 */
184 public void tunnelTarget(boolean secure, HttpParams params)
185 throws IOException {
186
187 if (params == null) {
188 throw new IllegalArgumentException
189 ("Parameters must not be null.");
190 }
191
192 if ((this.tracker == null) || !this.tracker.isConnected()) {
193 throw new IllegalStateException("Connection not open.");
194 }
195 if (this.tracker.isTunnelled()) {
196 throw new IllegalStateException
197 ("Connection is already tunnelled.");
198 }
199
200 this.connection.update(null, tracker.getTargetHost(),
201 secure, params);
202 this.tracker.tunnelTarget(secure);
203 }
204
205 /**
206 * Tracks tunnelling of the connection to a chained proxy.
207 * The tunnel has to be established outside by sending a CONNECT
208 * request to the previous proxy.
209 *
210 * @param next the proxy to which the tunnel was established.
211 * See {@link org.apache.http.conn.ManagedClientConnection#tunnelProxy
212 * ManagedClientConnection.tunnelProxy}
213 * for details.
214 * @param secure <code>true</code> if the tunnel should be
215 * considered secure, <code>false</code> otherwise
216 * @param params the parameters for tunnelling the connection
217 *
218 * @throws IOException in case of a problem
219 */
220 public void tunnelProxy(HttpHost next, boolean secure, HttpParams params)
221 throws IOException {
222
223 if (next == null) {
224 throw new IllegalArgumentException
225 ("Next proxy must not be null.");
226 }
227 if (params == null) {
228 throw new IllegalArgumentException
229 ("Parameters must not be null.");
230 }
231
232 //@@@ check for proxy in planned route?
233 if ((this.tracker == null) || !this.tracker.isConnected()) {
234 throw new IllegalStateException("Connection not open.");
235 }
236
237 this.connection.update(null, next, secure, params);
238 this.tracker.tunnelProxy(next, secure);
239 }
240
241 /**
242 * Layers a protocol on top of an established tunnel.
243 *
244 * @param context the context for layering
245 * @param params the parameters for layering
246 *
247 * @throws IOException in case of a problem
248 */
249 public void layerProtocol(HttpContext context, HttpParams params)
250 throws IOException {
251
252 //@@@ is context allowed to be null? depends on operator?
253 if (params == null) {
254 throw new IllegalArgumentException
255 ("Parameters must not be null.");
256 }
257
258 if ((this.tracker == null) || !this.tracker.isConnected()) {
259 throw new IllegalStateException("Connection not open.");
260 }
261 if (!this.tracker.isTunnelled()) {
262 //@@@ allow this?
263 throw new IllegalStateException
264 ("Protocol layering without a tunnel not supported.");
265 }
266 if (this.tracker.isLayered()) {
267 throw new IllegalStateException
268 ("Multiple protocol layering not supported.");
269 }
270
271 // - collect the arguments
272 // - call the operator
273 // - update the tracking data
274 // In this order, we can be sure that only a successful
275 // layering on top of the connection will be tracked.
276
277 final HttpHost target = tracker.getTargetHost();
278
279 connOperator.updateSecureConnection(this.connection, target,
280 context, params);
281
282 this.tracker.layerProtocol(this.connection.isSecure());
283
284 }
285
286 /**
287 * Shuts down the entry.
288 *
289 * If {@link #open(HttpRoute, HttpContext, HttpParams)} is in progress,
290 * this will cause that open to possibly throw an {@link IOException}.
291 */
292 protected void shutdownEntry() {
293 tracker = null;
294 state = null;
295 }
296
297 }
298