001: package com.sun.portal.netlet.eproxy;
002:
003: import java.net.Socket;
004: import java.util.logging.Level;
005: import java.util.logging.Logger;
006:
007: import com.sun.portal.log.common.PortalLogger;
008: import com.sun.portal.netlet.econnection.ReaderWriter;
009: import com.sun.portal.netlet.econnection.ReaderWriterClear;
010: import com.sun.portal.netlet.econnection.ReaderWriterLock;
011: import com.sun.portal.util.*;
012: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
013:
014: public class RWGroupClear extends ReaderWriterLock {
015: private Socket fromClient;
016:
017: private Socket toServer;
018:
019: private ReaderWriterClear src_to_dst;
020:
021: private ReaderWriterClear dst_to_src;
022:
023: private boolean src_to_dst_clean = false;
024:
025: private boolean dst_to_src_clean = false;
026:
027: private static Logger logger = PortalLogger
028: .getLogger(RWGroupClear.class);
029:
030: public RWGroupClear(Socket fromClient, int sslPort, Integer logId) {
031:
032: this .fromClient = fromClient;
033:
034: // do the connect in here to see if we can "save" the connect obj
035: // open connection to remote for this client
036: String bindAddress = SystemProperties
037: .get("gateway.bindipaddress");
038: if (bindAddress == null) {
039: bindAddress = "127.0.0.1";
040: }
041: toServer = SConn.connect(sslPort, bindAddress);
042: if (toServer == null) {
043: logger
044: .warning("RWGroupClear cannot open connection to localhost:"
045: + sslPort);
046: return;
047: }
048: // logger.info("RWGroupClear opened " + toServer);
049: Object[] params0 = { toServer };
050: logger.log(Level.INFO, "PSSRNTLT_CSPNEPROX069", params0);
051:
052: // Save the socket object for use during end to end SSL
053: // fromClient.getLocalPort - returnst the local port to which this
054: // socket is connected
055: //
056: String unqID = toServer.getLocalPort() + "_" + "A";
057: if (GWLogManager.logIdMap.containsKey(unqID))
058: GWLogManager.logIdMap.remove(unqID);
059:
060: GWLogManager.logIdMap.put(unqID, new LogInfoContainer(logId,
061: fromClient));
062:
063: // logger.info("RWGroupClear original non-ssl socket request mapped on
064: // port " + toServer.getLocalPort());
065: Object[] params1 = { toServer.getLocalPort() + "" };
066: logger.log(Level.INFO, "PSSRNTLT_CSPNEPROX070", params1);
067: // logger.info("non-ssl socket info " + fromClient);
068: Object[] params2 = { fromClient };
069: logger.log(Level.INFO, "PSSRNTLT_CSPNEPROX071", params2);
070:
071: try {
072: src_to_dst = new ReaderWriterClear(this , fromClient
073: .getInputStream(), toServer.getOutputStream(),
074: logId, fromClient.getOutputStream());
075: dst_to_src = new ReaderWriterClear(this , toServer
076: .getInputStream(), fromClient.getOutputStream(),
077: logId);
078: } catch (Exception ex) {
079: // logger.log(Level.SEVERE, "RWGroupCrypt: Unable to create reader
080: // writers.", ex);
081: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX072");
082: }
083:
084: src_to_dst.useKeepAliveLogic();
085: dst_to_src.useKeepAliveLogic();
086:
087: try {
088: GWThreadPool.run(src_to_dst);
089: GWThreadPool.run(dst_to_src);
090: } catch (InterruptedException e) {
091: // logger.log(Level.SEVERE, "Could not start ReaderWriterClear
092: // tasks", e);
093: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX073");
094: }
095: }
096:
097: public synchronized void notifyFinished(ReaderWriter obj) {
098: if (obj == src_to_dst) {
099: if (dst_to_src.isAlive()) {
100: dst_to_src.stop();
101: }
102: } else if (obj == dst_to_src) {
103: if (src_to_dst.isAlive()) {
104: src_to_dst.stop();
105: }
106: }
107: // Lihue Keep-Alive
108: cleanup();
109: // EOC :: Lihue Keep-Alive
110: if (obj == src_to_dst) {
111: src_to_dst_clean = true;
112: } else if (obj == dst_to_src) {
113: dst_to_src_clean = true;
114: }
115: }
116:
117: public void cleanup() {
118: if (fromClient != null) {
119: try {
120: fromClient.close();
121: if (fromClient instanceof org.mozilla.jss.ssl.SSLSocket) {
122: MonitoringSubsystem
123: .handleEvent(SRAEvent.SSL_SOCKET_DESTROYED);
124: } else {
125: MonitoringSubsystem
126: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
127: }
128: } catch (Exception e) {
129: e.printStackTrace();
130: } finally {
131: fromClient = null;
132: }
133: }
134:
135: if (toServer != null) {
136: try {
137: toServer.close();
138:
139: if (toServer instanceof org.mozilla.jss.ssl.SSLSocket) {
140: MonitoringSubsystem
141: .handleEvent(SRAEvent.SSL_SOCKET_DESTROYED);
142: } else {
143: MonitoringSubsystem
144: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
145: }
146: } catch (Exception e) {
147: } finally {
148: toServer = null;
149: }
150: }
151:
152: }
153:
154: public boolean isDone() {
155: if (dst_to_src_clean && src_to_dst_clean) {
156: dst_to_src = null;
157: src_to_dst = null;
158: }
159: return (dst_to_src_clean && src_to_dst_clean);
160: }
161:
162: /*
163: * Added for RFE 4492648
164: */
165:
166: public long getLastActivityTime() {
167: // Lihue - KeepAlive
168: // optimize it a bit and remove any possible
169: // errors due to simultaneous access to these variables
170: // from different threads
171: long stime = src_to_dst.getLastActivityTime();
172: long ctime = dst_to_src.getLastActivityTime();
173: if (stime > ctime)
174: return ctime;
175: else
176: return stime;
177: /*
178: * if(src_to_dst.getLastActivityTime() >
179: * dst_to_src.getLastActivityTime()) return
180: * src_to_dst.getLastActivityTime(); else return
181: * dst_to_src.getLastActivityTime();
182: */
183: // End of Code : Lihue - KeepAlive
184: }
185:
186: public synchronized void stopAll() {
187: // logger.info("WARNING STOPPING ALL SERVER");
188: logger.info("PSSRNTLT_CSPNEPROX074");
189: if (dst_to_src != null) {
190: dst_to_src.stop();
191: }
192: if (src_to_dst != null) {
193: src_to_dst.stop();
194: }
195: cleanup();
196: }
197:
198: /*
199: * Addded for Lihue PRD 1.8.1 for Lihue Dummy implementation
200: */
201:
202: public int getAppletSrcPort() {
203: return 0;
204: }
205:
206: public long getStartTime() {
207: return 0;
208: }
209:
210: }
|