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