KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > vfs > IvyWebdavConnectionManager


1 package fr.jayasoft.ivy.repository.vfs;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5
6 import org.apache.commons.httpclient.HostConfiguration;
7 import org.apache.commons.httpclient.HttpConnection;
8 import org.apache.commons.httpclient.HttpConnectionManager;
9 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
10
11
12 /**
13  * Modified version of the WebdavConnectionManager from VFS which adds support for httpclient 3.x.
14  * See http://issues.apache.org/jira/browse/VFS-74 for more info.
15  *
16  * A connection manager that provides access to a single HttpConnection. This
17  * manager makes no attempt to provide exclusive access to the contained
18  * HttpConnection.
19  * <p/>
20  * imario@apache.org: Keep connection in ThreadLocal.
21  *
22  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
23  * @author <a HREF="mailto:becke@u.washington.edu">Michael Becke</a>
24  * @author Eric Johnson
25  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
26  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
27  * @author Laura Werner
28  * @author Maarten Coene
29  */

30 class IvyWebdavConnectionManager implements HttpConnectionManager {
31
32     /**
33      * Since the same connection is about to be reused, make sure the
34      * previous request was completely processed, and if not
35      * consume it now.
36      *
37      * @param conn The connection
38      */

39     static void finishLastResponse(HttpConnection conn)
40     {
41         InputStream JavaDoc lastResponse = conn.getLastResponseInputStream();
42         if (lastResponse != null)
43         {
44             conn.setLastResponseInputStream(null);
45             try
46             {
47                 lastResponse.close();
48             }
49             catch (IOException JavaDoc ioe)
50             {
51                 //FIXME: badness - close to force reconnect.
52
conn.close();
53             }
54         }
55     }
56
57     /**
58      * The thread data
59      */

60     protected ThreadLocal JavaDoc localHttpConnection = new ThreadLocal JavaDoc()
61     {
62         protected Object JavaDoc initialValue()
63         {
64             return new Entry();
65         }
66     };
67
68     /**
69      * Collection of parameters associated with this connection manager.
70      */

71     private HttpConnectionManagerParams params = new HttpConnectionManagerParams();
72
73     /**
74      * release the connection of the current thread
75      */

76     public void releaseLocalConnection()
77     {
78         if (getLocalHttpConnection() != null)
79         {
80             releaseConnection(getLocalHttpConnection());
81         }
82     }
83
84     private static class Entry
85     {
86         /**
87          * The http connection
88          */

89         private HttpConnection conn = null;
90
91         /**
92          * The time the connection was made idle.
93          */

94         private long idleStartTime = Long.MAX_VALUE;
95     }
96
97     public IvyWebdavConnectionManager()
98     {
99     }
100
101     protected HttpConnection getLocalHttpConnection()
102     {
103         return ((Entry) localHttpConnection.get()).conn;
104     }
105
106     protected void setLocalHttpConnection(HttpConnection conn)
107     {
108         ((Entry) localHttpConnection.get()).conn = conn;
109     }
110
111     protected long getIdleStartTime()
112     {
113         return ((Entry) localHttpConnection.get()).idleStartTime;
114     }
115
116     protected void setIdleStartTime(long idleStartTime)
117     {
118         ((Entry) localHttpConnection.get()).idleStartTime = idleStartTime;
119     }
120
121     /**
122      * @see HttpConnectionManager#getConnection(org.apache.commons.httpclient.HostConfiguration)
123      */

124     public HttpConnection getConnection(HostConfiguration hostConfiguration)
125     {
126         return getConnection(hostConfiguration, 0);
127     }
128
129     /**
130      * Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
131      *
132      * @return <code>true</code> if stale checking will be enabled on HttpConections
133      * @see HttpConnection#isStaleCheckingEnabled()
134      */

135     public boolean isConnectionStaleCheckingEnabled()
136     {
137         return this.params.isStaleCheckingEnabled();
138     }
139
140     /**
141      * Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
142      *
143      * @param connectionStaleCheckingEnabled <code>true</code> if stale checking will be enabled
144      * on HttpConections
145      * @see HttpConnection#setStaleCheckingEnabled(boolean)
146      */

147     public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled)
148     {
149         this.params.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
150     }
151
152     /**
153      * @see HttpConnectionManager#getConnection(HostConfiguration, long)
154      * @since 3.0
155      */

156     public HttpConnection getConnectionWithTimeout(
157         HostConfiguration hostConfiguration, long timeout)
158     {
159
160         HttpConnection httpConnection = getLocalHttpConnection();
161         if (httpConnection == null)
162         {
163             httpConnection = new HttpConnection(hostConfiguration);
164             setLocalHttpConnection(httpConnection);
165             httpConnection.setHttpConnectionManager(this);
166             httpConnection.getParams().setDefaults(this.params);
167         }
168         else
169         {
170
171             // make sure the host and proxy are correct for this connection
172
// close it and set the values if they are not
173
if (!hostConfiguration.hostEquals(httpConnection)
174                 || !hostConfiguration.proxyEquals(httpConnection))
175             {
176
177                 if (httpConnection.isOpen())
178                 {
179                     httpConnection.close();
180                 }
181
182                 httpConnection.setHost(hostConfiguration.getHost());
183                 httpConnection.setPort(hostConfiguration.getPort());
184                 httpConnection.setProtocol(hostConfiguration.getProtocol());
185                 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
186
187                 httpConnection.setProxyHost(hostConfiguration.getProxyHost());
188                 httpConnection.setProxyPort(hostConfiguration.getProxyPort());
189             }
190             else
191             {
192                 finishLastResponse(httpConnection);
193             }
194         }
195
196         // remove the connection from the timeout handler
197
setIdleStartTime(Long.MAX_VALUE);
198
199         return httpConnection;
200     }
201
202     /**
203      * @see HttpConnectionManager#getConnection(HostConfiguration, long)
204      * @deprecated Use #getConnectionWithTimeout(HostConfiguration, long)
205      */

206     public HttpConnection getConnection(
207         HostConfiguration hostConfiguration, long timeout)
208     {
209         return getConnectionWithTimeout(hostConfiguration, timeout);
210     }
211
212     /**
213      * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
214      */

215     public void releaseConnection(HttpConnection conn)
216     {
217         if (conn != getLocalHttpConnection())
218         {
219             throw new IllegalStateException JavaDoc("Unexpected release of an unknown connection.");
220         }
221
222         finishLastResponse(getLocalHttpConnection());
223
224         // track the time the connection was made idle
225
setIdleStartTime(System.currentTimeMillis());
226     }
227
228     /**
229      * @since 3.0
230      */

231     public void closeIdleConnections(long idleTimeout)
232     {
233         long maxIdleTime = System.currentTimeMillis() - idleTimeout;
234         if (getIdleStartTime() <= maxIdleTime)
235         {
236             getLocalHttpConnection().close();
237         }
238     }
239     
240     /**
241      * {@inheritDoc}
242      */

243     public HttpConnectionManagerParams getParams() {
244         return params;
245     }
246
247     /**
248      * {@inheritDoc}
249      */

250     public void setParams(HttpConnectionManagerParams params) {
251         this.params = params;
252     }
253
254 }
255
Popular Tags