001: /*
002: * SocketSocket.java
003: *
004: * $Author: ss150821 $
005: *
006: * $Date: 2005/11/30 11:27:22 $ $Revision: 1.9 $
007: *
008: * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
009: *
010: * Developed by SunPS and SunIR
011: */
012:
013: package com.sun.portal.rproxy.connectionhandler;
014:
015: import java.net.InetAddress;
016: import java.net.UnknownHostException;
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.logging.Level;
020: import java.util.logging.Logger;
021:
022: import com.sun.portal.log.common.PortalLogger;
023:
024: /**
025: * The SocketCache class maintains a Hashtable of CachedSockets to use. Requests
026: * made of the SocketCache for a socket will return an available socket that
027: * matches the host and port signature. If an available socket is not found in
028: * the cache, a new one is created.
029: *
030: * @author Kevin Hartig
031: * @see CachedSocket
032: */
033: public final class SocketCache {
034: private HashSet _socketCache;
035:
036: private static int _max_sockets;
037:
038: private static int socketCount;
039:
040: private static final int DEFAULT_MAX_SOCKETS = 2000;
041:
042: private static Logger logger = PortalLogger
043: .getLogger(SocketCache.class);
044:
045: public SocketCache() {
046: _socketCache = new HashSet();
047: // _max_sockets = GatewayProfile.getInt("MaxSockets",
048: // DEFAULT_MAX_SOCKETS);
049:
050: socketCount = 0;
051: }
052:
053: /**
054: * Get an IDLE socket from the cache that matches the signature and type.
055: * The socket is returned in the ACTIVE state.
056: */
057: public synchronized CachedSocket getSocket(String host, int port,
058: String socketType) {
059: CachedSocket cachedSocket = null;
060:
061: if (_socketCache.isEmpty()) {
062: // logger.info("SocketCache is empty");
063: logger.info("PSSRRPROXY_CSPRCONHNDLR183");
064: } else {
065: try {
066: String inetName = InetAddress.getByName(host)
067: .getHostAddress();
068:
069: Iterator iterator = _socketCache.iterator();
070: while (iterator.hasNext()) {
071: cachedSocket = (CachedSocket) iterator.next();
072: if (cachedSocket.getState() == cachedSocket.IDLE) {
073: cachedSocket.setActive();
074: return cachedSocket;
075: }
076: }
077: } catch (UnknownHostException uhe) {
078: // logger.log(Level.SEVERE, "Exception when getting
079: // CachedSocket", uhe);
080: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR184",
081: uhe);
082: }
083: }
084:
085: // reached this point if there was no available match in the cache.
086: // logger.warning("SocketCache cannot find matching socket for " + host
087: // + ":" + port);
088: Object[] params = { host, port + "" };
089: logger.log(Level.WARNING, "PSSRRPROXY_CSPRCONHNDLR185", params);
090:
091: return null;
092: }
093:
094: /**
095: * Put a newly created socket in the cache.
096: */
097: public synchronized void putSocket(CachedSocket cachedSocket) {
098: ++socketCount;
099: // if (socketCount > _max_sockets) {
100: // } else {
101: _socketCache.add(cachedSocket);
102: // }
103: }
104:
105: /**
106: * Remove a CachedSocket from the cache
107: *
108: * @param hashKey
109: * The hash table key designating the object to remove from the
110: * table.
111: */
112: public synchronized void removeSocket(CachedSocket hashKey) {
113: if (socketCount > 0)
114: --socketCount;
115: if (!_socketCache.remove(hashKey)) {
116: // logger.severe("Failed to remove " + hashKey + " from
117: // SocketCache");
118: Object[] params = { hashKey };
119: logger.log(Level.SEVERE, "", params);
120: }
121: }
122: }
|