KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > HttpConnectionManager


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.io.IOException JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 /**
29  *
30  * Manages a pool of {@link HttpConnection} objects for an {@link HttpClient}.
31  *
32  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
33  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
34  */

35 public class HttpConnectionManager {
36
37     private Vector JavaDoc connections = new Vector JavaDoc();
38     HttpClient client;
39
40     static int newIdx = 0;
41     static int reuseIdx = 0;
42
43     private int maxPoolSize = Integer.MAX_VALUE;
44
45     // #ifdef DEBUG
46
org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(HttpConnectionManager.class);
47
48     // #endif
49

50     public HttpConnectionManager(HttpClient client) {
51         this.client = client;
52     }
53
54     /**
55      * Get a connection. If there is a valid pooled available, then that will be
56      * returned otherwise a new connection will be created.
57      *
58      * @return HttpConnection
59      * @throws IOException
60      * @throws UnknownHostException
61      */

62     public synchronized HttpConnection getConnection() throws IOException JavaDoc, UnknownHostException JavaDoc, HttpException,
63                     UnsupportedAuthenticationException, AuthenticationCancelledException {
64
65         HttpConnection con;
66
67         if (connections.size() == 0) {
68             // #ifdef DEBUG
69
log.info(Messages.getString("HttpConnectionManager.creatingNewConnection")); //$NON-NLS-1$
70
// #endif
71
con = new HttpConnection(client);
72         } else {
73             con = (HttpConnection) connections.elementAt(0);
74             connections.removeElementAt(0);
75             // #ifdef DEBUG
76
log.info(MessageFormat.format(Messages.getString("HttpConnectionManager.reusingConnection"), new Object JavaDoc[] { new Integer JavaDoc(connections.size()), con.toString() })); //$NON-NLS-1$
77
// #endif
78
con.verify();
79         }
80         // #ifdef DEBUG
81
log.info("Returning pooled connection"); //$NON-NLS-1$
82
// #endif
83
return con;
84     }
85
86     /**
87      * Set the maximum size of the connection pool. Set to zero to place no
88      * restriction.
89      *
90      * @param maxPoolSize maximum pool size
91      */

92     public synchronized void setMaxPoolSize(int maxPoolSize) {
93         this.maxPoolSize = maxPoolSize;
94     }
95
96     /**
97      * Release a connection back to the pool.
98      *
99      * @param connection to release
100      */

101     public synchronized void releaseConnection(HttpConnection con) {
102         // Can we reuse this connection?
103
if (con.canReuse() && !con.isClosed()) {
104             if (maxPoolSize == 0 || connections.size() < maxPoolSize)
105                 connections.addElement(con);
106             else
107                 con.close();
108             // #ifdef DEBUG
109
log.info(MessageFormat.format(Messages.getString("HttpConnectionManager.releasedToPool"), new Object JavaDoc[] { new Integer JavaDoc(connections.size()), con.toString() })); //$NON-NLS-1$ //$NON-NLS-2$
110
// #endif
111
// Set the last time this was accessed.
112
con.updateState();
113         } else {
114             // #ifdef DEBUG
115
log.info(Messages.getString("HttpConnectionManager.willNotReuse")); //$NON-NLS-1$
116
// #endif
117
}
118
119     }
120
121     /**
122      * Release all connections back to the pool.
123      */

124     public synchronized void releaseAllConnections() {
125         for (Enumeration JavaDoc e1 = connections.elements(); e1.hasMoreElements();) {
126             HttpConnection con = (HttpConnection) e1.nextElement();
127             releaseConnection(con);
128         }
129     }
130
131     /**
132      * Check if a connection is closed or un-reusable and reconnect it
133      *
134      * @param con connection
135      * @throws UnknownHostException
136      * @throws IOException
137      * @throws HttpException
138      * @throws UnsupportedAuthenticationException
139      * @throws AuthenticationCancelledException
140      */

141     public void checkConnection(HttpConnection con) throws UnknownHostException JavaDoc, IOException JavaDoc, HttpException,
142                     UnsupportedAuthenticationException, AuthenticationCancelledException {
143         if (con.isClosed || !con.canReuse) {
144             con.reconnect();
145         }
146     }
147
148     /**
149      * Close all connections
150      */

151     public synchronized void closeConnections() {
152
153         for (Enumeration JavaDoc e1 = connections.elements(); e1.hasMoreElements();) {
154             HttpConnection con = (HttpConnection) e1.nextElement();
155             con.close();
156         }
157     }
158
159     /**
160      * Get the number of connections in the pool
161      *
162      * @return number of pooled connections
163      */

164     public synchronized int getConnectionCount() {
165         return connections.size();
166     }
167
168     /**
169      * Get the number of connections currently open.
170      *
171      * @return number of open connections
172      */

173     public synchronized int getOpenConnectionCount() {
174         int open = 0;
175         for (Enumeration JavaDoc e1 = connections.elements(); e1.hasMoreElements();) {
176             HttpConnection con = (HttpConnection) e1.nextElement();
177             if (!con.isClosed()) {
178                 open++;
179             }
180         }
181         return open;
182
183     }
184
185 }
186
Popular Tags