001: package com.sun.portal.netlet.eproxy;
002:
003: import java.io.IOException;
004: import java.net.ServerSocket;
005: import java.net.Socket;
006: import java.net.SocketException;
007: import java.util.logging.Level;
008: import java.util.logging.Logger;
009:
010: import com.sun.portal.log.common.PortalLogger;
011: import com.sun.portal.util.GWLocale;
012: import com.sun.portal.util.GWLogManager;
013: import com.sun.portal.util.SRAPSocket;
014: import com.sun.portal.util.SRAEvent;
015: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
016:
017: abstract public class ProxyConnection {
018:
019: // The "dummyLockString" is used to synchronize calls to GW.run_GW() and
020: // GW.unrun_GW()
021: private static String dummyLockString = "DummyLockString";
022:
023: private String threadName = null;
024:
025: private ServerSocket serverSocket = null;
026:
027: private String connectionInfoKey = null;
028:
029: private static Logger logger = PortalLogger
030: .getLogger(ProxyConnection.class);
031:
032: public ProxyConnection(String threadName, String connectionInfoKey) {
033: this .threadName = threadName;
034: this .connectionInfoKey = connectionInfoKey;
035: }
036:
037: public void start() throws SocketException {
038:
039: Thread.currentThread().setName(threadName);
040: serverSocket = makeServerSocket();
041: Socket inconnection;
042:
043: Integer logid = new Integer(0);
044: GW.run_GW();
045:
046: while (serverSocket != null) {
047: inconnection = null;
048: try {
049:
050: try {
051: inconnection = serverSocket.accept();
052: incrementSocketCount();
053: } catch (SocketException se) {
054: continue; // Famous Fix
055: } catch (IOException e) {
056: // logger.log(Level.SEVERE, threadName + " cannot accept new
057: // connection on " + serverSocket, e);
058: Object[] params0 = {
059: " cannot accept new connection on ",
060: serverSocket, e };
061: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX051",
062: params0);
063:
064: closeServerSocket();
065: // hack, if io error, retry?
066: resetServerSocket();
067: continue;
068: }
069:
070: if (GWLogManager.loggingEnabled) {
071: logid = new Integer(GWLogManager.getLogId());
072: // System.out.println("Before write ... " + inconnection);
073: // System.out.println("threadName ... " + threadName);
074: // System.out.println("connectionInfoKey ... " +
075: // connectionInfoKey);
076:
077: try {
078: GWLogManager
079: .write(
080: threadName,
081: GWLocale
082: .getPFString(
083: connectionInfoKey,
084: new Object[] {
085: logid,
086: inconnection
087: .getInetAddress()
088: .toString(),
089: new Integer(
090: inconnection
091: .getPort()) }));
092: } catch (Exception ex) {
093: // ex.printStackTrace();
094: }
095: }
096:
097: try {
098: inconnection.setTcpNoDelay(true);
099: } catch (SocketException e) {
100: // logger.log(Level.SEVERE, threadName + " cannot set
101: // TcpNoDelay on " + inconnection, e);
102: Object[] params1 = { " cannot set TcpNoDelay on ",
103: inconnection, e };
104: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX052",
105: params1);
106:
107: closeSocket(inconnection);
108: inconnection = null;
109: continue;
110: }
111:
112: process(new SRAPSocket(inconnection), logid);
113:
114: } catch (Throwable t) {
115: // logger.log(Level.SEVERE, threadName + ": Uncaught
116: // exception:", t);
117: Object[] params2 = { ": Uncaught exception:", t };
118: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX053",
119: params2);
120: }
121: }
122: stop();
123: }
124:
125: private void resetServerSocket() throws SocketException {
126:
127: try {
128: Thread.sleep(5);
129: } catch (InterruptedException ee) {
130: }
131:
132: // now, reopen the connection and listen for requests...
133: synchronized (dummyLockString) {
134: GW.unrun_GW();
135: GW.setNumberOfInstances(1);
136: serverSocket = makeServerSocket();
137: GW.run_GW();
138: }
139: }
140:
141: protected void closeSocket(Socket socket) {
142:
143: try {
144: socket.close();
145: boolean isHTTPS = serverSocket instanceof org.mozilla.jss.ssl.SSLServerSocket;
146: if (isHTTPS) {
147: MonitoringSubsystem
148: .handleEvent(SRAEvent.SSL_SOCKET_DESTROYED);
149: } else {
150: MonitoringSubsystem
151: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
152: }
153:
154: } catch (IOException ee) {
155: // logger.log(Level.SEVERE, threadName + ": IO Exception while
156: // closing server socket:", ee);
157: Object[] params3 = {
158: ": IO Exception while closing server socket:", ee };
159: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX054", params3);
160: } finally {
161: socket = null;
162: }
163: }
164:
165: private void closeServerSocket() {
166:
167: /**
168: * Bug 4710658
169: */
170: if (serverSocket != null) {
171: try {
172: serverSocket.close();
173: } catch (IOException ee) {
174: // logger.log(Level.SEVERE, threadName + " cannot close server
175: // socket", ee);
176: Object[] params4 = { " cannot close server socket", ee };
177: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX055",
178: params4);
179: } finally {
180: MonitoringSubsystem
181: .handleEvent(SRAEvent.SERVER_SOCKET_DESTROYED);
182: serverSocket = null;
183: }
184: }
185: // End of code change for Bug 4710658
186:
187: }
188:
189: private void incrementSocketCount() {
190: boolean isHTTPS = serverSocket instanceof org.mozilla.jss.ssl.SSLServerSocket;
191:
192: if (isHTTPS) {
193: MonitoringSubsystem
194: .handleEvent(SRAEvent.SSL_SOCKET_CREATED);
195: } else {
196: MonitoringSubsystem
197: .handleEvent(SRAEvent.PLAIN_SOCKET_CREATED);
198: }
199: }
200:
201: private void stop() {
202: // logger.severe("FATAL ERROR: " + threadName + " exited");
203: Object[] params5 = { threadName, " exited" };
204: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX056", params5);
205: }
206:
207: abstract protected ServerSocket makeServerSocket()
208: throws SocketException;
209:
210: abstract protected void process(Socket connection, Integer logId);
211:
212: }
|