KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > browser > MozillaFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.help.internal.browser;
12
13 import java.io.*;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Locale JavaDoc;
16
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.help.browser.*;
19 import org.eclipse.help.internal.base.*;
20 public class MozillaFactory implements IBrowserFactory, IExecutableExtension {
21     private String JavaDoc executable;
22     private String JavaDoc executableName;
23     private String JavaDoc osList;
24     private MozillaBrowserAdapter browserInstance = null;
25     /**
26      * Constructor.
27      */

28     public MozillaFactory() {
29         super();
30     }
31     /*
32      * @see IBrowserFactory#isAvailable()
33      */

34     public boolean isAvailable() {
35         if (!isSupportedOS(System.getProperty("os.name"))) { //$NON-NLS-1$
36
return false;
37         }
38         try {
39             Process JavaDoc pr = Runtime.getRuntime().exec("which " + executable); //$NON-NLS-1$
40
StreamConsumer outputs = new StreamConsumer(pr.getInputStream());
41             (outputs).start();
42             StreamConsumer errors = new StreamConsumer(pr.getErrorStream());
43             (errors).start();
44             pr.waitFor();
45             int ret = pr.exitValue();
46             if (ret == 0) {
47                 return !errorsInOutput(outputs, errors);
48             }
49             return false;
50         } catch (InterruptedException JavaDoc e) {
51             return false;
52         } catch (IOException e) {
53             // launching which failed, assume browser executable is present
54
return true;
55         }
56     }
57     /**
58      * On some OSes 0 is always returned by "which" command it is necessary to
59      * examine ouput to find out failure.
60      *
61      * @param outputs
62      * @param errors
63      * @return @throws
64      * InterruptedException
65      */

66     private boolean errorsInOutput(StreamConsumer outputs, StreamConsumer errors) {
67         try {
68             outputs.join(1000);
69             if (outputs.getLastLine() != null
70                     && outputs.getLastLine()
71                             .indexOf("no " + executable + " in") //$NON-NLS-1$ //$NON-NLS-2$
72
>= 0) {
73                 return true;
74             }
75             errors.join(1000);
76             if (errors.getLastLine() != null
77                     && errors.getLastLine().indexOf("no " + executable + " in") //$NON-NLS-1$ //$NON-NLS-2$
78
>= 0) {
79                 return true;
80             }
81         } catch (InterruptedException JavaDoc ie) {
82             // ignore
83
}
84         return false;
85     }
86     /*
87      * @see IBrowserFactory#createBrowser()
88      */

89     public IBrowser createBrowser() {
90         // Create single browser for all clients
91
if (browserInstance == null) {
92             browserInstance = new MozillaBrowserAdapter(executable,
93                     executableName);
94         }
95         return browserInstance;
96     }
97     /**
98      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
99      * java.lang.String, java.lang.Object)
100      */

101     public void setInitializationData(IConfigurationElement config,
102             String JavaDoc propertyName, Object JavaDoc data) throws CoreException {
103         try {
104             Hashtable JavaDoc params = (Hashtable JavaDoc) data;
105             executable = (String JavaDoc) params.get("executable"); //$NON-NLS-1$
106
executableName = (String JavaDoc) params.get("executableName"); //$NON-NLS-1$
107
osList = (String JavaDoc) params.get("os"); //$NON-NLS-1$
108
} catch (Exception JavaDoc e) {
109             throw new CoreException(new Status(IStatus.ERROR,
110                     HelpBasePlugin.PLUGIN_ID, IStatus.OK, HelpBaseResources.MozillaFactory_dataMissing,
111                     e));
112         }
113     }
114     private boolean isSupportedOS(String JavaDoc os) {
115         if (osList == null || osList.length() <= 0) {
116             // parameter missing
117
return false;
118         }
119         String JavaDoc[] OSes = osList.split(",\\s*"); //$NON-NLS-1$
120
for (int i = 0; i < OSes.length; i++) {
121             if (os.toLowerCase(Locale.ENGLISH).startsWith(OSes[i].toLowerCase(Locale.ENGLISH))) {
122                 return true;
123             }
124         }
125         return false;
126     }
127 }
128
Popular Tags