001: package com.sun.portal.proxylet.client.common.browser;
002:
003: import java.io.*;
004: import java.awt.*;
005: import java.net.Socket;
006:
007: import com.sun.portal.proxylet.client.common.Log;
008: import com.sun.portal.proxylet.client.common.Param;
009: import com.sun.portal.proxylet.client.common.SessionTimeoutException;
010: import com.sun.portal.proxylet.client.common.server.Server;
011: import com.sun.portal.proxylet.client.common.ui.ProxyletUI;
012: import com.sun.portal.proxylet.client.common.ui.AbstractEventHandler;
013:
014: import javax.swing.*;
015:
016: /**
017: * User: @amitosh
018: * Date: Apr 19, 2005
019: * Time: 6:30:46 PM
020: */
021: public class MozillaEventHandler_JNLP extends AbstractEventHandler {
022: private final String READ_PROXY_SETTINGS_COMMAND = "read_proxy_settings_command";
023: private final String SET_PROXY_SETTINGS_COMMAND = "set_proxy_settings_command";
024: private final String RESTORE_PROXY_SETTINGS_COMMAND = "restore_proxy_settings_command";
025: private final String GET_PROXY_MODE_COMMAND = "get_proxy_mode_command";
026: private final String GET_PROXY_MODE_INFO_COMMAND = "get_proxy_mode_info_command";
027: private final String GET_AUTO_CONFIG_URL_COMMAND = "get_auto_config_url_command";
028: private final String INVOKE_HELP_COMMAND = "invoke_help_command";
029:
030: private String configLocation;
031: private Server connection;
032: boolean stopped = false;
033:
034: private final String APPLET_MESSAGE_PREFIX = "pp1.0:";
035: private final String CLOSE_MESSAGE = "close";
036:
037: public void handleStart(boolean firstTime) {
038: ProxyletUI.setCursor(Cursor
039: .getPredefinedCursor(Cursor.WAIT_CURSOR));
040: ProxyletUI.progressIndicator.setString("");
041: ProxyletUI.stopProxyletB.setEnabled(false);
042: ProxyletUI.startProxyletB.setEnabled(false);
043: Log.info(Param.getString("pinf.3",
044: "Please wait while Proxylet configures your browser"));
045:
046: try {
047: ProxyletUI.progressIndicator.setValue(10);
048: ProxyletUI.progressIndicator.setString("10%");
049:
050: if (!firstTime) {
051: // Load resource bundle
052: try {
053: Param.loadResourceBundle(true);
054: } catch (Exception ignore) {
055: Log
056: .info(Param
057: .getString("pexcp.1",
058: "Failed to get resource bundle for locale. Defaulting to en_US"));
059: }
060: }
061:
062: ProxyletUI.progressIndicator.setValue(30);
063: ProxyletUI.progressIndicator.setString("30%");
064:
065: // Read existing proxy information
066: String proxyMode, proxyModeInfo, autoConfigURL;
067: readProxySetting();
068: proxyMode = getProxyMode();
069: proxyModeInfo = getProxyModeInfo();
070: autoConfigURL = getAutoConfigURL();
071:
072: Log.debugu("Connection Type" + proxyMode);
073: Log.debugu("Proxy Info " + proxyModeInfo);
074: Log.debugu("AutoConfig URL " + autoConfigURL);
075:
076: ProxyletUI.progressIndicator.setValue(50);
077: ProxyletUI.progressIndicator.setString("50%");
078: // Process proxy information
079:
080: long proxyTypeProxy = 1;
081: long proxyTypeAutoProxyUrl = 2;
082: long proxyTypeAutoDetect = 4;
083:
084: configLocation = BrowserHelper.processProxyInfo(
085: proxyTypeProxy, proxyTypeAutoProxyUrl,
086: proxyTypeAutoDetect, ProxyletUI.frame, proxyMode,
087: autoConfigURL, proxyModeInfo);
088:
089: ProxyletUI.progressIndicator.setValue(70);
090: ProxyletUI.progressIndicator.setString("70%");
091: Log.debug("Config Location " + configLocation);
092:
093: // Configure proxy information
094: setProxySetting();
095: ProxyletUI.progressIndicator.setValue(80);
096: ProxyletUI.progressIndicator.setString("80%");
097:
098: // Invoke proxy server... if already running just resume it to
099: // accept connections..
100: if (Server.running == -1) {
101: System.out.println("Server not running..start it");
102: connection = new Server(Param.getBindPort(), this );
103: Thread t = new Thread(connection);
104: t.start();
105: } else {
106: System.out
107: .println("server running already..resume operations.");
108: connection.resume();
109: }
110:
111: ProxyletUI.progressIndicator.setValue(100);
112: ProxyletUI.progressIndicator.setString("100%");
113: } catch (Exception e) {
114: e.printStackTrace();
115: Log.debug(e.getMessage());
116: }
117:
118: ProxyletUI.startProxyletB.setEnabled(false);
119: ProxyletUI.stopProxyletB.setEnabled(true);
120: ProxyletUI.progressIndicator.setValue(0);
121: ProxyletUI.progressIndicator.setString("0%");
122: ProxyletUI.progressIndicator.setString(Param.getString(
123: "pinfo.23", "Proxylet Started Successfully"));
124: ProxyletUI.setText(Param.getString("pinf.4",
125: "Proxylet Initialized Successfully"));
126: ProxyletUI
127: .setText(Param
128: .getString("pinf.5",
129: "Please click on help if you are not sure how to proceed further"));
130: ProxyletUI.setCursor(null);
131: }
132:
133: public void handleSuspend() {
134: ProxyletUI.setCursor(Cursor
135: .getPredefinedCursor(Cursor.WAIT_CURSOR));
136:
137: ProxyletUI.startProxyletB.setEnabled(false);
138: ProxyletUI.stopProxyletB.setEnabled(false);
139: if (ProxyletUI.progressIndicator.isIndeterminate())
140: ProxyletUI.progressIndicator.setIndeterminate(false);
141: ProxyletUI.progressIndicator.setString("");
142:
143: try {
144:
145: ProxyletUI.progressIndicator.setValue(10);
146: ProxyletUI.progressIndicator.setString("10%");
147:
148: // Restore Proxy settings
149: restoreProxySetting();
150: // win.call("restoreProxySetting", null);
151: ProxyletUI.progressIndicator.setValue(40);
152: ProxyletUI.progressIndicator.setString("40%");
153:
154: // Nullify resource
155: Param.nullifyResource();
156: ProxyletUI.progressIndicator.setValue(60);
157: ProxyletUI.progressIndicator.setString("60%");
158:
159: // Stop server now..
160: connection.stop();
161: ProxyletUI.progressIndicator.setValue(100);
162: ProxyletUI.progressIndicator.setString("100%");
163:
164: // remove PAC file
165: String location = Param.getPacfileLocation();
166: File f = new File(location);
167: if (f.exists())
168: f.delete();
169: } catch (Exception e) {
170: Log.debug(e.getMessage());
171: }
172: Log
173: .info(Param
174: .getString(
175: "pinfo.8",
176: "Suspending Proxylet operations. You may restart Proxylet anytime.\nTo restart Proxylet, click on the Start button"));
177: ProxyletUI.startProxyletB.setEnabled(true);
178: ProxyletUI.progressIndicator.setValue(0);
179: ProxyletUI.progressIndicator.setString("0%");
180: ProxyletUI.setCursor(null);
181: ProxyletUI.progressIndicator.setString(Param.getString(
182: "pinfo.24", "Proxylet Suspended Successfully"));
183: }
184:
185: public void handleStop() {
186: System.out.println("Stopping Proxylet...");
187:
188: if (stopped == true)
189: return;
190:
191: ProxyletUI.setCursor(Cursor
192: .getPredefinedCursor(Cursor.WAIT_CURSOR));
193: ProxyletUI.progressIndicator.setString("");
194: ProxyletUI.startProxyletB.setEnabled(false);
195: ProxyletUI.stopProxyletB.setEnabled(false);
196: if (ProxyletUI.progressIndicator.isIndeterminate())
197: ProxyletUI.progressIndicator.setIndeterminate(false);
198:
199: try {
200:
201: ProxyletUI.progressIndicator.setValue(10);
202: ProxyletUI.progressIndicator.setString("10%");
203:
204: // Restore Proxy settings
205: restoreProxySetting();
206: closeServerSocket();
207:
208: ProxyletUI.progressIndicator.setValue(40);
209: ProxyletUI.progressIndicator.setString("40%");
210:
211: // Nullify resource
212: Param.nullifyResource();
213: ProxyletUI.progressIndicator.setValue(60);
214: ProxyletUI.progressIndicator.setString("60%");
215:
216: // Stop server now..
217: connection.stop();
218: ProxyletUI.progressIndicator.setValue(80);
219: ProxyletUI.progressIndicator.setString("80%");
220:
221: // Indicate that we are done to the server
222: try {
223: Param.sendMsgtoServlet("?command=setJWSUnLoaded",
224: false, false, null);
225: } catch (SessionTimeoutException e) {
226: // ignore..session has timedout and we are closing anyway...
227: }
228:
229: ProxyletUI.progressIndicator.setValue(100);
230: ProxyletUI.progressIndicator.setString("100%");
231:
232: // remove PAC file
233: String location = Param.getPacfileLocation();
234: File f = new File(location);
235: if (f.exists())
236: f.delete();
237:
238: stopped = true;
239: ProxyletUI.setCursor(null);
240:
241: if (ProxyletUI.frame instanceof JFrame) {
242: ((JFrame) (ProxyletUI.frame)).dispose();
243: ProxyletUI.frame.setVisible(false);
244: }
245: } catch (Exception e) {
246: Log.debug(e.getMessage());
247: e.printStackTrace();
248: }
249: }
250:
251: public void handleHelp() {
252: try {
253: String lang = Param.getString("lang", "en");
254: String helpFileName = "proxylet.htm";
255: String servletURL = Param.getServletURL();
256: int ls = servletURL.lastIndexOf("/",
257: servletURL.length() - 2);
258: String helpUrl = servletURL.substring(0, ls) + "/docs/"
259: + lang + "/proxylet/" + helpFileName;
260: invokeHelp(helpUrl);
261: } catch (Exception e) {
262: Log.debugu("could not launch help browser.");
263: }
264: }
265:
266: public void readProxySetting() {
267: processCommand(READ_PROXY_SETTINGS_COMMAND);
268: }
269:
270: protected void setProxySetting() {
271: processCommand(SET_PROXY_SETTINGS_COMMAND + "?"
272: + configLocation);
273: }
274:
275: protected void restoreProxySetting() {
276: processCommand(RESTORE_PROXY_SETTINGS_COMMAND);
277: }
278:
279: protected String getProxyMode() {
280: return processCommand(GET_PROXY_MODE_COMMAND);
281: }
282:
283: protected String getProxyModeInfo() {
284: return processCommand(GET_PROXY_MODE_INFO_COMMAND);
285: }
286:
287: protected String getAutoConfigURL() {
288: return processCommand(GET_AUTO_CONFIG_URL_COMMAND);
289: }
290:
291: protected void invokeHelp(String helpUrl) {
292: processCommand(INVOKE_HELP_COMMAND + "?" + helpUrl);
293: }
294:
295: protected void closeServerSocket() {
296: processCommand(CLOSE_MESSAGE);
297: }
298:
299: private String processCommand(String command) {
300: String response = "";
301: try {
302: Socket connect = null;
303: while (true) {
304: try {
305: Thread.sleep(1000);
306: connect = new Socket("localhost", 58084);
307: break;
308: } catch (Exception e) {
309: System.out.println("trying to connect at 58084..");
310: }
311: }
312: OutputStream outputStream = connect.getOutputStream();
313: PrintWriter out = new PrintWriter(outputStream);
314:
315: InputStream inputStream = connect.getInputStream();
316: BufferedReader in = new BufferedReader(
317: new InputStreamReader(inputStream));
318:
319: System.out.println("sent command = " + command);
320: out.println(APPLET_MESSAGE_PREFIX + command);
321: out.flush();
322:
323: System.out
324: .println("Sent Message..Will wait for response now");
325: while ((response = in.readLine()) == null) {
326: System.out.println("Waiting for some response");
327: try {
328: Thread.sleep(10000);
329: } catch (InterruptedException e) {
330: // ignore
331: }
332: }
333:
334: System.out.println("response = " + response);
335:
336: in.close();
337: out.close();
338: connect.close();
339: } catch (Exception e) {
340: e.printStackTrace();
341: Log.debug("could not process command - " + command);
342: Log.debug(e.getMessage());
343: }
344:
345: return response;
346: }
347: }
|