Source for java.lang.System

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