Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1fb00f631e3d7f7aae12a821cd64a4f886a7dbdb (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;

/**
 * Determine the current host environment.
 */
public final class Host {
	private static Boolean isWindowsHost = null;
	private static Boolean isInteractive = null;
	private static Boolean isHeadless = null;
	private static Boolean isLinuxHost = null;

	/**
	 * Method looking up the current host (once) and returning a boolean indicating whether the host
	 * is Unix(false) or Windows(true).
	 *
	 * @return boolean true if running on Windows host
	 */
	public static boolean isWindowsHost() {
		if (isWindowsHost == null) {
			if (System.getProperty("os.name", "").toLowerCase().startsWith("windows")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				isWindowsHost = Boolean.TRUE;
			}
			else {
				isWindowsHost = Boolean.FALSE;
			}
		}
		return isWindowsHost.booleanValue();
	}

	/**
	 * Method looking up the current host (once) and returning a boolean indicating whether the host
	 * is Linux(true) or something else (false).
	 *
	 * @return boolean true if running on Linux host
	 */
	public static boolean isLinuxHost() {
		if (isLinuxHost == null) {
			if (System.getProperty("os.name", "").toLowerCase().startsWith("linux")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				isLinuxHost = Boolean.TRUE;
			}
			else {
				isLinuxHost = Boolean.FALSE;
			}
		}
		return isLinuxHost.booleanValue();
	}

	/**
	 * Returns the content of the release file which exists in every linux distribution in /etc.
	 * e.g. /etc/redhat-release . The file holds information about the distribution itself.
	 *
	 * @return String containing the first line of the linux distri's release file. e.g. Red Hat
	 *         Enterprise Linux WS release 4 (Nahant Update 3)
	 */
	public static String getLinuxRelease() {
		String firstLine = "Unknown"; //$NON-NLS-1$
		File etcdir = new File("/etc");//$NON-NLS-1$
		String[] list = etcdir.list(new FilenameFilter() {
			@Override
            public boolean accept(File dir, String name) {
				String[] distStrings = { "fedora-release", //$NON-NLS-1$
				"redhat-release", //$NON-NLS-1$
				"SuSE-release", //$NON-NLS-1$
				"lsb-release" }; //$NON-NLS-1$
				// Strip path information:
				String f = new File(name).getName();
				String filter;
				for (String distString : distStrings) {
					filter = distString;
					if (f.equalsIgnoreCase(filter)) {
						return true;
					}
				}
				return false;
			}
		});

		if (list == null || list.length == 0) {
			return "Unknown"; //$NON-NLS-1$
		}
		String entry = list[0];

		if (Boolean.getBoolean("shell.debug")) { //$NON-NLS-1$
			System.out.println("UtilityEnvironment#getLinuxRelease: reading file: " + etcdir + "/" + entry); //$NON-NLS-1$ //$NON-NLS-2$
		}

		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(new File(etcdir + "/" + entry))); //$NON-NLS-1$
			// In case of "lsb-release" we have to look for the line starting with
			// "DISTRIB_DESCRIPTION"
			if ("lsb-release".equalsIgnoreCase(entry)) { //$NON-NLS-1$
				while (firstLine != null && !firstLine.toUpperCase()
				                .startsWith("DISTRIB_DESCRIPTION")) { //$NON-NLS-1$
					if (Boolean.getBoolean("shell.debug")) { //$NON-NLS-1$
						System.out.println("UtilityEnvironment#getLinuxRelease: firstLine='" + firstLine + "'"); //$NON-NLS-1$ //$NON-NLS-2$
					}
					firstLine = reader.readLine();
				}
			}
			else {
				// Just read the first line
				firstLine = reader.readLine();
			}
		}
		catch (IOException ioe) {
			firstLine = "Unknown"; //$NON-NLS-1$
		}
		finally {
			if (reader != null) {
				try {
					reader.close();
				}
				catch (IOException ex) {
					// silently ignored
				}
			}
		}

		firstLine = firstLine != null ? firstLine : "Unknown"; //$NON-NLS-1$

		if (Boolean.getBoolean("shell.debug")) { //$NON-NLS-1$
			System.out.println("UtilityEnvironment#getLinuxRelease: return value='" + firstLine + "'"); //$NON-NLS-1$ //$NON-NLS-2$
		}

		return firstLine;
	}

	/**
	 * Check if running interactive (default) or in batch mode (e.g. during unit tests).
	 *
	 * @return boolean <code>true</code> if running in interactive mode (default) or
	 *         <code>false</code> if not (-DNOINTERACTIVE=true).
	 */
	public static boolean isInteractive() {
		if (isInteractive == null) {
			boolean batchMode = Boolean.valueOf(System.getProperty("NOINTERACTIVE")).booleanValue(); //$NON-NLS-1$
			isInteractive = Boolean.valueOf(!batchMode);
		}
		return isInteractive.booleanValue();
	}

	/**
	 * Check if running in headless mode or with full UI.
	 *
	 * @return <code>true</code> if running in headless mode, <code>false</code> otherwise.
	 */
	public static boolean isHeadless() {
		if (isHeadless == null) {
			String headless = System.getProperty("HEADLESS"); //$NON-NLS-1$
			isHeadless = Boolean.valueOf(headless);
		}
		return isHeadless.booleanValue();
	}
}

Back to the top