KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > launcher > BrowserLauncher


1 /**
2  * $RCSfile: BrowserLauncher.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2004/10/25 23:41:59 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.launcher;
13
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.lang.reflect.Constructor JavaDoc;
17 import java.lang.reflect.Field JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20
21 /**
22  * BrowserLauncher is a class that provides one static method, openURL, which opens the default
23  * web browser for the current user of the system to the given URL. It may support other
24  * protocols depending on the system -- mailto, ftp, etc. -- but that has not been rigorously
25  * tested and is not guaranteed to work.
26  * <p/>
27  * Yes, this is platform-specific code, and yes, it may rely on classes on certain platforms
28  * that are not part of the standard JDK. What we're trying to do, though, is to take something
29  * that's frequently desirable but inherently platform-specific -- opening a default browser --
30  * and allow programmers (you, for example) to do so without worrying about dropping into native
31  * code or doing anything else similarly evil.
32  * <p/>
33  * Anyway, this code is completely in Java and will run on all JDK 1.1-compliant systems without
34  * modification or a need for additional libraries. All classes that are required on certain
35  * platforms to allow this to run are dynamically loaded at runtime via reflection and, if not
36  * found, will not cause this to do anything other than returning an error when opening the
37  * browser.
38  * <p/>
39  * There are certain system requirements for this class, as it's running through Runtime.exec(),
40  * which is Java's way of making a native system call. Currently, this requires that a Macintosh
41  * have a Finder which supports the GURL event, which is true for Mac OS 8.0 and 8.1 systems that
42  * have the Internet Scripting AppleScript dictionary installed in the Scripting Additions folder
43  * in the Extensions folder (which is installed by default as far as I know under Mac OS 8.0 and
44  * 8.1), and for all Mac OS 8.5 and later systems. On Windows, it only runs under Win32 systems
45  * (Windows 95, 98, and NT 4.0, as well as later versions of all). On other systems, this drops
46  * back from the inherently platform-sensitive concept of a default browser and simply attempts
47  * to launch Netscape via a shell command.
48  * <p/>
49  * This code is Copyright 1999-2001 by Eric Albert (ejalbert@cs.stanford.edu) and may be
50  * redistributed or modified in any form without restrictions as long as the portion of this
51  * comment from this paragraph through the end of the comment is not removed. The author
52  * requests that he be notified of any application, applet, or other binary that makes use of
53  * this code, but that's more out of curiosity than anything and is not required. This software
54  * includes no warranty. The author is not repsonsible for any loss of data or functionality
55  * or any adverse or unexpected effects of using this software.
56  * <p/>
57  * Credits:
58  * <br>Steven Spencer, JavaWorld magazine (<a HREF="http://www.javaworld.com/javaworld/javatips/jw-javatip66.html">Java Tip 66</a>)
59  * <br>Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore,
60  * Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk
61  *
62  * @author Eric Albert (<a HREF="mailto:ejalbert@cs.stanford.edu">ejalbert@cs.stanford.edu</a>)
63  * @version 1.4b1 (Released June 20, 2001)
64  */

65 public class BrowserLauncher {
66
67     /**
68      * The Java virtual machine that we are running on. Actually, in most cases we only care
69      * about the operating system, but some operating systems require us to switch on the VM.
70      */

71     private static int jvm;
72
73     /**
74      * The browser for the system
75      */

76     private static Object JavaDoc browser;
77
78     /**
79      * Caches whether any classes, methods, and fields that are not part of the JDK and need to
80      * be dynamically loaded at runtime loaded successfully.
81      * <p/>
82      * Note that if this is <code>false</code>, <code>openURL()</code> will always return an
83      * IOException.
84      */

85     private static boolean loadedWithoutErrors;
86
87     /**
88      * The com.apple.mrj.MRJFileUtils class
89      */

90     private static Class JavaDoc mrjFileUtilsClass;
91
92     /**
93      * The com.apple.mrj.MRJOSType class
94      */

95     private static Class JavaDoc mrjOSTypeClass;
96
97     /**
98      * The com.apple.MacOS.AEDesc class
99      */

100     private static Class JavaDoc aeDescClass;
101
102     /**
103      * The <init>(int) method of com.apple.MacOS.AETarget
104      */

105     private static Constructor JavaDoc aeTargetConstructor;
106
107     /**
108      * The <init>(int, int, int) method of com.apple.MacOS.AppleEvent
109      */

110     private static Constructor JavaDoc appleEventConstructor;
111
112     /**
113      * The <init>(String) method of com.apple.MacOS.AEDesc
114      */

115     private static Constructor JavaDoc aeDescConstructor;
116
117     /**
118      * The findFolder method of com.apple.mrj.MRJFileUtils
119      */

120     private static Method JavaDoc findFolder;
121
122     /**
123      * The getFileCreator method of com.apple.mrj.MRJFileUtils
124      */

125     private static Method JavaDoc getFileCreator;
126
127     /**
128      * The getFileType method of com.apple.mrj.MRJFileUtils
129      */

130     private static Method JavaDoc getFileType;
131
132     /**
133      * The openURL method of com.apple.mrj.MRJFileUtils
134      */

135     private static Method JavaDoc openURL;
136
137     /**
138      * The makeOSType method of com.apple.MacOS.OSUtils
139      */

140     private static Method JavaDoc makeOSType;
141
142     /**
143      * The putParameter method of com.apple.MacOS.AppleEvent
144      */

145     private static Method JavaDoc putParameter;
146
147     /**
148      * The sendNoReply method of com.apple.MacOS.AppleEvent
149      */

150     private static Method JavaDoc sendNoReply;
151
152     /**
153      * Actually an MRJOSType pointing to the System Folder on a Macintosh
154      */

155     private static Object JavaDoc kSystemFolderType;
156
157     /**
158      * The keyDirectObject AppleEvent parameter type
159      */

160     private static Integer JavaDoc keyDirectObject;
161
162     /**
163      * The kAutoGenerateReturnID AppleEvent code
164      */

165     private static Integer JavaDoc kAutoGenerateReturnID;
166
167     /**
168      * The kAnyTransactionID AppleEvent code
169      */

170     private static Integer JavaDoc kAnyTransactionID;
171
172     /**
173      * The linkage object required for JDirect 3 on Mac OS X.
174      */

175     private static Object JavaDoc linkage;
176
177     /**
178      * The framework to reference on Mac OS X
179      */

180     private static final String JavaDoc JDirect_MacOSX = "/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox";
181
182     /**
183      * JVM constant for MRJ 2.0
184      */

185     private static final int MRJ_2_0 = 0;
186
187     /**
188      * JVM constant for MRJ 2.1 or later
189      */

190     private static final int MRJ_2_1 = 1;
191
192     /**
193      * JVM constant for Java on Mac OS X 10.0 (MRJ 3.0)
194      */

195     private static final int MRJ_3_0 = 3;
196
197     /**
198      * JVM constant for MRJ 3.1
199      */

200     private static final int MRJ_3_1 = 4;
201
202     /**
203      * JVM constant for any Windows NT JVM
204      */

205     private static final int WINDOWS_NT = 5;
206
207     /**
208      * JVM constant for any Windows 9x JVM
209      */

210     private static final int WINDOWS_9x = 6;
211
212     /**
213      * JVM constant for any other platform
214      */

215     private static final int OTHER = -1;
216
217     /**
218      * The file type of the Finder on a Macintosh. Hardcoding "Finder" would keep non-U.S. English
219      * systems from working properly.
220      */

221     private static final String JavaDoc FINDER_TYPE = "FNDR";
222
223     /**
224      * The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the
225      * application.
226      */

227     private static final String JavaDoc FINDER_CREATOR = "MACS";
228
229     /**
230      * The name for the AppleEvent type corresponding to a GetURL event.
231      */

232     private static final String JavaDoc GURL_EVENT = "GURL";
233
234     /**
235      * The first parameter that needs to be passed into Runtime.exec() to open the default web
236      * browser on Windows.
237      */

238     private static final String JavaDoc FIRST_WINDOWS_PARAMETER = "/c";
239
240     /**
241      * The second parameter for Runtime.exec() on Windows.
242      */

243     private static final String JavaDoc SECOND_WINDOWS_PARAMETER = "start";
244
245     /**
246      * The third parameter for Runtime.exec() on Windows. This is a "title"
247      * parameter that the command line expects. Setting this parameter allows
248      * URLs containing spaces to work.
249      */

250     private static final String JavaDoc THIRD_WINDOWS_PARAMETER = "\"\"";
251
252     /**
253      * The shell parameters for Netscape that opens a given URL in an already-open copy of Netscape
254      * on many command-line systems.
255      */

256     private static final String JavaDoc NETSCAPE_REMOTE_PARAMETER = "-remote";
257     private static final String JavaDoc NETSCAPE_OPEN_PARAMETER_START = "'openURL(";
258     private static final String JavaDoc NETSCAPE_OPEN_PARAMETER_END = ")'";
259
260     /**
261      * The message from any exception thrown throughout the initialization process.
262      */

263     private static String JavaDoc errorMessage;
264
265     /**
266      * An initialization block that determines the operating system and loads the necessary
267      * runtime data.
268      */

269     static {
270         loadedWithoutErrors = true;
271         String JavaDoc osName = System.getProperty("os.name");
272         if (osName.startsWith("Mac OS")) {
273             String JavaDoc mrjVersion = System.getProperty("mrj.version");
274             String JavaDoc majorMRJVersion = mrjVersion.substring(0, 3);
275             try {
276                 double version = Double.valueOf(majorMRJVersion).doubleValue();
277                 if (version == 2) {
278                     jvm = MRJ_2_0;
279                 }
280                 else if (version >= 2.1 && version < 3) {
281                     // Assume that all 2.x versions of MRJ work the same. MRJ 2.1 actually
282
// works via Runtime.exec() and 2.2 supports that but has an openURL() method
283
// as well that we currently ignore.
284
jvm = MRJ_2_1;
285                 }
286                 else if (version == 3.0) {
287                     jvm = MRJ_3_0;
288                 }
289                 else if (version >= 3.1) {
290                     // Assume that all 3.1 and later versions of MRJ work the same.
291
jvm = MRJ_3_1;
292                 }
293                 else {
294                     loadedWithoutErrors = false;
295                     errorMessage = "Unsupported MRJ version: " + version;
296                 }
297             }
298             catch (NumberFormatException JavaDoc nfe) {
299                 loadedWithoutErrors = false;
300                 errorMessage = "Invalid MRJ version: " + mrjVersion;
301             }
302         }
303         else if (osName.startsWith("Windows")) {
304             if (osName.indexOf("9") != -1) {
305                 jvm = WINDOWS_9x;
306             }
307             else {
308                 jvm = WINDOWS_NT;
309             }
310         }
311         else {
312             jvm = OTHER;
313         }
314
315         if (loadedWithoutErrors) { // if we haven't hit any errors yet
316
loadedWithoutErrors = loadClasses();
317         }
318     }
319
320     /**
321      * This class should be never be instantiated; this just ensures so.
322      */

323     private BrowserLauncher() {
324     }
325
326     /**
327      * Called by a static initializer to load any classes, fields, and methods required at runtime
328      * to locate the user's web browser.
329      *
330      * @return <code>true</code> if all intialization succeeded
331      * <code>false</code> if any portion of the initialization failed
332      */

333     private static boolean loadClasses() {
334         switch (jvm) {
335             case MRJ_2_0:
336                 try {
337                     Class JavaDoc aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
338                     Class JavaDoc osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
339                     Class JavaDoc appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
340                     Class JavaDoc aeClass = Class.forName("com.apple.MacOS.ae");
341                     aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
342
343                     aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class JavaDoc[]{int.class});
344                     appleEventConstructor = appleEventClass.getDeclaredConstructor(new Class JavaDoc[]{int.class, int.class, aeTargetClass, int.class, int.class});
345                     aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class JavaDoc[]{String JavaDoc.class});
346
347                     makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class JavaDoc[]{String JavaDoc.class});
348                     putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class JavaDoc[]{int.class, aeDescClass});
349                     sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class JavaDoc[]{});
350
351                     Field JavaDoc keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject");
352                     keyDirectObject = (Integer JavaDoc)keyDirectObjectField.get(null);
353                     Field JavaDoc autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID");
354                     kAutoGenerateReturnID = (Integer JavaDoc)autoGenerateReturnIDField.get(null);
355                     Field JavaDoc anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID");
356                     kAnyTransactionID = (Integer JavaDoc)anyTransactionIDField.get(null);
357                 }
358                 catch (ClassNotFoundException JavaDoc cnfe) {
359                     errorMessage = cnfe.getMessage();
360                     return false;
361                 }
362                 catch (NoSuchMethodException JavaDoc nsme) {
363                     errorMessage = nsme.getMessage();
364                     return false;
365                 }
366                 catch (NoSuchFieldException JavaDoc nsfe) {
367                     errorMessage = nsfe.getMessage();
368                     return false;
369                 }
370                 catch (IllegalAccessException JavaDoc iae) {
371                     errorMessage = iae.getMessage();
372                     return false;
373                 }
374                 break;
375             case MRJ_2_1:
376                 try {
377                     mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
378                     mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
379                     Field JavaDoc systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType");
380                     kSystemFolderType = systemFolderField.get(null);
381                     findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class JavaDoc[]{mrjOSTypeClass});
382                     getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class JavaDoc[]{File JavaDoc.class});
383                     getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class JavaDoc[]{File JavaDoc.class});
384                 }
385                 catch (ClassNotFoundException JavaDoc cnfe) {
386                     errorMessage = cnfe.getMessage();
387                     return false;
388                 }
389                 catch (NoSuchFieldException JavaDoc nsfe) {
390                     errorMessage = nsfe.getMessage();
391                     return false;
392                 }
393                 catch (NoSuchMethodException JavaDoc nsme) {
394                     errorMessage = nsme.getMessage();
395                     return false;
396                 }
397                 catch (SecurityException JavaDoc se) {
398                     errorMessage = se.getMessage();
399                     return false;
400                 }
401                 catch (IllegalAccessException JavaDoc iae) {
402                     errorMessage = iae.getMessage();
403                     return false;
404                 }
405                 break;
406             case MRJ_3_0:
407                 try {
408                     Class JavaDoc linker = Class.forName("com.apple.mrj.jdirect.Linker");
409                     Constructor JavaDoc constructor = linker.getConstructor(new Class JavaDoc[]{Class JavaDoc.class});
410                     linkage = constructor.newInstance(new Object JavaDoc[]{BrowserLauncher.class});
411                 }
412                 catch (ClassNotFoundException JavaDoc cnfe) {
413                     errorMessage = cnfe.getMessage();
414                     return false;
415                 }
416                 catch (NoSuchMethodException JavaDoc nsme) {
417                     errorMessage = nsme.getMessage();
418                     return false;
419                 }
420                 catch (InvocationTargetException JavaDoc ite) {
421                     errorMessage = ite.getMessage();
422                     return false;
423                 }
424                 catch (InstantiationException JavaDoc ie) {
425                     errorMessage = ie.getMessage();
426                     return false;
427                 }
428                 catch (IllegalAccessException JavaDoc iae) {
429                     errorMessage = iae.getMessage();
430                     return false;
431                 }
432                 break;
433             case MRJ_3_1:
434                 try {
435                     mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
436                     openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class JavaDoc[]{String JavaDoc.class});
437                 }
438                 catch (ClassNotFoundException JavaDoc cnfe) {
439                     errorMessage = cnfe.getMessage();
440                     return false;
441                 }
442                 catch (NoSuchMethodException JavaDoc nsme) {
443                     errorMessage = nsme.getMessage();
444                     return false;
445                 }
446                 break;
447             default:
448                 break;
449         }
450         return true;
451     }
452
453     /**
454      * Attempts to locate the default web browser on the local system. Caches results so it
455      * only locates the browser once for each use of this class per JVM instance.
456      *
457      * @return The browser for the system. Note that this may not be what you would consider
458      * to be a standard web browser; instead, it's the application that gets called to
459      * open the default web browser. In some cases, this will be a non-String object
460      * that provides the means of calling the default browser.
461      */

462     private static Object JavaDoc locateBrowser() {
463         if (browser != null) {
464             return browser;
465         }
466         switch (jvm) {
467             case MRJ_2_0:
468                 try {
469                     Integer JavaDoc finderCreatorCode = (Integer JavaDoc)makeOSType.invoke(null, new Object JavaDoc[]{FINDER_CREATOR});
470                     Object JavaDoc aeTarget = aeTargetConstructor.newInstance(new Object JavaDoc[]{finderCreatorCode});
471                     Integer JavaDoc gurlType = (Integer JavaDoc)makeOSType.invoke(null, new Object JavaDoc[]{GURL_EVENT});
472                     Object JavaDoc appleEvent = appleEventConstructor.newInstance(new Object JavaDoc[]{gurlType, gurlType, aeTarget, kAutoGenerateReturnID, kAnyTransactionID});
473                     // Don't set browser = appleEvent because then the next time we call
474
// locateBrowser(), we'll get the same AppleEvent, to which we'll already have
475
// added the relevant parameter. Instead, regenerate the AppleEvent every time.
476
// There's probably a way to do this better; if any has any ideas, please let
477
// me know.
478
return appleEvent;
479                 }
480                 catch (IllegalAccessException JavaDoc iae) {
481                     browser = null;
482                     errorMessage = iae.getMessage();
483                     return browser;
484                 }
485                 catch (InstantiationException JavaDoc ie) {
486                     browser = null;
487                     errorMessage = ie.getMessage();
488                     return browser;
489                 }
490                 catch (InvocationTargetException JavaDoc ite) {
491                     browser = null;
492                     errorMessage = ite.getMessage();
493                     return browser;
494                 }
495             case MRJ_2_1:
496                 File JavaDoc systemFolder;
497                 try {
498                     systemFolder = (File JavaDoc)findFolder.invoke(null, new Object JavaDoc[]{kSystemFolderType});
499                 }
500                 catch (IllegalArgumentException JavaDoc iare) {
501                     browser = null;
502                     errorMessage = iare.getMessage();
503                     return browser;
504                 }
505                 catch (IllegalAccessException JavaDoc iae) {
506                     browser = null;
507                     errorMessage = iae.getMessage();
508                     return browser;
509                 }
510                 catch (InvocationTargetException JavaDoc ite) {
511                     browser = null;
512                     errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
513                     return browser;
514                 }
515                 String JavaDoc[] systemFolderFiles = systemFolder.list();
516                 // Avoid a FilenameFilter because that can't be stopped mid-list
517
for (int i = 0; i < systemFolderFiles.length; i++) {
518                     try {
519                         File JavaDoc file = new File JavaDoc(systemFolder, systemFolderFiles[i]);
520                         if (!file.isFile()) {
521                             continue;
522                         }
523                         // We're looking for a file with a creator code of 'MACS' and
524
// a type of 'FNDR'. Only requiring the type results in non-Finder
525
// applications being picked up on certain Mac OS 9 systems,
526
// especially German ones, and sending a GURL event to those
527
// applications results in a logout under Multiple Users.
528
Object JavaDoc fileType = getFileType.invoke(null, new Object JavaDoc[]{file});
529                         if (FINDER_TYPE.equals(fileType.toString())) {
530                             Object JavaDoc fileCreator = getFileCreator.invoke(null, new Object JavaDoc[]{file});
531                             if (FINDER_CREATOR.equals(fileCreator.toString())) {
532                                 browser = file.toString(); // Actually the Finder, but that's OK
533
return browser;
534                             }
535                         }
536                     }
537                     catch (IllegalArgumentException JavaDoc iare) {
538                         browser = browser;
539                         errorMessage = iare.getMessage();
540                         return null;
541                     }
542                     catch (IllegalAccessException JavaDoc iae) {
543                         browser = null;
544                         errorMessage = iae.getMessage();
545                         return browser;
546                     }
547                     catch (InvocationTargetException JavaDoc ite) {
548                         browser = null;
549                         errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
550                         return browser;
551                     }
552                 }
553                 browser = null;
554                 break;
555             case MRJ_3_0:
556             case MRJ_3_1:
557                 browser = ""; // Return something non-null
558
break;
559             case WINDOWS_NT:
560                 browser = "cmd.exe";
561                 break;
562             case WINDOWS_9x:
563                 browser = "command.com";
564                 break;
565             case OTHER:
566             default:
567                 browser = "netscape";
568                 break;
569         }
570         return browser;
571     }
572
573     /**
574      * Attempts to open the default web browser to the given URL.
575      *
576      * @param url The URL to open
577      * @throws IOException If the web browser could not be located or does not run
578      */

579     public static void openURL(String JavaDoc url) throws IOException JavaDoc {
580         if (!loadedWithoutErrors) {
581             throw new IOException JavaDoc("Exception in finding browser: " + errorMessage);
582         }
583         Object JavaDoc browser = locateBrowser();
584         if (browser == null) {
585             throw new IOException JavaDoc("Unable to locate browser: " + errorMessage);
586         }
587
588         switch (jvm) {
589             case MRJ_2_0:
590                 Object JavaDoc aeDesc = null;
591                 try {
592                     aeDesc = aeDescConstructor.newInstance(new Object JavaDoc[]{url});
593                     putParameter.invoke(browser, new Object JavaDoc[]{keyDirectObject, aeDesc});
594                     sendNoReply.invoke(browser, new Object JavaDoc[]{});
595                 }
596                 catch (InvocationTargetException JavaDoc ite) {
597                     throw new IOException JavaDoc("InvocationTargetException while creating AEDesc: " + ite.getMessage());
598                 }
599                 catch (IllegalAccessException JavaDoc iae) {
600                     throw new IOException JavaDoc("IllegalAccessException while building AppleEvent: " + iae.getMessage());
601                 }
602                 catch (InstantiationException JavaDoc ie) {
603                     throw new IOException JavaDoc("InstantiationException while creating AEDesc: " + ie.getMessage());
604                 }
605                 finally {
606                     aeDesc = null; // Encourage it to get disposed if it was created
607
browser = null; // Ditto
608
}
609                 break;
610             case MRJ_2_1:
611                 Runtime.getRuntime().exec(new String JavaDoc[]{(String JavaDoc)browser, url});
612                 break;
613             case MRJ_3_0:
614                 int[] instance = new int[1];
615                 int result = ICStart(instance, 0);
616                 if (result == 0) {
617                     int[] selectionStart = new int[]{0};
618                     byte[] urlBytes = url.getBytes();
619                     int[] selectionEnd = new int[]{urlBytes.length};
620                     result = ICLaunchURL(instance[0], new byte[]{0}, urlBytes,
621                             urlBytes.length, selectionStart,
622                             selectionEnd);
623                     if (result == 0) {
624                         // Ignore the return value; the URL was launched successfully
625
// regardless of what happens here.
626
ICStop(instance);
627                     }
628                     else {
629                         throw new IOException JavaDoc("Unable to launch URL: " + result);
630                     }
631                 }
632                 else {
633                     throw new IOException JavaDoc("Unable to create an Internet Config instance: " + result);
634                 }
635                 break;
636             case MRJ_3_1:
637                 try {
638                     openURL.invoke(null, new Object JavaDoc[]{url});
639                 }
640                 catch (InvocationTargetException JavaDoc ite) {
641                     throw new IOException JavaDoc("InvocationTargetException while calling openURL: " + ite.getMessage());
642                 }
643                 catch (IllegalAccessException JavaDoc iae) {
644                     throw new IOException JavaDoc("IllegalAccessException while calling openURL: " + iae.getMessage());
645                 }
646                 break;
647             case WINDOWS_NT:
648             case WINDOWS_9x:
649                 // Add quotes around the URL to allow ampersands and other special
650
// characters to work.
651
Process JavaDoc process = Runtime.getRuntime().exec(new String JavaDoc[]{(String JavaDoc)browser,
652                                                                          FIRST_WINDOWS_PARAMETER,
653                                                                          SECOND_WINDOWS_PARAMETER,
654                                                                          THIRD_WINDOWS_PARAMETER,
655                                                                          '"' + url + '"'});
656                 // This avoids a memory leak on some versions of Java on Windows.
657
// That's hinted at in <http://developer.java.sun.com/developer/qow/archive/68/>.
658
try {
659                     process.waitFor();
660                     process.exitValue();
661                 }
662                 catch (InterruptedException JavaDoc ie) {
663                     throw new IOException JavaDoc("InterruptedException while launching browser: " + ie.getMessage());
664                 }
665                 break;
666             case OTHER:
667                 // Assume that we're on Unix and that Netscape is installed
668

669                 // First, attempt to open the URL in a currently running session of Netscape
670
process = Runtime.getRuntime().exec(new String JavaDoc[]{(String JavaDoc)browser,
671                                                                  NETSCAPE_REMOTE_PARAMETER,
672                                                                  NETSCAPE_OPEN_PARAMETER_START +
673                         url +
674                         NETSCAPE_OPEN_PARAMETER_END});
675                 try {
676                     int exitCode = process.waitFor();
677                     if (exitCode != 0) { // if Netscape was not open
678
Runtime.getRuntime().exec(new String JavaDoc[]{(String JavaDoc)browser, url});
679                     }
680                 }
681                 catch (InterruptedException JavaDoc ie) {
682                     throw new IOException JavaDoc("InterruptedException while launching browser: " + ie.getMessage());
683                 }
684                 break;
685             default:
686                 // This should never occur, but if it does, we'll try the simplest thing possible
687
Runtime.getRuntime().exec(new String JavaDoc[]{(String JavaDoc)browser, url});
688                 break;
689         }
690     }
691
692     /**
693      * Methods required for Mac OS X. The presence of native methods does not cause
694      * any problems on other platforms.
695      */

696     private native static int ICStart(int[] instance, int signature);
697
698     private native static int ICStop(int[] instance);
699
700     private native static int ICLaunchURL(int instance, byte[] hint, byte[] data, int len,
701                                           int[] selectionStart, int[] selectionEnd);
702 }
703
704
705
Popular Tags