KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > webdav > WebdavConnectionManager


1 package org.apache.commons.vfs.provider.webdav;
2
3 import org.apache.commons.httpclient.HostConfiguration;
4 import org.apache.commons.httpclient.HttpConnection;
5 import org.apache.commons.httpclient.HttpConnectionManager;
6
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9
10 /**
11  * A connection manager that provides access to a single HttpConnection. This
12  * manager makes no attempt to provide exclusive access to the contained
13  * HttpConnection.
14  * <p/>
15  * imario@apache.org: Keep connection in ThreadLocal.
16  *
17  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
18  * @author <a HREF="mailto:becke@u.washington.edu">Michael Becke</a>
19  * @author Eric Johnson
20  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
21  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
22  * @author Laura Werner
23  * @since 2.0
24  */

25 public class WebdavConnectionManager implements HttpConnectionManager
26 {
27     private static class ConnectionParameters
28     {
29         private boolean staleCheck;
30
31         public boolean isStaleCheckingEnabled()
32         {
33             return staleCheck;
34         }
35
36         public void setStaleCheckingEnabled(boolean b)
37         {
38             staleCheck = b;
39         }
40
41         public void populateParameters(HttpConnection connection)
42         {
43             connection.setStaleCheckingEnabled(staleCheck);
44         }
45     }
46
47     /**
48      * Since the same connection is about to be reused, make sure the
49      * previous request was completely processed, and if not
50      * consume it now.
51      *
52      * @param conn The connection
53      */

54     static void finishLastResponse(HttpConnection conn)
55     {
56         InputStream JavaDoc lastResponse = conn.getLastResponseInputStream();
57         if (lastResponse != null)
58         {
59             conn.setLastResponseInputStream(null);
60             try
61             {
62                 lastResponse.close();
63             }
64             catch (IOException JavaDoc ioe)
65             {
66                 //FIXME: badness - close to force reconnect.
67
conn.close();
68             }
69         }
70     }
71
72     /**
73      * The thread data
74      */

75     protected ThreadLocal JavaDoc localHttpConnection = new ThreadLocal JavaDoc()
76     {
77         protected Object JavaDoc initialValue()
78         {
79             return new Entry();
80         }
81     };
82
83     /**
84      * Collection of parameters associated with this connection manager.
85      */

86     private ConnectionParameters params = new ConnectionParameters();
87
88     /**
89      * release the connection of the current thread
90      */

91     public void releaseLocalConnection()
92     {
93         if (getLocalHttpConnection() != null)
94         {
95             releaseConnection(getLocalHttpConnection());
96         }
97     }
98
99     private static class Entry
100     {
101         /**
102          * The http connection
103          */

104         private HttpConnection conn = null;
105
106         /**
107          * The time the connection was made idle.
108          */

109         private long idleStartTime = Long.MAX_VALUE;
110     }
111
112     public WebdavConnectionManager()
113     {
114     }
115
116     protected HttpConnection getLocalHttpConnection()
117     {
118         return ((Entry) localHttpConnection.get()).conn;
119     }
120
121     protected void setLocalHttpConnection(HttpConnection conn)
122     {
123         ((Entry) localHttpConnection.get()).conn = conn;
124     }
125
126     protected long getIdleStartTime()
127     {
128         return ((Entry) localHttpConnection.get()).idleStartTime;
129     }
130
131     protected void setIdleStartTime(long idleStartTime)
132     {
133         ((Entry) localHttpConnection.get()).idleStartTime = idleStartTime;
134     }
135
136     /**
137      * @see HttpConnectionManager#getConnection(org.apache.commons.httpclient.HostConfiguration)
138      */

139     public HttpConnection getConnection(HostConfiguration hostConfiguration)
140     {
141         return getConnection(hostConfiguration, 0);
142     }
143
144     /**
145      * Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
146      *
147      * @return <code>true</code> if stale checking will be enabled on HttpConections
148      * @see HttpConnection#isStaleCheckingEnabled()
149      * @deprecated Use {@link HttpConnectionManagerParams#isStaleCheckingEnabled()},
150      * {@link HttpConnectionManager#getParams()}.
151      */

152     public boolean isConnectionStaleCheckingEnabled()
153     {
154         return this.params.isStaleCheckingEnabled();
155     }
156
157     /**
158      * Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
159      *
160      * @param connectionStaleCheckingEnabled <code>true</code> if stale checking will be enabled
161      * on HttpConections
162      * @see HttpConnection#setStaleCheckingEnabled(boolean)
163      * @deprecated Use {@link HttpConnectionManagerParams#setStaleCheckingEnabled(boolean)},
164      * {@link HttpConnectionManager#getParams()}.
165      */

166     public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled)
167     {
168         this.params.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
169     }
170
171     /**
172      * @see HttpConnectionManager#getConnection(HostConfiguration, long)
173      * @since 3.0
174      */

175     public HttpConnection getConnectionWithTimeout(
176         HostConfiguration hostConfiguration, long timeout)
177     {
178
179         HttpConnection httpConnection = getLocalHttpConnection();
180         if (httpConnection == null)
181         {
182             httpConnection = new HttpConnection(hostConfiguration);
183             setLocalHttpConnection(httpConnection);
184             httpConnection.setHttpConnectionManager(this);
185             this.params.populateParameters(httpConnection);
186         }
187         else
188         {
189
190             // make sure the host and proxy are correct for this connection
191
// close it and set the values if they are not
192
if (!hostConfiguration.hostEquals(httpConnection)
193                 || !hostConfiguration.proxyEquals(httpConnection))
194             {
195
196                 if (httpConnection.isOpen())
197                 {
198                     httpConnection.close();
199                 }
200
201                 httpConnection.setHost(hostConfiguration.getHost());
202                 httpConnection.setPort(hostConfiguration.getPort());
203                 httpConnection.setProtocol(hostConfiguration.getProtocol());
204                 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
205
206                 httpConnection.setProxyHost(hostConfiguration.getProxyHost());
207                 httpConnection.setProxyPort(hostConfiguration.getProxyPort());
208             }
209             else
210             {
211                 finishLastResponse(httpConnection);
212             }
213         }
214
215         // remove the connection from the timeout handler
216
setIdleStartTime(Long.MAX_VALUE);
217
218         return httpConnection;
219     }
220
221     /**
222      * @see HttpConnectionManager#getConnection(HostConfiguration, long)
223      * @deprecated Use #getConnectionWithTimeout(HostConfiguration, long)
224      */

225     public HttpConnection getConnection(
226         HostConfiguration hostConfiguration, long timeout)
227     {
228         return getConnectionWithTimeout(hostConfiguration, timeout);
229     }
230
231     /**
232      * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
233      */

234     public void releaseConnection(HttpConnection conn)
235     {
236         if (conn != getLocalHttpConnection())
237         {
238             throw new IllegalStateException JavaDoc("Unexpected release of an unknown connection.");
239         }
240
241         finishLastResponse(getLocalHttpConnection());
242
243         // track the time the connection was made idle
244
setIdleStartTime(System.currentTimeMillis());
245     }
246
247     /**
248      * @since 3.0
249      */

250     public void closeIdleConnections(long idleTimeout)
251     {
252         long maxIdleTime = System.currentTimeMillis() - idleTimeout;
253         if (getIdleStartTime() <= maxIdleTime)
254         {
255             getLocalHttpConnection().close();
256         }
257     }
258 }
259
Popular Tags