Skip to main content
summaryrefslogtreecommitdiffstats
blob: d3a710b12aab30ff14b5cf7b2e7d0d3d38ddc22d (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*******************************************************************************
 * Copyright (c) 2003, 2009 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.core.runtime.internal.adaptor;

import java.util.*;
import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
import org.eclipse.osgi.service.environment.Constants;
import org.eclipse.osgi.service.environment.EnvironmentInfo;
import org.eclipse.osgi.util.NLS;

/**
 * Internal class.
 */
public class EclipseEnvironmentInfo implements EnvironmentInfo {
	private static EclipseEnvironmentInfo singleton;
	private static String nl;
	private static String os;
	private static String ws;
	private static String arch;
	private volatile static String[] allArgs;
	private volatile static String[] frameworkArgs;
	private volatile static String[] appArgs;

	// While we recognize the SunOS operating system, we change
	// this internally to be Solaris.
	private static final String INTERNAL_OS_SUNOS = "SunOS"; //$NON-NLS-1$
	private static final String INTERNAL_OS_LINUX = "Linux"; //$NON-NLS-1$
	private static final String INTERNAL_OS_MACOSX = "Mac OS"; //$NON-NLS-1$
	private static final String INTERNAL_OS_AIX = "AIX"; //$NON-NLS-1$
	private static final String INTERNAL_OS_HPUX = "HP-UX"; //$NON-NLS-1$
	private static final String INTERNAL_OS_QNX = "QNX"; //$NON-NLS-1$
	private static final String INTERNAL_OS_OS400 = "OS/400"; //$NON-NLS-1$
	private static final String INTERNAL_OS_OS390 = "OS/390"; //$NON-NLS-1$
	private static final String INTERNAL_OS_ZOS = "z/OS"; //$NON-NLS-1$

	// While we recognize the i386 architecture, we change
	// this internally to be x86.
	private static final String INTERNAL_ARCH_I386 = "i386"; //$NON-NLS-1$
	// While we recognize the amd64 architecture, we change
	// this internally to be x86_64.
	private static final String INTERNAL_AMD64 = "amd64"; //$NON-NLS-1$

	private EclipseEnvironmentInfo() {
		super();
		setupSystemContext();
	}

	public static EclipseEnvironmentInfo getDefault() {
		if (singleton == null)
			singleton = new EclipseEnvironmentInfo();
		return singleton;
	}

	public boolean inDevelopmentMode() {
		return FrameworkProperties.getProperty("osgi.dev") != null; //$NON-NLS-1$
	}

	public boolean inDebugMode() {
		return FrameworkProperties.getProperty("osgi.debug") != null; //$NON-NLS-1$
	}

	public String[] getCommandLineArgs() {
		return allArgs;
	}

	public String[] getFrameworkArgs() {
		return frameworkArgs;
	}

	public String[] getNonFrameworkArgs() {
		return appArgs;
	}

	public String getOSArch() {
		return arch;
	}

	public String getNL() {
		return nl;
	}

	public String getOS() {
		return os;
	}

	public String getWS() {
		return ws;
	}

	/**
	 * Initializes the execution context for this run of the platform.  The context
	 * includes information about the locale, operating system and window system.
	 * 
	 * NOTE: The OS, WS, and ARCH values should never be null. The executable should
	 * be setting these values and therefore this code path is obsolete for Eclipse
	 * when run from the executable.
	 */
	private static void setupSystemContext() {
		// if the user didn't set the locale with a command line argument then use the default.
		nl = FrameworkProperties.getProperty("osgi.nl"); //$NON-NLS-1$
		if (nl != null) {
			StringTokenizer tokenizer = new StringTokenizer(nl, "_"); //$NON-NLS-1$
			int segments = tokenizer.countTokens();
			try {
				Locale userLocale = null;
				switch (segments) {
					case 1 :
						// use the 2 arg constructor to maintain compatibility with 1.3.1
						userLocale = new Locale(tokenizer.nextToken(), ""); //$NON-NLS-1$
						break;
					case 2 :
						userLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
						break;
					case 3 :
						userLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken(), tokenizer.nextToken());
						break;
					default :
						// if the user passed us in a bogus value then log a message and use the default
						System.err.println(NLS.bind(EclipseAdaptorMsg.error_badNL, nl));
						userLocale = Locale.getDefault();
						break;
				}
				Locale.setDefault(userLocale);
				// TODO what the heck is this for?? why not just use osgi.nl
				FrameworkProperties.setProperty("osgi.nl.user", nl); //$NON-NLS-1$
			} catch (NoSuchElementException e) {
				// fall through and use the default
			}
		}
		nl = Locale.getDefault().toString();
		FrameworkProperties.setProperty("osgi.nl", nl); //$NON-NLS-1$

		// if the user didn't set the operating system with a command line 
		// argument then use the default.
		os = FrameworkProperties.getProperty("osgi.os"); //$NON-NLS-1$
		if (os == null) {
			os = guessOS(FrameworkProperties.getProperty("os.name"));//$NON-NLS-1$);
			FrameworkProperties.setProperty("osgi.os", os); //$NON-NLS-1$
		}

		// if the user didn't set the window system with a command line 
		// argument then use the default.
		ws = FrameworkProperties.getProperty("osgi.ws"); //$NON-NLS-1$
		if (ws == null) {
			ws = guessWS(os);
			FrameworkProperties.setProperty("osgi.ws", ws); //$NON-NLS-1$
		}

		// if the user didn't set the system architecture with a command line 
		// argument then use the default.
		arch = FrameworkProperties.getProperty("osgi.arch"); //$NON-NLS-1$
		if (arch == null) {
			String name = FrameworkProperties.getProperty("os.arch");//$NON-NLS-1$
			// Map i386 architecture to x86
			if (name.equalsIgnoreCase(INTERNAL_ARCH_I386))
				arch = Constants.ARCH_X86;
			// Map amd64 architecture to x86_64
			else if (name.equalsIgnoreCase(INTERNAL_AMD64))
				arch = Constants.ARCH_X86_64;
			else
				arch = name;
			FrameworkProperties.setProperty("osgi.arch", arch); //$NON-NLS-1$			
		}
	}

	public static void setAllArgs(String[] allArgs) {
		// do not check if this is set already to allow arguments to change when multiple applications are launched
		EclipseEnvironmentInfo.allArgs = allArgs;
	}

	public static void setAppArgs(String[] appArgs) {
		// do not check if this is set already to allow arguments to change when multiple applications are launched
		EclipseEnvironmentInfo.appArgs = appArgs;
	}

	public static void setFrameworkArgs(String[] frameworkArgs) {
		// do not check if this is set already to allow arguments to change when multiple applications are launched
		EclipseEnvironmentInfo.frameworkArgs = frameworkArgs;
	}

	public static String guessWS(String os) {
		// setup default values for known OSes if nothing was specified
		if (os.equals(Constants.OS_WIN32))
			return Constants.WS_WIN32;
		if (os.equals(Constants.OS_LINUX))
			return Constants.WS_GTK;
		if (os.equals(Constants.OS_MACOSX))
			return Constants.WS_COCOA;
		if (os.equals(Constants.OS_HPUX))
			return Constants.WS_MOTIF;
		if (os.equals(Constants.OS_AIX))
			return Constants.WS_MOTIF;
		if (os.equals(Constants.OS_SOLARIS))
			return Constants.WS_GTK;
		if (os.equals(Constants.OS_QNX))
			return Constants.WS_PHOTON;
		return Constants.WS_UNKNOWN;
	}

	public static String guessOS(String osName) {
		// check to see if the OS name is "Windows 98" or some other
		// flavour which should be converted to win32.
		if (osName.regionMatches(true, 0, Constants.OS_WIN32, 0, 3))
			return Constants.OS_WIN32;
		// EXCEPTION: All mappings of SunOS convert to Solaris
		if (osName.equalsIgnoreCase(INTERNAL_OS_SUNOS))
			return Constants.OS_SOLARIS;
		if (osName.equalsIgnoreCase(INTERNAL_OS_LINUX))
			return Constants.OS_LINUX;
		if (osName.equalsIgnoreCase(INTERNAL_OS_QNX))
			return Constants.OS_QNX;
		if (osName.equalsIgnoreCase(INTERNAL_OS_AIX))
			return Constants.OS_AIX;
		if (osName.equalsIgnoreCase(INTERNAL_OS_HPUX))
			return Constants.OS_HPUX;
		if (osName.equalsIgnoreCase(INTERNAL_OS_OS400))
			return Constants.OS_OS400;
		if (osName.equalsIgnoreCase(INTERNAL_OS_OS390))
			return Constants.OS_OS390;
		if (osName.equalsIgnoreCase(INTERNAL_OS_ZOS))
			return Constants.OS_ZOS;
		// os.name on Mac OS can be either Mac OS or Mac OS X
		if (osName.regionMatches(true, 0, INTERNAL_OS_MACOSX, 0, INTERNAL_OS_MACOSX.length()))
			return Constants.OS_MACOSX;
		return Constants.OS_UNKNOWN;
	}

	public String getProperty(String key) {
		return FrameworkProperties.getProperty(key);
	}

	public String setProperty(String key, String value) {
		return FrameworkProperties.setProperty(key, value);
	}
}

Back to the top