001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client;
024:
025: import HTTPClient.SSLSupport;
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028: import org.skunk.trace.Debug;
029:
030: /**
031: * a pool for DAVConnection objects.
032: *
033: * @author Jacob Smullyan
034: */
035: public abstract class DAVConnectionPool {
036: private static Hashtable connectionHash = new Hashtable();
037:
038: /**
039: * indicates at runtime whether the current build can support SSL
040: * @return whether SSL is supported
041: */
042: public static final boolean canSupportSSL() {
043: return SSLSupport.canSupport();
044: }
045:
046: /**
047: * returns a pooled DAVConnection instance
048: * @param host the host
049: * @param port the port
050: * @param secure whether the protocol of the connection is https (otherwise, http)
051: * @return the pooled DAVConnection instance
052: */
053: public static DAVConnection getDAVConnection(String host, int port,
054: boolean secure) {
055: String key = getKey(host, port);
056: Debug.trace(DAVConnectionPool.class, Debug.DP4, "hashKey: "
057: + key);
058: DAVConnection conn;
059: if (connectionHash.containsKey(key)) {
060: conn = (DAVConnection) connectionHash.get(key);
061: } else {
062: Debug.trace(DAVConnection.class, Debug.DP4,
063: "new connection created for host " + host
064: + " and port " + port);
065: conn = new DAVConnection(host, port, secure);
066: connectionHash.put(key, conn);
067: }
068: return conn;
069: }
070:
071: /**
072: * returns a pooled DAVConnection instance with the http protocol
073: * @param host the host
074: * @param port the port
075: * @return the pooled DAVConnection instance
076: */
077: public static DAVConnection getDAVConnection(String host, int port) {
078: return getDAVConnection(host, port, false);
079: }
080:
081: /**
082: * removes the DAVConnection with the given host and port from the pool
083: * @param host the host
084: * @param port the port
085: */
086: public static void removeDAVConnection(String host, int port) {
087: String key = getKey(host, port);
088: if (connectionHash.containsKey(key)) {
089: DAVConnection dc = (DAVConnection) connectionHash
090: .remove(key);
091: if (!dc.isClosed()) {
092: dc.closeConnection();
093: }
094: }
095: }
096:
097: /**
098: * returns the key used in the DAVConnectionPool hashtable for a DAVConnection with the given host and port.
099: * @param host the host
100: * @param port the port
101: * @return the key used
102: */
103: protected static String getKey(String host, int port) {
104: return new StringBuffer().append(host).append(":").append(port)
105: .toString();
106: }
107:
108: public static Enumeration getDAVConnections() {
109: return connectionHash.elements();
110: }
111: }
112:
113: /* $Log: DAVConnectionPool.java,v $
114: /* Revision 1.7 2001/01/23 20:50:09 smulloni
115: /* added configurable socket timeout
116: /*
117: /* Revision 1.6 2001/01/03 22:51:53 smulloni
118: /* added documentation
119: /*
120: /* Revision 1.5 2000/12/19 22:06:15 smulloni
121: /* adding documentation.
122: /*
123: /* Revision 1.4 2000/12/18 23:22:47 smulloni
124: /* added optional SSL capability to the gui application. Separate make targets
125: /* exist for building it will SSL support.
126: /*
127: /* Revision 1.3 2000/12/03 23:53:25 smulloni
128: /* added license and copyright preamble to java files.
129: /*
130: /* Revision 1.2 2000/11/09 23:34:49 smullyan
131: /* log added to every Java file, with the help of python. Lock stealing
132: /* implemented, and treatment of locks made more robust.
133: /* */
|