001: package com.sun.portal.proxylet.client.common.browser;
002:
003: import com.sun.portal.proxylet.client.common.Log;
004: import com.sun.portal.proxylet.client.common.Param;
005: import com.sun.portal.proxylet.client.common.regxwrapper;
006: import com.sun.portal.proxylet.client.common.server.Server;
007: import com.sun.portal.proxylet.client.common.ui.AbstractEventHandler;
008: import com.sun.portal.proxylet.client.common.ui.ProxyletUI;
009:
010: import javax.swing.*;
011: import java.awt.*;
012: import java.io.File;
013: import java.io.IOException;
014: import java.net.URL;
015: import java.lang.reflect.*;
016:
017: /**
018: * Created by IntelliJ IDEA.
019: * User: Sudha
020: * Date: Feb 2, 2005
021: * Time: 2:13:01 PM
022: * JNLP handler
023: */
024:
025: public class RegistryEventHandler extends AbstractEventHandler {
026:
027: Server connection;
028: String dllLocation;
029: boolean stopped = false;
030: Class CResetProxyManager = null;
031: Method reset = null;
032:
033: public void handleStart(boolean firstTime) {
034: ProxyletUI.setCursor(Cursor
035: .getPredefinedCursor(Cursor.WAIT_CURSOR));
036: ProxyletUI.stopProxyletB.setEnabled(false);
037: ProxyletUI.startProxyletB.setEnabled(false);
038: Log.info(Param.getString("pinf.3",
039: "Please wait while Proxylet configures your browser"));
040:
041: ProxyletUI.progressIndicator.setIndeterminate(true);
042: ProxyletUI.progressIndicator.setString("");
043: String browser = Param.getBrowserType().toString();
044: try {
045:
046: regxwrapper r = null;
047: boolean result;
048: if (browser != null && browser.equals("IE")) {
049:
050: // get an instace of regxwrapper for configuring browser
051: r = regxwrapper.getInstance();
052: if (ProxyletUI.progressIndicator.isIndeterminate())
053: ProxyletUI.progressIndicator
054: .setIndeterminate(false);
055:
056: // This is the first thing u need to do before calling
057: // any other function of regxwrapper. This initializes the OLE context
058: result = r.InitInstance();
059: if (!result) {
060: Log.debug(Param.getString("perr.1",
061: "ERR: Failed to initialize OLE"));
062: return;
063: }
064: ProxyletUI.progressIndicator.setValue(10);
065: ProxyletUI.progressIndicator.setString("10%");
066:
067: //
068: // The very first time proxylet is loaded, we install the dll and
069: // register the ocx control. The next time when some one starts proxylet
070: // in the same session, we skip all this
071: //
072: if (firstTime) {
073:
074: // install native dll
075: try {
076: dllLocation = r.extractResource("RegX.dll");
077: } catch (Exception e) {
078: Log.info(Param.getString("perr.2",
079: "ERR: installing dll on location"
080: + dllLocation));
081: ProxyletUI.progressIndicator.setValue(0);
082: ProxyletUI.progressIndicator.setString("0%");
083: ProxyletUI.stopProxyletB.setEnabled(true);
084: return;
085: }
086: ProxyletUI.progressIndicator.setValue(20);
087: ProxyletUI.progressIndicator.setString("20%");
088:
089: // unregister regx.dll if already present
090: result = r.Unregister(dllLocation);
091: if (!result) {
092: Log
093: .debug(Param
094: .getString(
095: "pexcp.2",
096: "Failed to unregister ocx control. Either the ocx is not present or there is some error."));
097: }
098: ProxyletUI.progressIndicator.setValue(30);
099: ProxyletUI.progressIndicator.setString("30%");
100:
101: // register dll
102: result = r.Register(dllLocation);
103: if (!result) {
104: Log.info(Param.getString("perr.3",
105: "ERR: Registering OCX ctrl"));
106: return;
107: }
108: ProxyletUI.progressIndicator.setValue(40);
109: ProxyletUI.progressIndicator.setString("40%");
110:
111: }
112:
113: // Read exisint proxy information into regxwrapper object
114: result = r.ReadSettings();
115: if (!result) {
116: Log
117: .info(Param
118: .getString("perr.4",
119: "ERR: Unable to read proxy setting information"));
120: // Prompt for proxy settings
121: }
122: ProxyletUI.progressIndicator.setValue(60);
123: ProxyletUI.progressIndicator.setString("60%");
124:
125: // Process Proxy Info
126: long proxyTypeProxy = 2;
127: long proxyTypeAutoProxyUrl = 4;
128: long proxyTypeAutoDetect = 8;
129:
130: String configLocation = BrowserHelper
131: .processProxyInfo(proxyTypeProxy,
132: proxyTypeAutoProxyUrl,
133: proxyTypeAutoDetect, ProxyletUI.frame,
134: Long.toString(r.getConnectionType()), r
135: .getAutoConfigURL(), r
136: .getProxyServer());
137: ProxyletUI.progressIndicator.setValue(65);
138: ProxyletUI.progressIndicator.setString("65%");
139:
140: // configure browser with PAC file location
141: result = r.updateSettings(configLocation);
142: if (!result) {
143: // important step..so report error
144: Log
145: .info(Param
146: .getString("perr.5",
147: "ERR: Failed to configure browser proxy settings"));
148:
149: }
150: // Make JVM re-read proxy configuration
151: // This is only required incase of IE browser
152: try {
153: CResetProxyManager = Class
154: .forName("sun.plugin.net.proxy.PluginProxyManager");
155: } catch (ClassNotFoundException cnf) {
156: try {
157: CResetProxyManager = Class
158: .forName("com.sun.deploy.net.proxy.DynamicProxyManager");
159: } catch (ClassNotFoundException cnf1) {
160: Log
161: .debug("Unable to reset plugin proxy information for this JVM");
162: }
163: }
164:
165: if (CResetProxyManager != null) {
166: try {
167: reset = CResetProxyManager.getMethod("reset",
168: null);
169: reset.invoke(null, null);
170: } catch (Exception e) {
171: Log
172: .debug("Unable to reset plugin proxy information for this JVM");
173: }
174: }
175:
176: //Send a message to corresponding applet listener to bounce JVM's settings.
177: try {
178: System.out
179: .println("send message to applet listener on 58083");
180: Param.sendMsgToSelf("58083", "BOUNCEPROXY", null);
181: } catch (Exception ignore) {
182: }
183:
184: ProxyletUI.progressIndicator.setValue(80);
185: ProxyletUI.progressIndicator.setString("80%");
186:
187: // There should be a equivalent call for InitiNstance.. this
188: // uninits OLE context
189: result = r.UninitInstance();
190: if (!result) {
191: Log.debug(Param.getString("perr.1",
192: "ERR: Failed to unload OLE!"));
193: }
194: ProxyletUI.progressIndicator.setValue(90);
195: ProxyletUI.progressIndicator.setString("90%");
196:
197: // Invoke proxy server... if already running just resume it to
198: // accept connections..
199: if (Server.running == -1) {
200: System.out.println("Server not running..start it");
201: connection = new Server(Param.getBindPort(), this );
202: Thread t = new Thread(connection);
203: t.start();
204: } else {
205: System.out
206: .println("server running already..resume operations.");
207: connection.resume();
208: }
209: ProxyletUI.progressIndicator.setValue(100);
210: ProxyletUI.progressIndicator.setString("100%");
211:
212: // Enable Stop button and set progress bar to 0
213: ProxyletUI.startProxyletB.setEnabled(false);
214: ProxyletUI.stopProxyletB.setEnabled(true);
215:
216: ProxyletUI.progressIndicator.setString(Param.getString(
217: "pinfo.23", "Proxylet Started Successfully"));
218: ProxyletUI.setText(Param.getString("pinf.4",
219: "Proxylet Initialized Successfully"));
220: ProxyletUI
221: .setText(Param
222: .getString("pinf.5",
223: "Please click on help if you are not sure how to proceed further..."));
224:
225: } else {
226: Log.info(Param.getString("pinf.6",
227: "JNLP is supported only on Internet Explorer"));
228: return;
229: }
230:
231: ProxyletUI.stopProxyletB.setEnabled(true);
232: ProxyletUI.setCursor(null);
233:
234: } catch (Exception e) {
235: Log.debug(e.getMessage());
236: }
237:
238: }
239:
240: public void handleSuspend() {
241: ProxyletUI.setCursor(Cursor
242: .getPredefinedCursor(Cursor.WAIT_CURSOR));
243: ProxyletUI.startProxyletB.setEnabled(false);
244: ProxyletUI.stopProxyletB.setEnabled(false);
245: if (ProxyletUI.progressIndicator.isIndeterminate())
246: ProxyletUI.progressIndicator.setIndeterminate(false);
247:
248: try {
249: ProxyletUI.progressIndicator.setString("");
250: ProxyletUI.progressIndicator.setValue(10);
251: ProxyletUI.progressIndicator.setString("10%");
252:
253: // Restore Proxy settings
254: regxwrapper r = null;
255: r = regxwrapper.getInstance();
256:
257: // Just suspend the server so that it stops accepting
258: // connections...STOP it only when user really closes the UI
259: connection.suspend();
260: ProxyletUI.progressIndicator.setValue(20);
261: ProxyletUI.progressIndicator.setString("20%");
262:
263: boolean result;
264: // Init OLE control..
265: result = r.InitInstance();
266: if (!result) {
267: Log.debug(Param.getString("perr.1",
268: "ERR:Failed to initialize OLE"));
269: }
270: ProxyletUI.progressIndicator.setValue(30);
271: ProxyletUI.progressIndicator.setString("30%");
272:
273: // Restore proxy settings to original value
274: result = r.restoreSettings(r.getAutoConfigURL(), r
275: .getConnectionType());
276: if (!result) {
277: Log
278: .info(Param
279: .getString("perr.6",
280: "ERR: Error trying to restore browser proxy settings"));
281: return;
282: }
283: ProxyletUI.progressIndicator.setValue(60);
284: ProxyletUI.progressIndicator.setString("60%");
285:
286: System.out.println("Force JVM load proxy settings");
287:
288: if (CResetProxyManager != null) {
289: try {
290: System.out
291: .println("reset method going to be invoked");
292: reset = CResetProxyManager.getMethod("reset", null);
293: reset.invoke(null, null);
294: System.out.println("Reset proxy settings");
295: } catch (Exception e) {
296: Log
297: .debug("Unable to reset plugin proxy information for this JVM");
298: }
299: }
300:
301: // uninit ole context
302: result = r.UninitInstance();
303: if (!result) {
304: Log.debug(Param.getString("perr.1",
305: "ERR: Failed to uninit OLE"));
306: }
307: ProxyletUI.progressIndicator.setValue(70);
308: ProxyletUI.progressIndicator.setString("70%");
309:
310: // Remove pac file
311: String location = Param.getPacfileLocation();
312: File f = new File(location);
313: if (f.exists())
314: f.delete();
315: ProxyletUI.progressIndicator.setValue(100);
316: ProxyletUI.progressIndicator.setString("100%");
317: ProxyletUI.progressIndicator.setString(Param.getString(
318: "pinfo.24", "Proxylet Suspended Successfully"));
319:
320: } catch (Exception e1) {
321: }
322:
323: Log
324: .info(Param
325: .getString(
326: "pinfo.8",
327: "Suspending Proxylet operations. You may restart Proxylet anytime.\nTo restart Proxylet, click on the Start button"));
328: ProxyletUI.startProxyletB.setEnabled(true);
329:
330: ProxyletUI.setCursor(null);
331: }
332:
333: public void handleStop() {
334: System.out.println("Stopping Proxylet...");
335: ProxyletUI.setCursor(Cursor
336: .getPredefinedCursor(Cursor.WAIT_CURSOR));
337: ProxyletUI.progressIndicator.setString("");
338: ProxyletUI.startProxyletB.setEnabled(false);
339: ProxyletUI.stopProxyletB.setEnabled(false);
340: if (ProxyletUI.progressIndicator.isIndeterminate())
341: ProxyletUI.progressIndicator.setIndeterminate(false);
342:
343: if (stopped == true) {
344: System.out.println("Stopped already...");
345: return;
346: }
347:
348: try {
349: ProxyletUI.progressIndicator.setValue(10);
350: ProxyletUI.progressIndicator.setString("10%");
351: // Restore Proxy settings
352: regxwrapper r = null;
353: r = regxwrapper.getInstance();
354:
355: boolean bresult = r.InitInstance();
356: if (!bresult) {
357: Log.debug(Param.getString("perr.1",
358: "ERR:Failed to initialize OLE"));
359: }
360: ProxyletUI.progressIndicator.setValue(20);
361: ProxyletUI.progressIndicator.setString("20%");
362:
363: // Nullify resource
364: Param.nullifyResource();
365:
366: // Restore proxy settings before we exit
367: r.restoreSettings(r.getAutoConfigURL(), r
368: .getConnectionType());
369: ProxyletUI.progressIndicator.setValue(30);
370: ProxyletUI.progressIndicator.setString("30%");
371:
372: System.out
373: .println("Sucessfully reset browser proxy settings");
374: System.out.println("Force JVM load proxy settings");
375:
376: if (CResetProxyManager != null) {
377: try {
378: System.out
379: .println("reset method going to be invoked");
380: reset = CResetProxyManager.getMethod("reset", null);
381: reset.invoke(null, null);
382: System.out.println("Reset proxy settings");
383: } catch (Exception e) {
384: Log
385: .debug("Unable to reset plugin proxy information for this JVM");
386: }
387: }
388:
389: ProxyletUI.progressIndicator.setValue(40);
390: ProxyletUI.progressIndicator.setString("40%");
391:
392: // Indicate that we are done to the server
393: try {
394: Param.sendMsgtoServlet("?command=setJWSUnLoaded",
395: false, false, null);
396: System.out
397: .println("sent message to server setJWSUnloaded");
398: } catch (Exception e) {
399: Log.debug(e.getMessage());
400: }
401:
402: // Unregister ocx control
403: boolean result;
404: System.out.println("dll location " + dllLocation);
405: result = r.Unregister(dllLocation);
406: if (!result) {
407: Log.debug(Param.getString("perr.7",
408: "WARNING: Failed to unregister OCX ctrl"));
409: }
410: ProxyletUI.progressIndicator.setValue(50);
411: ProxyletUI.progressIndicator.setString("50%");
412:
413: System.out.println("Uninit OLE");
414:
415: // Uninitialize ole
416: result = r.UninitInstance();
417: if (!result) {
418: Log.debug(Param.getString("perr.1",
419: "ERR: Failed to uninit OLE"));
420: }
421:
422: ProxyletUI.progressIndicator.setValue(60);
423: ProxyletUI.progressIndicator.setString("60%");
424:
425: // Remove regx.dll
426: /*System.out.println("handle stop " + dllLocation);
427:
428: File f = new File(dllLocation);
429: if( f.exists()) f.delete();
430:
431: String regxwrapperLocation = Param.getTempDirectory() + "regxwrapper.dll";
432: System.out.println("regxwrapperLocation " + regxwrapperLocation);
433: f = new File(regxwrapperLocation);
434: if( f.exists()) f.delete();
435:
436: ProxyletUI.progressIndicator.setValue(70);
437: ProxyletUI.progressIndicator.setString("70%");
438:
439: // Remove pac file
440: String location = Param.getPacfileLocation();
441: System.out.println("pac location " + location);
442: f = new File(location);
443: if ( f.exists()) f.delete(); */
444:
445: // Stop server now..
446: System.out.println("Stopped proxy server");
447: connection.stop();
448:
449: ProxyletUI.progressIndicator.setValue(100);
450: ProxyletUI.progressIndicator.setString("100%");
451: System.out.println("Remove JFrame");
452:
453: if (ProxyletUI.frame instanceof JFrame) {
454: ProxyletUI.frame.setVisible(false);
455: ((JFrame) (ProxyletUI.frame)).dispose();
456: }
457:
458: ProxyletUI.setCursor(null);
459: stopped = true;
460:
461: } catch (Exception e) {
462: Log.debug(e.getMessage());
463: return;
464: } finally {
465: stopped = true;
466: }
467: }
468:
469: public void handleHelp() {
470: try {
471: String lang = Param.getString("lang", "en");
472: String helpFileName = "proxylet.htm";
473: String servletURL = Param.getServletURL();
474: int ls = servletURL.lastIndexOf("/",
475: servletURL.length() - 2);
476: String helpLink = servletURL.substring(0, ls) + "/docs/"
477: + lang + "/proxylet/" + helpFileName;
478:
479: Log.debugu("helpLink = " + helpLink);
480:
481: if (ProxyletUI.frame instanceof JApplet)
482: ((JApplet) ProxyletUI.frame).getAppletContext()
483: .showDocument(new URL(helpLink), "_blank");
484: } catch (IOException e) {
485: e.printStackTrace();
486: Log.debugu("could not launch help browser.");
487: }
488: }
489: }
|