001: package com.sun.portal.proxylet.client.common.browser;
002:
003: import com.sun.portal.proxylet.client.common.ui.AbstractEventHandler;
004: import com.sun.portal.proxylet.client.common.ui.ProxyletUI;
005: import com.sun.portal.proxylet.client.common.Log;
006: import com.sun.portal.proxylet.client.common.Param;
007: import com.sun.portal.proxylet.client.common.SessionTimeoutException;
008: import com.sun.portal.proxylet.client.common.server.Server;
009:
010: import javax.swing.*;
011:
012: import netscape.javascript.JSObject;
013:
014: import java.io.File;
015: import java.io.IOException;
016: import java.awt.*;
017: import java.net.URL;
018:
019: /**
020: * Created by IntelliJ IDEA.
021: * User: Administrator
022: * Date: Mar 9, 2005
023: * Time: 11:36:09 AM
024: * To change this template use File | Settings | File Templates.
025: */
026: public class MozillaEventHandler extends AbstractEventHandler {
027: JSObject win;
028: Server connection;
029: boolean stopped = false;
030:
031: public void handleStop() {
032: System.out.println("Stopping Proxylet...");
033:
034: if (stopped == true)
035: return;
036:
037: ProxyletUI.setCursor(Cursor
038: .getPredefinedCursor(Cursor.WAIT_CURSOR));
039: ProxyletUI.progressIndicator.setString("");
040: ProxyletUI.startProxyletB.setEnabled(false);
041: ProxyletUI.stopProxyletB.setEnabled(false);
042: if (ProxyletUI.progressIndicator.isIndeterminate())
043: ProxyletUI.progressIndicator.setIndeterminate(false);
044:
045: try {
046:
047: ProxyletUI.progressIndicator.setValue(10);
048: ProxyletUI.progressIndicator.setString("10%");
049:
050: // Restore Proxy settings
051: win.call("resetProxysetting", null);
052: ProxyletUI.progressIndicator.setValue(40);
053: ProxyletUI.progressIndicator.setString("40%");
054:
055: // Nullify resource
056: Param.nullifyResource();
057: ProxyletUI.progressIndicator.setValue(60);
058: ProxyletUI.progressIndicator.setString("60%");
059:
060: // Stop server now..
061: connection.stop();
062: ProxyletUI.progressIndicator.setValue(80);
063: ProxyletUI.progressIndicator.setString("80%");
064:
065: // Indicate that we are done to the server
066: try {
067: Param.sendMsgtoServlet("?command=setJWSUnLoaded",
068: false, false, null);
069: } catch (SessionTimeoutException e) {
070: // ignore..session has timedout and we are closing anyway...
071: }
072:
073: ProxyletUI.progressIndicator.setValue(100);
074: ProxyletUI.progressIndicator.setString("100%");
075:
076: // remove PAC file
077: String location = Param.getPacfileLocation();
078: File f = new File(location);
079: if (f.exists())
080: f.delete();
081:
082: stopped = true;
083: ProxyletUI.setCursor(null);
084:
085: if (ProxyletUI.frame instanceof JFrame) {
086: ((JFrame) (ProxyletUI.frame)).dispose();
087: ProxyletUI.frame.setVisible(false);
088: }
089:
090: } catch (Exception e) {
091: Log.debug(e.getMessage());
092: }
093:
094: }
095:
096: public void handleStart(boolean firstTime) {
097: ProxyletUI.setCursor(Cursor
098: .getPredefinedCursor(Cursor.WAIT_CURSOR));
099: ProxyletUI.progressIndicator.setString("");
100: ProxyletUI.stopProxyletB.setEnabled(false);
101: ProxyletUI.startProxyletB.setEnabled(false);
102: Log.info(Param.getString("pinf.3",
103: "Please wait while Proxylet configures your browser"));
104:
105: try {
106:
107: ProxyletUI.progressIndicator.setValue(10);
108: ProxyletUI.progressIndicator.setString("10%");
109:
110: if (!firstTime) {
111: // Load resource bundle
112: try {
113: Param.loadResourceBundle(true);
114: } catch (Exception ignore) {
115: Log
116: .info(Param
117: .getString("pexcp.1",
118: "Failed to get resource bundle for locale. Defaulting to en_US"));
119: }
120: } else {
121: // Stop download Mgr
122: Param.sendMsgToSelf(Param.getDownloadMgrPort(),
123: "CLOSESOCKET", null);
124: }
125:
126: ProxyletUI.progressIndicator.setValue(30);
127: ProxyletUI.progressIndicator.setString("30%");
128:
129: //
130: // Read existing proxy information
131: //
132: String proxyMode, proxyModeInfo, autoConfigURL;
133:
134: JApplet appletContext = (JApplet) ProxyletUI.frame;
135: win = JSObject.getWindow(appletContext);
136: win.call("readProxySetting", null);
137: proxyMode = copyFromHTML("getProxyMode");
138: proxyModeInfo = copyFromHTML("getProxyModeInfo");
139: autoConfigURL = copyFromHTML("getAutoConfigURL");
140:
141: Log.debugu("Connection Type" + proxyMode);
142: Log.debugu("Proxy Info " + proxyModeInfo);
143: Log.debugu("AutoConfig URL " + autoConfigURL);
144:
145: ProxyletUI.progressIndicator.setValue(50);
146: ProxyletUI.progressIndicator.setString("50%");
147: // Process proxy information
148:
149: long proxyTypeProxy = 1;
150: long proxyTypeAutoProxyUrl = 2;
151: long proxyTypeAutoDetect = 4;
152:
153: String configLocation = BrowserHelper.processProxyInfo(
154: proxyTypeProxy, proxyTypeAutoProxyUrl,
155: proxyTypeAutoDetect, ProxyletUI.frame, proxyMode,
156: autoConfigURL, proxyModeInfo);
157:
158: ProxyletUI.progressIndicator.setValue(70);
159: ProxyletUI.progressIndicator.setString("70%");
160: Log.debug("Config Location " + configLocation);
161:
162: // Configure proxy information
163: Object arg[] = new Object[1];
164: arg[0] = configLocation;
165: win.call("setproxysetting", arg);
166: ProxyletUI.progressIndicator.setValue(80);
167: ProxyletUI.progressIndicator.setString("80%");
168:
169: // Invoke proxy server... if already running just resume it to
170: // accept connections..
171: if (Server.running == -1) {
172: System.out.println("Server not running..start it");
173: connection = new Server(Param.getBindPort(), this );
174: Thread t = new Thread(connection);
175: t.start();
176: } else {
177: System.out
178: .println("server running already..resume operations.");
179: connection.resume();
180: }
181:
182: ProxyletUI.progressIndicator.setValue(100);
183: ProxyletUI.progressIndicator.setString("100%");
184: } catch (Exception e) {
185: Log.debug(e.getMessage());
186: }
187:
188: ProxyletUI.startProxyletB.setEnabled(false);
189: ProxyletUI.stopProxyletB.setEnabled(true);
190: ProxyletUI.progressIndicator.setValue(0);
191: ProxyletUI.progressIndicator.setString("0%");
192: ProxyletUI.progressIndicator.setString(Param.getString(
193: "pinfo.23", "Proxylet Started Successfully"));
194: ProxyletUI.setText(Param.getString("pinf.4",
195: "Proxylet Initialized Successfully"));
196: ProxyletUI
197: .setText(Param
198: .getString("pinf.5",
199: "Please click on help if you are not sure how to proceed further"));
200: ProxyletUI.setCursor(null);
201: }
202:
203: public void handleSuspend() {
204: ProxyletUI.setCursor(Cursor
205: .getPredefinedCursor(Cursor.WAIT_CURSOR));
206:
207: ProxyletUI.startProxyletB.setEnabled(false);
208: ProxyletUI.stopProxyletB.setEnabled(false);
209: if (ProxyletUI.progressIndicator.isIndeterminate())
210: ProxyletUI.progressIndicator.setIndeterminate(false);
211: ProxyletUI.progressIndicator.setString("");
212:
213: try {
214:
215: ProxyletUI.progressIndicator.setValue(10);
216: ProxyletUI.progressIndicator.setString("10%");
217:
218: // Restore Proxy settings
219: win.call("resetProxysetting", null);
220: ProxyletUI.progressIndicator.setValue(40);
221: ProxyletUI.progressIndicator.setString("40%");
222:
223: // Nullify resource
224: Param.nullifyResource();
225: ProxyletUI.progressIndicator.setValue(60);
226: ProxyletUI.progressIndicator.setString("60%");
227:
228: // Stop server now..
229: connection.stop();
230: ProxyletUI.progressIndicator.setValue(100);
231: ProxyletUI.progressIndicator.setString("100%");
232:
233: // remove PAC file
234: String location = Param.getPacfileLocation();
235: File f = new File(location);
236: if (f.exists())
237: f.delete();
238:
239: } catch (Exception e) {
240: Log.debug(e.getMessage());
241: }
242: Log
243: .info(Param
244: .getString(
245: "pinfo.8",
246: "Suspending Proxylet operations. You may restart Proxylet anytime.\nTo restart Proxylet, click on the Start button"));
247: ProxyletUI.startProxyletB.setEnabled(true);
248: ProxyletUI.progressIndicator.setValue(0);
249: ProxyletUI.progressIndicator.setString("0%");
250: ProxyletUI.setCursor(null);
251: ProxyletUI.progressIndicator.setString(Param.getString(
252: "pinfo.24", "Proxylet Suspended Successfully"));
253: }
254:
255: /**
256: * This function invokes a function ( function name passed in as a parameter)
257: * and gets the value of the form element
258: * @param funcName script name to invoke
259: * @return
260: */
261: protected String copyFromHTML(String funcName) {
262: String copy = "";
263: String msg[] = new String[1];
264: try {
265: msg[0] = "";
266: copy = (win.call(funcName, msg)).toString();
267: } catch (Exception ex) {
268: Log.debug("Could not value:" + ex.getMessage());
269: ex.printStackTrace();
270: }
271: return copy;
272: }
273:
274: public void handleHelp() {
275: try {
276: String lang = Param.getString("lang", "en");
277: String helpFileName = "proxylet.htm";
278: String servletURL = Param.getServletURL();
279: int ls = servletURL.lastIndexOf("/",
280: servletURL.length() - 2);
281: String helpLink = servletURL.substring(0, ls) + "/docs/"
282: + lang + "/proxylet/" + helpFileName;
283:
284: Log.debugu("helpLink = " + helpLink);
285: ((JApplet) ProxyletUI.frame).getAppletContext()
286: .showDocument(new URL(helpLink), "_blank");
287: } catch (IOException e) {
288: e.printStackTrace();
289: Log.debugu("could not launch help browser.");
290: }
291: }
292: }
|