001: /*
002: * CachedSocketFactory.java
003: *
004: * $Author: ss150821 $
005: *
006: * $Date: 2005/09/21 11:28:55 $ $Revision: 1.12 $
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.io.IOException;
016: import java.net.Socket;
017: import java.util.logging.Level;
018: import java.util.logging.Logger;
019:
020: import com.sun.portal.log.common.PortalLogger;
021: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
022: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
023: import com.sun.portal.util.GWLocale;
024: import com.sun.portal.util.GWLogManager;
025: import com.sun.portal.util.SystemProperties;
026: import com.sun.portal.util.SRAEvent;
027:
028: /**
029: * This class defines a factory used to create CachedSockets. The types of
030: * Sockets that it can create are defined in a configuration file called
031: * CachedSocketFactory.config.
032: *
033: * @author Kevin Hartig
034: */
035:
036: public class CachedSocketFactory {
037:
038: // private static SocketCache _cache;
039: private static final String BLOCKED_SOCKET_TIMEOUT = "BlockedSocketTimeout";
040:
041: private static final int DEFAULT_SOCKET_TIMEOUT = 200;
042:
043: public static int _timeout;
044:
045: // private static Logger logger =
046: // Logger.getLogger("com.sun.portal.sra.rproxy");
047: private static Logger logger = PortalLogger
048: .getLogger(CachedSocketFactory.class);
049:
050: static {
051: _timeout = GatewayProfile.getInt(BLOCKED_SOCKET_TIMEOUT,
052: DEFAULT_SOCKET_TIMEOUT) * 1000;
053: // _cache = new SocketCache();
054: }
055:
056: /**
057: * Create a new socket.
058: *
059: * @param host
060: * the host address the client socket attaches to (d%.d%.d%.d%
061: * format)
062: * @param port
063: * the port number to bind to the Socket
064: * @param socketType
065: * the type of socket to create inside the CachedSocket
066: * @return a CachedSocket bound to the designated host:port and containing a
067: * Socket of the requested type. This value is null if socket
068: * creation failed.
069: */
070: private static CachedSocket createSocket(String host, int port,
071: String socketType, Integer logId) {
072: CachedSocket cachedSocket = null;
073: Socket socket = null;
074:
075: // Construct the socket desired and pass it to a newly constructed
076: // CachedSocket.
077: // logger.info("Connecting to " + host + ":" + port);
078: Object[] params0 = { host, ":", port + "" };
079: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR009", params0);
080:
081: try {
082: socket = new Socket(host, port);
083: MonitoringSubsystem
084: .handleEvent(SRAEvent.PLAIN_SOCKET_CREATED);
085: } catch (Exception se) {
086: // For some reason, we cannot open a new socket, retry!
087: // logger.log(Level.SEVERE, "CachedSocketFactory cannot open
088: // connection to " + host + ":" + port, se);
089: Object[] params1 = { host, ":", port + "", se };
090: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR010",
091: params1);
092:
093: String retryTimes = SystemProperties
094: .get("gateway.sockretries");
095: if (retryTimes == null) {
096: return null;
097: }
098:
099: int retries = Integer.parseInt(retryTimes);
100: for (int i = 1; i <= retries; i++) {
101: try {
102: Thread.sleep(3000); // sleep for 3 seconds
103: } catch (InterruptedException ie) {
104: }
105:
106: try {
107: logger
108: .warning("CachedSocketFactory: Open new socket; retry #"
109: + i);
110: socket = new Socket(host, port);
111: MonitoringSubsystem
112: .handleEvent(SRAEvent.PLAIN_SOCKET_CREATED);
113: if (socket != null)
114: break; // socket creation retry is successful
115: } catch (Exception rse) {
116: // logger.log(Level.SEVERE, "CachedSocketFactory cannot open
117: // connection to " + host + ":" + port, rse);
118: Object[] params2 = { host, ":", port + "", rse };
119: logger.log(Level.SEVERE,
120: "PSSRRPROXY_CSPRCONHNDLR011", params2);
121: }
122: }
123:
124: if (socket == null)
125: return null;
126: }
127:
128: if (GWLogManager.loggingEnabled) {
129: GWLogManager.write("RProxy", GWLocale.getPFString("csf1",
130: new Object[] { logId, host,
131: new Integer(socket.getPort()) }));
132: }
133: try {
134: socket.setSoTimeout(_timeout);
135: socket.setTcpNoDelay(true);
136:
137: cachedSocket = new CachedSocket(socket);
138: /*
139: * cachedSocket.setType(socketType); cachedSocket.setActive();
140: */
141: // cachedSocket.setSocketCache(_cache);
142: // _cache.putSocket(cachedSocket);
143: } catch (Exception e) {
144: // logger.log(Level.SEVERE, "CachedSocketFactory socket error", e);
145: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR012", e);
146: if (socket != null) {
147: try {
148: socket.close();
149: MonitoringSubsystem
150: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
151: } catch (IOException e1) {
152: }
153: /**
154: * Bug 4710658
155: */
156: finally {
157: socket = null;
158: }
159: // End of code change for Bug 4710658
160: }
161: }
162:
163: return cachedSocket;
164: }
165:
166: /**
167: * Get a CachedSocket with a Socket of appropriate type. The CachedSocket
168: * returned could be a newly created one or one already in the socket cache
169: * ready for reuse.
170: *
171: * @param host
172: * the host address the client socket attaches to (d%.d%.d%.d%
173: * format)
174: * @param port
175: * the port number to bind to the Socket
176: * @param socketType
177: * the type of socket to create inside the CachedSocket
178: * @return a CachedSocket bound to the designated host:port and containing a
179: * Socket of the requested type. This value is null if socket
180: * creation failed.
181: *
182: */
183: public static CachedSocket getCachedSocket(String host, int port,
184: String socketType, Integer logId) {
185: // Try to get a socket already in the cache
186: // There are problems with InetAddress.getByName().getHostAddress()
187: // -- sometimes hangs for a period of time. Hence, use CachedSockets
188:
189: return createSocket(host, port, socketType, logId);
190: }
191:
192: /**
193: * Get a CachedSocket with a Socket of appropriate type. The CachedSocket
194: * returned could be a newly created one or one already in the socket cache
195: * ready for reuse.
196: *
197: * @param host
198: * the host address the client socket attaches to (d%.d%.d%.d%
199: * format)
200: * @param port
201: * the port number to bind to the Socket
202: * @param socketType
203: * the type of socket to create inside the CachedSocket
204: * @return a CachedSocket bound to the designated host:port and containing a
205: * Socket of the requested type. This value is null if socket
206: * creation failed.
207: *
208: */
209: public static CachedSocket getNewCachedSocket(String host,
210: int port, String socketType, Integer logId) {
211: // get a newly created socket that was just put in the cache
212: return createSocket(host, port, socketType, logId);
213: }
214: }
|