001: // Copyright 02/29/00 Sun Microsystems, Inc. All Rights Reserved.
002: //#pragma ident "@(#)Connection.java 1.2 00/02/29 Sun Microsystems"
003:
004: package com.sun.portal.netlet.client.common;
005:
006: import java.io.IOException;
007: import java.net.Socket;
008: import java.net.SocketException;
009: import java.net.UnknownHostException;
010:
011: class Connection {
012: protected Socket conn;
013:
014: private DoConn conn_obj;
015: private Thread conn_thread;
016:
017: public Connection(int p, String h) {
018: int count = 0;
019: while ((conn == null) && count < 10) {
020: conn_obj = new DoConn(p, h);
021: conn_thread = new Thread(conn_obj);
022: conn_thread.start();
023: try {
024: conn_thread.join(5000);
025: } catch (InterruptedException e) {
026: System.out.println("SConn thread interrupted");
027: e.printStackTrace();
028: }
029: conn = conn_obj.getconn();
030: if (conn == null) {
031: if (conn_thread.isAlive()) {
032: conn_thread.stop();
033: }
034: conn_obj = null;
035: }
036: conn_thread.stop();
037: conn_thread = null;
038: count++;
039: }
040: }
041:
042: public Socket getconn() {
043: return (conn);
044: }
045:
046: public void clean() {
047: conn = null;
048: if (conn_obj != null) {
049: conn_obj.clean();
050: conn_obj = null;
051: }
052: }
053: }
054:
055: final class DoConn implements Runnable {
056:
057: private int port;
058: private String host;
059: private Socket conn;
060:
061: public DoConn(int p, String h) {
062: port = p;
063: host = h;
064: }
065:
066: public void clean() {
067: host = null;
068: conn = null;
069: }
070:
071: public void run() {
072: conn = null;
073: BrowserType browser = BrowserType.getInstance();
074: if (browser.getNetscapeBrowser()
075: && browser.getJavaVMVersion() == 1) {
076: try {
077: BrowserType.SecurityCall sc = browser
078: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_CONNECT);
079: sc.getMethod().invoke(null, sc.getArgs());
080: sc = browser
081: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_LISTEN);
082: sc.getMethod().invoke(null, sc.getArgs());
083: sc = browser
084: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_ACCEPT);
085: sc.getMethod().invoke(null, sc.getArgs());
086: } catch (Exception e) {
087: System.out
088: .println("Unknown exception while enabling privilege.");
089: }
090: }
091: try {
092: conn = new Socket(host, port);
093: conn.setTcpNoDelay(true);
094: } catch (UnknownHostException e) {
095: System.out.println("Socket DoConn unknown host: " + e);
096: } catch (SocketException e) {
097: System.out.println("Socket DoConn tcpNoDelay exception: "
098: + e);
099: } catch (IOException e) {
100: System.out.println("Socket DoConn connect exception: " + e);
101: }
102: }
103:
104: public Socket getconn() {
105: return (conn);
106: }
107: }
|