KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > SimpleHttpConnectionManager


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v 1.12.2.2 2004/02/22 18:21:13 olegk Exp $
3  * $Revision: 1.12.2.2 $
4  * $Date: 2004/02/22 18:21:13 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2002-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient;
33
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36
37 /**
38  * A connection manager that provides access to a single HttpConnection. This
39  * manager makes no attempt to provide exclusive access to the contained
40  * HttpConnection.
41  *
42  * @author <a HREF="mailto:becke@u.washington.edu">Michael Becke</a>
43  * @author Eric Johnson
44  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
45  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
46  * @author Laura Werner
47  *
48  * @since 2.0
49  */

50 public class SimpleHttpConnectionManager implements HttpConnectionManager {
51
52     /** The http connection */
53     private HttpConnection httpConnection;
54
55     /** The value to set when calling setStaleCheckingEnabled() on connections */
56     private boolean connectionStaleCheckingEnabled = true;
57
58     /**
59      * Constructor for SimpleHttpConnectionManager.
60      */

61     public SimpleHttpConnectionManager() {
62         super();
63     }
64
65     /**
66      * @see HttpConnectionManager#getConnection(HostConfiguration)
67      */

68     public HttpConnection getConnection(HostConfiguration hostConfiguration) {
69         return getConnection(hostConfiguration, 0);
70     }
71
72     /**
73      * Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
74      *
75      * @return <code>true</code> if stale checking will be enabled on HttpConections
76      *
77      * @see HttpConnection#isStaleCheckingEnabled()
78      */

79     public boolean isConnectionStaleCheckingEnabled() {
80         return connectionStaleCheckingEnabled;
81     }
82
83     /**
84      * Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
85      *
86      * @param connectionStaleCheckingEnabled <code>true</code> if stale checking will be enabled
87      * on HttpConections
88      *
89      * @see HttpConnection#setStaleCheckingEnabled(boolean)
90      */

91     public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled) {
92         this.connectionStaleCheckingEnabled = connectionStaleCheckingEnabled;
93     }
94
95     /**
96      * @see HttpConnectionManager#getConnection(HostConfiguration, long)
97      */

98     public HttpConnection getConnection(
99         HostConfiguration hostConfiguration, long timeout) {
100
101         if (httpConnection == null) {
102             httpConnection = new HttpConnection(hostConfiguration);
103             httpConnection.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
104         } else {
105
106             // make sure the host and proxy are correct for this connection
107
// close it and set the values if they are not
108
if (!hostConfiguration.hostEquals(httpConnection)
109                 || !hostConfiguration.proxyEquals(httpConnection)) {
110                     
111                 if (httpConnection.isOpen()) {
112                     httpConnection.close();
113                 }
114
115                 httpConnection.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
116
117                 httpConnection.setHost(hostConfiguration.getHost());
118                 httpConnection.setVirtualHost(hostConfiguration.getVirtualHost());
119                 httpConnection.setPort(hostConfiguration.getPort());
120                 httpConnection.setProtocol(hostConfiguration.getProtocol());
121                 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
122
123                 httpConnection.setProxyHost(hostConfiguration.getProxyHost());
124                 httpConnection.setProxyPort(hostConfiguration.getProxyPort());
125             } else {
126                 finishLastResponse(httpConnection);
127             }
128         }
129
130         return httpConnection;
131     }
132
133     /**
134      * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
135      */

136     public void releaseConnection(HttpConnection conn) {
137         if (conn != httpConnection) {
138             throw new IllegalStateException JavaDoc("Unexpected close on a different connection.");
139         }
140
141         finishLastResponse(httpConnection);
142     }
143
144     /**
145      * Since the same connection is about to be reused, make sure the
146      * previous request was completely processed, and if not
147      * consume it now.
148      * @param conn The connection
149      */

150     static void finishLastResponse(HttpConnection conn) {
151         InputStream JavaDoc lastResponse = conn.getLastResponseInputStream();
152         if (lastResponse != null) {
153             conn.setLastResponseInputStream(null);
154             try {
155                 lastResponse.close();
156             } catch (IOException JavaDoc ioe) {
157                 //FIXME: badness - close to force reconnect.
158
conn.close();
159             }
160         }
161     }
162 }
163
Popular Tags