Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed to the Apache Software Foundation (ASF) under one or more |
michael@0 | 5 | * contributor license agreements. See the NOTICE file distributed with |
michael@0 | 6 | * this work for additional information regarding copyright ownership. |
michael@0 | 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
michael@0 | 8 | * (the "License"); you may not use this file except in compliance with |
michael@0 | 9 | * the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 16 | * See the License for the specific language governing permissions and |
michael@0 | 17 | * limitations under the License. |
michael@0 | 18 | * ==================================================================== |
michael@0 | 19 | * |
michael@0 | 20 | * This software consists of voluntary contributions made by many |
michael@0 | 21 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 22 | * information on the Apache Software Foundation, please see |
michael@0 | 23 | * <http://www.apache.org/>. |
michael@0 | 24 | * |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | package ch.boye.httpclientandroidlib.impl.conn; |
michael@0 | 28 | |
michael@0 | 29 | import java.io.IOException; |
michael@0 | 30 | |
michael@0 | 31 | import ch.boye.httpclientandroidlib.HttpHost; |
michael@0 | 32 | import ch.boye.httpclientandroidlib.params.HttpParams; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.protocol.HttpContext; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.conn.OperatedClientConnection; |
michael@0 | 37 | |
michael@0 | 38 | /** |
michael@0 | 39 | * Abstract adapter from pool {@link AbstractPoolEntry entries} to |
michael@0 | 40 | * {@link ch.boye.httpclientandroidlib.conn.ManagedClientConnection managed} |
michael@0 | 41 | * client connections. |
michael@0 | 42 | * The connection in the pool entry is used to initialize the base class. |
michael@0 | 43 | * In addition, methods to establish a route are delegated to the |
michael@0 | 44 | * pool entry. {@link #shutdown shutdown} and {@link #close close} |
michael@0 | 45 | * will clear the tracked route in the pool entry and call the |
michael@0 | 46 | * respective method of the wrapped connection. |
michael@0 | 47 | * |
michael@0 | 48 | * @since 4.0 |
michael@0 | 49 | */ |
michael@0 | 50 | public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapter { |
michael@0 | 51 | |
michael@0 | 52 | /** The wrapped pool entry. */ |
michael@0 | 53 | protected volatile AbstractPoolEntry poolEntry; |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * Creates a new connection adapter. |
michael@0 | 57 | * |
michael@0 | 58 | * @param manager the connection manager |
michael@0 | 59 | * @param entry the pool entry for the connection being wrapped |
michael@0 | 60 | */ |
michael@0 | 61 | protected AbstractPooledConnAdapter(ClientConnectionManager manager, |
michael@0 | 62 | AbstractPoolEntry entry) { |
michael@0 | 63 | super(manager, entry.connection); |
michael@0 | 64 | this.poolEntry = entry; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | /** |
michael@0 | 68 | * Obtains the pool entry. |
michael@0 | 69 | * |
michael@0 | 70 | * @return the pool entry, or <code>null</code> if detached |
michael@0 | 71 | */ |
michael@0 | 72 | protected AbstractPoolEntry getPoolEntry() { |
michael@0 | 73 | return this.poolEntry; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * Asserts that there is a valid pool entry. |
michael@0 | 78 | * |
michael@0 | 79 | * @throws ConnectionShutdownException if there is no pool entry |
michael@0 | 80 | * or connection has been aborted |
michael@0 | 81 | * |
michael@0 | 82 | * @see #assertValid(OperatedClientConnection) |
michael@0 | 83 | */ |
michael@0 | 84 | protected void assertValid(final AbstractPoolEntry entry) { |
michael@0 | 85 | if (isReleased() || entry == null) { |
michael@0 | 86 | throw new ConnectionShutdownException(); |
michael@0 | 87 | } |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * @deprecated use {@link #assertValid(AbstractPoolEntry)} |
michael@0 | 92 | */ |
michael@0 | 93 | @Deprecated |
michael@0 | 94 | protected final void assertAttached() { |
michael@0 | 95 | if (poolEntry == null) { |
michael@0 | 96 | throw new ConnectionShutdownException(); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | /** |
michael@0 | 101 | * Detaches this adapter from the wrapped connection. |
michael@0 | 102 | * This adapter becomes useless. |
michael@0 | 103 | */ |
michael@0 | 104 | @Override |
michael@0 | 105 | protected synchronized void detach() { |
michael@0 | 106 | poolEntry = null; |
michael@0 | 107 | super.detach(); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | public HttpRoute getRoute() { |
michael@0 | 111 | AbstractPoolEntry entry = getPoolEntry(); |
michael@0 | 112 | assertValid(entry); |
michael@0 | 113 | return (entry.tracker == null) ? null : entry.tracker.toRoute(); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | public void open(HttpRoute route, |
michael@0 | 117 | HttpContext context, HttpParams params) |
michael@0 | 118 | throws IOException { |
michael@0 | 119 | AbstractPoolEntry entry = getPoolEntry(); |
michael@0 | 120 | assertValid(entry); |
michael@0 | 121 | entry.open(route, context, params); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | public void tunnelTarget(boolean secure, HttpParams params) |
michael@0 | 125 | throws IOException { |
michael@0 | 126 | AbstractPoolEntry entry = getPoolEntry(); |
michael@0 | 127 | assertValid(entry); |
michael@0 | 128 | entry.tunnelTarget(secure, params); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | public void tunnelProxy(HttpHost next, boolean secure, HttpParams params) |
michael@0 | 132 | throws IOException { |
michael@0 | 133 | AbstractPoolEntry entry = getPoolEntry(); |
michael@0 | 134 | assertValid(entry); |
michael@0 | 135 | entry.tunnelProxy(next, secure, params); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | public void layerProtocol(HttpContext context, HttpParams params) |
michael@0 | 139 | throws IOException { |
michael@0 | 140 | AbstractPoolEntry entry = getPoolEntry(); |
michael@0 | 141 | assertValid(entry); |
michael@0 | 142 | entry.layerProtocol(context, params); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | public void close() throws IOException { |
michael@0 | 146 | AbstractPoolEntry entry = getPoolEntry(); |
michael@0 | 147 | if (entry != null) |
michael@0 | 148 | entry.shutdownEntry(); |
michael@0 | 149 | |
michael@0 | 150 | OperatedClientConnection conn = getWrappedConnection(); |
michael@0 | 151 | if (conn != null) { |
michael@0 | 152 | conn.close(); |
michael@0 | 153 | } |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | public void shutdown() throws IOException { |
michael@0 | 157 | AbstractPoolEntry entry = getPoolEntry(); |
michael@0 | 158 | if (entry != null) |
michael@0 | 159 | entry.shutdownEntry(); |
michael@0 | 160 | |
michael@0 | 161 | OperatedClientConnection conn = getWrappedConnection(); |
michael@0 | 162 | if (conn != null) { |
michael@0 | 163 | conn.shutdown(); |
michael@0 | 164 | } |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | public Object getState() { |
michael@0 | 168 | AbstractPoolEntry entry = getPoolEntry(); |
michael@0 | 169 | assertValid(entry); |
michael@0 | 170 | return entry.getState(); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | public void setState(final Object state) { |
michael@0 | 174 | AbstractPoolEntry entry = getPoolEntry(); |
michael@0 | 175 | assertValid(entry); |
michael@0 | 176 | entry.setState(state); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | } |