001: // @(#)RWGroupCrypt.java 1.13 "@(#)RWGroupCrypt.java 1.13 99/09/23 Sun Microsystems"
002:
003: package com.sun.portal.netlet.eproxy;
004:
005: import java.net.Socket;
006: import java.util.logging.Level;
007: import java.util.logging.Logger;
008:
009: import com.sun.portal.log.common.PortalLogger;
010: import com.sun.portal.netlet.econnection.ReaderWriter;
011: import com.sun.portal.netlet.econnection.ReaderWriterDecrypt;
012: import com.sun.portal.netlet.econnection.ReaderWriterEncrypt;
013: import com.sun.portal.netlet.econnection.ReaderWriterLock;
014: import com.sun.portal.rproxy.configservlet.client.NetletProfile;
015: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
016: import com.sun.portal.util.GWLocale;
017: import com.sun.portal.util.GWThreadPool;
018: import com.sun.portal.util.NetletLogMgr;
019: import com.sun.portal.util.SRAEvent;
020:
021: public class RWGroupCrypt extends ReaderWriterLock {
022:
023: public boolean done = false;
024: private int srcPort;
025:
026: private String destPort;
027:
028: private String destHost;
029:
030: private Socket fromClient;
031:
032: private Socket toServer;
033:
034: private ReaderWriterDecrypt src_to_dst;
035:
036: private ReaderWriterEncrypt dst_to_src;
037:
038: private SessionAuthenticator sessionAuth;
039:
040: private SessionRequest sessionReq;
041:
042: private Integer netletLogId;
043:
044: private long startTime;
045:
046: private boolean stopLogged = false;
047:
048: private static Logger logger = PortalLogger
049: .getLogger(RWGroupCrypt.class);
050:
051: public ReaderWriterDecrypt getReaderWriterDecrypt() {
052: return src_to_dst;
053: }
054:
055: public ReaderWriterEncrypt getReaderWriterEncrypt() {
056: return dst_to_src;
057: }
058:
059: public String getDestPort() {
060: return destPort;
061: }
062:
063: public String getDestHost() {
064: return destHost;
065: }
066:
067: public RWGroupCrypt(Socket in, int sport, String dport,
068: String dhost, SessionAuthenticator sa, SessionRequest rq) {
069: fromClient = in;
070: srcPort = sport;
071: destPort = new String(dport);
072: destHost = new String(dhost);
073: sessionAuth = sa;
074: sessionReq = rq;
075:
076: // do the connect in here to see if we can "save" the connect obj
077: // open connection to remote for this client
078: toServer = SConn.connect(destPort, destHost);
079:
080: if (toServer == null) {
081: // logger.severe("RWGroupCrypt: unable to make server connection!");
082: logger.severe("PSSRNTLT_CSPNEPROX075");
083: cleanup();
084: return;
085: }
086:
087: try {
088: toServer.setKeepAlive(true);
089: fromClient.setKeepAlive(true);
090: } catch (Exception ex) {
091: // logger.log(Level.SEVERE, "RWGroupCrypt: failed to set KeepAlive
092: // for socket " , ex);
093: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX076");
094: }
095:
096: // Added for Netlet PRD 5.1 for Lihue
097: if (NetletLogMgr.loggingEnabled) {
098: try {
099: netletLogId = new Integer(NetletLogMgr.getLogId());
100: NetletLogMgr.write("Netlet", GWLocale.getPFString(
101: "en1", new Object[] {
102: netletLogId,
103: new String(NetletLogMgr
104: .getUserId(sessionReq
105: .getSessionID())),
106: "START",
107: fromClient.getInetAddress().toString(),
108: new Integer(srcPort),
109: toServer.getInetAddress().toString(),
110: new Integer(toServer.getPort()) }));
111: } catch (Exception e) {
112: } // May cause null pointer exception
113: }
114:
115: if (getNetletKeepAliveInterval(rq) > 0) {
116: try {
117: toServer
118: .setSoTimeout(getNetletKeepAliveInterval(rq) * 1000);
119: } catch (Exception se) {
120: System.out.println("Unable to set socket time out.."
121: + se);
122: }
123: }
124:
125: try {
126: src_to_dst = new ReaderWriterDecrypt(this , fromClient
127: .getInputStream(), toServer.getOutputStream());
128: dst_to_src = new ReaderWriterEncrypt(this , toServer
129: .getInputStream(), fromClient.getOutputStream(),
130: getNetletKeepAliveInterval(rq));
131: } catch (Exception ex) {
132: // logger.log(Level.SEVERE, "RWGroupCrypt: Unable to create reader
133: // writers.", ex);
134: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX077");
135: }
136:
137: // start keep alive timer here, to make sure it starts
138: // before RW threads, so that KP is not left running amok!
139: /*
140: * if (ServiceIdentifier.isGateway() && !getNetletRunUponLogout(rq)) {
141: * kp = new KeepAlive(sessionAuth, sessionReq, this, src_to_dst,
142: * dst_to_src); try { GWThreadPool.run(kp); } catch
143: * (InterruptedException e) { // logger.log(Level.SEVERE, "Could not
144: * start KeepAlive", e);
145: * logger.log(Level.SEVERE,"PSSRNTLT_CSPNEPROX078"); } } else { kp =
146: * null; }
147: */
148: try {
149: GWThreadPool.run(src_to_dst);
150: GWThreadPool.run(dst_to_src);
151: } catch (InterruptedException e) {
152: // logger.log(Level.SEVERE, "Could not start ReaderWriterClear
153: // tasks", e);
154: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX079");
155: }
156: // System.out.println("Ran ReaderWriter....");
157: startTime = System.currentTimeMillis();
158: }
159:
160: public synchronized void notifyFinished(ReaderWriter obj) {
161: if (!stopLogged && NetletLogMgr.loggingEnabled) {
162: try {
163: netletLogId = new Integer(NetletLogMgr.getLogId());
164: NetletLogMgr.write("Netlet", GWLocale.getPFString(
165: "en1", new Object[] {
166: netletLogId,
167: new String(NetletLogMgr
168: .getUserId(sessionReq
169: .getSessionID())),
170: new String("STOP"),
171: fromClient.getInetAddress().toString(),
172: new Integer(srcPort),
173: toServer.getInetAddress().toString(),
174: new Integer(toServer.getPort()) }));
175: stopLogged = true;
176: } catch (Exception e) {
177: } // May cause Null pointer exception
178: }
179: if (obj == src_to_dst) {
180: if (dst_to_src != null) {
181: dst_to_src.stop();
182: }
183: } else if (obj == dst_to_src) {
184: if (src_to_dst != null) {
185: src_to_dst.stop();
186: }
187: }
188: cleanup();
189: }
190:
191: public synchronized void stopAll() {
192:
193: if (!done) {
194: if (dst_to_src != null) {
195: dst_to_src.stop();
196: }
197: if (src_to_dst != null) {
198: src_to_dst.stop();
199: }
200: cleanup();
201: }
202: }
203:
204: public void cleanup() {
205: try {
206: /*
207: * if (kp != null) { try { kp.stop(); // send one more to extend
208: * session from last action kp.sendRequest(); } catch (Exception e) { }
209: * finally { kp = null; } }
210: */if (fromClient != null) {
211: try {
212: fromClient.close();
213: } catch (Exception e) {
214: } finally {
215: MonitoringSubsystem
216: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
217: fromClient = null;
218: }
219: }
220: if (toServer != null) {
221: try {
222: toServer.close();
223: } catch (Exception e) {
224: } finally {
225: MonitoringSubsystem
226: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
227: toServer = null;
228: }
229: }
230: } finally {
231: src_to_dst = null;
232: dst_to_src = null;
233: done = true;
234: }
235: }
236:
237: public boolean isDone() {
238: return (done);
239: }
240:
241: /*
242: * Added for RFE 4492648
243: */
244:
245: public long getLastActivityTime() {
246:
247: if (src_to_dst.getLastActivityTime() > dst_to_src
248: .getLastActivityTime())
249: return src_to_dst.getLastActivityTime();
250: else
251: return dst_to_src.getLastActivityTime();
252:
253: }
254:
255: /*
256: * Added for RFE - 4645457 returns boolean which determines whether the
257: * netlet should continue upon logout or not @ returns false on error
258: */
259: /*
260: * private boolean getNetletRunUponLogout(SessionRequest rq) { try { String
261: * sid = rq.getSessionID(); NetletProfile profile = new NetletProfile(sid);
262: * return !(profile.getBoolean("TerminateAtLogout", true)); }
263: * catch(Exception e){ return false; } }
264: */
265:
266: private int getNetletKeepAliveInterval(SessionRequest rq) {
267: try {
268: String sid = rq.getSessionID();
269: NetletProfile profile = new NetletProfile(sid);
270: // logger.info("RWGroupCrypt: Netlet keep alive interval -> " +
271: // profile.getInt("KeepAliveInterval", 0));
272: Object[] params5 = { new Integer(profile.getInt(
273: "KeepAliveInterval", 0)) };
274: logger.log(Level.INFO, "PSSRNTLT_CSPNEPROX080", params5);
275: return profile.getInt("KeepAliveInterval", 0);
276: } catch (Exception e) {
277: return 0;
278: }
279: }
280:
281: public long getStartTime() {
282: return startTime;
283: }
284:
285: public int getAppletSrcPort() {
286: return srcPort;
287: }
288:
289: }
|