~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/java/lang/VMSystemProperties.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2004-2011 Jeroen Frijters
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
package java.lang;
 
25
 
 
26
import java.util.Properties;
 
27
import static ikvm.internal.Util.SafeGetEnvironmentVariable;
 
28
 
 
29
final class VMSystemProperties
 
30
{
 
31
    private VMSystemProperties() { }
 
32
 
 
33
    public static final String SPEC_TITLE = "Java Platform API Specification";
 
34
    public static final String SPEC_VERSION = "1.7";
 
35
    public static final String SPEC_VENDOR = "Oracle Corporation";
 
36
 
 
37
    private static String getLibraryPath()
 
38
    {
 
39
        String libraryPath;
 
40
        if(ikvm.internal.Util.WINDOWS)
 
41
        {
 
42
            // see /hotspot/src/os/windows/vm/os_windows.cpp for the comment that describes how we build the path
 
43
            String windir = SafeGetEnvironmentVariable("SystemRoot");
 
44
            if(windir != null)
 
45
            {
 
46
                libraryPath = cli.System.IO.Path.PathSeparator + windir + "\\Sun\\Java\\bin";
 
47
            }
 
48
            else
 
49
            {
 
50
                libraryPath = null;
 
51
            }
 
52
            try
 
53
            {
 
54
                if(false) throw new cli.System.Security.SecurityException();
 
55
                if (libraryPath == null)
 
56
                {
 
57
                    libraryPath = cli.System.Environment.get_SystemDirectory();
 
58
                }
 
59
                else
 
60
                {
 
61
                    libraryPath += cli.System.IO.Path.PathSeparator + cli.System.Environment.get_SystemDirectory();
 
62
                }
 
63
            }
 
64
            catch(cli.System.Security.SecurityException _)
 
65
            {
 
66
            }
 
67
            if(windir != null)
 
68
            {
 
69
                libraryPath += cli.System.IO.Path.PathSeparator + windir;
 
70
            }
 
71
            String path = SafeGetEnvironmentVariable("PATH");
 
72
            if(path != null)
 
73
            {
 
74
                libraryPath += cli.System.IO.Path.PathSeparator + path;
 
75
            }
 
76
        }
 
77
        else if(ikvm.internal.Util.MACOSX)
 
78
        {
 
79
            libraryPath = ".";
 
80
        }
 
81
        else /* assume Linux, since that's the only other platform we support */
 
82
        {
 
83
            // on Linux we have some hardcoded paths (from /hotspot/src/os/linux/vm/os_linux.cpp)
 
84
            // and we can only guess the cpu arch based on bitness (that means only x86 and x64)
 
85
            String cpu_arch = cli.System.IntPtr.get_Size() == 4 ? "i386" : "amd64";
 
86
            libraryPath = "/usr/java/packages/lib/" + cpu_arch + ":/lib:/usr/lib";
 
87
            String ld_library_path = SafeGetEnvironmentVariable("LD_LIBRARY_PATH");
 
88
            if(ld_library_path != null)
 
89
            {
 
90
                libraryPath = ld_library_path + ":" + libraryPath;
 
91
            }
 
92
        }
 
93
        try
 
94
        {
 
95
            cli.System.Reflection.Assembly entryAsm = cli.System.Reflection.Assembly.GetEntryAssembly();
 
96
            // If the CLR was started by a native app (e.g. via COM interop) there is no entry assembly
 
97
            if (entryAsm != null)
 
98
            {
 
99
                // the application (or launcher) directory is prepended to the library path
 
100
                // (similar to how the JDK prepends its directory to the path)
 
101
                libraryPath = new cli.System.IO.FileInfo(entryAsm.get_Location()).get_DirectoryName() + cli.System.IO.Path.PathSeparator + libraryPath;
 
102
            }
 
103
        }
 
104
        catch(Throwable _)
 
105
        {
 
106
            // ignore
 
107
        }
 
108
        if(ikvm.internal.Util.WINDOWS)
 
109
        {
 
110
            libraryPath += cli.System.IO.Path.PathSeparator + ".";
 
111
        }
 
112
        return libraryPath;
 
113
    }
 
114
 
 
115
    private static void initCommonProperties(Properties p)
 
116
    {
 
117
        p.setProperty("java.version", "1.7.0");
 
118
        p.setProperty("java.vendor", "Jeroen Frijters");
 
119
        p.setProperty("java.vendor.url", "http://ikvm.net/");
 
120
        p.setProperty("java.vendor.url.bug", "http://www.ikvm.net/bugs");
 
121
        p.setProperty("java.vm.specification.version", "1.7");
 
122
        p.setProperty("java.vm.specification.vendor", "Oracle Corporation");
 
123
        p.setProperty("java.vm.specification.name", "Java Virtual Machine Specification");
 
124
        p.setProperty("java.vm.version", PropertyConstants.java_vm_version);
 
125
        p.setProperty("java.vm.vendor", "Jeroen Frijters");
 
126
        p.setProperty("java.vm.name", "IKVM.NET");
 
127
        p.setProperty("java.runtime.name", "IKVM.NET");
 
128
        p.setProperty("java.runtime.version", PropertyConstants.java_runtime_version);
 
129
        p.setProperty("java.specification.version", SPEC_VERSION);
 
130
        p.setProperty("java.specification.vendor", SPEC_VENDOR);
 
131
        p.setProperty("java.specification.name", SPEC_TITLE);
 
132
        p.setProperty("java.class.version", "51.0");
 
133
        p.setProperty("java.class.path", "");
 
134
        p.setProperty("java.library.path", getLibraryPath());
 
135
        try
 
136
        {
 
137
            if(false) throw new cli.System.Security.SecurityException();
 
138
            p.setProperty("java.io.tmpdir", cli.System.IO.Path.GetTempPath());
 
139
        }
 
140
        catch(cli.System.Security.SecurityException _)
 
141
        {
 
142
            // TODO should we set another value?
 
143
            p.setProperty("java.io.tmpdir", ".");
 
144
        }
 
145
        p.setProperty("java.ext.dirs", "");
 
146
        // NOTE os.name *must* contain "Windows" when running on Windows, because Classpath tests on that
 
147
        String osname = null;
 
148
        String osver = null;
 
149
        cli.System.OperatingSystem os = cli.System.Environment.get_OSVersion();
 
150
        int major = os.get_Version().get_Major();
 
151
        int minor = os.get_Version().get_Minor();
 
152
        switch(os.get_Platform().Value)
 
153
        {
 
154
            case cli.System.PlatformID.Win32NT:
 
155
                osname = "Windows NT (unknown)";
 
156
                switch(major)
 
157
                {
 
158
                    case 3:
 
159
                    case 4:
 
160
                        osver = major + "." + minor;
 
161
                        osname = "Windows NT";
 
162
                        break;
 
163
                    case 5:
 
164
                        switch(minor)
 
165
                        {
 
166
                            case 0:
 
167
                                osver = "5.0";
 
168
                                osname = "Windows 2000";
 
169
                                break;
 
170
                            case 1:
 
171
                                osver = "5.1";
 
172
                                osname = "Windows XP";
 
173
                                break;
 
174
                            case 2:
 
175
                                osver = "5.2";
 
176
                                osname = "Windows 2003";
 
177
                                break;
 
178
                        }
 
179
                        break;
 
180
                    case 6:
 
181
                        // since there appears to be no managed way to differentiate between Client/Server, we report client names
 
182
                        switch(minor)
 
183
                        {
 
184
                            case 0:
 
185
                                osver = "6.0";
 
186
                                osname = "Windows Vista";
 
187
                                break;
 
188
                            case 1:
 
189
                                osver = "6.1";
 
190
                                osname = "Windows 7";
 
191
                                break;
 
192
                        }
 
193
                        break;
 
194
                }
 
195
                break;
 
196
            case cli.System.PlatformID.Win32Windows:
 
197
                if(major == 4)
 
198
                {
 
199
                    switch(minor)
 
200
                    {
 
201
                        case 0:
 
202
                            osver = "4.0";
 
203
                            osname = "Windows 95";
 
204
                            break;
 
205
                        case 10:
 
206
                            osver = "4.10";
 
207
                            osname = "Windows 98";
 
208
                            break;
 
209
                        case 90:
 
210
                            osver = "4.90";
 
211
                            osname = "Windows Me";
 
212
                            break;
 
213
                    }
 
214
                }
 
215
                break;
 
216
            case cli.System.PlatformID.Unix:
 
217
                if(ikvm.internal.Util.MACOSX)
 
218
                {
 
219
                    // for back compat Mono will return PlatformID.Unix when running on the Mac,
 
220
                    // so we handle that explicitly here
 
221
                    osname = "Mac OS X";
 
222
                    // HACK this tries to map the Darwin version to the OS X version
 
223
                    // (based on http://en.wikipedia.org/wiki/Darwin_(operating_system)#Releases)
 
224
                    cli.System.Version ver = cli.System.Environment.get_OSVersion().get_Version();
 
225
                    osver = "10." + (ver.get_Major() - 4) + "." + ver.get_Minor();
 
226
                }
 
227
                break;
 
228
        }
 
229
        if(osname == null)
 
230
        {
 
231
            osname = cli.System.Environment.get_OSVersion().ToString();
 
232
        }
 
233
        if(osver == null)
 
234
        {
 
235
            osver = major + "." + minor;
 
236
        }
 
237
        p.setProperty("os.name", osname);
 
238
        p.setProperty("os.version", osver);
 
239
        String arch = SafeGetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
 
240
        if(arch == null)
 
241
        {
 
242
            // we don't know, so we make a guess
 
243
            if(cli.System.IntPtr.get_Size() == 4)
 
244
            {
 
245
                arch = ikvm.internal.Util.WINDOWS ? "x86" : "i386";
 
246
            }
 
247
            else
 
248
            {
 
249
                arch = "amd64";
 
250
            }
 
251
        }
 
252
        if(arch.equals("AMD64"))
 
253
        {
 
254
            arch = "amd64";
 
255
        }
 
256
        p.setProperty("os.arch", arch);
 
257
        p.setProperty("sun.arch.data.model", "" + (cli.System.IntPtr.get_Size() * 8));
 
258
        p.setProperty("file.separator", "" + cli.System.IO.Path.DirectorySeparatorChar);
 
259
        p.setProperty("file.encoding", cli.System.Text.Encoding.get_Default().get_WebName());
 
260
        p.setProperty("path.separator", "" + cli.System.IO.Path.PathSeparator);
 
261
        p.setProperty("line.separator", cli.System.Environment.get_NewLine());
 
262
        try
 
263
        {
 
264
            if(false) throw new cli.System.Security.SecurityException();
 
265
            p.setProperty("user.name", cli.System.Environment.get_UserName());
 
266
        }
 
267
        catch(cli.System.Security.SecurityException _)
 
268
        {
 
269
            p.setProperty("user.name", "(unknown)");
 
270
        }
 
271
        String home = SafeGetEnvironmentVariable("USERPROFILE");
 
272
        if(home == null)
 
273
        {
 
274
            // maybe we're on *nix
 
275
            home = SafeGetEnvironmentVariable("HOME");
 
276
            if(home == null)
 
277
            {
 
278
                // TODO maybe there is a better way
 
279
                // NOTE on MS .NET this doesn't return the correct path
 
280
                // (it returns "C:\\Documents and Settings\\username\\My Documents", but we really need
 
281
                // "C:\\Documents and Settings\\username" to be compatible with Sun, that's why we use %USERPROFILE% if it exists)
 
282
                try
 
283
                {
 
284
                    if(false) throw new cli.System.Security.SecurityException();
 
285
                    home = cli.System.Environment.GetFolderPath(cli.System.Environment.SpecialFolder.wrap(cli.System.Environment.SpecialFolder.Personal));
 
286
                }
 
287
                catch(cli.System.Security.SecurityException _)
 
288
                {
 
289
                    home = ".";
 
290
                }
 
291
            }
 
292
        }
 
293
        p.setProperty("user.home", home);
 
294
        try
 
295
        {
 
296
            if(false) throw new cli.System.Security.SecurityException();
 
297
            p.setProperty("user.dir", cli.System.Environment.get_CurrentDirectory());
 
298
        }
 
299
        catch(cli.System.Security.SecurityException _)
 
300
        {
 
301
            p.setProperty("user.dir", ".");
 
302
        }
 
303
        p.setProperty("awt.toolkit", PropertyConstants.awt_toolkit);
 
304
    }
 
305
 
 
306
    public static void initProperties(Properties p)
 
307
    {
 
308
        p.setProperty("openjdk.version", PropertyConstants.openjdk_version);
 
309
        String vfsroot = getVirtualFileSystemRoot();
 
310
        p.setProperty("java.home", vfsroot.substring(0, vfsroot.length() - 1));
 
311
        // the %home%\lib\endorsed directory does not exist, but neither does it on JDK 1.7
 
312
        p.setProperty("java.endorsed.dirs", vfsroot + "lib" + cli.System.IO.Path.DirectorySeparatorChar + "endorsed");
 
313
        p.setProperty("sun.boot.library.path", vfsroot + "bin");
 
314
        p.setProperty("sun.boot.class.path", getBootClassPath());
 
315
        initCommonProperties(p);
 
316
        setupI18N(p);
 
317
        p.setProperty("sun.cpu.endian", cli.System.BitConverter.IsLittleEndian ? "little" : "big");
 
318
        p.setProperty("file.encoding.pkg", "sun.io");
 
319
        p.setProperty("user.timezone", "");
 
320
        p.setProperty("sun.os.patch.level", cli.System.Environment.get_OSVersion().get_ServicePack());
 
321
        p.setProperty("java.vm.info", "compiled mode");
 
322
        p.setProperty("sun.nio.MaxDirectMemorySize", "-1");
 
323
        p.setProperty("java.awt.graphicsenv", PropertyConstants.java_awt_graphicsenv);
 
324
        p.setProperty("java.awt.printerjob", "sun.awt.windows.WPrinterJob");
 
325
        
 
326
        // TODO
 
327
        // sun.cpu.isalist:=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
 
328
        // sun.desktop:=windows
 
329
        // sun.io.unicode.encoding:=UnicodeLittle
 
330
        // sun.jnu.encoding:=Cp1252
 
331
        // sun.management.compiler:=HotSpot Client Compiler
 
332
        try
 
333
        {
 
334
            // read properties from app.config
 
335
            if(false) throw new cli.System.Configuration.ConfigurationException();
 
336
            cli.System.Collections.Specialized.NameValueCollection appSettings = cli.System.Configuration.ConfigurationSettings.get_AppSettings();
 
337
            cli.System.Collections.IEnumerator keys = appSettings.GetEnumerator();
 
338
            while(keys.MoveNext())
 
339
            {
 
340
                String key = (String)keys.get_Current();
 
341
                if(key.startsWith("ikvm:"))
 
342
                {
 
343
                    p.setProperty(key.substring(5), appSettings.get_Item(key));
 
344
                }
 
345
            }
 
346
        }
 
347
        catch(cli.System.Configuration.ConfigurationException _)
 
348
        {
 
349
            // app.config is invalid, ignore
 
350
        }
 
351
        // set the properties that were specified with ikvm.runtime.Startup.setProperties()
 
352
        cli.System.Collections.IDictionary props = ikvm.runtime.Startup.props;
 
353
        if(props != null)
 
354
        {
 
355
            cli.System.Collections.IDictionaryEnumerator entries = props.GetEnumerator();
 
356
            while(entries.MoveNext())
 
357
            {
 
358
                p.setProperty((String)entries.get_Key(), (String)entries.get_Value());
 
359
            }
 
360
        }
 
361
    }
 
362
 
 
363
    private static void setupI18N(Properties p)
 
364
    {
 
365
        String[] culture = ((cli.System.String)(Object)cli.System.Globalization.CultureInfo.get_CurrentCulture().get_Name()).Split(new char[] { '-' });
 
366
        String language;
 
367
        String script;
 
368
        String region;
 
369
        String variant;
 
370
        if (culture.length == 2)
 
371
        {
 
372
            language = culture[0];
 
373
            if (culture[1].length() == 4)
 
374
            {
 
375
                script = culture[1];
 
376
                region = "";
 
377
            }
 
378
            else
 
379
            {
 
380
                script = "";
 
381
                region = culture[1];
 
382
            }
 
383
        }
 
384
        else if (culture.length == 3)
 
385
        {
 
386
            language = culture[0];
 
387
            script = culture[1];
 
388
            region = culture[2];
 
389
        }
 
390
        else
 
391
        {
 
392
            language = "en";
 
393
            script = "";
 
394
            region = "US";
 
395
        }
 
396
        // Norwegian
 
397
        if (language.equals("nb"))
 
398
        {
 
399
            language = "no";
 
400
            region = "NO";
 
401
            variant = "";
 
402
        }
 
403
        else if (language.equals("nn"))
 
404
        {
 
405
            language = "no";
 
406
            region = "NO";
 
407
            variant = "NY";
 
408
        }
 
409
        else
 
410
        {
 
411
            variant = "";
 
412
        }
 
413
        p.setProperty("user.language", language);
 
414
        p.setProperty("user.country", region);
 
415
        p.setProperty("user.variant", variant);
 
416
        p.setProperty("user.script", script);
 
417
    }
 
418
 
 
419
    private static native String getVirtualFileSystemRoot();
 
420
    private static native String getBootClassPath();
 
421
}