source: trunk/gcc/libjava/java/lang/System.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 22.2 KB
Line 
1/* System.java -- useful methods to interface with the system
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.lang;
40
41import java.io.*;
42import java.util.Properties;
43import java.util.PropertyPermission;
44import gnu.classpath.Configuration;
45
46/**
47 * System represents system-wide resources; things that represent the
48 * general environment. As such, all methods are static.
49 *
50 * @author John Keiser
51 * @author Eric Blake <ebb9@email.byu.edu>
52 * @since 1.0
53 * @status still missing 1.4 functionality
54 */
55public final class System
56{
57 // WARNING: System is a CORE class in the bootstrap cycle. See the comments
58 // in vm/reference/java/lang/Runtime for implications of this fact.
59
60 /**
61 * Add to the default properties. The field is stored in Runtime, because
62 * of the bootstrap sequence; but this adds several useful properties to
63 * the defaults. Once the default is stabilized, it should not be modified;
64 * instead it is passed as a parent properties for fast setup of the
65 * defaults when calling <code>setProperties(null)</code>.
66 */
67 static
68 {
69 // Note that this loadLibrary() takes precedence over the one in Object,
70 // since Object.<clinit> is waiting for System.<clinit> to complete
71 // first; but loading a library twice is harmless.
72 if (Configuration.INIT_LOAD_LIBRARY)
73 loadLibrary("javalang");
74
75 Properties defaultProperties = Runtime.defaultProperties;
76
77 // Set base URL if not already set.
78 if (defaultProperties.get("gnu.classpath.home.url") == null)
79 defaultProperties.put("gnu.classpath.home.url",
80 "file://"
81 + defaultProperties.get("gnu.classpath.home")
82 + "/lib");
83
84 // Set short name if not already set.
85 if (defaultProperties.get("gnu.classpath.vm.shortname") == null)
86 {
87 String value = defaultProperties.getProperty("java.vm.name");
88 int index = value.lastIndexOf(' ');
89 if (index != -1)
90 value = value.substring(index + 1);
91 defaultProperties.put("gnu.classpath.vm.shortname", value);
92 }
93
94 defaultProperties.put("gnu.cpu.endian",
95 isWordsBigEndian() ? "big" : "little");
96 // XXX FIXME - Temp hack for old systems that set the wrong property
97 if (defaultProperties.get("java.io.tmpdir") == null)
98 defaultProperties.put("java.io.tmpdir",
99 defaultProperties.get("java.tmpdir"));
100 }
101
102 /**
103 * Stores the current system properties. This can be modified by
104 * {@link #setProperties(Properties)}, but will never be null, because
105 * setProperties(null) sucks in the default properties.
106 */
107 // Note that we use clone here and not new. Some programs assume
108 // that the system properties do not have a parent.
109 private static Properties properties
110 = (Properties) Runtime.defaultProperties.clone();
111
112 /**
113 * The standard InputStream. This is assigned at startup and starts its
114 * life perfectly valid. Although it is marked final, you can change it
115 * using {@link #setIn(InputStream)} through some hefty VM magic.
116 *
117 * <p>This corresponds to the C stdin and C++ cin variables, which
118 * typically input from the keyboard, but may be used to pipe input from
119 * other processes or files. That should all be transparent to you,
120 * however.
121 */
122 public static final InputStream in
123 = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
124 /**
125 * The standard output PrintStream. This is assigned at startup and
126 * starts its life perfectly valid. Although it is marked final, you can
127 * change it using {@link #setOut(PrintStream)} through some hefty VM magic.
128 *
129 * <p>This corresponds to the C stdout and C++ cout variables, which
130 * typically output normal messages to the screen, but may be used to pipe
131 * output to other processes or files. That should all be transparent to
132 * you, however.
133 */
134 public static final PrintStream out
135 = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), true);
136 /**
137 * The standard output PrintStream. This is assigned at startup and
138 * starts its life perfectly valid. Although it is marked final, you can
139 * change it using {@link #setOut(PrintStream)} through some hefty VM magic.
140 *
141 * <p>This corresponds to the C stderr and C++ cerr variables, which
142 * typically output error messages to the screen, but may be used to pipe
143 * output to other processes or files. That should all be transparent to
144 * you, however.
145 */
146 public static final PrintStream err
147 = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.err)), true);
148
149 /**
150 * This class is uninstantiable.
151 */
152 private System()
153 {
154 }
155
156 /**
157 * Set {@link #in} to a new InputStream. This uses some VM magic to change
158 * a "final" variable, so naturally there is a security check,
159 * <code>RuntimePermission("setIO")</code>.
160 *
161 * @param in the new InputStream
162 * @throws SecurityException if permission is denied
163 * @since 1.1
164 */
165 public static void setIn(InputStream in)
166 {
167 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
168 if (sm != null)
169 sm.checkPermission(new RuntimePermission("setIO"));
170 setIn0(in);
171 }
172
173 /**
174 * Set {@link #out} to a new PrintStream. This uses some VM magic to change
175 * a "final" variable, so naturally there is a security check,
176 * <code>RuntimePermission("setIO")</code>.
177 *
178 * @param out the new PrintStream
179 * @throws SecurityException if permission is denied
180 * @since 1.1
181 */
182 public static void setOut(PrintStream out)
183 {
184 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
185 if (sm != null)
186 sm.checkPermission(new RuntimePermission("setIO"));
187 setOut0(out);
188 }
189
190 /**
191 * Set {@link #err} to a new PrintStream. This uses some VM magic to change
192 * a "final" variable, so naturally there is a security check,
193 * <code>RuntimePermission("setIO")</code>.
194 *
195 * @param err the new PrintStream
196 * @throws SecurityException if permission is denied
197 * @since 1.1
198 */
199 public static void setErr(PrintStream err)
200 {
201 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
202 if (sm != null)
203 sm.checkPermission(new RuntimePermission("setIO"));
204 setErr0(err);
205 }
206
207 /**
208 * Set the current SecurityManager. If a security manager already exists,
209 * then <code>RuntimePermission("setSecurityManager")</code> is checked
210 * first. Since this permission is denied by the default security manager,
211 * setting the security manager is often an irreversible action.
212 *
213 * <STRONG>Spec Note:</STRONG> Don't ask me, I didn't write it. It looks
214 * pretty vulnerable; whoever gets to the gate first gets to set the policy.
215 * There is probably some way to set the original security manager as a
216 * command line argument to the VM, but I don't know it.
217 *
218 * @param sm the new SecurityManager
219 * @throws SecurityException if permission is denied
220 */
221 public synchronized static void setSecurityManager(SecurityManager sm)
222 {
223 // Implementation note: the field lives in Runtime because of bootstrap
224 // initialization issues. This method is synchronized so that no other
225 // thread changes it to null before this thread makes the change.
226 if (Runtime.securityManager != null)
227 Runtime.securityManager.checkPermission
228 (new RuntimePermission("setSecurityManager"));
229 Runtime.securityManager = sm;
230 }
231
232 /**
233 * Get the current SecurityManager. If the SecurityManager has not been
234 * set yet, then this method returns null.
235 *
236 * @return the current SecurityManager, or null
237 */
238 public static SecurityManager getSecurityManager()
239 {
240 // Implementation note: the field lives in Runtime because of bootstrap
241 // initialization issues.
242 return Runtime.securityManager;
243 }
244
245 /**
246 * Get the current time, measured in the number of milliseconds from the
247 * beginning of Jan. 1, 1970. This is gathered from the system clock, with
248 * any attendant incorrectness (it may be timezone dependent).
249 *
250 * @return the current time
251 * @see java.util.Date
252 */
253 public static native long currentTimeMillis();
254
255 /**
256 * Copy one array onto another from <code>src[srcStart]</code> ...
257 * <code>src[srcStart+len-1]</code> to <code>dest[destStart]</code> ...
258 * <code>dest[destStart+len-1]</code>. First, the arguments are validated:
259 * neither array may be null, they must be of compatible types, and the
260 * start and length must fit within both arrays. Then the copying starts,
261 * and proceeds through increasing slots. If src and dest are the same
262 * array, this will appear to copy the data to a temporary location first.
263 * An ArrayStoreException in the middle of copying will leave earlier
264 * elements copied, but later elements unchanged.
265 *
266 * @param src the array to copy elements from
267 * @param srcStart the starting position in src
268 * @param dest the array to copy elements to
269 * @param destStart the starting position in dest
270 * @param len the number of elements to copy
271 * @throws NullPointerException if src or dest is null
272 * @throws ArrayStoreException if src or dest is not an array, if they are
273 * not compatible array types, or if an incompatible runtime type
274 * is stored in dest
275 * @throws IndexOutOfBoundsException if len is negative, or if the start or
276 * end copy position in either array is out of bounds
277 */
278 public static native void arraycopy(Object src, int srcStart,
279 Object dest, int destStart, int len);
280
281 /**
282 * Get a hash code computed by the VM for the Object. This hash code will
283 * be the same as Object's hashCode() method. It is usually some
284 * convolution of the pointer to the Object internal to the VM. It
285 * follows standard hash code rules, in that it will remain the same for a
286 * given Object for the lifetime of that Object.
287 *
288 * @param o the Object to get the hash code for
289 * @return the VM-dependent hash code for this Object
290 * @since 1.1
291 */
292 public static native int identityHashCode(Object o);
293
294 /**
295 * Get all the system properties at once. A security check may be performed,
296 * <code>checkPropertiesAccess</code>. Note that a security manager may
297 * allow getting a single property, but not the entire group.
298 *
299 * <p>The required properties include:
300 * <dl>
301 * <dt>java.version <dd>Java version number
302 * <dt>java.vendor <dd>Java vendor specific string
303 * <dt>java.vendor.url <dd>Java vendor URL
304 * <dt>java.home <dd>Java installation directory
305 * <dt>java.vm.specification.version <dd>VM Spec version
306 * <dt>java.vm.specification.vendor <dd>VM Spec vendor
307 * <dt>java.vm.specification.name <dd>VM Spec name
308 * <dt>java.vm.version <dd>VM implementation version
309 * <dt>java.vm.vendor <dd>VM implementation vendor
310 * <dt>java.vm.name <dd>VM implementation name
311 * <dt>java.specification.version <dd>Java Runtime Environment version
312 * <dt>java.specification.vendor <dd>Java Runtime Environment vendor
313 * <dt>java.specification.name <dd>Java Runtime Environment name
314 * <dt>java.class.version <dd>Java class version number
315 * <dt>java.class.path <dd>Java classpath
316 * <dt>java.library.path <dd>Path for finding Java libraries
317 * <dt>java.io.tmpdir <dd>Default temp file path
318 * <dt>java.compiler <dd>Name of JIT to use
319 * <dt>java.ext.dirs <dd>Java extension path
320 * <dt>os.name <dd>Operating System Name
321 * <dt>os.arch <dd>Operating System Architecture
322 * <dt>os.version <dd>Operating System Version
323 * <dt>file.separator <dd>File separator ("/" on Unix)
324 * <dt>path.separator <dd>Path separator (":" on Unix)
325 * <dt>line.separator <dd>Line separator ("\n" on Unix)
326 * <dt>user.name <dd>User account name
327 * <dt>user.home <dd>User home directory
328 * <dt>user.dir <dd>User's current working directory
329 * </dl>
330 *
331 * In addition, gnu defines several other properties, where ? stands for
332 * each character in '0' through '9':
333 * <dl>
334 * <dl> gnu.classpath.vm.shortname <dd> Succinct version of the VM name;
335 * used for finding property files in file system
336 * <dl> gnu.classpath.home.url <dd> Base URL; used for finding
337 * property files in file system
338 * <dt> gnu.cpu.endian <dd>big or little
339 * <dt> gnu.java.io.encoding_scheme_alias.ISO-8859-? <dd>8859_?
340 * <dt> gnu.java.io.encoding_scheme_alias.iso-8859-? <dd>8859_?
341 * <dt> gnu.java.io.encoding_scheme_alias.iso8859_? <dd>8859_?
342 * <dt> gnu.java.io.encoding_scheme_alias.iso-latin-_? <dd>8859_?
343 * <dt> gnu.java.io.encoding_scheme_alias.latin? <dd>8859_?
344 * <dt> gnu.java.io.encoding_scheme_alias.UTF-8 <dd>UTF8
345 * <dt> gnu.java.io.encoding_scheme_alias.utf-8 <dd>UTF8
346 * </dl>
347 *
348 * @return the system properties, will never be null
349 * @throws SecurityException if permission is denied
350 */
351 public static Properties getProperties()
352 {
353 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
354 if (sm != null)
355 sm.checkPropertiesAccess();
356 return properties;
357 }
358
359 /**
360 * Set all the system properties at once. A security check may be performed,
361 * <code>checkPropertiesAccess</code>. Note that a security manager may
362 * allow setting a single property, but not the entire group. An argument
363 * of null resets the properties to the startup default.
364 *
365 * @param properties the new set of system properties
366 * @throws SecurityException if permission is denied
367 */
368 public static void setProperties(Properties properties)
369 {
370 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
371 if (sm != null)
372 sm.checkPropertiesAccess();
373 if (properties == null)
374 {
375 // Note that we use clone here and not new. Some programs
376 // assume that the system properties do not have a parent.
377 properties = (Properties) Runtime.defaultProperties.clone();
378 }
379 System.properties = properties;
380 }
381
382 /**
383 * Get a single system property by name. A security check may be performed,
384 * <code>checkPropertyAccess(key)</code>.
385 *
386 * @param key the name of the system property to get
387 * @return the property, or null if not found
388 * @throws SecurityException if permission is denied
389 * @throws NullPointerException if key is null
390 * @throws IllegalArgumentException if key is ""
391 */
392 public static String getProperty(String key)
393 {
394 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
395 if (sm != null)
396 sm.checkPropertyAccess(key);
397 else if (key.length() == 0)
398 throw new IllegalArgumentException("key can't be empty");
399 return properties.getProperty(key);
400 }
401
402 /**
403 * Get a single system property by name. A security check may be performed,
404 * <code>checkPropertyAccess(key)</code>.
405 *
406 * @param key the name of the system property to get
407 * @param def the default
408 * @return the property, or def if not found
409 * @throws SecurityException if permission is denied
410 * @throws NullPointerException if key is null
411 * @throws IllegalArgumentException if key is ""
412 */
413 public static String getProperty(String key, String def)
414 {
415 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
416 if (sm != null)
417 sm.checkPropertyAccess(key);
418 return properties.getProperty(key, def);
419 }
420
421 /**
422 * Set a single system property by name. A security check may be performed,
423 * <code>checkPropertyAccess(key, "write")</code>.
424 *
425 * @param key the name of the system property to set
426 * @param value the new value
427 * @return the previous value, or null
428 * @throws SecurityException if permission is denied
429 * @throws NullPointerException if key is null
430 * @throws IllegalArgumentException if key is ""
431 * @since 1.2
432 */
433 public static String setProperty(String key, String value)
434 {
435 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
436 if (sm != null)
437 sm.checkPermission(new PropertyPermission(key, "write"));
438 return (String) properties.setProperty(key, value);
439 }
440
441 /**
442 * This used to get an environment variable, but following Sun's lead,
443 * it now throws an Error. Use <code>getProperty</code> instead.
444 *
445 * @param name the name of the environment variable
446 * @return this does not return
447 * @throws Error this is not supported
448 * @deprecated use {@link #getProperty(String)}; getenv is not supported
449 */
450 public static String getenv(String name)
451 {
452 throw new Error("getenv no longer supported, use properties instead: "
453 + name);
454 }
455
456 /**
457 * Terminate the Virtual Machine. This just calls
458 * <code>Runtime.getRuntime().exit(status)</code>, and never returns.
459 * Obviously, a security check is in order, <code>checkExit</code>.
460 *
461 * @param status the exit status; by convention non-zero is abnormal
462 * @throws SecurityException if permission is denied
463 * @see Runtime#exit(int)
464 */
465 public static void exit(int status)
466 {
467 Runtime.getRuntime().exit(status);
468 }
469
470 /**
471 * Calls the garbage collector. This is only a hint, and it is up to the
472 * implementation what this hint suggests, but it usually causes a
473 * best-effort attempt to reclaim unused memory from discarded objects.
474 * This calls <code>Runtime.getRuntime().gc()</code>.
475 *
476 * @see Runtime#gc()
477 */
478 public static void gc()
479 {
480 Runtime.getRuntime().gc();
481 }
482
483 /**
484 * Runs object finalization on pending objects. This is only a hint, and
485 * it is up to the implementation what this hint suggests, but it usually
486 * causes a best-effort attempt to run finalizers on all objects ready
487 * to be reclaimed. This calls
488 * <code>Runtime.getRuntime().runFinalization()</code>.
489 *
490 * @see Runtime#runFinalization()
491 */
492 public static void runFinalization()
493 {
494 Runtime.getRuntime().runFinalization();
495 }
496
497 /**
498 * Tell the Runtime whether to run finalization before exiting the
499 * JVM. This is inherently unsafe in multi-threaded applications,
500 * since it can force initialization on objects which are still in use
501 * by live threads, leading to deadlock; therefore this is disabled by
502 * default. There may be a security check, <code>checkExit(0)</code>. This
503 * calls <code>Runtime.getRuntime().runFinalizersOnExit()</code>.
504 *
505 * @param finalizeOnExit whether to run finalizers on exit
506 * @throws SecurityException if permission is denied
507 * @see Runtime#runFinalizersOnExit()
508 * @since 1.1
509 * @deprecated never rely on finalizers to do a clean, thread-safe,
510 * mop-up from your code
511 */
512 public static void runFinalizersOnExit(boolean finalizeOnExit)
513 {
514 Runtime.getRuntime().runFinalizersOnExit(finalizeOnExit);
515 }
516
517 /**
518 * Load a code file using its explicit system-dependent filename. A security
519 * check may be performed, <code>checkLink</code>. This just calls
520 * <code>Runtime.getRuntime().load(filename)</code>.
521 *
522 * @param filename the code file to load
523 * @throws SecurityException if permission is denied
524 * @throws UnsatisfiedLinkError if the file cannot be loaded
525 * @see Runtime#load(String)
526 */
527 public static void load(String filename)
528 {
529 Runtime.getRuntime().load(filename);
530 }
531
532 /**
533 * Load a library using its explicit system-dependent filename. A security
534 * check may be performed, <code>checkLink</code>. This just calls
535 * <code>Runtime.getRuntime().load(filename)</code>.
536 *
537 * @param libname the library file to load
538 * @throws SecurityException if permission is denied
539 * @throws UnsatisfiedLinkError if the file cannot be loaded
540 * @see Runtime#load(String)
541 */
542 public static void loadLibrary(String libname)
543 {
544 Runtime.getRuntime().loadLibrary(libname);
545 }
546
547 /**
548 * Convert a library name to its platform-specific variant.
549 *
550 * @param libname the library name, as used in <code>loadLibrary</code>
551 * @return the platform-specific mangling of the name
552 * @since 1.2
553 */
554 public static String mapLibraryName(String libname)
555 {
556 // XXX Fix this!!!!
557 return Runtime.nativeGetLibname("", libname);
558 }
559
560 /**
561 * Detect big-endian systems.
562 *
563 * @return true if the system is big-endian.
564 */
565 static native boolean isWordsBigEndian();
566
567 /**
568 * Set {@link #in} to a new InputStream.
569 *
570 * @param in the new InputStream
571 * @see #setIn(InputStream)
572 */
573 private static native void setIn0(InputStream in);
574
575 /**
576 * Set {@link #out} to a new PrintStream.
577 *
578 * @param out the new PrintStream
579 * @see #setOut(PrintStream)
580 */
581 private static native void setOut0(PrintStream out);
582
583 /**
584 * Set {@link #err} to a new PrintStream.
585 *
586 * @param err the new PrintStream
587 * @see #setErr(PrintStream)
588 */
589 private static native void setErr0(PrintStream err);
590} // class System
Note: See TracBrowser for help on using the repository browser.