KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > browsers > MozillaBrowser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.browser.browsers;
12
13 import java.io.*;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.ui.browser.AbstractWebBrowser;
18 import org.eclipse.ui.internal.browser.WebBrowserUIPlugin;
19 /**
20  * Browser adapter for browsers supporting -remote openURL command line option
21  * i.e. Mozilla and Netscape.
22  */

23 public class MozillaBrowser extends AbstractWebBrowser {
24     // delay that it takes mozilla to start responding
25
// to remote command after mozilla has been called
26
protected static final int DELAY = 5000;
27
28     protected long browserFullyOpenedAt = 0;
29
30     private BrowserThread lastBrowserThread = null;
31
32     protected String JavaDoc executable;
33     
34     protected boolean firstLaunch = true;
35
36     /**
37      * Constructor
38      *
39      * @executable executable filename to launch
40      * @executableName name of the program to display when error occurs
41      */

42     public MozillaBrowser(String JavaDoc id, String JavaDoc executable) {
43         super(id);
44         this.executable = executable;
45     }
46
47     /*
48      * @see IBrowser#displayURL(String)
49      */

50     public void openURL(URL JavaDoc url2) {
51         String JavaDoc url = url2.toExternalForm();
52         if (lastBrowserThread != null)
53             lastBrowserThread.exitRequested = true;
54
55         lastBrowserThread = new BrowserThread(url);
56         lastBrowserThread.setDaemon(true);
57         lastBrowserThread.start();
58     }
59
60     private class BrowserThread extends Thread JavaDoc {
61         public boolean exitRequested = false;
62
63         private String JavaDoc url;
64
65         public BrowserThread(String JavaDoc urlName) {
66             this.url = urlName;
67         }
68
69         /**
70          * @param browserCmd
71          * @return int 0 if success
72          */

73         private int openBrowser(String JavaDoc browserCmd) {
74             try {
75                 Process JavaDoc pr = Runtime.getRuntime().exec(browserCmd);
76                 StreamConsumer outputs = new StreamConsumer(pr.getInputStream());
77                 (outputs).start();
78                 StreamConsumer errors = new StreamConsumer(pr.getErrorStream());
79                 (errors).start();
80                 pr.waitFor();
81                 int ret = pr.exitValue();
82
83                 if (ret == 0 && errorsInOutput(outputs, errors)) {
84                     return -1;
85                 }
86                 return ret;
87             } catch (InterruptedException JavaDoc e) {
88                 // ignore
89
} catch (IOException e) {
90                 WebBrowserUIPlugin.logError("Launching " + executable //$NON-NLS-1$
91
+ " has failed.", e); //$NON-NLS-1$
92
// TODO: log error
93
/*String msg = HelpBaseResources.getString(
94                         "MozillaBrowser.executeFailed", //$NON-NLS-1$
95                         executableName);
96                 BaseHelpSystem.getDefaultErrorUtil()
97                         .displayError(msg, uiThread);*/

98                 // return success, so second command does not execute
99
return 0;
100             }
101             return -1;
102         }
103
104         /**
105          * On some OSes 0 is always returned by netscape -remote. It is
106          * necessary to examine ouput to find out failure
107          *
108          * @param outputs
109          * @param errors
110          * @return @throws
111          * InterruptedException
112          */

113         private boolean errorsInOutput(StreamConsumer outputs,
114                 StreamConsumer errors) {
115             try {
116                 outputs.join(1000);
117                 if (outputs.getLastLine() != null
118                         && (outputs.getLastLine().indexOf(
119                                 "No running window found") //$NON-NLS-1$
120
>= 0 || outputs.getLastLine().indexOf(
121                                 "not running on display") //$NON-NLS-1$
122
>= 0)) {
123                     return true;
124                 }
125                 errors.join(1000);
126                 if (errors.getLastLine() != null
127                         && (errors.getLastLine().indexOf(
128                                 "No running window found") //$NON-NLS-1$
129
>= 0 || errors.getLastLine().indexOf(
130                                 "not running on display") //$NON-NLS-1$
131
>= 0)) {
132                     return true;
133                 }
134             } catch (InterruptedException JavaDoc ie) {
135                 // ignore
136
}
137             return false;
138         }
139
140         public void run() {
141             // if browser is opening, wait until it fully opens
142
waitForBrowser();
143             if (exitRequested)
144                 return;
145             if (firstLaunch && Platform.OS_WIN32.equals(Platform.getOS())) {
146                 if (openBrowser(executable + " " + url) == 0) //$NON-NLS-1$
147
return;
148                 browserFullyOpenedAt = System.currentTimeMillis() + DELAY;
149                 return;
150             }
151             if (openBrowser(executable + " -remote openURL(" + url + ")") //$NON-NLS-1$ //$NON-NLS-2$
152
== 0)
153                 return;
154             
155             if (exitRequested)
156                 return;
157             browserFullyOpenedAt = System.currentTimeMillis() + DELAY;
158             openBrowser(executable + " " + url); //$NON-NLS-1$
159
}
160
161         private void waitForBrowser() {
162             while (System.currentTimeMillis() < browserFullyOpenedAt)
163                 try {
164                     if (exitRequested)
165                         return;
166                     Thread.sleep(100);
167                 } catch (InterruptedException JavaDoc ie) {
168                     // ignore
169
}
170         }
171     }
172 }
Popular Tags