KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > internal > adaptor > EclipseEnvironmentInfo


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.core.runtime.internal.adaptor;
12
13 import java.util.*;
14 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
15 import org.eclipse.osgi.service.environment.Constants;
16 import org.eclipse.osgi.service.environment.EnvironmentInfo;
17 import org.eclipse.osgi.util.NLS;
18
19 /**
20  * Internal class.
21  */

22 public class EclipseEnvironmentInfo implements EnvironmentInfo {
23     private static EclipseEnvironmentInfo singleton;
24     private static String JavaDoc nl;
25     private static String JavaDoc os;
26     private static String JavaDoc ws;
27     private static String JavaDoc arch;
28     static String JavaDoc[] allArgs;
29     static String JavaDoc[] frameworkArgs;
30     static String JavaDoc[] appArgs;
31
32     // While we recognize the SunOS operating system, we change
33
// this internally to be Solaris.
34
private static final String JavaDoc INTERNAL_OS_SUNOS = "SunOS"; //$NON-NLS-1$
35
private static final String JavaDoc INTERNAL_OS_LINUX = "Linux"; //$NON-NLS-1$
36
private static final String JavaDoc INTERNAL_OS_MACOSX = "Mac OS"; //$NON-NLS-1$
37
private static final String JavaDoc INTERNAL_OS_AIX = "AIX"; //$NON-NLS-1$
38
private static final String JavaDoc INTERNAL_OS_HPUX = "HP-UX"; //$NON-NLS-1$
39
private static final String JavaDoc INTERNAL_OS_QNX = "QNX"; //$NON-NLS-1$
40

41     // While we recognize the i386 architecture, we change
42
// this internally to be x86.
43
private static final String JavaDoc INTERNAL_ARCH_I386 = "i386"; //$NON-NLS-1$
44
// While we recognize the amd64 architecture, we change
45
// this internally to be x86_64.
46
private static final String JavaDoc INTERNAL_AMD64 = "amd64"; //$NON-NLS-1$
47

48     private EclipseEnvironmentInfo() {
49         super();
50         setupSystemContext();
51     }
52
53     public static EclipseEnvironmentInfo getDefault() {
54         if (singleton == null)
55             singleton = new EclipseEnvironmentInfo();
56         return singleton;
57     }
58
59     public boolean inDevelopmentMode() {
60         return FrameworkProperties.getProperty("osgi.dev") != null; //$NON-NLS-1$
61
}
62
63     public boolean inDebugMode() {
64         return FrameworkProperties.getProperty("osgi.debug") != null; //$NON-NLS-1$
65
}
66
67     public String JavaDoc[] getCommandLineArgs() {
68         return allArgs;
69     }
70
71     public String JavaDoc[] getFrameworkArgs() {
72         return frameworkArgs;
73     }
74
75     public String JavaDoc[] getNonFrameworkArgs() {
76         return appArgs;
77     }
78
79     public String JavaDoc getOSArch() {
80         return arch;
81     }
82
83     public String JavaDoc getNL() {
84         return nl;
85     }
86
87     public String JavaDoc getOS() {
88         return os;
89     }
90
91     public String JavaDoc getWS() {
92         return ws;
93     }
94
95     /**
96      * Initializes the execution context for this run of the platform. The context
97      * includes information about the locale, operating system and window system.
98      *
99      * NOTE: The OS, WS, and ARCH values should never be null. The executable should
100      * be setting these values and therefore this code path is obsolete for Eclipse
101      * when run from the executable.
102      */

103     private static void setupSystemContext() {
104         // if the user didn't set the locale with a command line argument then use the default.
105
nl = FrameworkProperties.getProperty("osgi.nl"); //$NON-NLS-1$
106
if (nl != null) {
107             StringTokenizer tokenizer = new StringTokenizer(nl, "_"); //$NON-NLS-1$
108
int segments = tokenizer.countTokens();
109             try {
110                 Locale userLocale = null;
111                 switch (segments) {
112                     case 1 :
113                         // use the 2 arg constructor to maintain compatibility with 1.3.1
114
userLocale = new Locale(tokenizer.nextToken(), ""); //$NON-NLS-1$
115
break;
116                     case 2 :
117                         userLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
118                         break;
119                     case 3 :
120                         userLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken(), tokenizer.nextToken());
121                         break;
122                     default :
123                         // if the user passed us in a bogus value then log a message and use the default
124
System.err.println(NLS.bind(EclipseAdaptorMsg.error_badNL, nl));
125                         userLocale = Locale.getDefault();
126                         break;
127                 }
128                 Locale.setDefault(userLocale);
129                 // TODO what the heck is this for?? why not just use osgi.nl
130
FrameworkProperties.setProperty("osgi.nl.user", nl); //$NON-NLS-1$
131
} catch (NoSuchElementException e) {
132                 // fall through and use the default
133
}
134         }
135         nl = Locale.getDefault().toString();
136         FrameworkProperties.setProperty("osgi.nl", nl); //$NON-NLS-1$
137

138         // if the user didn't set the operating system with a command line
139
// argument then use the default.
140
os = FrameworkProperties.getProperty("osgi.os"); //$NON-NLS-1$
141
if (os == null) {
142             os = guessOS(FrameworkProperties.getProperty("os.name"));//$NON-NLS-1$);
143
FrameworkProperties.setProperty("osgi.os", os); //$NON-NLS-1$
144
}
145
146         // if the user didn't set the window system with a command line
147
// argument then use the default.
148
ws = FrameworkProperties.getProperty("osgi.ws"); //$NON-NLS-1$
149
if (ws == null) {
150             ws = guessWS(os);
151             FrameworkProperties.setProperty("osgi.ws", ws); //$NON-NLS-1$
152
}
153
154         // if the user didn't set the system architecture with a command line
155
// argument then use the default.
156
arch = FrameworkProperties.getProperty("osgi.arch"); //$NON-NLS-1$
157
if (arch == null) {
158             String JavaDoc name = FrameworkProperties.getProperty("os.arch");//$NON-NLS-1$
159
// Map i386 architecture to x86
160
if (name.equalsIgnoreCase(INTERNAL_ARCH_I386))
161                 arch = Constants.ARCH_X86;
162             // Map amd64 architecture to x86_64
163
else if (name.equalsIgnoreCase(INTERNAL_AMD64))
164                 arch = Constants.ARCH_X86_64;
165             else
166                 arch = name;
167             FrameworkProperties.setProperty("osgi.arch", arch); //$NON-NLS-1$
168
}
169     }
170
171     public static void setAllArgs(String JavaDoc[] allArgs) {
172         if (EclipseEnvironmentInfo.allArgs == null)
173             EclipseEnvironmentInfo.allArgs = allArgs;
174     }
175
176     public static void setAppArgs(String JavaDoc[] appArgs) {
177         if (EclipseEnvironmentInfo.appArgs == null)
178             EclipseEnvironmentInfo.appArgs = appArgs;
179     }
180
181     public static void setFrameworkArgs(String JavaDoc[] frameworkArgs) {
182         if (EclipseEnvironmentInfo.frameworkArgs == null)
183             EclipseEnvironmentInfo.frameworkArgs = frameworkArgs;
184     }
185
186     public static String JavaDoc guessWS(String JavaDoc os) {
187         // setup default values for known OSes if nothing was specified
188
if (os.equals(Constants.OS_WIN32))
189             return Constants.WS_WIN32;
190         if (os.equals(Constants.OS_LINUX))
191             return Constants.WS_MOTIF;
192         if (os.equals(Constants.OS_MACOSX))
193             return Constants.WS_CARBON;
194         if (os.equals(Constants.OS_HPUX))
195             return Constants.WS_MOTIF;
196         if (os.equals(Constants.OS_AIX))
197             return Constants.WS_MOTIF;
198         if (os.equals(Constants.OS_SOLARIS))
199             return Constants.WS_MOTIF;
200         if (os.equals(Constants.OS_QNX))
201             return Constants.WS_PHOTON;
202         return Constants.WS_UNKNOWN;
203     }
204
205     public static String JavaDoc guessOS(String JavaDoc osName) {
206         // check to see if the OS name is "Windows 98" or some other
207
// flavour which should be converted to win32.
208
if (osName.regionMatches(true, 0, Constants.OS_WIN32, 0, 3))
209             return Constants.OS_WIN32;
210         // EXCEPTION: All mappings of SunOS convert to Solaris
211
if (osName.equalsIgnoreCase(INTERNAL_OS_SUNOS))
212             return Constants.OS_SOLARIS;
213         if (osName.equalsIgnoreCase(INTERNAL_OS_LINUX))
214             return Constants.OS_LINUX;
215         if (osName.equalsIgnoreCase(INTERNAL_OS_QNX))
216             return Constants.OS_QNX;
217         if (osName.equalsIgnoreCase(INTERNAL_OS_AIX))
218             return Constants.OS_AIX;
219         if (osName.equalsIgnoreCase(INTERNAL_OS_HPUX))
220             return Constants.OS_HPUX;
221         // os.name on Mac OS can be either Mac OS or Mac OS X
222
if (osName.regionMatches(true, 0, INTERNAL_OS_MACOSX, 0, INTERNAL_OS_MACOSX.length()))
223             return Constants.OS_MACOSX;
224         return Constants.OS_UNKNOWN;
225     }
226 }
227
Popular Tags