KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > launcher > AgentLauncher


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client.launcher;
21
22 import java.applet.Applet JavaDoc;
23 import java.awt.BorderLayout JavaDoc;
24 import java.awt.Button JavaDoc;
25 import java.awt.Color JavaDoc;
26 import java.awt.Label JavaDoc;
27 import java.awt.Panel JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.net.URLConnection JavaDoc;
38 import java.net.URLEncoder JavaDoc;
39 import java.text.MessageFormat JavaDoc;
40 import java.util.Hashtable JavaDoc;
41 import java.util.Locale JavaDoc;
42 import java.util.StringTokenizer JavaDoc;
43
44 import com.sshtools.ui.awt.ImageCanvas;
45 import com.sshtools.ui.awt.UIUtil;
46 import com.sshtools.ui.awt.options.OptionDialog;
47 import com.sslexplorer.agent.client.util.AbstractApplicationLauncher;
48 import com.sslexplorer.agent.client.util.ApplicationLauncherEvents;
49 import com.sslexplorer.agent.client.util.ProcessMonitor;
50 import com.sslexplorer.agent.client.util.TunnelConfiguration;
51 import com.sslexplorer.agent.client.util.Utils;
52 import com.sslexplorer.agent.client.util.XMLElement;
53
54
55 /**
56  * Applet used for downloading, installing an launching the <i>SSL-Explorer Agent</i> or
57  * other application extensions.
58  *
59  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
60  */

61 public class AgentLauncher extends Applet JavaDoc implements ApplicationLauncherEvents {
62     private boolean isStandalone = false;
63     String JavaDoc ticket;
64     ProgressBar progress;
65     String JavaDoc cmdline;
66     String JavaDoc name;
67     boolean monitor = false;
68     boolean debug = false;
69     boolean cleanOnExit = false;
70     Button JavaDoc launch;
71     String JavaDoc localProxyURL;
72     ProcessMonitor processMonitor;
73     long totalNumBytes;
74     String JavaDoc userAgent;
75     Locale JavaDoc locale;
76     Panel JavaDoc mainPanel;
77     int timeout;
78     URL JavaDoc debugCodebase;
79
80     // New properties
81
String JavaDoc appName = null;
82     String JavaDoc extensionId = "sslexplorer-agent"; //$NON-NLS-1$
83
boolean isAgent = true;
84     String JavaDoc launcherImage = "/images/launcher-agent.gif"; //$NON-NLS-1$
85
String JavaDoc localeName = "en"; //$NON-NLS-1$
86

87     /**
88      * Get a parameter value of a default if it does not exist
89      * @param key
90      * @param def
91      * @return parameter value
92      */

93     public String JavaDoc getParameter(String JavaDoc key, String JavaDoc def) {
94         return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def);
95     }
96
97     /* (non-Javadoc)
98      * @see java.applet.Applet#init()
99      */

100     public void init() {
101         // Get the message resources
102
loadResourceBundles();
103         
104         // Start
105

106         appName = Messages.getString("VPNLauncher.applicationName"); //$NON-NLS-1$
107
System.out.println(Messages.getString("VPNLauncher.sysout.initialising")); //$NON-NLS-1$
108

109         // #ifdef MSJAVA
110
/*
111          * try { if (Class.forName("com.ms.security.PolicyEngine") != null) {
112          * com.ms.security.PolicyEngine.assertPermission(com.ms.security.PermissionID.SYSTEM);
113          * com.ms.security.PolicyEngine.assertPermission(com.ms.security.PermissionID.PROPERTY); } }
114          * catch (Throwable cnfe) { cnfe.printStackTrace(); }
115          */

116         // #endif
117
// #ifdef NETSCAPE
118
/*
119          * try {
120          * netscape.security.PrivilegeManager.enablePrivilege("UniversalExecAccess"); }
121          * catch (Exception cnfe) {
122          * System.out.println("netscape.security.PrivilegeManager class not
123          * found"); cnfe.printStackTrace(); }
124          */

125         // #endif
126
/**
127          * This section loads the launcher type information
128          */

129         System.out.println(Messages.getString("VPNLauncher.sysout.gettingLauncherType")); //$NON-NLS-1$
130

131         // Allow the socket to timeout 10 seconds after the agent registration timeout
132
timeout = Integer.parseInt(getParameter("timeout", "60000")) + 10000;
133         
134         appName = getParameter("appName", appName); //$NON-NLS-1$
135
extensionId = getParameter("extensionId", extensionId); //$NON-NLS-1$
136
launcherImage = getParameter("launcherImage", launcherImage); //$NON-NLS-1$
137
isAgent = Boolean.valueOf(getParameter("isAgent", String.valueOf(isAgent))).booleanValue(); //$NON-NLS-1$
138

139         System.out.println(Messages.getString("VPNLauncher.sysout.retrievingDebugStatus")); //$NON-NLS-1$
140
debug = Boolean.valueOf(getParameter("debug", "false")).booleanValue(); //$NON-NLS-1$ //$NON-NLS-2$debug = Boolean.valueOf(getParameter("debug", "false")).booleanValue(); //$NON-NLS-1$ //$NON-NLS-2$
141
if(debug) {
142             String JavaDoc debugCodebaseLocation = getParameter("debugCodebase", "");
143             if(!debugCodebaseLocation.equals("")) {
144                 try {
145                     debugCodebase = new URL JavaDoc(debugCodebaseLocation);
146                 }
147                 catch(MalformedURLException JavaDoc murle) {
148                 }
149             }
150         }
151         cleanOnExit = Boolean.valueOf(getParameter("cleanOnExit", "false")).booleanValue(); //$NON-NLS-1$ //$NON-NLS-
152

153         if (!isAgent) {
154             try {
155                 // Set a fake codebase so that images are loaded from the root
156
// of SSL-Explorer (i.e. in the images folders)
157
URL JavaDoc cb = getActualCodebase();
158                 UIUtil.setCodeBase(new URL JavaDoc(cb.getProtocol(), cb.getHost(),
159                                 cb.getPort() == -1 ? 443 : cb.getPort(), "")); //$NON-NLS-1$
160
} catch (MalformedURLException JavaDoc ex) {
161             }
162         }
163         System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.launchingApplication"), //$NON-NLS-1$
164
new Object JavaDoc[] { appName, extensionId, launcherImage, new Boolean JavaDoc(isAgent) }));
165         try {
166             jbInit();
167         } catch (Exception JavaDoc e) {
168             e.printStackTrace();
169         }
170     }
171
172     /* (non-Javadoc)
173      * @see java.applet.Applet#start()
174      */

175     public void start() {
176         if ("true".equals(getParameter("autoStart", "false"))) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
177
System.out.println(Messages.getString("VPNLauncher.sysout.autostartLaunching")); //$NON-NLS-1$
178
launch();
179         } else {
180             System.out.println(Messages.getString("VPNLauncher.sysout.nonAutostartLaunching")); //$NON-NLS-1$
181
}
182     }
183
184     /* (non-Javadoc)
185      * @see java.applet.Applet#stop()
186      */

187     public void stop() {
188         System.out.println(Messages.getString("VPNLauncher.sysout.stopping")); //$NON-NLS-1$
189
}
190     
191     /**
192      * The actual code base may come from a parameter if debuggin
193      *
194      * @return actual codebase
195      */

196     public URL JavaDoc getActualCodebase() {
197         if(debugCodebase != null) {
198             return debugCodebase;
199         }
200         return getCodeBase();
201     }
202
203     /**
204      * Launch the application
205      */

206     public void launch() {
207         try {
208
209             // Remove me!!! Testing only
210
// com.maverick.ssl.https.HTTPSURLStreamHandlerFactory.installHTTPSSupport();
211

212             System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.launching"), new Object JavaDoc[] { appName } )); //$NON-NLS-1$
213

214             // Get the pending VPN session ticket from the browser
215
ticket = this.getParameter("ticket", null); //$NON-NLS-1$
216

217             // We cannot have a null ticket!
218
if (ticket == null && isAgent) {
219                 // Show an error message...
220
System.out.println(Messages.getString("VPNLauncher.sysout.nullTicket")); //$NON-NLS-1$
221
return;
222             }
223
224             /**
225              * Determine if we need to monitor the executed process. If we do
226              * then all of the output from the process will be written to
227              * System.out which should make it appear in the console.
228              *
229              * If are monitoring and we close the applet it causes some problems
230              * with the process which results in an unresponsive period. Not
231              * sure what this is but once this applet is deployed and tested we
232              * should have no need to monitor.
233              */

234             System.out.println(Messages.getString("VPNLauncher.sysout.retrievingMonitorStatus")); //$NON-NLS-1$
235
monitor = Boolean.valueOf(getParameter("monitor", "false")).booleanValue(); //$NON-NLS-1$ //$NON-NLS-2$
236

237             // Determine proxy URL
238
System.out.println(Messages.getString("VPNLauncher.sysout.checkingBrowserProxySettings")); //$NON-NLS-1$
239

240             userAgent = getParameter("userAgent", "Unknown"); //$NON-NLS-1$ //$NON-NLS-2$
241
System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.userAgent"), new Object JavaDoc[] { userAgent } )); //$NON-NLS-1$
242

243             localProxyURL = getParameter("proxyURL", ""); //$NON-NLS-1$ //$NON-NLS-2$
244
String JavaDoc pluginProxyURL = ""; //$NON-NLS-1$
245

246             if (localProxyURL.toLowerCase().startsWith("browser://")) { //$NON-NLS-1$
247
System.out.println(Messages.getString("VPNLauncher.sysout.checkingJavaProxyProperties")); //$NON-NLS-1$
248

249                 /*
250                  * If the launcher is running using the plugin we might be able
251                  * to extract the proxy settings from there
252                  */

253                 String JavaDoc browserProxy = System.getProperty("javaplugin.proxy.config.list"); //$NON-NLS-1$
254
if (browserProxy != null && !browserProxy.equals("")) { //$NON-NLS-1$
255
StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(browserProxy, ","); //$NON-NLS-1$
256
while (t.hasMoreTokens()) {
257                         String JavaDoc type = t.nextToken();
258                         int idx = type.indexOf('=');
259                         if (idx != -1) {
260                             String JavaDoc value = type.substring(idx + 1);
261                             type = type.substring(0, idx);
262                             if (type.equals("http")) { //$NON-NLS-1$
263
pluginProxyURL = "http://" + value; //$NON-NLS-1$
264
break;
265                             }
266                         } else {
267                             // Unknown form
268
}
269                     }
270                 }
271
272             } else if (localProxyURL != null && !localProxyURL.equals("")) { //$NON-NLS-1$
273
if (!localProxyURL.startsWith("http://") && !localProxyURL.startsWith("https://") //$NON-NLS-1$ //$NON-NLS-2$
274
&& !localProxyURL.startsWith("browser://")) //$NON-NLS-1$
275
localProxyURL = "http://" + localProxyURL; //$NON-NLS-1$
276
}
277
278             // Add the required parameters for our ApplicationLauncher
279
final Hashtable JavaDoc params = new Hashtable JavaDoc();
280             params.put("id", extensionId); //$NON-NLS-1$
281
params.put("ticket", ticket); //$NON-NLS-1$
282
params.put("cleanOnExit", String.valueOf(cleanOnExit)); //$NON-NLS-1$
283
params.put("userAgent", userAgent); //$NON-NLS-1$
284
params.put("pluginProxyURL", pluginProxyURL); //$NON-NLS-1$
285

286             if (isAgent) {
287
288                 setIfNotEmpty("java.version", params); //$NON-NLS-1$
289
setIfNotEmpty("java.vendor", params); //$NON-NLS-1$
290
setIfNotEmpty("sun.os.patch.level", params); //$NON-NLS-1$
291
setIfNotEmpty("os.name", params); //$NON-NLS-1$
292
setIfNotEmpty("os.version", params); //$NON-NLS-1$
293
setIfNotEmpty("os.arch", params); //$NON-NLS-1$
294
}
295             Thread JavaDoc thread = new Thread JavaDoc() {
296
297                 public void run() {
298
299                     // #ifdef MSJAVA
300
/*
301                      * try { System.out.println("Asserting SYSTEM permission");
302                      *
303                      * com.ms.security.PolicyEngine.assertPermission(com.ms.security.PermissionID.SYSTEM);
304                      * System.out.println("Asserting FILEIO permission");
305                      *
306                      * com.ms.security.PolicyEngine.assertPermission(com.ms.security.PermissionID.FILEIO);
307                      * System.out.println("Asserting EXEC permission");
308                      * com.ms.security.PolicyEngine.assertPermission(com.ms.security.PermissionID.EXEC);
309                      * System.out.println("Asserting PROPERTY permission");
310                      *
311                      * com.ms.security.PolicyEngine.assertPermission(com.ms.security.PermissionID.PROPERTY);
312                      * System.out.println("Asserting USERFILEIO permission");
313                      *
314                      * com.ms.security.PolicyEngine.assertPermission(com.ms.security.PermissionID.USERFILEIO); }
315                      * catch(Throwable t) { t.printStackTrace(); }
316                      */

317                     // #endif
318
System.out.println(Messages.getString("VPNLauncher.sysout.startingApplicationLauncher")); //$NON-NLS-1$
319

320                     System.out.println(System.getProperties().toString());
321                     try {
322                         startingLaunch(appName);
323                         URL JavaDoc codebase = getActualCodebase();
324                         AbstractApplicationLauncher launcher = new AgentLauncherApplicationLauncher(new File JavaDoc(Utils.getHomeDirectory(), ".sslexplorer"), "https", null, codebase //$NON-NLS-1$
325
.getHost().equals("") ? "localhost" : codebase.getHost(), //$NON-NLS-1$ //$NON-NLS-2$
326
codebase.getPort() == -1 ? 443 : codebase.getPort(), params, AgentLauncher.this);
327
328                         launcher.setDebug(debug);
329
330                         System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.settingLocalProxy"), new Object JavaDoc[] { localProxyURL } ) ); //$NON-NLS-1$
331
launcher.setLocalProxyURL(localProxyURL);
332                         launcher.prepare();
333                         System.out.println(Messages.getString("VPNLauncher.sysout.preparedLauncher")); //$NON-NLS-1$
334

335                         /*
336                          * Only start the synchronisation thread if this is
337                          * an agent as it is the only type that sends a
338                          * sync message
339                          */

340                         if(isAgent) {
341                             SynchronizationThread sync = new SynchronizationThread(launcher);
342                             System.out.println(Messages.getString("VPNLauncher.sysout.startingSyncThread")); //$NON-NLS-1$
343
sync.start();
344                             // Give the connection time to get established
345
try {
346                                 Thread.sleep(1000);
347                             } catch (InterruptedException JavaDoc ie) {
348                                 ie.printStackTrace();
349                             }
350                         }
351
352                         launcher.start();
353                         
354                         /*
355                          * If this is not the agent then there is no
356                          * synchronisation thread that will handle the
357                          * redirect so we do it directly.
358                          */

359                         if(!isAgent) {
360                             String JavaDoc returnTo = getParameter("returnTo", ""); //$NON-NLS-1$ //$NON-NLS-2$
361
URL JavaDoc returnToUrl = returnTo.equals("") ? getDocumentBase() : new URL JavaDoc(getDocumentBase(), returnTo); //$NON-NLS-1$
362
returnToUrl = new URL JavaDoc(addRedirectParameter(returnToUrl.toExternalForm(), "vpnMessage", appName + " launched.")); //$NON-NLS-1$ //$NON-NLS-2$
363
System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.redirecting"), new Object JavaDoc[] { returnToUrl.toExternalForm() })); //$NON-NLS-1$
364
setMessage(Messages.getString("VPNLauncher.complete")); //$NON-NLS-1$
365
getAppletContext().showDocument(returnToUrl);
366                         }
367                         else {
368
369                             /*
370                              * Monitor the process if required
371                              */

372                             
373                             if (monitor) {
374                                 System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.monitoring"), new Object JavaDoc[] { appName })); //$NON-NLS-1$
375
processMonitor = launcher.getApplicationType().getProcessMonitor();
376                                 if (processMonitor != null) {
377                                     MonitorOutputStream out = new MonitorOutputStream();
378                                     finishedLaunch();
379                                     int exitcode = processMonitor.watch(out, out);
380                                     System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.finished"), new Object JavaDoc[] { appName, new Integer JavaDoc(exitcode) })); //$NON-NLS-1$
381
} else {
382                                     System.out.println(Messages.getString("VPNLauncher.sysout.noProgressMonitor")); //$NON-NLS-1$
383
}
384                             }
385                         }
386
387                     } catch (Exception JavaDoc ex) {
388                         // Show an error message .....
389
setMessage(ex.getMessage());
390                         ex.printStackTrace();
391                     } finally {
392                         finishedLaunch();
393                     }
394                 }
395             };
396
397             thread.start();
398         } catch (Exception JavaDoc e) {
399             e.printStackTrace();
400             // addLaunchButton(); // Add the launch button in case the vpn
401
// client
402
// is closed during the lifetime of this applet
403
}
404
405     }
406
407     protected static void setIfNotEmpty(String JavaDoc name, Hashtable JavaDoc p) {
408         String JavaDoc v = System.getProperty(name);
409         if (v != null && !v.equals("")) { //$NON-NLS-1$
410
p.put(name, v);
411         }
412     }
413
414     // Get Applet information
415
public String JavaDoc getAppletInfo() {
416         return Messages.getString("VPNLauncher.appletInfo"); //$NON-NLS-1$
417
}
418
419     // Get parameter info
420
public String JavaDoc[][] getParameterInfo() {
421         String JavaDoc[][] pinfo = { { "ticket", "String", "" }, }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
422
return pinfo;
423     }
424
425     public void startDownload(long totalNumBytes) {
426         progress.setMessage(Messages.getString("VPNLauncher.downloadingFiles")); //$NON-NLS-1$
427
this.totalNumBytes = totalNumBytes;
428         progress.updateValue(1L);
429         progress.updateValue(20);
430     }
431
432     public void progressedDownload(long bytesSoFar) {
433         int percent = (int) (((float) bytesSoFar / (float) totalNumBytes) * 70f) + 20;
434         progress.updateValue(percent);
435     }
436
437     public void completedDownload() {
438         progress.setMessage(Messages.getString("VPNLauncher.downloadComplete")); //$NON-NLS-1$
439
progress.updateValue(90);
440     }
441
442     public void executingApplication(String JavaDoc name, String JavaDoc cmdline) {
443         progress.setMessage(MessageFormat.format(Messages.getString("VPNLauncher.executingApplication"), new Object JavaDoc[] { name } ) ); //$NON-NLS-1$
444
progress.updateValue(95);
445     }
446
447     public TunnelConfiguration createTunnel(String JavaDoc name, String JavaDoc hostToConnect, int portToConnect, boolean usePreferredPort,
448                     boolean singleConnection, String JavaDoc sourceInterface) {
449         // We should not be creating any tunnels
450
return null;
451     }
452
453     public void debug(String JavaDoc msg) {
454         System.out.println(msg);
455     }
456
457     public void startingLaunch(String JavaDoc application) {
458         progress = new ProgressBar(this,
459                         MessageFormat.format(Messages.getString("VPNLauncher.startingLaunch"), new Object JavaDoc[] { application } ), "SSL-Explorer", 100L, false); //$NON-NLS-1$ //$NON-NLS-2$
460
progress.updateValue(7L);
461     }
462
463     public void processingDescriptor() {
464         progress.updateValue(14L);
465         progress.setMessage(Messages.getString("VPNLauncher.processingApplicationDescriptor")); //$NON-NLS-1$
466
}
467
468     public void finishedLaunch() {
469         if (progress.getCurrentValue() != 100) {
470             progress.setMessage(Messages.getString("VPNLauncher.launchedAgent")); //$NON-NLS-1$
471
progress.updateValue(100);
472             Thread JavaDoc t = new Thread JavaDoc() {
473                 public void run() {
474                     try {
475                         Thread.sleep(2000);
476                     } catch (InterruptedException JavaDoc ex) {
477                     }
478                     progress.dispose();
479                 }
480             };
481
482             t.start();
483         }
484     }
485
486     /*
487      * (non-Javadoc)
488      *
489      * @see java.applet.Applet#destroy()
490      */

491     public void destroy() {
492         System.out.println(Messages.getString("VPNLauncher.destroyingApplet")); //$NON-NLS-1$
493
super.destroy();
494     }
495
496     class SynchronizationThread extends Thread JavaDoc {
497
498         AbstractApplicationLauncher launcher;
499
500         SynchronizationThread(AbstractApplicationLauncher launcher) {
501             this.launcher = launcher;
502         }
503
504         public void run() {
505
506             try {
507
508                 String JavaDoc returnTo = getParameter("returnTo", ""); //$NON-NLS-1$ //$NON-NLS-2$
509
setMessage(Messages.getString("VPNLauncher.synchronizing")); //$NON-NLS-1$
510

511                 /**
512                  * Connect to the server to synchronize with the VPN
513                  * session. This will allow us to refresh the browser page
514                  * once we get notification that the client has registered
515                  * with the server or kill the process after the timeout has
516                  * elasped.
517                  */

518                 URL JavaDoc codebase = getActualCodebase();
519                 URL JavaDoc url = new URL JavaDoc("https", codebase.getHost().equals("") ? "localhost" : //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
520
codebase.getHost(), codebase.getPort() == -1 ? 443
521                                 : codebase.getPort(),
522                                 "/registerClientSynchronization.do?ticket=" + ticket); //$NON-NLS-1$
523
System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.sendingSync"), new Object JavaDoc[] { url })); //$NON-NLS-1$
524

525                 URLConnection JavaDoc con = url.openConnection();
526                 setReadTimeout(con, timeout);
527
528                 con.connect();
529                 System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.connectedTo"), new Object JavaDoc[] { url } )); //$NON-NLS-1$
530

531                 XMLElement result = new XMLElement();
532                 result.parseFromReader(new InputStreamReader JavaDoc(con.getInputStream()));
533
534                 if (result.getName().equalsIgnoreCase("success")) { //$NON-NLS-1$
535
System.out.println(Messages.getString("VPNLauncher.sysout.syncOk")); //$NON-NLS-1$
536

537                     /*
538                      * TODO A nasty hack - If the returnTo points back to
539                      * the VPN Client, then make sure the port is correct
540                      * (the server would not have known the port for the
541                      * client at the point the returnTO link was created).
542                      */

543                     URL JavaDoc returnToUrl = returnTo.equals("") ? getDocumentBase() : new URL JavaDoc(getDocumentBase(), returnTo); //$NON-NLS-1$
544
if (returnToUrl.getProtocol().equals("http") && returnToUrl.getHost().equals("localhost") //$NON-NLS-1$ //$NON-NLS-2$
545
&& returnToUrl.getPort() == -1) {
546                         returnToUrl = new URL JavaDoc(returnToUrl.getProtocol(), returnToUrl.getHost(), Integer.parseInt(result
547                                         .getAttribute("clientPort").toString()), returnToUrl.getFile()); //$NON-NLS-1$
548
}
549                     returnToUrl = new URL JavaDoc(addRedirectParameter(returnToUrl.toExternalForm(),
550                                     "vpnMessage", appName + " launched.")); //$NON-NLS-1$ //$NON-NLS-2$
551
System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.redirecting"), new Object JavaDoc[] { returnToUrl.toExternalForm() })); //$NON-NLS-1$
552
getAppletContext().showDocument(returnToUrl);
553                     setMessage(Messages.getString("VPNLauncher.complete")); //$NON-NLS-1$
554

555                 } else {
556                     // Show an error message and kill the process
557
System.out.println(Messages.getString("VPNLauncher.sysout.failedSync")); //$NON-NLS-1$
558
System.out.println(result.getContent());
559                     ProcessMonitor monitor = launcher.getApplicationType().getProcessMonitor();
560                     if (monitor != null) {
561                         // We cant kill the process as there may be error
562
// dialogs
563
// monitor.kill();
564
setMessage(Messages.getString("VPNLauncher.failedSync")); //$NON-NLS-1$
565
}
566                 }
567             } catch (Exception JavaDoc ex) {
568                 ex.printStackTrace();
569                 addLaunchButton(); // Add the launch button in case the vpn
570
// client is closed during the lifetime of
571
// this applet
572
} finally {
573                 progress.dispose();
574             }
575
576         }
577     }
578
579     private String JavaDoc addRedirectParameter(String JavaDoc redirect, String JavaDoc name, String JavaDoc value) {
580         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(redirect);
581         int idx = redirect.indexOf('?');
582         if (idx == -1) {
583             buf.append("?"); //$NON-NLS-1$
584
} else {
585             buf.append("&"); //$NON-NLS-1$
586
}
587         buf.append(name);
588         buf.append('=');
589         buf.append(URLEncoder.encode(value));
590         return buf.toString();
591     }
592     
593     private void loadResourceBundles() {
594         localeName = getParameter("locale", localeName); //$NON-NLS-1$
595
locale = Utils.createLocale(localeName);
596         URL JavaDoc codebase = getActualCodebase();
597 // NetworkClassLoader cl = new NetworkClassLoader(getClass().getClassLoader());
598
ClassLoader JavaDoc cl = getClass().getClassLoader();
599         try {
600             URL JavaDoc url = new URL JavaDoc("https", codebase.getHost().equals("") ? "localhost" : codebase.getHost(), codebase.getPort() == -1 ? 443
601                             : codebase.getPort(), "/loadMessageResources/");
602 // cl.addURL(url);
603
// System.out.println("Loading resources from " + url);
604
Messages.setBundle(Utils.getBundle(
605                 "com.sslexplorer.agent.client.launcher.ApplicationResources", // $NON-NLS-1$
606
locale,
607                 cl, url));
608             com.sslexplorer.agent.client.util.Messages.setBundle(Utils.getBundle(
609                 "com.sslexplorer.agent.client.util.ApplicationResources", // $NON-NLS-1$
610
locale,
611                 cl, url));
612             com.sslexplorer.agent.client.util.types.Messages.setBundle(Utils.getBundle(
613                 "com.sslexplorer.agent.client.util.types.ApplicationResources", // $NON-NLS-1$
614
locale,
615                 cl, url));
616             com.sshtools.ui.awt.Messages.setBundle(Utils.getBundle(
617                 "com.sshtools.ui.awt.ApplicationResources", // $NON-NLS-1$
618
locale,
619                 cl, url));
620         }
621         catch(MalformedURLException JavaDoc murle) {
622             murle.printStackTrace();
623         }
624     }
625     
626     private static void setReadTimeout(URLConnection JavaDoc conx, int timeout) {
627         try {
628             Method JavaDoc m = conx.getClass().getMethod("setReadTimeout", new Class JavaDoc[] { int.class });
629             m.invoke(conx, new Object JavaDoc[] { new Integer JavaDoc(timeout) } );
630         }
631         catch(Throwable JavaDoc t) {
632             // Cannot set read timeout. Sync errors may occur
633
}
634     }
635
636     private void jbInit() throws Exception JavaDoc {
637         String JavaDoc background = getParameter("background", "#e6e6e6"); //$NON-NLS-1$ //$NON-NLS-2$
638
setBackground(Color.decode(background));
639         String JavaDoc foreground = getParameter("foreground", "#000000"); //$NON-NLS-1$ //$NON-NLS-2$
640
setForeground(Color.decode(foreground));
641         setLayout(new BorderLayout JavaDoc());
642         ImageCanvas ic = new ImageCanvas(UIUtil.waitFor(UIUtil.loadImage(getClass(), launcherImage), this));
643         ic.setValign(ImageCanvas.CENTER_ALIGNMENT);
644         add(ic, BorderLayout.WEST);
645         mainPanel = new Panel JavaDoc(new BorderLayout JavaDoc());
646         add(mainPanel, BorderLayout.CENTER);
647
648         if ("true".equals(getParameter("autoStart", "false"))) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
649
mainPanel.add(new Label JavaDoc(Messages.getString("VPNLauncher.launching")), BorderLayout.CENTER); //$NON-NLS-1$
650
} /*
651              * else { addLaunchButton(); }
652              */

653     }
654
655     private void setMessage(String JavaDoc text) {
656         mainPanel.invalidate();
657         mainPanel.removeAll();
658         mainPanel.add(new Label JavaDoc(text), BorderLayout.CENTER);
659         mainPanel.validate();
660
661     }
662
663     private void addLaunchButton() {
664
665         mainPanel.invalidate();
666         mainPanel.removeAll();
667         launch = new Button JavaDoc(Messages.getString("VPNLauncher.launcher")); //$NON-NLS-1$
668
launch.addActionListener(new ActionListener JavaDoc() {
669             public void actionPerformed(ActionEvent JavaDoc e) {
670                 launch();
671             }
672         });
673         mainPanel.add(launch, BorderLayout.SOUTH);
674         mainPanel.validate();
675     }
676
677     public void error(String JavaDoc msg) {
678         System.out.println(msg);
679         OptionDialog.error(mainPanel, Messages.getString("VPNLauncher.error"), msg); //$NON-NLS-1$
680
}
681
682     class MonitorOutputStream extends OutputStream JavaDoc {
683         public void write(int b) throws IOException JavaDoc {
684             System.out.write(b);
685         }
686
687         public void write(byte[] buf, int off, int len) throws IOException JavaDoc {
688             System.out.write(buf, off, len);
689         }
690     }
691 }
Popular Tags