001: package com.sun.portal.netlet.client.jnlp;
002:
003: import com.sun.portal.netlet.client.common.*;
004:
005: import java.net.URLEncoder;
006: import java.util.*;
007: import java.io.InputStream;
008: import java.io.IOException;
009:
010: public class NetletHandler {
011:
012: private boolean netletRunning = false;
013: private boolean netletLoaded = false;
014: Hashtable clientPorts;
015:
016: private SClientMgr scm = null;
017:
018: public NetletHandler(Hashtable params) {
019: ClientConfig.addParams(params);
020: try {
021: Class.forName("javax.net.ssl.SSLContext");
022: ClientConfig.addParam("isJSSEEnabled", "true");
023: } catch (ClassNotFoundException e) {
024: System.out.println("JSSE Not supported");
025: }
026: ClientConfig.addParam("codebaseURL", ClientManager
027: .getCodeBaseUrl());
028: String proxyletEnabledParameter = System
029: .getProperty("com.sun.portal.proxylet.proxyletMode");
030: if (proxyletEnabledParameter != null
031: && proxyletEnabledParameter.equals("true")) {
032: ClientConfig.addParam("proxyletMode", "true");
033: }
034: }
035:
036: public void setNetletLoaded() {
037: if (!netletLoaded) {
038: // All params loaded.
039: ClientConfig.init();
040: loadProxyDetails();
041: netletLoaded = true;
042: }
043: }
044:
045: private void loadProxyDetails() {
046: ProxyInfo jwsPI = new JWSProxyInfo();
047: ClientConfig.loadProxyDetails(jwsPI);
048: }
049:
050: public void startClient() {
051: boolean addNetletTab = false;
052: if (!netletLoaded) {
053: //netlet not loaded, get params from server.
054: InputStream is = ClientUtil.netletConfig("loadNetletArgs");
055: Properties props = new Properties();
056: try {
057: props.load(is);
058: } catch (IOException e) {
059: System.err.println("Unable to load netlet args");
060: }
061: Hashtable newParams = (Hashtable) props.values();
062: //add params
063: ClientConfig.addParams(newParams);
064: setNetletLoaded();
065: addNetletTab = true;
066: }
067: if (netletRunning) {
068: return;
069: }
070: if (scm == null) {
071: scm = new SClientMgr(ClientConfig.getNumOfRules());
072: }
073:
074: loadRules();
075:
076: if (scm != null) {
077: scm.start();
078: /**
079: * wait until all the SClient threads start, and send the clientport
080: * information to the servlet.
081: */
082: scm.waitToStart();
083: netletRunning = true;
084: System.out.println("netlet started");
085: String clntPorts = scm.getFormattedClientPorts();
086: String clntListenURL = ClientManager.getClientListener()
087: .getClientListenerURL();
088: ClientUtil.sendByPost("setJWSLoaded&jwsListenURL="
089: + URLEncoder.encode(clntListenURL), "clientPorts="
090: + URLEncoder.encode(clntPorts));
091: }
092:
093: if (ClientManager.getSRAContentPane() != null) {
094: if (addNetletTab) {
095: ClientManager.getSRAContentPane().refresh();
096: } else {
097: ClientManager.getSRAContentPane().refreshNetletTab();
098: }
099: }
100: }
101:
102: private void loadRules() {
103: int srcPort = 0;
104: String ruleName;
105: String serverHost;
106: String serverPort;
107: String threadname;
108: String cipherName;
109:
110: int nor = ClientConfig.getNumOfRules();
111:
112: for (int i = 0; i < nor; i++) {
113: // read in the parms...
114: try {
115: srcPort = Integer.parseInt(ClientConfig
116: .getParam("listenPort_" + i));
117: } catch (NumberFormatException n) {
118: System.out
119: .println("Netlet unable to parse source port: "
120: + i);
121: }
122:
123: ruleName = ClientConfig.getParam("ruleName_" + i);
124: serverHost = ClientConfig.getParam("serverHost_" + i);
125: serverPort = ClientConfig.getParam("serverPort_" + i);
126:
127: System.out.println("Netlet rule " + i + ": local:"
128: + srcPort + " destination:" + serverHost + ":"
129: + serverPort);
130:
131: // create "unique" thread name...
132: threadname = "atprox_" + i + "_"
133: + ClientConfig.getDestPort() + "_"
134: + ClientConfig.getDestHost();
135:
136: cipherName = ClientConfig.getParam("cipher_" + i);
137:
138: scm.addconfig(ruleName, srcPort, serverPort, serverHost,
139: threadname, cipherName);
140: }
141: }
142:
143: public Hashtable getClientPorts() {
144: return scm.getClientPorts();
145: }
146:
147: public boolean netletClientRuning() {
148: return netletRunning;
149: }
150:
151: public boolean netletClientLoaded() {
152: return netletLoaded;
153: }
154:
155: public void stopClient(boolean sendMessage) {
156: if (netletRunning) {
157: scm.stopProcessing();
158: if (scm != null) {
159: scm.stop();
160: scm = null;
161: }
162: if (sendMessage) {
163: // Avoid sending message while unloading
164: ClientUtil.netletConfig("stopped");
165: }
166: netletRunning = false;
167: }
168: }
169:
170: public void unloadNetlet() {
171: stopClient(false);
172: ClientUtil.netletConfig("unload");
173: netletLoaded = false;
174: }
175:
176: }
|