Skip to main content
summaryrefslogtreecommitdiffstats
blob: c631e842fdf8e02e914e5dfed6cb204a632c29d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.browser;

import java.io.*;
import java.util.Hashtable;
import java.util.Locale;

import org.eclipse.core.runtime.*;
import org.eclipse.help.browser.*;
import org.eclipse.help.internal.base.*;
public class MozillaFactory implements IBrowserFactory, IExecutableExtension {
	private String executable;
	private String executableName;
	private String osList;
	private MozillaBrowserAdapter browserInstance = null;
	/**
	 * Constructor.
	 */
	public MozillaFactory() {
		super();
	}
	/*
	 * @see IBrowserFactory#isAvailable()
	 */
	public boolean isAvailable() {
		if (!isSupportedOS(System.getProperty("os.name"))) { //$NON-NLS-1$
			return false;
		}
		try {
			Process pr = Runtime.getRuntime().exec("which " + executable); //$NON-NLS-1$
			StreamConsumer outputs = new StreamConsumer(pr.getInputStream());
			(outputs).start();
			StreamConsumer errors = new StreamConsumer(pr.getErrorStream());
			(errors).start();
			pr.waitFor();
			int ret = pr.exitValue();
			if (ret == 0) {
				return !errorsInOutput(outputs, errors);
			}
			return false;
		} catch (InterruptedException e) {
			return false;
		} catch (IOException e) {
			// launching which failed, assume browser executable is present
			return true;
		}
	}
	/**
	 * On some OSes 0 is always returned by "which" command it is necessary to
	 * examine ouput to find out failure.
	 * 
	 * @param outputs
	 * @param errors
	 * @return @throws
	 *         InterruptedException
	 */
	private boolean errorsInOutput(StreamConsumer outputs, StreamConsumer errors) {
		try {
			outputs.join(1000);
			if (outputs.getLastLine() != null
					&& outputs.getLastLine()
							.indexOf("no " + executable + " in") //$NON-NLS-1$ //$NON-NLS-2$
					>= 0) {
				return true;
			}
			errors.join(1000);
			if (errors.getLastLine() != null
					&& errors.getLastLine().indexOf("no " + executable + " in") //$NON-NLS-1$ //$NON-NLS-2$
					>= 0) {
				return true;
			}
		} catch (InterruptedException ie) {
			// ignore
		}
		return false;
	}
	/*
	 * @see IBrowserFactory#createBrowser()
	 */
	public IBrowser createBrowser() {
		// Create single browser for all clients
		if (browserInstance == null) {
			browserInstance = new MozillaBrowserAdapter(executable,
					executableName);
		}
		return browserInstance;
	}
	/**
	 * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
	 *      java.lang.String, java.lang.Object)
	 */
	public void setInitializationData(IConfigurationElement config,
			String propertyName, Object data) throws CoreException {
		try {
			Hashtable params = (Hashtable) data;
			executable = (String) params.get("executable"); //$NON-NLS-1$
			executableName = (String) params.get("executableName"); //$NON-NLS-1$
			osList = (String) params.get("os"); //$NON-NLS-1$
		} catch (Exception e) {
			throw new CoreException(new Status(IStatus.ERROR,
					HelpBasePlugin.PLUGIN_ID, IStatus.OK, HelpBaseResources.MozillaFactory_dataMissing,
					e));
		}
	}
	private boolean isSupportedOS(String os) {
		if (osList == null || osList.length() <= 0) {
			// parameter missing
			return false;
		}
		String[] OSes = osList.split(",\\s*"); //$NON-NLS-1$
		for (int i = 0; i < OSes.length; i++) {
			if (os.toLowerCase(Locale.ENGLISH).startsWith(OSes[i].toLowerCase(Locale.ENGLISH))) {
				return true;
			}
		}
		return false;
	}
}

Back to the top