KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > luxor > browser > albert > BrowserLauncher


1 /*
2 ** Luxor - XML User Interface Language (XUL) Toolkit
3 ** Copyright (c) 2001, 2002 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

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

87 public class BrowserLauncher
88 {
89
90    /**
91     * The creator code of the Finder on a Macintosh, which is needed to send
92     * AppleEvents to the application.
93     */

94    private final static String JavaDoc FINDER_CREATOR = "MACS";
95
96    /**
97     * The file type of the Finder on a Macintosh. Hardcoding "Finder" would
98     * keep non-U.S. English systems from working properly.
99     */

100    private final static String JavaDoc FINDER_TYPE = "FNDR";
101
102    /**
103     * The first parameter that needs to be passed into Runtime.exec() to open
104     * the default web browser on Windows.
105     */

106    private final static String JavaDoc FIRST_WINDOWS_PARAMETER = "/c";
107
108    /**
109     * The name for the AppleEvent type corresponding to a GetURL event.
110     */

111    private final static String JavaDoc GURL_EVENT = "GURL";
112
113    /**
114     * The framework to reference on Mac OS X
115     */

116    private final static String JavaDoc JDirect_MacOSX = "/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox";
117
118    /**
119     * JVM constant for MRJ 2.0
120     */

121    private final static int MRJ_2_0 = 0;
122
123    /**
124     * JVM constant for MRJ 2.1 or later
125     */

126    private final static int MRJ_2_1 = 1;
127
128    /**
129     * JVM constant for Java on Mac OS X 10.0 (MRJ 3.0)
130     */

131    private final static int MRJ_3_0 = 3;
132
133    /**
134     * JVM constant for MRJ 3.1
135     */

136    private final static int MRJ_3_1 = 4;
137    private final static String JavaDoc NETSCAPE_OPEN_PARAMETER_END = ")'";
138    private final static String JavaDoc NETSCAPE_OPEN_PARAMETER_START = "'openURL(";
139
140    /**
141     * The shell parameters for Netscape that opens a given URL in an
142     * already-open copy of Netscape on many command-line systems.
143     */

144    private final static String JavaDoc NETSCAPE_REMOTE_PARAMETER = "-remote";
145
146    /**
147     * JVM constant for any other platform
148     */

149    private final static int OTHER = -1;
150
151    /**
152     * The second parameter for Runtime.exec() on Windows.
153     */

154    private final static String JavaDoc SECOND_WINDOWS_PARAMETER = "start";
155
156    /**
157     * The third parameter for Runtime.exec() on Windows. This is a "title"
158     * parameter that the command line expects. Setting this parameter allows
159     * URLs containing spaces to work.
160     */

161    private final static String JavaDoc THIRD_WINDOWS_PARAMETER = "\"\"";
162
163    /**
164     * JVM constant for any Windows 9x JVM
165     */

166    private final static int WINDOWS_9x = 6;
167
168    /**
169     * JVM constant for any Windows NT JVM
170     */

171    private final static int WINDOWS_NT = 5;
172
173    /**
174     * The com.apple.MacOS.AEDesc class
175     */

176    private static Class JavaDoc aeDescClass;
177
178
179    /**
180     * The <init>(String) method of com.apple.MacOS.AEDesc
181     */

182    private static Constructor JavaDoc aeDescConstructor;
183
184    /**
185     * The <init>(int) method of com.apple.MacOS.AETarget
186     */

187    private static Constructor JavaDoc aeTargetConstructor;
188
189    /**
190     * The <init>(int, int, int) method of com.apple.MacOS.AppleEvent
191     */

192    private static Constructor JavaDoc appleEventConstructor;
193
194    /**
195     * The browser for the system
196     */

197    private static Object JavaDoc browser;
198
199    /**
200     * The message from any exception thrown throughout the initialization
201     * process.
202     */

203    private static String JavaDoc errorMessage;
204
205    /**
206     * The findFolder method of com.apple.mrj.MRJFileUtils
207     */

208    private static Method JavaDoc findFolder;
209
210    /**
211     * The getFileCreator method of com.apple.mrj.MRJFileUtils
212     */

213    private static Method JavaDoc getFileCreator;
214
215    /**
216     * The getFileType method of com.apple.mrj.MRJFileUtils
217     */

218    private static Method JavaDoc getFileType;
219
220    /**
221     * The Java virtual machine that we are running on. Actually, in most cases
222     * we only care about the operating system, but some operating systems
223     * require us to switch on the VM.
224     */

225    private static int jvm;
226
227    /**
228     * The kAnyTransactionID AppleEvent code
229     */

230    private static Integer JavaDoc kAnyTransactionID;
231
232    /**
233     * The kAutoGenerateReturnID AppleEvent code
234     */

235    private static Integer JavaDoc kAutoGenerateReturnID;
236
237    /**
238     * Actually an MRJOSType pointing to the System Folder on a Macintosh
239     */

240    private static Object JavaDoc kSystemFolderType;
241
242    /**
243     * The keyDirectObject AppleEvent parameter type
244     */

245    private static Integer JavaDoc keyDirectObject;
246
247    /**
248     * The linkage object required for JDirect 3 on Mac OS X.
249     */

250    private static Object JavaDoc linkage;
251
252    /**
253     * Caches whether any classes, methods, and fields that are not part of the
254     * JDK and need to be dynamically loaded at runtime loaded successfully. <p>
255     *
256     * Note that if this is <code>false</code>, <code>openURL()</code> will
257     * always return an IOException.
258     */

259    private static boolean loadedWithoutErrors;
260
261    /**
262     * The makeOSType method of com.apple.MacOS.OSUtils
263     */

264    private static Method JavaDoc makeOSType;
265
266    /**
267     * The com.apple.mrj.MRJFileUtils class
268     */

269    private static Class JavaDoc mrjFileUtilsClass;
270
271    /**
272     * The com.apple.mrj.MRJOSType class
273     */

274    private static Class JavaDoc mrjOSTypeClass;
275
276    /**
277     * The openURL method of com.apple.mrj.MRJFileUtils
278     */

279    private static Method JavaDoc openURL;
280
281    /**
282     * The putParameter method of com.apple.MacOS.AppleEvent
283     */

284    private static Method JavaDoc putParameter;
285
286    /**
287     * The sendNoReply method of com.apple.MacOS.AppleEvent
288     */

289    private static Method JavaDoc sendNoReply;
290
291    /**
292     * This class should be never be instantiated; this just ensures so.
293     */

294    private BrowserLauncher() { }
295
296    /**
297     * Attempts to open the default web browser to the given URL.
298     *
299     *@param url The URL to open
300     *@throws IOException If the web browser could not be located or does not
301     * run
302     */

303    public static void openURL( String JavaDoc url ) throws IOException JavaDoc
304    {
305       if( !loadedWithoutErrors )
306       {
307          throw new IOException JavaDoc( "Exception in finding browser: " + errorMessage );
308       }
309       Object JavaDoc browser = locateBrowser();
310       if( browser == null )
311       {
312          throw new IOException JavaDoc( "Unable to locate browser: " + errorMessage );
313       }
314
315       switch ( jvm )
316       {
317          case MRJ_2_0:
318             Object JavaDoc aeDesc = null;
319             try
320             {
321                aeDesc = aeDescConstructor.newInstance( new Object JavaDoc[]{url} );
322                putParameter.invoke( browser, new Object JavaDoc[]{keyDirectObject, aeDesc} );
323                sendNoReply.invoke( browser, new Object JavaDoc[]{} );
324             }
325             catch( InvocationTargetException JavaDoc ite )
326             {
327                throw new IOException JavaDoc( "InvocationTargetException while creating AEDesc: " + ite.getMessage() );
328             }
329             catch( IllegalAccessException JavaDoc iae )
330             {
331                throw new IOException JavaDoc( "IllegalAccessException while building AppleEvent: " + iae.getMessage() );
332             }
333             catch( InstantiationException JavaDoc ie )
334             {
335                throw new IOException JavaDoc( "InstantiationException while creating AEDesc: " + ie.getMessage() );
336             }
337             finally
338             {
339                aeDesc = null;
340                // Encourage it to get disposed if it was created
341
browser = null;
342                // Ditto
343
}
344             break;
345          case MRJ_2_1:
346             Runtime.getRuntime().exec( new String JavaDoc[]{( String JavaDoc ) browser, url} );
347             break;
348          case MRJ_3_0:
349             int[] instance = new int[1];
350             int result = ICStart( instance, 0 );
351             if( result == 0 )
352             {
353                int[] selectionStart = new int[]{0};
354                byte[] urlBytes = url.getBytes();
355                int[] selectionEnd = new int[]{urlBytes.length};
356                result = ICLaunchURL( instance[0], new byte[]{0}, urlBytes,
357                      urlBytes.length, selectionStart,
358                      selectionEnd );
359                if( result == 0 )
360                {
361                   // Ignore the return value; the URL was launched successfully
362
// regardless of what happens here.
363
ICStop( instance );
364                }
365                else
366                {
367                   throw new IOException JavaDoc( "Unable to launch URL: " + result );
368                }
369             }
370             else
371             {
372                throw new IOException JavaDoc( "Unable to create an Internet Config instance: " + result );
373             }
374             break;
375          case MRJ_3_1:
376             try
377             {
378                openURL.invoke( null, new Object JavaDoc[]{url} );
379             }
380             catch( InvocationTargetException JavaDoc ite )
381             {
382                throw new IOException JavaDoc( "InvocationTargetException while calling openURL: " + ite.getMessage() );
383             }
384             catch( IllegalAccessException JavaDoc iae )
385             {
386                throw new IOException JavaDoc( "IllegalAccessException while calling openURL: " + iae.getMessage() );
387             }
388             break;
389          case WINDOWS_NT:
390          case WINDOWS_9x:
391             // Add quotes around the URL to allow ampersands and other special
392
// characters to work.
393
Process JavaDoc process = Runtime.getRuntime().exec( new String JavaDoc[]{( String JavaDoc ) browser,
394                   FIRST_WINDOWS_PARAMETER,
395                   SECOND_WINDOWS_PARAMETER,
396                   THIRD_WINDOWS_PARAMETER,
397                   '"' + url + '"'} );
398             // This avoids a memory leak on some versions of Java on Windows.
399
// That's hinted at in <http://developer.java.sun.com/developer/qow/archive/68/>.
400
try
401             {
402                process.waitFor();
403                process.exitValue();
404             }
405             catch( InterruptedException JavaDoc ie )
406             {
407                throw new IOException JavaDoc( "InterruptedException while launching browser: " + ie.getMessage() );
408             }
409             break;
410          case OTHER:
411             // Assume that we're on Unix and that Netscape is installed
412

413             // First, attempt to open the URL in a currently running session of Netscape
414
process = Runtime.getRuntime().exec( new String JavaDoc[]{( String JavaDoc ) browser,
415                   NETSCAPE_REMOTE_PARAMETER,
416                   NETSCAPE_OPEN_PARAMETER_START +
417                   url +
418                   NETSCAPE_OPEN_PARAMETER_END} );
419             try
420             {
421                int exitCode = process.waitFor();
422                if( exitCode != 0 )
423                {
424                   // if Netscape was not open
425
Runtime.getRuntime().exec( new String JavaDoc[]{( String JavaDoc ) browser, url} );
426                }
427             }
428             catch( InterruptedException JavaDoc ie )
429             {
430                throw new IOException JavaDoc( "InterruptedException while launching browser: " + ie.getMessage() );
431             }
432             break;
433          default:
434             // This should never occur, but if it does, we'll try the simplest thing possible
435
Runtime.getRuntime().exec( new String JavaDoc[]{( String JavaDoc ) browser, url} );
436             break;
437       }
438    }
439
440    private native static int ICLaunchURL( int instance, byte[] hint, byte[] data, int len,
441          int[] selectionStart, int[] selectionEnd );
442
443    /**
444     * Methods required for Mac OS X. The presence of native methods does not
445     * cause any problems on other platforms.
446     */

447    private native static int ICStart( int[] instance, int signature );
448
449    private native static int ICStop( int[] instance );
450
451    /**
452     * Called by a static initializer to load any classes, fields, and methods
453     * required at runtime to locate the user's web browser.
454     *
455     *@return <code>true</code> if all intialization succeeded <code>false</code>
456     * if any portion of the initialization failed
457     */

458    private static boolean loadClasses()
459    {
460       switch ( jvm )
461       {
462          case MRJ_2_0:
463             try
464             {
465                Class JavaDoc aeTargetClass = Class.forName( "com.apple.MacOS.AETarget" );
466                Class JavaDoc osUtilsClass = Class.forName( "com.apple.MacOS.OSUtils" );
467                Class JavaDoc appleEventClass = Class.forName( "com.apple.MacOS.AppleEvent" );
468                Class JavaDoc aeClass = Class.forName( "com.apple.MacOS.ae" );
469                aeDescClass = Class.forName( "com.apple.MacOS.AEDesc" );
470
471                aeTargetConstructor = aeTargetClass.getDeclaredConstructor( new Class JavaDoc[]{int.class} );
472                appleEventConstructor = appleEventClass.getDeclaredConstructor( new Class JavaDoc[]{int.class, int.class, aeTargetClass, int.class, int.class} );
473                aeDescConstructor = aeDescClass.getDeclaredConstructor( new Class JavaDoc[]{String JavaDoc.class} );
474
475                makeOSType = osUtilsClass.getDeclaredMethod( "makeOSType", new Class JavaDoc[]{String JavaDoc.class} );
476                putParameter = appleEventClass.getDeclaredMethod( "putParameter", new Class JavaDoc[]{int.class, aeDescClass} );
477                sendNoReply = appleEventClass.getDeclaredMethod( "sendNoReply", new Class JavaDoc[]{} );
478
479                Field JavaDoc keyDirectObjectField = aeClass.getDeclaredField( "keyDirectObject" );
480                keyDirectObject = ( Integer JavaDoc ) keyDirectObjectField.get( null );
481                Field JavaDoc autoGenerateReturnIDField = appleEventClass.getDeclaredField( "kAutoGenerateReturnID" );
482                kAutoGenerateReturnID = ( Integer JavaDoc ) autoGenerateReturnIDField.get( null );
483                Field JavaDoc anyTransactionIDField = appleEventClass.getDeclaredField( "kAnyTransactionID" );
484                kAnyTransactionID = ( Integer JavaDoc ) anyTransactionIDField.get( null );
485             }
486             catch( ClassNotFoundException JavaDoc cnfe )
487             {
488                errorMessage = cnfe.getMessage();
489                return false;
490             }
491             catch( NoSuchMethodException JavaDoc nsme )
492             {
493                errorMessage = nsme.getMessage();
494                return false;
495             }
496             catch( NoSuchFieldException JavaDoc nsfe )
497             {
498                errorMessage = nsfe.getMessage();
499                return false;
500             }
501             catch( IllegalAccessException JavaDoc iae )
502             {
503                errorMessage = iae.getMessage();
504                return false;
505             }
506             break;
507          case MRJ_2_1:
508             try
509             {
510                mrjFileUtilsClass = Class.forName( "com.apple.mrj.MRJFileUtils" );
511                mrjOSTypeClass = Class.forName( "com.apple.mrj.MRJOSType" );
512                Field JavaDoc systemFolderField = mrjFileUtilsClass.getDeclaredField( "kSystemFolderType" );
513                kSystemFolderType = systemFolderField.get( null );
514                findFolder = mrjFileUtilsClass.getDeclaredMethod( "findFolder", new Class JavaDoc[]{mrjOSTypeClass} );
515                getFileCreator = mrjFileUtilsClass.getDeclaredMethod( "getFileCreator", new Class JavaDoc[]{File JavaDoc.class} );
516                getFileType = mrjFileUtilsClass.getDeclaredMethod( "getFileType", new Class JavaDoc[]{File JavaDoc.class} );
517             }
518             catch( ClassNotFoundException JavaDoc cnfe )
519             {
520                errorMessage = cnfe.getMessage();
521                return false;
522             }
523             catch( NoSuchFieldException JavaDoc nsfe )
524             {
525                errorMessage = nsfe.getMessage();
526                return false;
527             }
528             catch( NoSuchMethodException JavaDoc nsme )
529             {
530                errorMessage = nsme.getMessage();
531                return false;
532             }
533             catch( SecurityException JavaDoc se )
534             {
535                errorMessage = se.getMessage();
536                return false;
537             }
538             catch( IllegalAccessException JavaDoc iae )
539             {
540                errorMessage = iae.getMessage();
541                return false;
542             }
543             break;
544          case MRJ_3_0:
545             try
546             {
547                Class JavaDoc linker = Class.forName( "com.apple.mrj.jdirect.Linker" );
548                Constructor JavaDoc constructor = linker.getConstructor( new Class JavaDoc[]{Class JavaDoc.class} );
549                linkage = constructor.newInstance( new Object JavaDoc[]{BrowserLauncher.class} );
550             }
551             catch( ClassNotFoundException JavaDoc cnfe )
552             {
553                errorMessage = cnfe.getMessage();
554                return false;
555             }
556             catch( NoSuchMethodException JavaDoc nsme )
557             {
558                errorMessage = nsme.getMessage();
559                return false;
560             }
561             catch( InvocationTargetException JavaDoc ite )
562             {
563                errorMessage = ite.getMessage();
564                return false;
565             }
566             catch( InstantiationException JavaDoc ie )
567             {
568                errorMessage = ie.getMessage();
569                return false;
570             }
571             catch( IllegalAccessException JavaDoc iae )
572             {
573                errorMessage = iae.getMessage();
574                return false;
575             }
576             break;
577          case MRJ_3_1:
578             try
579             {
580                mrjFileUtilsClass = Class.forName( "com.apple.mrj.MRJFileUtils" );
581                openURL = mrjFileUtilsClass.getDeclaredMethod( "openURL", new Class JavaDoc[]{String JavaDoc.class} );
582             }
583             catch( ClassNotFoundException JavaDoc cnfe )
584             {
585                errorMessage = cnfe.getMessage();
586                return false;
587             }
588             catch( NoSuchMethodException JavaDoc nsme )
589             {
590                errorMessage = nsme.getMessage();
591                return false;
592             }
593             break;
594          default:
595             break;
596       }
597       return true;
598    }
599
600    /**
601     * Attempts to locate the default web browser on the local system. Caches
602     * results so it only locates the browser once for each use of this class
603     * per JVM instance.
604     *
605     *@return The browser for the system. Note that this may not be what you
606     * would consider to be a standard web browser; instead, it's the
607     * application that gets called to open the default web browser. In some
608     * cases, this will be a non-String object that provides the means of
609     * calling the default browser.
610     */

611    private static Object JavaDoc locateBrowser()
612    {
613       if( browser != null )
614       {
615          return browser;
616       }
617       switch ( jvm )
618       {
619          case MRJ_2_0:
620             try
621             {
622                Integer JavaDoc finderCreatorCode = ( Integer JavaDoc ) makeOSType.invoke( null, new Object JavaDoc[]{FINDER_CREATOR} );
623                Object JavaDoc aeTarget = aeTargetConstructor.newInstance( new Object JavaDoc[]{finderCreatorCode} );
624                Integer JavaDoc gurlType = ( Integer JavaDoc ) makeOSType.invoke( null, new Object JavaDoc[]{GURL_EVENT} );
625                Object JavaDoc appleEvent = appleEventConstructor.newInstance( new Object JavaDoc[]{gurlType, gurlType, aeTarget, kAutoGenerateReturnID, kAnyTransactionID} );
626                // Don't set browser = appleEvent because then the next time we call
627
// locateBrowser(), we'll get the same AppleEvent, to which we'll already have
628
// added the relevant parameter. Instead, regenerate the AppleEvent every time.
629
// There's probably a way to do this better; if any has any ideas, please let
630
// me know.
631
return appleEvent;
632             }
633             catch( IllegalAccessException JavaDoc iae )
634             {
635                browser = null;
636                errorMessage = iae.getMessage();
637                return browser;
638             }
639             catch( InstantiationException JavaDoc ie )
640             {
641                browser = null;
642                errorMessage = ie.getMessage();
643                return browser;
644             }
645             catch( InvocationTargetException JavaDoc ite )
646             {
647                browser = null;
648                errorMessage = ite.getMessage();
649                return browser;
650             }
651          case MRJ_2_1:
652             File JavaDoc systemFolder;
653             try
654             {
655                systemFolder = ( File JavaDoc ) findFolder.invoke( null, new Object JavaDoc[]{kSystemFolderType} );
656             }
657             catch( IllegalArgumentException JavaDoc iare )
658             {
659                browser = null;
660                errorMessage = iare.getMessage();
661                return browser;
662             }
663             catch( IllegalAccessException JavaDoc iae )
664             {
665                browser = null;
666                errorMessage = iae.getMessage();
667                return browser;
668             }
669             catch( InvocationTargetException JavaDoc ite )
670             {
671                browser = null;
672                errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
673                return browser;
674             }
675             String JavaDoc[] systemFolderFiles = systemFolder.list();
676             // Avoid a FilenameFilter because that can't be stopped mid-list
677
for( int i = 0; i < systemFolderFiles.length; i++ )
678             {
679                try
680                {
681                   File JavaDoc file = new File JavaDoc( systemFolder, systemFolderFiles[i] );
682                   if( !file.isFile() )
683                   {
684                      continue;
685                   }
686                   // We're looking for a file with a creator code of 'MACS' and
687
// a type of 'FNDR'. Only requiring the type results in non-Finder
688
// applications being picked up on certain Mac OS 9 systems,
689
// especially German ones, and sending a GURL event to those
690
// applications results in a logout under Multiple Users.
691
Object JavaDoc fileType = getFileType.invoke( null, new Object JavaDoc[]{file} );
692                   if( FINDER_TYPE.equals( fileType.toString() ) )
693                   {
694                      Object JavaDoc fileCreator = getFileCreator.invoke( null, new Object JavaDoc[]{file} );
695                      if( FINDER_CREATOR.equals( fileCreator.toString() ) )
696                      {
697                         browser = file.toString();
698                         // Actually the Finder, but that's OK
699
return browser;
700                      }
701                   }
702                }
703                catch( IllegalArgumentException JavaDoc iare )
704                {
705                   browser = browser;
706                   errorMessage = iare.getMessage();
707                   return null;
708                }
709                catch( IllegalAccessException JavaDoc iae )
710                {
711                   browser = null;
712                   errorMessage = iae.getMessage();
713                   return browser;
714                }
715                catch( InvocationTargetException JavaDoc ite )
716                {
717                   browser = null;
718                   errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
719                   return browser;
720                }
721             }
722             browser = null;
723             break;
724          case MRJ_3_0:
725          case MRJ_3_1:
726             browser = "";
727             // Return something non-null
728
break;
729          case WINDOWS_NT:
730             browser = "cmd.exe";
731             break;
732          case WINDOWS_9x:
733             browser = "command.com";
734             break;
735          case OTHER:
736          default:
737             browser = "netscape";
738             break;
739       }
740       return browser;
741    }
742
743    static
744    {
745       loadedWithoutErrors = true;
746       String JavaDoc osName = System.getProperty( "os.name" );
747       if( osName.startsWith( "Mac OS" ) )
748       {
749          String JavaDoc mrjVersion = System.getProperty( "mrj.version" );
750          String JavaDoc majorMRJVersion = mrjVersion.substring( 0, 3 );
751          try
752          {
753             double version = Double.valueOf( majorMRJVersion ).doubleValue();
754             if( version == 2 )
755             {
756                jvm = MRJ_2_0;
757             }
758             else if( version >= 2.1 && version < 3 )
759             {
760                // Assume that all 2.x versions of MRJ work the same. MRJ 2.1 actually
761
// works via Runtime.exec() and 2.2 supports that but has an openURL() method
762
// as well that we currently ignore.
763
jvm = MRJ_2_1;
764             }
765             else if( version == 3.0 )
766             {
767                jvm = MRJ_3_0;
768             }
769             else if( version >= 3.1 )
770             {
771                // Assume that all 3.1 and later versions of MRJ work the same.
772
jvm = MRJ_3_1;
773             }
774             else
775             {
776                loadedWithoutErrors = false;
777                errorMessage = "Unsupported MRJ version: " + version;
778             }
779          }
780          catch( NumberFormatException JavaDoc nfe )
781          {
782             loadedWithoutErrors = false;
783             errorMessage = "Invalid MRJ version: " + mrjVersion;
784          }
785       }
786       else if( osName.startsWith( "Windows" ) )
787       {
788          if( osName.indexOf( "9" ) != -1 )
789          {
790             jvm = WINDOWS_9x;
791          }
792          else
793          {
794             jvm = WINDOWS_NT;
795          }
796       }
797       else
798       {
799          jvm = OTHER;
800       }
801
802       if( loadedWithoutErrors )
803       {
804          // if we haven't hit any errors yet
805
loadedWithoutErrors = loadClasses();
806       }
807    }
808 }
809
Popular Tags