001: /*
002: * CachedSocketFactory.java
003: *
004: * $Author: ss150821 $
005: *
006: * $Date: 2005/09/21 11:28:55 $ $Revision: 1.10 $
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.Socket;
016: import java.util.logging.Level;
017: import java.util.logging.Logger;
018:
019: import javax.net.ssl.SSLSocket;
020: import javax.net.ssl.SSLSocketFactory;
021:
022: import com.sun.portal.log.common.PortalLogger;
023: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
024: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
025: import com.sun.portal.util.GWLocale;
026: import com.sun.portal.util.GWLogManager;
027: import com.sun.portal.util.SRAEvent;
028:
029: /**
030: * This class defines a factory used to create CachedSockets. The types of
031: * Sockets that it can create are defined in a configuration file called
032: * CachedSocketFactory.config.
033: *
034: * @author Kevin Hartig
035: */
036:
037: public class SSLCachedSSLSocketFactory {
038:
039: private static SocketCache _cache;
040:
041: private static final String BLOCKED_SOCKET_TIMEOUT = "BlockedSocketTimeout";
042:
043: private static final int DEFAULT_SOCKET_TIMEOUT = 200;
044:
045: private static int _timeout;
046:
047: // private static Logger logger =
048: // Logger.getLogger("com.sun.portal.sra.rproxy");
049: private static Logger logger = PortalLogger
050: .getLogger(SSLCachedSSLSocketFactory.class);
051:
052: static {
053: _timeout = GatewayProfile.getInt(BLOCKED_SOCKET_TIMEOUT,
054: DEFAULT_SOCKET_TIMEOUT) * 1000;
055: _cache = new SocketCache();
056: }
057:
058: /**
059: * Create a new socket.
060: *
061: * @param host
062: * the host address the client socket attaches to (d%.d%.d%.d%
063: * format)
064: * @param port
065: * the port number to bind to the Socket
066: * @param socketType
067: * the type of socket to create inside the CachedSocket
068: * @return a CachedSocket bound to the designated host:port and containing a
069: * Socket of the requested type. This value is null if socket
070: * creation failed.
071: */
072: private static CachedSocket createSocket(String host, int port,
073: String socketType, Integer logId) {
074: CachedSocket cachedSocket = null;
075: SSLSocket sslSocket = null;
076: Socket socket = null; // the delegate socket used by the returned
077: // CachedSocket
078:
079: // Construct the socket desired and pass it to a newly constructed
080: // CachedSocket.
081: try {
082: socket = SSLSocketFactory.getDefault().createSocket(host,
083: port);
084: MonitoringSubsystem
085: .handleEvent(SRAEvent.SSL_SOCKET_CREATED);
086: if (GWLogManager.loggingEnabled)
087: GWLogManager.write("RProxy", GWLocale.getPFString(
088: "csslsf1", new Object[] { logId,
089: socket.getInetAddress().toString(),
090: new Integer(socket.getPort()) }));
091:
092: sslSocket = (SSLSocket) socket;
093: sslSocket.setUseClientMode(true);
094: socket.setSoTimeout(_timeout);
095:
096: cachedSocket = new CachedSocket(socket);
097: cachedSocket.setType(socketType);
098: cachedSocket.setActive();
099: cachedSocket.setSocketCache(_cache);
100: _cache.putSocket(cachedSocket);
101: } catch (Exception e) {
102: // logger.log(Level.SEVERE, "SSLCachedSSLSocketFactory cannot open
103: // connection to " + host + ":" + port, e);
104: Object[] params = { host, port + "", e };
105: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR116",
106: params);
107: }
108:
109: return cachedSocket;
110: }
111:
112: public static CachedSocket createSocket(SSLSocket sslSocket) {
113: CachedSocket cachedSocket = null;
114:
115: try {
116: sslSocket.setUseClientMode(true);
117: sslSocket.setSoTimeout(_timeout);
118:
119: cachedSocket = new CachedSocket(sslSocket);
120: cachedSocket.setType(new String("ssl"));
121: cachedSocket.setActive();
122: cachedSocket.setSocketCache(_cache);
123: _cache.putSocket(cachedSocket);
124: } catch (Exception e) {
125: // logger.log(Level.SEVERE, "SSLCachedSSLSocketFactory cannot create
126: // socket", e);
127: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR117", e);
128: // e.printStackTrace();
129: }
130:
131: return cachedSocket;
132: }
133:
134: /**
135: * Get a CachedSocket with a Socket of appropriate type. The CachedSocket
136: * returned could be a newly created one or one already in the socket cache
137: * ready for reuse.
138: *
139: * @param host
140: * the host address the client socket attaches to (d%.d%.d%.d%
141: * format)
142: * @param port
143: * the port number to bind to the Socket
144: * @param socketType
145: * the type of socket to create inside the CachedSocket
146: * @return a CachedSocket bound to the designated host:port and containing a
147: * Socket of the requested type. This value is null if socket
148: * creation failed.
149: *
150: */
151: public static CachedSocket getCachedSocket(String host, int port,
152: String socketType, Integer logId) {
153: CachedSocket cachedSocket = null;
154:
155: // Try to get a socket already in the cache
156:
157: // There are problems with InetAddress.getByName().getHostAddress()
158: // -- sometimes hangs for a period of time.
159:
160: // cachedSocket = _cache.getSocket(host, port, socketType);
161:
162: // If cachedSocket is null, no cached socket was found.
163: // Create a new socket and put it in the cache
164: if (cachedSocket == null) {
165: cachedSocket = createSocket(host, port, socketType, logId);
166: }
167:
168: return cachedSocket;
169: }
170:
171: /**
172: * Get a CachedSocket with a Socket of appropriate type. The CachedSocket
173: * returned could be a newly created one or one already in the socket cache
174: * ready for reuse.
175: *
176: * @param host
177: * the host address the client socket attaches to (d%.d%.d%.d%
178: * format)
179: * @param port
180: * the port number to bind to the Socket
181: * @param socketType
182: * the type of socket to create inside the CachedSocket
183: * @return a CachedSocket bound to the designated host:port and containing a
184: * Socket of the requested type. This value is null if socket
185: * creation failed.
186: *
187: */
188: public static CachedSocket getNewCachedSocket(String host,
189: int port, String socketType, Integer logId) {
190: // get a newly created socket that was just put in the cache
191: return createSocket(host, port, socketType, logId);
192: }
193: }
|