|
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 ch.boye.httpclientandroidlib.impl.conn; |
|
28 |
|
29 import java.io.IOException; |
|
30 |
|
31 import ch.boye.httpclientandroidlib.HttpHost; |
|
32 import ch.boye.httpclientandroidlib.params.HttpParams; |
|
33 import ch.boye.httpclientandroidlib.protocol.HttpContext; |
|
34 import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; |
|
35 import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; |
|
36 import ch.boye.httpclientandroidlib.conn.OperatedClientConnection; |
|
37 |
|
38 /** |
|
39 * Abstract adapter from pool {@link AbstractPoolEntry entries} to |
|
40 * {@link ch.boye.httpclientandroidlib.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 public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapter { |
|
51 |
|
52 /** The wrapped pool entry. */ |
|
53 protected volatile AbstractPoolEntry poolEntry; |
|
54 |
|
55 /** |
|
56 * Creates a new connection adapter. |
|
57 * |
|
58 * @param manager the connection manager |
|
59 * @param entry the pool entry for the connection being wrapped |
|
60 */ |
|
61 protected AbstractPooledConnAdapter(ClientConnectionManager manager, |
|
62 AbstractPoolEntry entry) { |
|
63 super(manager, entry.connection); |
|
64 this.poolEntry = entry; |
|
65 } |
|
66 |
|
67 /** |
|
68 * Obtains the pool entry. |
|
69 * |
|
70 * @return the pool entry, or <code>null</code> if detached |
|
71 */ |
|
72 protected AbstractPoolEntry getPoolEntry() { |
|
73 return this.poolEntry; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Asserts that there is a valid pool entry. |
|
78 * |
|
79 * @throws ConnectionShutdownException if there is no pool entry |
|
80 * or connection has been aborted |
|
81 * |
|
82 * @see #assertValid(OperatedClientConnection) |
|
83 */ |
|
84 protected void assertValid(final AbstractPoolEntry entry) { |
|
85 if (isReleased() || entry == null) { |
|
86 throw new ConnectionShutdownException(); |
|
87 } |
|
88 } |
|
89 |
|
90 /** |
|
91 * @deprecated use {@link #assertValid(AbstractPoolEntry)} |
|
92 */ |
|
93 @Deprecated |
|
94 protected final void assertAttached() { |
|
95 if (poolEntry == null) { |
|
96 throw new ConnectionShutdownException(); |
|
97 } |
|
98 } |
|
99 |
|
100 /** |
|
101 * Detaches this adapter from the wrapped connection. |
|
102 * This adapter becomes useless. |
|
103 */ |
|
104 @Override |
|
105 protected synchronized void detach() { |
|
106 poolEntry = null; |
|
107 super.detach(); |
|
108 } |
|
109 |
|
110 public HttpRoute getRoute() { |
|
111 AbstractPoolEntry entry = getPoolEntry(); |
|
112 assertValid(entry); |
|
113 return (entry.tracker == null) ? null : entry.tracker.toRoute(); |
|
114 } |
|
115 |
|
116 public void open(HttpRoute route, |
|
117 HttpContext context, HttpParams params) |
|
118 throws IOException { |
|
119 AbstractPoolEntry entry = getPoolEntry(); |
|
120 assertValid(entry); |
|
121 entry.open(route, context, params); |
|
122 } |
|
123 |
|
124 public void tunnelTarget(boolean secure, HttpParams params) |
|
125 throws IOException { |
|
126 AbstractPoolEntry entry = getPoolEntry(); |
|
127 assertValid(entry); |
|
128 entry.tunnelTarget(secure, params); |
|
129 } |
|
130 |
|
131 public void tunnelProxy(HttpHost next, boolean secure, HttpParams params) |
|
132 throws IOException { |
|
133 AbstractPoolEntry entry = getPoolEntry(); |
|
134 assertValid(entry); |
|
135 entry.tunnelProxy(next, secure, params); |
|
136 } |
|
137 |
|
138 public void layerProtocol(HttpContext context, HttpParams params) |
|
139 throws IOException { |
|
140 AbstractPoolEntry entry = getPoolEntry(); |
|
141 assertValid(entry); |
|
142 entry.layerProtocol(context, params); |
|
143 } |
|
144 |
|
145 public void close() throws IOException { |
|
146 AbstractPoolEntry entry = getPoolEntry(); |
|
147 if (entry != null) |
|
148 entry.shutdownEntry(); |
|
149 |
|
150 OperatedClientConnection conn = getWrappedConnection(); |
|
151 if (conn != null) { |
|
152 conn.close(); |
|
153 } |
|
154 } |
|
155 |
|
156 public void shutdown() throws IOException { |
|
157 AbstractPoolEntry entry = getPoolEntry(); |
|
158 if (entry != null) |
|
159 entry.shutdownEntry(); |
|
160 |
|
161 OperatedClientConnection conn = getWrappedConnection(); |
|
162 if (conn != null) { |
|
163 conn.shutdown(); |
|
164 } |
|
165 } |
|
166 |
|
167 public Object getState() { |
|
168 AbstractPoolEntry entry = getPoolEntry(); |
|
169 assertValid(entry); |
|
170 return entry.getState(); |
|
171 } |
|
172 |
|
173 public void setState(final Object state) { |
|
174 AbstractPoolEntry entry = getPoolEntry(); |
|
175 assertValid(entry); |
|
176 entry.setState(state); |
|
177 } |
|
178 |
|
179 } |