001: /*
002: * $Id: CachedSSLSocketFactory.java,v 1.11 2005/11/30 11:27:20 ss150821 Exp $
003: * $Source: /m/portal/ps/srap/src/com/sun/portal/rproxy/connectionhandler/CachedSSLSocketFactory.java,v $
004: * $Log: CachedSSLSocketFactory.java,v $
005: * Revision 1.11 2005/11/30 11:27:20 ss150821
006: * 6356996 - Srap Code base needs to save files in the unix file format and not windows
007: *
008: * Revision 1.10 2005/09/21 11:28:55 ss150821
009: * 5044474 - Modifying the time units of various attributes to seconds
010: *
011: * Revision 1.9 2005/02/25 09:44:12 ss150821
012: * RFE 6223490 - SRA Should use JDK based logging, changed to start throwing the full stacktrace for the exception in the logs
013: *
014: * Revision 1.8 2005/02/24 07:36:43 ss150821
015: * RFE 6223490 - SRA Should use JDK based logging
016: *
017: * Revision 1.7 2005/02/23 09:15:04 ss150821
018: * RFE 6223490 - SRA Should use JDK based logging
019: *
020: * Revision 1.6 2004/07/27 12:55:00 vt126379
021: * RFE#5075809, CRT#99
022: *
023: * Revision 1.5 2002/09/09 11:40:26 ss133690
024: * CRT: 1952 Enable Ciphers for secure connection between Gateway & the Server
025: *
026: * Revision 1.4 2002/08/16 15:13:05 bv131302
027: * Hana CRT#1888 - Check log settings before logging
028: *
029: * Revision 1.3 2002/08/16 12:19:54 bv131302
030: * Hana CRT#1884 - RProxy perf issues
031: *
032: * Revision 1.2 2002/06/21 13:04:13 bv131302
033: * LDAP Attribute name changes
034: *
035: * Revision 1.1 2002/06/14 09:53:50 rt130506
036: * SRAP rebranding
037: *
038: * Revision 1.5 2002/06/12 07:55:57 bv131302
039: * more rebranding - filenames
040: *
041: * Revision 1.4 2002/06/11 16:02:03 bv131302
042: * new branded
043: *
044: * Revision 1.3 2002/05/13 06:22:23 mm132998
045: * Perf related modifications
046: *
047: * Revision 1.2 2002/04/08 12:18:47 mm132998
048: * CRT : 742 , GWDebug statement causing Exceptions . Commenting it.
049: *
050: *
051: */
052: /*
053: * CachedSocketFactory.java
054: *
055: * $Author: ss150821 $
056: *
057: * $Date: 2005/11/30 11:27:20 $ $Revision: 1.11 $
058: *
059: * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
060: *
061: * Developed by SunPS and SunIR
062: */
063:
064: package com.sun.portal.rproxy.connectionhandler;
065:
066: import java.net.InetAddress;
067: import java.util.logging.Level;
068: import java.util.logging.Logger;
069:
070: import com.sun.portal.log.common.PortalLogger;
071: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
072: import com.sun.portal.util.GWLocale;
073: import com.sun.portal.util.GWLogManager;
074: import com.sun.portal.util.ServerCertApprovalCallback;
075: import com.sun.portal.util.SystemProperties;
076:
077: /**
078: * This class defines a factory used to create CachedSockets. The types of
079: * Sockets that it can create are defined in a configuration file called
080: * CachedSocketFactory.config.
081: *
082: * @author Kevin Hartig
083: */
084:
085: public class CachedSSLSocketFactory {
086:
087: // private static SocketCache _cache;
088: private static final String BLOCKED_SOCKET_TIMEOUT = "BlockedSocketTimeout";
089:
090: private static final int DEFAULT_SOCKET_TIMEOUT = 200;
091:
092: private static int _timeout;
093:
094: // private static Logger logger =
095: // Logger.getLogger("com.sun.portal.sra.rproxy");
096: private static Logger logger = PortalLogger
097: .getLogger(CachedSSLSocketFactory.class);
098:
099: static {
100: _timeout = GatewayProfile.getInt(BLOCKED_SOCKET_TIMEOUT,
101: DEFAULT_SOCKET_TIMEOUT) * 1000;
102: // _cache = new SocketCache();
103: }
104:
105: /**
106: * Create a new socket.
107: *
108: * @param host
109: * the host address the client socket attaches to (d%.d%.d%.d%
110: * format)
111: * @param port
112: * the port number to bind to the Socket
113: * @param socketType
114: * the type of socket to create inside the CachedSocket
115: * @return a CachedSocket bound to the designated host:port and containing a
116: * Socket of the requested type. This value is null if socket
117: * creation failed.
118: */
119:
120: public static CachedSocket createSocket(String host, int port,
121: String socketType, Integer logId,
122: ServerCertApprovalCallback approvalCB) {
123: CachedSocket cachedSocket = null;
124: org.mozilla.jss.ssl.SSLSocket sslSocket = null;
125:
126: // Construct the socket desired and pass it to a newly constructed
127: // CachedSocket.
128: try {
129: if (approvalCB == null)
130: sslSocket = new org.mozilla.jss.ssl.SSLSocket(host,
131: port);
132: else
133: sslSocket = new org.mozilla.jss.ssl.SSLSocket(
134: InetAddress.getByName(host), port, null, 0,
135: /* JSS3.1.1 change - begin */
136: // true,
137: /* JSS3.1.1 change - end */
138: approvalCB, null);
139: } catch (Exception se) {
140: // For some reason, we cannot open a new socket, retry!
141: // logger.log(Level.SEVERE, "CachedSSLSocketFactory cannot open
142: // connection to " + host + ":" + port, se);
143: Object[] params0 = { host, ":", port + "", se };
144: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR006",
145: params0);
146:
147: String retryTimes = SystemProperties
148: .get("gateway.sockretries");
149: if (retryTimes == null) {
150: return null;
151: }
152:
153: int retries = Integer.parseInt(retryTimes);
154: for (int i = 1; i <= retries; i++) {
155: try {
156: Thread.sleep(3000); // sleep for 3 seconds
157: } catch (InterruptedException ie) {
158: }
159:
160: try {
161: logger
162: .warning("CachedSSLSocketFactory: Open new socket; retry #"
163: + i);
164: if (approvalCB == null)
165: sslSocket = new org.mozilla.jss.ssl.SSLSocket(
166: host, port);
167: else
168: sslSocket = new org.mozilla.jss.ssl.SSLSocket(
169: InetAddress.getByName(host), port,
170: null, 0,
171: /* JSS3.1.1 change - begin */
172: // true,
173: /* JSS3.1.1 change - end */
174: approvalCB, null);
175:
176: if (sslSocket != null)
177: break; // socket creation retry is successful
178: } catch (Exception rse) {
179: // logger.log(Level.SEVERE, "CachedSSLSocketFactory cannot
180: // open connection to " + host + ":" + port, rse);
181: Object[] params1 = { host, ":", port + "", rse };
182: logger.log(Level.SEVERE,
183: "PSSRRPROXY_CSPRCONHNDLR007", params1);
184: }
185: }
186:
187: if (sslSocket == null)
188: return null;
189: }
190:
191: if (GWLogManager.loggingEnabled) {
192: // The call sslSocket.getInetAddress().toString() seems to be
193: // throwing exception with JSS3.1.1 - Mridul
194: // GWLogManager.write("RProxy", GWLocale.getPFString("csslsf1", new
195: // Object[] { logId, sslSocket.getInetAddress().toString(), new
196: // Integer(sslSocket.getPort())}));
197: GWLogManager.write("RProxy", GWLocale.getPFString(
198: "csslsf1", new Object[] { logId, host,
199: new Integer(sslSocket.getPort()) }));
200: // EOC : Mridul
201: }
202:
203: try {
204: sslSocket.setUseClientMode(true);
205: sslSocket.setSoTimeout(_timeout);
206: /**
207: * Bug 4740555 - Enable ciphers for SSL connection between Gateway &
208: * server
209: */
210: sslSocket.enableSSL2(true);
211: sslSocket.enableSSL3(true);
212: // End of code change for the bug 4740555
213: cachedSocket = new CachedSocket(sslSocket);
214: /*
215: * cachedSocket.setType(socketType); cachedSocket.setActive();
216: */
217: // cachedSocket.setSocketCache(_cache);
218: // _cache.putSocket(cachedSocket);
219: } catch (Exception e) {
220: // logger.log(Level.SEVERE, "CachedSSLSocketFactory socket error",
221: // e);
222: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR008", e);
223: }
224:
225: return cachedSocket;
226: }
227:
228: /**
229: * Get a CachedSocket with a Socket of appropriate type. The CachedSocket
230: * returned could be a newly created one or one already in the socket cache
231: * ready for reuse.
232: *
233: * @param host
234: * the host address the client socket attaches to (d%.d%.d%.d%
235: * format)
236: * @param port
237: * the port number to bind to the Socket
238: * @param socketType
239: * the type of socket to create inside the CachedSocket
240: * @return a CachedSocket bound to the designated host:port and containing a
241: * Socket of the requested type. This value is null if socket
242: * creation failed.
243: *
244: */
245: // public static CachedSocket getCachedSocket(String host, int port, String
246: // socketType, Integer logId) {
247: /* CachedSocket cachedSocket = null; */
248:
249: // Try to get a socket already in the cache
250: // There are problems with InetAddress.getByName().getHostAddress()
251: // -- sometimes hangs for a period of time.
252: // cachedSocket = _cache.getSocket(host, port, socketType);
253: // If cachedSocket is null, no cached socket was found.
254: // Create a new socket and put it in the cache
255: /*
256: * if (cachedSocket == null) { cachedSocket = createSocket(host, port,
257: * socketType, logId); }
258: *
259: * return cachedSocket;
260: */
261:
262: // return createSocket(host, port, socketType, logId);
263: // }
264: /**
265: * Get a CachedSocket with a Socket of appropriate type. The CachedSocket
266: * returned could be a newly created one or one already in the socket cache
267: * ready for reuse.
268: *
269: * @param host
270: * the host address the client socket attaches to (d%.d%.d%.d%
271: * format)
272: * @param port
273: * the port number to bind to the Socket
274: * @param socketType
275: * the type of socket to create inside the CachedSocket
276: * @return a CachedSocket bound to the designated host:port and containing a
277: * Socket of the requested type. This value is null if socket
278: * creation failed.
279: *
280: */
281: // public static CachedSocket getNewCachedSocket(String host, int port,
282: // String socketType, Integer logId) {
283: // get a newly created socket that was just put in the cache
284: // return createSocket(host, port, socketType, logId);
285: // }
286: }
|