KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > adaptor > EnvironmentInfo


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.runtime.adaptor;
12
13 import java.util.*;
14 import org.eclipse.osgi.service.environment.Constants;
15
16 /**
17  * Internal class.
18  */

19 public class EnvironmentInfo implements org.eclipse.osgi.service.environment.EnvironmentInfo {
20     private static EnvironmentInfo singleton;
21     private static String JavaDoc nl;
22     private static String JavaDoc os;
23     private static String JavaDoc ws;
24     private static String JavaDoc arch;
25     static String JavaDoc[] allArgs;
26     static String JavaDoc[] frameworkArgs;
27     static String JavaDoc[] appArgs;
28
29     // While we recognize the SunOS operating system, we change
30
// this internally to be Solaris.
31
private static final String JavaDoc INTERNAL_OS_SUNOS = "SunOS"; //$NON-NLS-1$
32
// While we recognize the i386 architecture, we change
33
// this internally to be x86.
34
private static final String JavaDoc INTERNAL_ARCH_I386 = "i386"; //$NON-NLS-1$
35

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

91     private void setupSystemContext() {
92         // if the user didn't set the locale with a command line argument then
93
// use the default.
94
nl = System.getProperty("osgi.nl"); //$NON-NLS-1$
95
if (nl != null) {
96             StringTokenizer tokenizer = new StringTokenizer(nl, "_"); //$NON-NLS-1$
97
int segments = tokenizer.countTokens();
98             try {
99                 Locale userLocale = null;
100                 switch (segments) {
101                     case 1:
102                         // use the 2 arg constructor to maintain compatibility with 1.3.1
103
userLocale = new Locale(tokenizer.nextToken(), ""); //$NON-NLS-1$
104
break;
105                     case 2:
106                         userLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
107                         break;
108                     case 3:
109                         userLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken(), tokenizer.nextToken());
110                         break;
111                     default:
112                         // if the user passed us in a bogus value then log a message and use the default
113
System.err.println(EclipseAdaptorMsg.formatter.getString("error.badNL", nl)); //$NON-NLS-1$
114
userLocale = Locale.getDefault();
115                         break;
116                 }
117                 Locale.setDefault(userLocale);
118             } catch (NoSuchElementException e) {
119                 // fall through and use the default
120
}
121         }
122         nl = Locale.getDefault().toString();
123         System.getProperties().put("osgi.nl", nl); //$NON-NLS-1$
124

125         // if the user didn't set the operating system with a command line
126
// argument then use the default.
127
os = System.getProperty("osgi.os"); //$NON-NLS-1$
128
if (os == null) {
129             String JavaDoc name = System.getProperty("os.name");//$NON-NLS-1$
130
// check to see if the VM returned "Windows 98" or some other
131
// flavour which should be converted to win32.
132
if (name.regionMatches(true, 0, Constants.OS_WIN32, 0, 3))
133                 os = Constants.OS_WIN32;
134             // EXCEPTION: All mappings of SunOS convert to Solaris
135
if (os == null)
136                 os = name.equalsIgnoreCase(INTERNAL_OS_SUNOS) ? Constants.OS_SOLARIS : Constants.OS_UNKNOWN;
137         }
138         System.getProperties().put("osgi.os", os); //$NON-NLS-1$
139

140         // if the user didn't set the window system with a command line
141
// argument then use the default.
142
ws = System.getProperty("osgi.ws"); //$NON-NLS-1$
143
if (ws == null) {
144             // setup default values for known OSes if nothing was specified
145
if (os.equals(Constants.OS_WIN32))
146                 ws = Constants.WS_WIN32;
147             else if (os.equals(Constants.OS_LINUX))
148                 ws = Constants.WS_MOTIF;
149             else if (os.equals(Constants.OS_MACOSX))
150                 ws = Constants.WS_CARBON;
151             else if (os.equals(Constants.OS_HPUX))
152                 ws = Constants.WS_MOTIF;
153             else if (os.equals(Constants.OS_AIX))
154                 ws = Constants.WS_MOTIF;
155             else if (os.equals(Constants.OS_SOLARIS))
156                 ws = Constants.WS_MOTIF;
157             else
158                 ws = Constants.WS_UNKNOWN;
159         }
160         System.getProperties().put("osgi.ws", ws); //$NON-NLS-1$
161

162         // if the user didn't set the system architecture with a command line
163
// argument then use the default.
164
arch = System.getProperty("osgi.arch"); //$NON-NLS-1$
165
if (arch == null) {
166             String JavaDoc name = System.getProperty("os.arch");//$NON-NLS-1$
167
// Map i386 architecture to x86
168
arch = name.equalsIgnoreCase(INTERNAL_ARCH_I386) ? Constants.ARCH_X86 : name;
169         }
170         System.getProperties().put("osgi.arch", arch); //$NON-NLS-1$
171
}
172
173 }
Popular Tags