001: package com.sun.portal.proxylet.client.jnlp;
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.server.Server;
006: import com.sun.portal.proxylet.client.common.Log;
007: import com.sun.portal.proxylet.client.common.Param;
008: import com.sun.portal.proxylet.client.common.regxwrapper;
009: import com.sun.portal.proxylet.client.common.browser.BrowserHelper;
010:
011: import javax.swing.*;
012: import javax.jnlp.BasicService;
013: import javax.jnlp.ServiceManager;
014: import java.awt.*;
015: import java.lang.reflect.Method;
016: import java.io.File;
017: import java.io.IOException;
018: import java.net.URL;
019:
020: /**
021: * Created by IntelliJ IDEA.
022: * User: amitosh
023: * Date: Nov 22, 2005
024: * Time: 2:13:01 PM
025: * JNLP handler
026: */
027:
028: public class RegistryEventHandler_JNLP extends AbstractEventHandler {
029:
030: Server connection;
031: String dllLocation;
032: boolean stopped = false;
033:
034: public void handleStart(boolean firstTime) {
035: ProxyletUI.setCursor(Cursor
036: .getPredefinedCursor(Cursor.WAIT_CURSOR));
037: ProxyletUI.stopProxyletB.setEnabled(false);
038: ProxyletUI.startProxyletB.setEnabled(false);
039: Log.info(Param.getString("pinf.3",
040: "Please wait while Proxylet configures your browser"));
041:
042: ProxyletUI.progressIndicator.setIndeterminate(true);
043: ProxyletUI.progressIndicator.setString("");
044: String browser = Param.getBrowserType().toString();
045: try {
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: // install native dll
074: try {
075: dllLocation = r.extractResource("RegX.dll");
076: } catch (Exception e) {
077: Log.info(Param.getString("perr.2",
078: "ERR: installing dll on location"
079: + dllLocation));
080: ProxyletUI.progressIndicator.setValue(0);
081: ProxyletUI.progressIndicator.setString("0%");
082: ProxyletUI.stopProxyletB.setEnabled(true);
083: return;
084: }
085: ProxyletUI.progressIndicator.setValue(20);
086: ProxyletUI.progressIndicator.setString("20%");
087:
088: // unregister regx.dll if already present
089: result = r.Unregister(dllLocation);
090: if (!result) {
091: Log
092: .debug(Param
093: .getString(
094: "pexcp.2",
095: "Failed to unregister ocx control. Either the ocx is not present or there is some error."));
096: }
097: ProxyletUI.progressIndicator.setValue(30);
098: ProxyletUI.progressIndicator.setString("30%");
099:
100: // register dll
101: result = r.Register(dllLocation);
102: if (!result) {
103: Log.info(Param.getString("perr.3",
104: "ERR: Registering OCX ctrl"));
105: return;
106: }
107: ProxyletUI.progressIndicator.setValue(40);
108: ProxyletUI.progressIndicator.setString("40%");
109: }
110:
111: // Read exisint proxy information into regxwrapper object
112: result = r.ReadSettings();
113: if (!result) {
114: Log
115: .info(Param
116: .getString("perr.4",
117: "ERR: Unable to read proxy setting information"));
118: // Prompt for proxy settings
119: }
120: ProxyletUI.progressIndicator.setValue(60);
121: ProxyletUI.progressIndicator.setString("60%");
122:
123: // Process Proxy Info
124: long proxyTypeProxy = 2;
125: long proxyTypeAutoProxyUrl = 4;
126: long proxyTypeAutoDetect = 8;
127:
128: String configLocation = BrowserHelper
129: .processProxyInfo(proxyTypeProxy,
130: proxyTypeAutoProxyUrl,
131: proxyTypeAutoDetect, ProxyletUI.frame,
132: Long.toString(r.getConnectionType()), r
133: .getAutoConfigURL(), r
134: .getProxyServer());
135: ProxyletUI.progressIndicator.setValue(65);
136: ProxyletUI.progressIndicator.setString("65%");
137:
138: // configure browser with PAC file location
139: result = r.updateSettings(configLocation);
140: if (!result) {
141: // important step..so report error
142: Log
143: .info(Param
144: .getString("perr.5",
145: "ERR: Failed to configure browser proxy settings"));
146:
147: }
148: // Make JVM re-read proxy configuration
149: // This is only required incase of IE browser
150: Class CResetProxyManager = null;
151: Method reset = null;
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: } catch (Exception e) {
234: Log.debug(e.getMessage());
235: }
236: }
237:
238: public void handleSuspend() {
239: ProxyletUI.setCursor(Cursor
240: .getPredefinedCursor(Cursor.WAIT_CURSOR));
241: ProxyletUI.startProxyletB.setEnabled(false);
242: ProxyletUI.stopProxyletB.setEnabled(false);
243: if (ProxyletUI.progressIndicator.isIndeterminate())
244: ProxyletUI.progressIndicator.setIndeterminate(false);
245:
246: try {
247: ProxyletUI.progressIndicator.setString("");
248: ProxyletUI.progressIndicator.setValue(10);
249: ProxyletUI.progressIndicator.setString("10%");
250:
251: // Restore Proxy settings
252: regxwrapper r = null;
253: r = regxwrapper.getInstance();
254:
255: // Just suspend the server so that it stops accepting
256: // connections...STOP it only when user really closes the UI
257: connection.suspend();
258: ProxyletUI.progressIndicator.setValue(20);
259: ProxyletUI.progressIndicator.setString("20%");
260:
261: boolean result;
262: // Init OLE control..
263: result = r.InitInstance();
264: if (!result) {
265: Log.debug(Param.getString("perr.1",
266: "ERR:Failed to initialize OLE"));
267: }
268: ProxyletUI.progressIndicator.setValue(30);
269: ProxyletUI.progressIndicator.setString("30%");
270:
271: // Restore proxy settings to original value
272: result = r.restoreSettings(r.getAutoConfigURL(), r
273: .getConnectionType());
274: if (!result) {
275: Log
276: .info(Param
277: .getString("perr.6",
278: "ERR: Error trying to restore browser proxy settings"));
279: return;
280: }
281: ProxyletUI.progressIndicator.setValue(60);
282: ProxyletUI.progressIndicator.setString("60%");
283:
284: // uninit ole context
285: result = r.UninitInstance();
286: if (!result) {
287: Log.debug(Param.getString("perr.1",
288: "ERR: Failed to uninit OLE"));
289: }
290: ProxyletUI.progressIndicator.setValue(70);
291: ProxyletUI.progressIndicator.setString("70%");
292:
293: // Remove pac file
294: String location = Param.getPacfileLocation();
295: File f = new File(location);
296: if (f.exists())
297: f.delete();
298: ProxyletUI.progressIndicator.setValue(100);
299: ProxyletUI.progressIndicator.setString("100%");
300: ProxyletUI.progressIndicator.setString(Param.getString(
301: "pinfo.24", "Proxylet Suspended Successfully"));
302: } catch (Exception e1) {
303: }
304:
305: Log
306: .info(Param
307: .getString(
308: "pinfo.8",
309: "Suspending Proxylet operations. You may restart Proxylet anytime.\nTo restart Proxylet, click on the Start button"));
310: ProxyletUI.startProxyletB.setEnabled(true);
311:
312: ProxyletUI.setCursor(null);
313: }
314:
315: public void handleStop() {
316:
317: System.out.println("Stopping Proxylet...");
318: ProxyletUI.setCursor(Cursor
319: .getPredefinedCursor(Cursor.WAIT_CURSOR));
320: ProxyletUI.progressIndicator.setString("");
321: ProxyletUI.startProxyletB.setEnabled(false);
322: ProxyletUI.stopProxyletB.setEnabled(false);
323: if (ProxyletUI.progressIndicator.isIndeterminate())
324: ProxyletUI.progressIndicator.setIndeterminate(false);
325:
326: if (stopped == true) {
327: System.out.println("Stopped already...");
328: return;
329: }
330:
331: try {
332: ProxyletUI.progressIndicator.setValue(10);
333: ProxyletUI.progressIndicator.setString("10%");
334: // Restore Proxy settings
335: regxwrapper r = null;
336: r = regxwrapper.getInstance();
337:
338: boolean bresult = r.InitInstance();
339: if (!bresult) {
340: Log.debug(Param.getString("perr.1",
341: "ERR:Failed to initialize OLE"));
342: }
343: ProxyletUI.progressIndicator.setValue(20);
344: ProxyletUI.progressIndicator.setString("20%");
345:
346: // Nullify resource
347: Param.nullifyResource();
348:
349: // Restore proxy settings before we exit
350: r.restoreSettings(r.getAutoConfigURL(), r
351: .getConnectionType());
352: ProxyletUI.progressIndicator.setValue(30);
353: ProxyletUI.progressIndicator.setString("30%");
354:
355: System.out
356: .println("Sucessfully reset browser proxy settings");
357:
358: // Indicate that we are done to the server
359: try {
360: Param.sendMsgtoServlet("?command=setJWSUnLoaded",
361: false, false, null);
362: System.out
363: .println("sent message to server setJWSUnloaded");
364: } catch (Exception e) {
365: Log.debug(e.getMessage());
366: }
367:
368: ProxyletUI.progressIndicator.setValue(40);
369: ProxyletUI.progressIndicator.setString("40%");
370:
371: // Unregister ocx control
372: boolean result;
373: System.out.println("dll location " + dllLocation);
374: result = r.Unregister(dllLocation);
375: if (!result) {
376: Log.debug(Param.getString("perr.7",
377: "WARNING: Failed to unregister OCX ctrl"));
378: }
379: ProxyletUI.progressIndicator.setValue(50);
380: ProxyletUI.progressIndicator.setString("50%");
381:
382: System.out.println("Uninit OLE");
383:
384: // Uninitialize ole
385: result = r.UninitInstance();
386: if (!result) {
387: Log.debug(Param.getString("perr.1",
388: "ERR: Failed to uninit OLE"));
389: }
390:
391: ProxyletUI.progressIndicator.setValue(60);
392: ProxyletUI.progressIndicator.setString("60%");
393:
394: // Remove regx.dll
395: /*System.out.println("handle stop " + dllLocation);
396:
397: File f = new File(dllLocation);
398: if( f.exists()) f.delete();
399:
400: String regxwrapperLocation = Param.createTemporaryDirectory() + "regxwrapper.dll";
401: System.out.println("regxwrapperLocation " + regxwrapperLocation);
402: f = new File(regxwrapperLocation);
403: if( f.exists()) f.delete();
404:
405: ProxyletUI.progressIndicator.setValue(70);
406: ProxyletUI.progressIndicator.setString("70%");
407:
408: // Remove pac file
409: String location = Param.getPacfileLocation();
410: System.out.println("pac location " + location);
411: f = new File(location);
412: if ( f.exists()) f.delete(); */
413:
414: // Stop server now..
415: System.out.println("Stopped proxy server");
416: connection.stop();
417:
418: ProxyletUI.progressIndicator.setValue(100);
419: ProxyletUI.progressIndicator.setString("100%");
420: System.out.println("Remove JFrame");
421:
422: if (ProxyletUI.frame instanceof JFrame) {
423: ProxyletUI.frame.setVisible(false);
424: ((JFrame) (ProxyletUI.frame)).dispose();
425: }
426:
427: ProxyletUI.setCursor(null);
428: stopped = true;
429: } catch (Exception e) {
430: Log.debug(e.getMessage());
431: e.printStackTrace();
432: return;
433: } finally {
434: stopped = true;
435: }
436: }
437:
438: public void handleHelp() {
439: try {
440: String lang = Param.getString("lang", "en");
441: String helpFileName = "proxylet.htm";
442: String servletURL = Param.getServletURL();
443: int ls = servletURL.lastIndexOf("/",
444: servletURL.length() - 2);
445: String helpLink = servletURL.substring(0, ls) + "/docs/"
446: + lang + "/proxylet/" + helpFileName;
447:
448: Log.debugu("helpLink = " + helpLink);
449:
450: if (ProxyletUI.frame instanceof JFrame) {
451: try {
452: BasicService bs = (BasicService) ServiceManager
453: .lookup("javax.jnlp.BasicService");
454: bs.showDocument(new URL(helpLink));
455: } catch (Exception ue) {
456: }
457: }
458: } catch (Exception e) {
459: e.printStackTrace();
460: Log.debugu("could not launch help browser.");
461: }
462: }
463: }
|