View Javadoc

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