Source Code Cross Referenced for SimpleHttpConnectionManager.java in  » Net » Apache-common-HttpClient » org » apache » commons » httpclient » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Apache common HttpClient » org.apache.commons.httpclient 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v 1.23 2004/10/16 22:40:08 mbecke Exp $
003:         * $Revision: 480424 $
004:         * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005:         *
006:         * ====================================================================
007:         *
008:         *  Licensed to the Apache Software Foundation (ASF) under one or more
009:         *  contributor license agreements.  See the NOTICE file distributed with
010:         *  this work for additional information regarding copyright ownership.
011:         *  The ASF licenses this file to You under the Apache License, Version 2.0
012:         *  (the "License"); you may not use this file except in compliance with
013:         *  the License.  You may obtain a copy of the License at
014:         *
015:         *      http://www.apache.org/licenses/LICENSE-2.0
016:         *
017:         *  Unless required by applicable law or agreed to in writing, software
018:         *  distributed under the License is distributed on an "AS IS" BASIS,
019:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020:         *  See the License for the specific language governing permissions and
021:         *  limitations under the License.
022:         * ====================================================================
023:         *
024:         * This software consists of voluntary contributions made by many
025:         * individuals on behalf of the Apache Software Foundation.  For more
026:         * information on the Apache Software Foundation, please see
027:         * <http://www.apache.org/>.
028:         *
029:         */
030:
031:        package org.apache.commons.httpclient;
032:
033:        import java.io.IOException;
034:        import java.io.InputStream;
035:
036:        import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
037:        import org.apache.commons.logging.Log;
038:        import org.apache.commons.logging.LogFactory;
039:
040:        /**
041:         * A connection manager that provides access to a single HttpConnection.  This
042:         * manager makes no attempt to provide exclusive access to the contained
043:         * HttpConnection.
044:         *
045:         * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
046:         * @author Eric Johnson
047:         * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
048:         * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
049:         * @author Laura Werner
050:         * 
051:         * @since 2.0
052:         */
053:        public class SimpleHttpConnectionManager implements 
054:                HttpConnectionManager {
055:
056:            private static final Log LOG = LogFactory
057:                    .getLog(SimpleHttpConnectionManager.class);
058:
059:            private static final String MISUSE_MESSAGE = "SimpleHttpConnectionManager being used incorrectly.  Be sure that"
060:                    + " HttpMethod.releaseConnection() is always called and that only one thread"
061:                    + " and/or method is using this connection manager at a time.";
062:
063:            /**
064:             * Since the same connection is about to be reused, make sure the
065:             * previous request was completely processed, and if not
066:             * consume it now.
067:             * @param conn The connection
068:             */
069:            static void finishLastResponse(HttpConnection conn) {
070:                InputStream lastResponse = conn.getLastResponseInputStream();
071:                if (lastResponse != null) {
072:                    conn.setLastResponseInputStream(null);
073:                    try {
074:                        lastResponse.close();
075:                    } catch (IOException ioe) {
076:                        conn.close();
077:                    }
078:                }
079:            }
080:
081:            /** The http connection */
082:            protected HttpConnection httpConnection;
083:
084:            /**
085:             * Collection of parameters associated with this connection manager.
086:             */
087:            private HttpConnectionManagerParams params = new HttpConnectionManagerParams();
088:
089:            /**
090:             * The time the connection was made idle.
091:             */
092:            private long idleStartTime = Long.MAX_VALUE;
093:
094:            /**
095:             * Used to test if {@link #httpConnection} is currently in use 
096:             * (i.e. checked out).  This is only used as a sanity check to help
097:             * debug cases where this connection manager is being used incorrectly.
098:             * It will not be used to enforce thread safety.
099:             */
100:            private volatile boolean inUse = false;
101:
102:            private boolean alwaysClose = false;
103:
104:            /**
105:             * The connection manager created with this constructor will try to keep the 
106:             * connection open (alive) between consecutive requests if the alwaysClose 
107:             * parameter is set to <tt>false</tt>. Otherwise the connection manager will 
108:             * always close connections upon release.
109:             * 
110:             * @param alwaysClose if set <tt>true</tt>, the connection manager will always
111:             *    close connections upon release.
112:             */
113:            public SimpleHttpConnectionManager(boolean alwaysClose) {
114:                super ();
115:                this .alwaysClose = alwaysClose;
116:            }
117:
118:            /**
119:             * The connection manager created with this constructor will always try to keep 
120:             * the connection open (alive) between consecutive requests.
121:             */
122:            public SimpleHttpConnectionManager() {
123:                super ();
124:            }
125:
126:            /**
127:             * @see HttpConnectionManager#getConnection(HostConfiguration)
128:             */
129:            public HttpConnection getConnection(
130:                    HostConfiguration hostConfiguration) {
131:                return getConnection(hostConfiguration, 0);
132:            }
133:
134:            /**
135:             * Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
136:             * 
137:             * @return <code>true</code> if stale checking will be enabled on HttpConections
138:             * 
139:             * @see HttpConnection#isStaleCheckingEnabled()
140:             * 
141:             * @deprecated Use {@link HttpConnectionManagerParams#isStaleCheckingEnabled()},
142:             * {@link HttpConnectionManager#getParams()}.
143:             */
144:            public boolean isConnectionStaleCheckingEnabled() {
145:                return this .params.isStaleCheckingEnabled();
146:            }
147:
148:            /**
149:             * Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
150:             * 
151:             * @param connectionStaleCheckingEnabled <code>true</code> if stale checking will be enabled 
152:             * on HttpConections
153:             * 
154:             * @see HttpConnection#setStaleCheckingEnabled(boolean)
155:             * 
156:             * @deprecated Use {@link HttpConnectionManagerParams#setStaleCheckingEnabled(boolean)},
157:             * {@link HttpConnectionManager#getParams()}.
158:             */
159:            public void setConnectionStaleCheckingEnabled(
160:                    boolean connectionStaleCheckingEnabled) {
161:                this .params
162:                        .setStaleCheckingEnabled(connectionStaleCheckingEnabled);
163:            }
164:
165:            /**
166:             * This method always returns the same connection object. If the connection is already
167:             * open, it will be closed and the new host configuration will be applied.
168:             * 
169:             * @param hostConfiguration The host configuration specifying the connection
170:             *        details.
171:             * @param timeout this parameter has no effect. The connection is always returned
172:             *        immediately.
173:             * @since 3.0
174:             */
175:            public HttpConnection getConnectionWithTimeout(
176:                    HostConfiguration hostConfiguration, long timeout) {
177:
178:                if (httpConnection == null) {
179:                    httpConnection = new HttpConnection(hostConfiguration);
180:                    httpConnection.setHttpConnectionManager(this );
181:                    httpConnection.getParams().setDefaults(this .params);
182:                } else {
183:
184:                    // make sure the host and proxy are correct for this connection
185:                    // close it and set the values if they are not
186:                    if (!hostConfiguration.hostEquals(httpConnection)
187:                            || !hostConfiguration.proxyEquals(httpConnection)) {
188:
189:                        if (httpConnection.isOpen()) {
190:                            httpConnection.close();
191:                        }
192:
193:                        httpConnection.setHost(hostConfiguration.getHost());
194:                        httpConnection.setPort(hostConfiguration.getPort());
195:                        httpConnection.setProtocol(hostConfiguration
196:                                .getProtocol());
197:                        httpConnection.setLocalAddress(hostConfiguration
198:                                .getLocalAddress());
199:
200:                        httpConnection.setProxyHost(hostConfiguration
201:                                .getProxyHost());
202:                        httpConnection.setProxyPort(hostConfiguration
203:                                .getProxyPort());
204:                    } else {
205:                        finishLastResponse(httpConnection);
206:                    }
207:                }
208:
209:                // remove the connection from the timeout handler
210:                idleStartTime = Long.MAX_VALUE;
211:
212:                if (inUse)
213:                    LOG.warn(MISUSE_MESSAGE);
214:                inUse = true;
215:
216:                return httpConnection;
217:            }
218:
219:            /**
220:             * @see HttpConnectionManager#getConnection(HostConfiguration, long)
221:             * 
222:             * @deprecated Use #getConnectionWithTimeout(HostConfiguration, long)
223:             */
224:            public HttpConnection getConnection(
225:                    HostConfiguration hostConfiguration, long timeout) {
226:                return getConnectionWithTimeout(hostConfiguration, timeout);
227:            }
228:
229:            /**
230:             * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
231:             */
232:            public void releaseConnection(HttpConnection conn) {
233:                if (conn != httpConnection) {
234:                    throw new IllegalStateException(
235:                            "Unexpected release of an unknown connection.");
236:                }
237:                if (this .alwaysClose) {
238:                    httpConnection.close();
239:                } else {
240:                    // make sure the connection is reuseable
241:                    finishLastResponse(httpConnection);
242:                }
243:
244:                inUse = false;
245:
246:                // track the time the connection was made idle
247:                idleStartTime = System.currentTimeMillis();
248:            }
249:
250:            /**
251:             * Returns {@link HttpConnectionManagerParams parameters} associated 
252:             * with this connection manager.
253:             * 
254:             * @since 2.1
255:             * 
256:             * @see HttpConnectionManagerParams
257:             */
258:            public HttpConnectionManagerParams getParams() {
259:                return this .params;
260:            }
261:
262:            /**
263:             * Assigns {@link HttpConnectionManagerParams parameters} for this 
264:             * connection manager.
265:             * 
266:             * @since 2.1
267:             * 
268:             * @see HttpConnectionManagerParams
269:             */
270:            public void setParams(final HttpConnectionManagerParams params) {
271:                if (params == null) {
272:                    throw new IllegalArgumentException(
273:                            "Parameters may not be null");
274:                }
275:                this .params = params;
276:            }
277:
278:            /**
279:             * @since 3.0
280:             */
281:            public void closeIdleConnections(long idleTimeout) {
282:                long maxIdleTime = System.currentTimeMillis() - idleTimeout;
283:                if (idleStartTime <= maxIdleTime) {
284:                    httpConnection.close();
285:                }
286:            }
287:
288:            /**
289:             * since 3.1
290:             */
291:            public void shutdown() {
292:                httpConnection.close();
293:            }
294:
295:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.