0001 /*
0002 * Copyright 1994-2007 Sun Microsystems, Inc. All Rights Reserved.
0003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0004 *
0005 * This code is free software; you can redistribute it and/or modify it
0006 * under the terms of the GNU General Public License version 2 only, as
0007 * published by the Free Software Foundation. Sun designates this
0008 * particular file as subject to the "Classpath" exception as provided
0009 * by Sun in the LICENSE file that accompanied this code.
0010 *
0011 * This code is distributed in the hope that it will be useful, but WITHOUT
0012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0014 * version 2 for more details (a copy is included in the LICENSE file that
0015 * accompanied this code).
0016 *
0017 * You should have received a copy of the GNU General Public License version
0018 * 2 along with this work; if not, write to the Free Software Foundation,
0019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0020 *
0021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0022 * CA 95054 USA or visit www.sun.com if you need additional information or
0023 * have any questions.
0024 */
0025 package java.lang;
0026
0027 import java.io.*;
0028 import java.util.Properties;
0029 import java.util.PropertyPermission;
0030 import java.util.StringTokenizer;
0031 import java.security.AccessController;
0032 import java.security.PrivilegedAction;
0033 import java.security.AllPermission;
0034 import java.nio.channels.Channel;
0035 import java.nio.channels.spi.SelectorProvider;
0036 import sun.nio.ch.Interruptible;
0037 import sun.net.InetAddressCachePolicy;
0038 import sun.reflect.Reflection;
0039 import sun.security.util.SecurityConstants;
0040 import sun.reflect.annotation.AnnotationType;
0041
0042 /**
0043 * The <code>System</code> class contains several useful class fields
0044 * and methods. It cannot be instantiated.
0045 *
0046 * <p>Among the facilities provided by the <code>System</code> class
0047 * are standard input, standard output, and error output streams;
0048 * access to externally defined properties and environment
0049 * variables; a means of loading files and libraries; and a utility
0050 * method for quickly copying a portion of an array.
0051 *
0052 * @author unascribed
0053 * @version 1.165, 06/11/07
0054 * @since JDK1.0
0055 */
0056 public final class System {
0057
0058 /* First thing---register the natives */
0059 private static native void registerNatives();
0060
0061 static {
0062 registerNatives();
0063 }
0064
0065 /** Don't let anyone instantiate this class */
0066 private System() {
0067 }
0068
0069 /**
0070 * The "standard" input stream. This stream is already
0071 * open and ready to supply input data. Typically this stream
0072 * corresponds to keyboard input or another input source specified by
0073 * the host environment or user.
0074 */
0075 public final static InputStream in = nullInputStream();
0076
0077 /**
0078 * The "standard" output stream. This stream is already
0079 * open and ready to accept output data. Typically this stream
0080 * corresponds to display output or another output destination
0081 * specified by the host environment or user.
0082 * <p>
0083 * For simple stand-alone Java applications, a typical way to write
0084 * a line of output data is:
0085 * <blockquote><pre>
0086 * System.out.println(data)
0087 * </pre></blockquote>
0088 * <p>
0089 * See the <code>println</code> methods in class <code>PrintStream</code>.
0090 *
0091 * @see java.io.PrintStream#println()
0092 * @see java.io.PrintStream#println(boolean)
0093 * @see java.io.PrintStream#println(char)
0094 * @see java.io.PrintStream#println(char[])
0095 * @see java.io.PrintStream#println(double)
0096 * @see java.io.PrintStream#println(float)
0097 * @see java.io.PrintStream#println(int)
0098 * @see java.io.PrintStream#println(long)
0099 * @see java.io.PrintStream#println(java.lang.Object)
0100 * @see java.io.PrintStream#println(java.lang.String)
0101 */
0102 public final static PrintStream out = nullPrintStream();
0103
0104 /**
0105 * The "standard" error output stream. This stream is already
0106 * open and ready to accept output data.
0107 * <p>
0108 * Typically this stream corresponds to display output or another
0109 * output destination specified by the host environment or user. By
0110 * convention, this output stream is used to display error messages
0111 * or other information that should come to the immediate attention
0112 * of a user even if the principal output stream, the value of the
0113 * variable <code>out</code>, has been redirected to a file or other
0114 * destination that is typically not continuously monitored.
0115 */
0116 public final static PrintStream err = nullPrintStream();
0117
0118 /* The security manager for the system.
0119 */
0120 private static volatile SecurityManager security = null;
0121
0122 /**
0123 * Reassigns the "standard" input stream.
0124 *
0125 * <p>First, if there is a security manager, its <code>checkPermission</code>
0126 * method is called with a <code>RuntimePermission("setIO")</code> permission
0127 * to see if it's ok to reassign the "standard" input stream.
0128 * <p>
0129 *
0130 * @param in the new standard input stream.
0131 *
0132 * @throws SecurityException
0133 * if a security manager exists and its
0134 * <code>checkPermission</code> method doesn't allow
0135 * reassigning of the standard input stream.
0136 *
0137 * @see SecurityManager#checkPermission
0138 * @see java.lang.RuntimePermission
0139 *
0140 * @since JDK1.1
0141 */
0142 public static void setIn(InputStream in) {
0143 checkIO();
0144 setIn0(in);
0145 }
0146
0147 /**
0148 * Reassigns the "standard" output stream.
0149 *
0150 * <p>First, if there is a security manager, its <code>checkPermission</code>
0151 * method is called with a <code>RuntimePermission("setIO")</code> permission
0152 * to see if it's ok to reassign the "standard" output stream.
0153 *
0154 * @param out the new standard output stream
0155 *
0156 * @throws SecurityException
0157 * if a security manager exists and its
0158 * <code>checkPermission</code> method doesn't allow
0159 * reassigning of the standard output stream.
0160 *
0161 * @see SecurityManager#checkPermission
0162 * @see java.lang.RuntimePermission
0163 *
0164 * @since JDK1.1
0165 */
0166 public static void setOut(PrintStream out) {
0167 checkIO();
0168 setOut0(out);
0169 }
0170
0171 /**
0172 * Reassigns the "standard" error output stream.
0173 *
0174 * <p>First, if there is a security manager, its <code>checkPermission</code>
0175 * method is called with a <code>RuntimePermission("setIO")</code> permission
0176 * to see if it's ok to reassign the "standard" error output stream.
0177 *
0178 * @param err the new standard error output stream.
0179 *
0180 * @throws SecurityException
0181 * if a security manager exists and its
0182 * <code>checkPermission</code> method doesn't allow
0183 * reassigning of the standard error output stream.
0184 *
0185 * @see SecurityManager#checkPermission
0186 * @see java.lang.RuntimePermission
0187 *
0188 * @since JDK1.1
0189 */
0190 public static void setErr(PrintStream err) {
0191 checkIO();
0192 setErr0(err);
0193 }
0194
0195 private static volatile Console cons = null;
0196
0197 /**
0198 * Returns the unique {@link java.io.Console Console} object associated
0199 * with the current Java virtual machine, if any.
0200 *
0201 * @return The system console, if any, otherwise <tt>null</tt>.
0202 *
0203 * @since 1.6
0204 */
0205 public static Console console() {
0206 if (cons == null) {
0207 synchronized (System.class) {
0208 cons = sun.misc.SharedSecrets.getJavaIOAccess()
0209 .console();
0210 }
0211 }
0212 return cons;
0213 }
0214
0215 /**
0216 * Returns the channel inherited from the entity that created this
0217 * Java virtual machine.
0218 *
0219 * <p> This method returns the channel obtained by invoking the
0220 * {@link java.nio.channels.spi.SelectorProvider#inheritedChannel
0221 * inheritedChannel} method of the system-wide default
0222 * {@link java.nio.channels.spi.SelectorProvider} object. </p>
0223 *
0224 * <p> In addition to the network-oriented channels described in
0225 * {@link java.nio.channels.spi.SelectorProvider#inheritedChannel
0226 * inheritedChannel}, this method may return other kinds of
0227 * channels in the future.
0228 *
0229 * @return The inherited channel, if any, otherwise <tt>null</tt>.
0230 *
0231 * @throws IOException
0232 * If an I/O error occurs
0233 *
0234 * @throws SecurityException
0235 * If a security manager is present and it does not
0236 * permit access to the channel.
0237 *
0238 * @since 1.5
0239 */
0240 public static Channel inheritedChannel() throws IOException {
0241 return SelectorProvider.provider().inheritedChannel();
0242 }
0243
0244 private static void checkIO() {
0245 SecurityManager sm = getSecurityManager();
0246 if (sm != null) {
0247 sm.checkPermission(new RuntimePermission("setIO"));
0248 }
0249 }
0250
0251 private static native void setIn0(InputStream in);
0252
0253 private static native void setOut0(PrintStream out);
0254
0255 private static native void setErr0(PrintStream err);
0256
0257 /**
0258 * Sets the System security.
0259 *
0260 * <p> If there is a security manager already installed, this method first
0261 * calls the security manager's <code>checkPermission</code> method
0262 * with a <code>RuntimePermission("setSecurityManager")</code>
0263 * permission to ensure it's ok to replace the existing
0264 * security manager.
0265 * This may result in throwing a <code>SecurityException</code>.
0266 *
0267 * <p> Otherwise, the argument is established as the current
0268 * security manager. If the argument is <code>null</code> and no
0269 * security manager has been established, then no action is taken and
0270 * the method simply returns.
0271 *
0272 * @param s the security manager.
0273 * @exception SecurityException if the security manager has already
0274 * been set and its <code>checkPermission</code> method
0275 * doesn't allow it to be replaced.
0276 * @see #getSecurityManager
0277 * @see SecurityManager#checkPermission
0278 * @see java.lang.RuntimePermission
0279 */
0280 public static void setSecurityManager(final SecurityManager s) {
0281 try {
0282 s.checkPackageAccess("java.lang");
0283 } catch (Exception e) {
0284 // no-op
0285 }
0286 setSecurityManager0(s);
0287 }
0288
0289 private static synchronized void setSecurityManager0(
0290 final SecurityManager s) {
0291 SecurityManager sm = getSecurityManager();
0292 if (sm != null) {
0293 // ask the currently installed security manager if we
0294 // can replace it.
0295 sm.checkPermission(new RuntimePermission(
0296 "setSecurityManager"));
0297 }
0298
0299 if ((s != null) && (s.getClass().getClassLoader() != null)) {
0300 // New security manager class is not on bootstrap classpath.
0301 // Cause policy to get initialized before we install the new
0302 // security manager, in order to prevent infinite loops when
0303 // trying to initialize the policy (which usually involves
0304 // accessing some security and/or system properties, which in turn
0305 // calls the installed security manager's checkPermission method
0306 // which will loop infinitely if there is a non-system class
0307 // (in this case: the new security manager class) on the stack).
0308 AccessController
0309 .doPrivileged(new PrivilegedAction<Object>() {
0310 public Object run() {
0311 s.getClass().getProtectionDomain().implies(
0312 SecurityConstants.ALL_PERMISSION);
0313 return null;
0314 }
0315 });
0316 }
0317
0318 security = s;
0319 InetAddressCachePolicy
0320 .setIfNotSet(InetAddressCachePolicy.FOREVER);
0321 }
0322
0323 /**
0324 * Gets the system security interface.
0325 *
0326 * @return if a security manager has already been established for the
0327 * current application, then that security manager is returned;
0328 * otherwise, <code>null</code> is returned.
0329 * @see #setSecurityManager
0330 */
0331 public static SecurityManager getSecurityManager() {
0332 return security;
0333 }
0334
0335 /**
0336 * Returns the current time in milliseconds. Note that
0337 * while the unit of time of the return value is a millisecond,
0338 * the granularity of the value depends on the underlying
0339 * operating system and may be larger. For example, many
0340 * operating systems measure time in units of tens of
0341 * milliseconds.
0342 *
0343 * <p> See the description of the class <code>Date</code> for
0344 * a discussion of slight discrepancies that may arise between
0345 * "computer time" and coordinated universal time (UTC).
0346 *
0347 * @return the difference, measured in milliseconds, between
0348 * the current time and midnight, January 1, 1970 UTC.
0349 * @see java.util.Date
0350 */
0351 public static native long currentTimeMillis();
0352
0353 /**
0354 * Returns the current value of the running Java Virtual Machine's
0355 * high-resolution time source, in nanoseconds.
0356 *
0357 * <p>This method can only be used to measure elapsed time and is
0358 * not related to any other notion of system or wall-clock time.
0359 * The value returned represents nanoseconds since some fixed but
0360 * arbitrary <i>origin</i> time (perhaps in the future, so values
0361 * may be negative). The same origin is used by all invocations of
0362 * this method in an instance of a Java virtual machine; other
0363 * virtual machine instances are likely to use a different origin.
0364 *
0365 * <p>This method provides nanosecond precision, but not necessarily
0366 * nanosecond resolution (that is, how frequently the value changes)
0367 * - no guarantees are made except that the resolution is at least as
0368 * good as that of {@link #currentTimeMillis()}.
0369 *
0370 * <p>Differences in successive calls that span greater than
0371 * approximately 292 years (2<sup>63</sup> nanoseconds) will not
0372 * correctly compute elapsed time due to numerical overflow.
0373 *
0374 * <p>The values returned by this method become meaningful only when
0375 * the difference between two such values, obtained within the same
0376 * instance of a Java virtual machine, is computed.
0377 *
0378 * <p> For example, to measure how long some code takes to execute:
0379 * <pre> {@code
0380 * long startTime = System.nanoTime();
0381 * // ... the code being measured ...
0382 * long estimatedTime = System.nanoTime() - startTime;}</pre>
0383 *
0384 * <p>To compare two nanoTime values
0385 * <pre> {@code
0386 * long t0 = System.nanoTime();
0387 * ...
0388 * long t1 = System.nanoTime();}</pre>
0389 *
0390 * one should use {@code t1 - t0 < 0}, not {@code t1 < t0},
0391 * because of the possibility of numerical overflow.
0392 *
0393 * @return the current value of the running Java Virtual Machine's
0394 * high-resolution time source, in nanoseconds
0395 * @since 1.5
0396 */
0397 public static native long nanoTime();
0398
0399 /**
0400 * Copies an array from the specified source array, beginning at the
0401 * specified position, to the specified position of the destination array.
0402 * A subsequence of array components are copied from the source
0403 * array referenced by <code>src</code> to the destination array
0404 * referenced by <code>dest</code>. The number of components copied is
0405 * equal to the <code>length</code> argument. The components at
0406 * positions <code>srcPos</code> through
0407 * <code>srcPos+length-1</code> in the source array are copied into
0408 * positions <code>destPos</code> through
0409 * <code>destPos+length-1</code>, respectively, of the destination
0410 * array.
0411 * <p>
0412 * If the <code>src</code> and <code>dest</code> arguments refer to the
0413 * same array object, then the copying is performed as if the
0414 * components at positions <code>srcPos</code> through
0415 * <code>srcPos+length-1</code> were first copied to a temporary
0416 * array with <code>length</code> components and then the contents of
0417 * the temporary array were copied into positions
0418 * <code>destPos</code> through <code>destPos+length-1</code> of the
0419 * destination array.
0420 * <p>
0421 * If <code>dest</code> is <code>null</code>, then a
0422 * <code>NullPointerException</code> is thrown.
0423 * <p>
0424 * If <code>src</code> is <code>null</code>, then a
0425 * <code>NullPointerException</code> is thrown and the destination
0426 * array is not modified.
0427 * <p>
0428 * Otherwise, if any of the following is true, an
0429 * <code>ArrayStoreException</code> is thrown and the destination is
0430 * not modified:
0431 * <ul>
0432 * <li>The <code>src</code> argument refers to an object that is not an
0433 * array.
0434 * <li>The <code>dest</code> argument refers to an object that is not an
0435 * array.
0436 * <li>The <code>src</code> argument and <code>dest</code> argument refer
0437 * to arrays whose component types are different primitive types.
0438 * <li>The <code>src</code> argument refers to an array with a primitive
0439 * component type and the <code>dest</code> argument refers to an array
0440 * with a reference component type.
0441 * <li>The <code>src</code> argument refers to an array with a reference
0442 * component type and the <code>dest</code> argument refers to an array
0443 * with a primitive component type.
0444 * </ul>
0445 * <p>
0446 * Otherwise, if any of the following is true, an
0447 * <code>IndexOutOfBoundsException</code> is
0448 * thrown and the destination is not modified:
0449 * <ul>
0450 * <li>The <code>srcPos</code> argument is negative.
0451 * <li>The <code>destPos</code> argument is negative.
0452 * <li>The <code>length</code> argument is negative.
0453 * <li><code>srcPos+length</code> is greater than
0454 * <code>src.length</code>, the length of the source array.
0455 * <li><code>destPos+length</code> is greater than
0456 * <code>dest.length</code>, the length of the destination array.
0457 * </ul>
0458 * <p>
0459 * Otherwise, if any actual component of the source array from
0460 * position <code>srcPos</code> through
0461 * <code>srcPos+length-1</code> cannot be converted to the component
0462 * type of the destination array by assignment conversion, an
0463 * <code>ArrayStoreException</code> is thrown. In this case, let
0464 * <b><i>k</i></b> be the smallest nonnegative integer less than
0465 * length such that <code>src[srcPos+</code><i>k</i><code>]</code>
0466 * cannot be converted to the component type of the destination
0467 * array; when the exception is thrown, source array components from
0468 * positions <code>srcPos</code> through
0469 * <code>srcPos+</code><i>k</i><code>-1</code>
0470 * will already have been copied to destination array positions
0471 * <code>destPos</code> through
0472 * <code>destPos+</code><i>k</I><code>-1</code> and no other
0473 * positions of the destination array will have been modified.
0474 * (Because of the restrictions already itemized, this
0475 * paragraph effectively applies only to the situation where both
0476 * arrays have component types that are reference types.)
0477 *
0478 * @param src the source array.
0479 * @param srcPos starting position in the source array.
0480 * @param dest the destination array.
0481 * @param destPos starting position in the destination data.
0482 * @param length the number of array elements to be copied.
0483 * @exception IndexOutOfBoundsException if copying would cause
0484 * access of data outside array bounds.
0485 * @exception ArrayStoreException if an element in the <code>src</code>
0486 * array could not be stored into the <code>dest</code> array
0487 * because of a type mismatch.
0488 * @exception NullPointerException if either <code>src</code> or
0489 * <code>dest</code> is <code>null</code>.
0490 */
0491 public static native void arraycopy(Object src, int srcPos,
0492 Object dest, int destPos, int length);
0493
0494 /**
0495 * Returns the same hash code for the given object as
0496 * would be returned by the default method hashCode(),
0497 * whether or not the given object's class overrides
0498 * hashCode().
0499 * The hash code for the null reference is zero.
0500 *
0501 * @param x object for which the hashCode is to be calculated
0502 * @return the hashCode
0503 * @since JDK1.1
0504 */
0505 public static native int identityHashCode(Object x);
0506
0507 /**
0508 * System properties. The following properties are guaranteed to be defined:
0509 * <dl>
0510 * <dt>java.version <dd>Java version number
0511 * <dt>java.vendor <dd>Java vendor specific string
0512 * <dt>java.vendor.url <dd>Java vendor URL
0513 * <dt>java.home <dd>Java installation directory
0514 * <dt>java.class.version <dd>Java class version number
0515 * <dt>java.class.path <dd>Java classpath
0516 * <dt>os.name <dd>Operating System Name
0517 * <dt>os.arch <dd>Operating System Architecture
0518 * <dt>os.version <dd>Operating System Version
0519 * <dt>file.separator <dd>File separator ("/" on Unix)
0520 * <dt>path.separator <dd>Path separator (":" on Unix)
0521 * <dt>line.separator <dd>Line separator ("\n" on Unix)
0522 * <dt>user.name <dd>User account name
0523 * <dt>user.home <dd>User home directory
0524 * <dt>user.dir <dd>User's current working directory
0525 * </dl>
0526 */
0527
0528 private static Properties props;
0529
0530 private static native Properties initProperties(Properties props);
0531
0532 /**
0533 * Determines the current system properties.
0534 * <p>
0535 * First, if there is a security manager, its
0536 * <code>checkPropertiesAccess</code> method is called with no
0537 * arguments. This may result in a security exception.
0538 * <p>
0539 * The current set of system properties for use by the
0540 * {@link #getProperty(String)} method is returned as a
0541 * <code>Properties</code> object. If there is no current set of
0542 * system properties, a set of system properties is first created and
0543 * initialized. This set of system properties always includes values
0544 * for the following keys:
0545 * <table summary="Shows property keys and associated values">
0546 * <tr><th>Key</th>
0547 * <th>Description of Associated Value</th></tr>
0548 * <tr><td><code>java.version</code></td>
0549 * <td>Java Runtime Environment version</td></tr>
0550 * <tr><td><code>java.vendor</code></td>
0551 * <td>Java Runtime Environment vendor</td></tr
0552 * <tr><td><code>java.vendor.url</code></td>
0553 * <td>Java vendor URL</td></tr>
0554 * <tr><td><code>java.home</code></td>
0555 * <td>Java installation directory</td></tr>
0556 * <tr><td><code>java.vm.specification.version</code></td>
0557 * <td>Java Virtual Machine specification version</td></tr>
0558 * <tr><td><code>java.vm.specification.vendor</code></td>
0559 * <td>Java Virtual Machine specification vendor</td></tr>
0560 * <tr><td><code>java.vm.specification.name</code></td>
0561 * <td>Java Virtual Machine specification name</td></tr>
0562 * <tr><td><code>java.vm.version</code></td>
0563 * <td>Java Virtual Machine implementation version</td></tr>
0564 * <tr><td><code>java.vm.vendor</code></td>
0565 * <td>Java Virtual Machine implementation vendor</td></tr>
0566 * <tr><td><code>java.vm.name</code></td>
0567 * <td>Java Virtual Machine implementation name</td></tr>
0568 * <tr><td><code>java.specification.version</code></td>
0569 * <td>Java Runtime Environment specification version</td></tr>
0570 * <tr><td><code>java.specification.vendor</code></td>
0571 * <td>Java Runtime Environment specification vendor</td></tr>
0572 * <tr><td><code>java.specification.name</code></td>
0573 * <td>Java Runtime Environment specification name</td></tr>
0574 * <tr><td><code>java.class.version</code></td>
0575 * <td>Java class format version number</td></tr>
0576 * <tr><td><code>java.class.path</code></td>
0577 * <td>Java class path</td></tr>
0578 * <tr><td><code>java.library.path</code></td>
0579 * <td>List of paths to search when loading libraries</td></tr>
0580 * <tr><td><code>java.io.tmpdir</code></td>
0581 * <td>Default temp file path</td></tr>
0582 * <tr><td><code>java.compiler</code></td>
0583 * <td>Name of JIT compiler to use</td></tr>
0584 * <tr><td><code>java.ext.dirs</code></td>
0585 * <td>Path of extension directory or directories</td></tr>
0586 * <tr><td><code>os.name</code></td>
0587 * <td>Operating system name</td></tr>
0588 * <tr><td><code>os.arch</code></td>
0589 * <td>Operating system architecture</td></tr>
0590 * <tr><td><code>os.version</code></td>
0591 * <td>Operating system version</td></tr>
0592 * <tr><td><code>file.separator</code></td>
0593 * <td>File separator ("/" on UNIX)</td></tr>
0594 * <tr><td><code>path.separator</code></td>
0595 * <td>Path separator (":" on UNIX)</td></tr>
0596 * <tr><td><code>line.separator</code></td>
0597 * <td>Line separator ("\n" on UNIX)</td></tr>
0598 * <tr><td><code>user.name</code></td>
0599 * <td>User's account name</td></tr>
0600 * <tr><td><code>user.home</code></td>
0601 * <td>User's home directory</td></tr>
0602 * <tr><td><code>user.dir</code></td>
0603 * <td>User's current working directory</td></tr>
0604 * </table>
0605 * <p>
0606 * Multiple paths in a system property value are separated by the path
0607 * separator character of the platform.
0608 * <p>
0609 * Note that even if the security manager does not permit the
0610 * <code>getProperties</code> operation, it may choose to permit the
0611 * {@link #getProperty(String)} operation.
0612 *
0613 * @return the system properties
0614 * @exception SecurityException if a security manager exists and its
0615 * <code>checkPropertiesAccess</code> method doesn't allow access
0616 * to the system properties.
0617 * @see #setProperties
0618 * @see java.lang.SecurityException
0619 * @see java.lang.SecurityManager#checkPropertiesAccess()
0620 * @see java.util.Properties
0621 */
0622 public static Properties getProperties() {
0623 SecurityManager sm = getSecurityManager();
0624 if (sm != null) {
0625 sm.checkPropertiesAccess();
0626 }
0627
0628 return props;
0629 }
0630
0631 /**
0632 * Sets the system properties to the <code>Properties</code>
0633 * argument.
0634 * <p>
0635 * First, if there is a security manager, its
0636 * <code>checkPropertiesAccess</code> method is called with no
0637 * arguments. This may result in a security exception.
0638 * <p>
0639 * The argument becomes the current set of system properties for use
0640 * by the {@link #getProperty(String)} method. If the argument is
0641 * <code>null</code>, then the current set of system properties is
0642 * forgotten.
0643 *
0644 * @param props the new system properties.
0645 * @exception SecurityException if a security manager exists and its
0646 * <code>checkPropertiesAccess</code> method doesn't allow access
0647 * to the system properties.
0648 * @see #getProperties
0649 * @see java.util.Properties
0650 * @see java.lang.SecurityException
0651 * @see java.lang.SecurityManager#checkPropertiesAccess()
0652 */
0653 public static void setProperties(Properties props) {
0654 SecurityManager sm = getSecurityManager();
0655 if (sm != null) {
0656 sm.checkPropertiesAccess();
0657 }
0658 if (props == null) {
0659 props = new Properties();
0660 initProperties(props);
0661 }
0662 System.props = props;
0663 }
0664
0665 /**
0666 * Gets the system property indicated by the specified key.
0667 * <p>
0668 * First, if there is a security manager, its
0669 * <code>checkPropertyAccess</code> method is called with the key as
0670 * its argument. This may result in a SecurityException.
0671 * <p>
0672 * If there is no current set of system properties, a set of system
0673 * properties is first created and initialized in the same manner as
0674 * for the <code>getProperties</code> method.
0675 *
0676 * @param key the name of the system property.
0677 * @return the string value of the system property,
0678 * or <code>null</code> if there is no property with that key.
0679 *
0680 * @exception SecurityException if a security manager exists and its
0681 * <code>checkPropertyAccess</code> method doesn't allow
0682 * access to the specified system property.
0683 * @exception NullPointerException if <code>key</code> is
0684 * <code>null</code>.
0685 * @exception IllegalArgumentException if <code>key</code> is empty.
0686 * @see #setProperty
0687 * @see java.lang.SecurityException
0688 * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
0689 * @see java.lang.System#getProperties()
0690 */
0691 public static String getProperty(String key) {
0692 checkKey(key);
0693 SecurityManager sm = getSecurityManager();
0694 if (sm != null) {
0695 sm.checkPropertyAccess(key);
0696 }
0697
0698 return props.getProperty(key);
0699 }
0700
0701 /**
0702 * Gets the system property indicated by the specified key.
0703 * <p>
0704 * First, if there is a security manager, its
0705 * <code>checkPropertyAccess</code> method is called with the
0706 * <code>key</code> as its argument.
0707 * <p>
0708 * If there is no current set of system properties, a set of system
0709 * properties is first created and initialized in the same manner as
0710 * for the <code>getProperties</code> method.
0711 *
0712 * @param key the name of the system property.
0713 * @param def a default value.
0714 * @return the string value of the system property,
0715 * or the default value if there is no property with that key.
0716 *
0717 * @exception SecurityException if a security manager exists and its
0718 * <code>checkPropertyAccess</code> method doesn't allow
0719 * access to the specified system property.
0720 * @exception NullPointerException if <code>key</code> is
0721 * <code>null</code>.
0722 * @exception IllegalArgumentException if <code>key</code> is empty.
0723 * @see #setProperty
0724 * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
0725 * @see java.lang.System#getProperties()
0726 */
0727 public static String getProperty(String key, String def) {
0728 checkKey(key);
0729 SecurityManager sm = getSecurityManager();
0730 if (sm != null) {
0731 sm.checkPropertyAccess(key);
0732 }
0733
0734 return props.getProperty(key, def);
0735 }
0736
0737 /**
0738 * Sets the system property indicated by the specified key.
0739 * <p>
0740 * First, if a security manager exists, its
0741 * <code>SecurityManager.checkPermission</code> method
0742 * is called with a <code>PropertyPermission(key, "write")</code>
0743 * permission. This may result in a SecurityException being thrown.
0744 * If no exception is thrown, the specified property is set to the given
0745 * value.
0746 * <p>
0747 *
0748 * @param key the name of the system property.
0749 * @param value the value of the system property.
0750 * @return the previous value of the system property,
0751 * or <code>null</code> if it did not have one.
0752 *
0753 * @exception SecurityException if a security manager exists and its
0754 * <code>checkPermission</code> method doesn't allow
0755 * setting of the specified property.
0756 * @exception NullPointerException if <code>key</code> or
0757 * <code>value</code> is <code>null</code>.
0758 * @exception IllegalArgumentException if <code>key</code> is empty.
0759 * @see #getProperty
0760 * @see java.lang.System#getProperty(java.lang.String)
0761 * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
0762 * @see java.util.PropertyPermission
0763 * @see SecurityManager#checkPermission
0764 * @since 1.2
0765 */
0766 public static String setProperty(String key, String value) {
0767 checkKey(key);
0768 SecurityManager sm = getSecurityManager();
0769 if (sm != null) {
0770 sm.checkPermission(new PropertyPermission(key,
0771 SecurityConstants.PROPERTY_WRITE_ACTION));
0772 }
0773
0774 return (String) props.setProperty(key, value);
0775 }
0776
0777 /**
0778 * Removes the system property indicated by the specified key.
0779 * <p>
0780 * First, if a security manager exists, its
0781 * <code>SecurityManager.checkPermission</code> method
0782 * is called with a <code>PropertyPermission(key, "write")</code>
0783 * permission. This may result in a SecurityException being thrown.
0784 * If no exception is thrown, the specified property is removed.
0785 * <p>
0786 *
0787 * @param key the name of the system property to be removed.
0788 * @return the previous string value of the system property,
0789 * or <code>null</code> if there was no property with that key.
0790 *
0791 * @exception SecurityException if a security manager exists and its
0792 * <code>checkPropertyAccess</code> method doesn't allow
0793 * access to the specified system property.
0794 * @exception NullPointerException if <code>key</code> is
0795 * <code>null</code>.
0796 * @exception IllegalArgumentException if <code>key</code> is empty.
0797 * @see #getProperty
0798 * @see #setProperty
0799 * @see java.util.Properties
0800 * @see java.lang.SecurityException
0801 * @see java.lang.SecurityManager#checkPropertiesAccess()
0802 * @since 1.5
0803 */
0804 public static String clearProperty(String key) {
0805 checkKey(key);
0806 SecurityManager sm = getSecurityManager();
0807 if (sm != null) {
0808 sm.checkPermission(new PropertyPermission(key, "write"));
0809 }
0810
0811 return (String) props.remove(key);
0812 }
0813
0814 private static void checkKey(String key) {
0815 if (key == null) {
0816 throw new NullPointerException("key can't be null");
0817 }
0818 if (key.equals("")) {
0819 throw new IllegalArgumentException("key can't be empty");
0820 }
0821 }
0822
0823 /**
0824 * Gets the value of the specified environment variable. An
0825 * environment variable is a system-dependent external named
0826 * value.
0827 *
0828 * <p>If a security manager exists, its
0829 * {@link SecurityManager#checkPermission checkPermission}
0830 * method is called with a
0831 * <code>{@link RuntimePermission}("getenv."+name)</code>
0832 * permission. This may result in a {@link SecurityException}
0833 * being thrown. If no exception is thrown the value of the
0834 * variable <code>name</code> is returned.
0835 *
0836 * <p><a name="EnvironmentVSSystemProperties"><i>System
0837 * properties</i> and <i>environment variables</i></a> are both
0838 * conceptually mappings between names and values. Both
0839 * mechanisms can be used to pass user-defined information to a
0840 * Java process. Environment variables have a more global effect,
0841 * because they are visible to all descendants of the process
0842 * which defines them, not just the immediate Java subprocess.
0843 * They can have subtly different semantics, such as case
0844 * insensitivity, on different operating systems. For these
0845 * reasons, environment variables are more likely to have
0846 * unintended side effects. It is best to use system properties
0847 * where possible. Environment variables should be used when a
0848 * global effect is desired, or when an external system interface
0849 * requires an environment variable (such as <code>PATH</code>).
0850 *
0851 * <p>On UNIX systems the alphabetic case of <code>name</code> is
0852 * typically significant, while on Microsoft Windows systems it is
0853 * typically not. For example, the expression
0854 * <code>System.getenv("FOO").equals(System.getenv("foo"))</code>
0855 * is likely to be true on Microsoft Windows.
0856 *
0857 * @param name the name of the environment variable
0858 * @return the string value of the variable, or <code>null</code>
0859 * if the variable is not defined in the system environment
0860 * @throws NullPointerException if <code>name</code> is <code>null</code>
0861 * @throws SecurityException
0862 * if a security manager exists and its
0863 * {@link SecurityManager#checkPermission checkPermission}
0864 * method doesn't allow access to the environment variable
0865 * <code>name</code>
0866 * @see #getenv()
0867 * @see ProcessBuilder#environment()
0868 */
0869 public static String getenv(String name) {
0870 SecurityManager sm = getSecurityManager();
0871 if (sm != null) {
0872 sm.checkPermission(new RuntimePermission("getenv." + name));
0873 }
0874
0875 return ProcessEnvironment.getenv(name);
0876 }
0877
0878 /**
0879 * Returns an unmodifiable string map view of the current system environment.
0880 * The environment is a system-dependent mapping from names to
0881 * values which is passed from parent to child processes.
0882 *
0883 * <p>If the system does not support environment variables, an
0884 * empty map is returned.
0885 *
0886 * <p>The returned map will never contain null keys or values.
0887 * Attempting to query the presence of a null key or value will
0888 * throw a {@link NullPointerException}. Attempting to query
0889 * the presence of a key or value which is not of type
0890 * {@link String} will throw a {@link ClassCastException}.
0891 *
0892 * <p>The returned map and its collection views may not obey the
0893 * general contract of the {@link Object#equals} and
0894 * {@link Object#hashCode} methods.
0895 *
0896 * <p>The returned map is typically case-sensitive on all platforms.
0897 *
0898 * <p>If a security manager exists, its
0899 * {@link SecurityManager#checkPermission checkPermission}
0900 * method is called with a
0901 * <code>{@link RuntimePermission}("getenv.*")</code>
0902 * permission. This may result in a {@link SecurityException} being
0903 * thrown.
0904 *
0905 * <p>When passing information to a Java subprocess,
0906 * <a href=#EnvironmentVSSystemProperties>system properties</a>
0907 * are generally preferred over environment variables.
0908 *
0909 * @return the environment as a map of variable names to values
0910 * @throws SecurityException
0911 * if a security manager exists and its
0912 * {@link SecurityManager#checkPermission checkPermission}
0913 * method doesn't allow access to the process environment
0914 * @see #getenv(String)
0915 * @see ProcessBuilder#environment()
0916 * @since 1.5
0917 */
0918 public static java.util.Map<String, String> getenv() {
0919 SecurityManager sm = getSecurityManager();
0920 if (sm != null) {
0921 sm.checkPermission(new RuntimePermission("getenv.*"));
0922 }
0923
0924 return ProcessEnvironment.getenv();
0925 }
0926
0927 /**
0928 * Terminates the currently running Java Virtual Machine. The
0929 * argument serves as a status code; by convention, a nonzero status
0930 * code indicates abnormal termination.
0931 * <p>
0932 * This method calls the <code>exit</code> method in class
0933 * <code>Runtime</code>. This method never returns normally.
0934 * <p>
0935 * The call <code>System.exit(n)</code> is effectively equivalent to
0936 * the call:
0937 * <blockquote><pre>
0938 * Runtime.getRuntime().exit(n)
0939 * </pre></blockquote>
0940 *
0941 * @param status exit status.
0942 * @throws SecurityException
0943 * if a security manager exists and its <code>checkExit</code>
0944 * method doesn't allow exit with the specified status.
0945 * @see java.lang.Runtime#exit(int)
0946 */
0947 public static void exit(int status) {
0948 Runtime.getRuntime().exit(status);
0949 }
0950
0951 /**
0952 * Runs the garbage collector.
0953 * <p>
0954 * Calling the <code>gc</code> method suggests that the Java Virtual
0955 * Machine expend effort toward recycling unused objects in order to
0956 * make the memory they currently occupy available for quick reuse.
0957 * When control returns from the method call, the Java Virtual
0958 * Machine has made a best effort to reclaim space from all discarded
0959 * objects.
0960 * <p>
0961 * The call <code>System.gc()</code> is effectively equivalent to the
0962 * call:
0963 * <blockquote><pre>
0964 * Runtime.getRuntime().gc()
0965 * </pre></blockquote>
0966 *
0967 * @see java.lang.Runtime#gc()
0968 */
0969 public static void gc() {
0970 Runtime.getRuntime().gc();
0971 }
0972
0973 /**
0974 * Runs the finalization methods of any objects pending finalization.
0975 * <p>
0976 * Calling this method suggests that the Java Virtual Machine expend
0977 * effort toward running the <code>finalize</code> methods of objects
0978 * that have been found to be discarded but whose <code>finalize</code>
0979 * methods have not yet been run. When control returns from the
0980 * method call, the Java Virtual Machine has made a best effort to
0981 * complete all outstanding finalizations.
0982 * <p>
0983 * The call <code>System.runFinalization()</code> is effectively
0984 * equivalent to the call:
0985 * <blockquote><pre>
0986 * Runtime.getRuntime().runFinalization()
0987 * </pre></blockquote>
0988 *
0989 * @see java.lang.Runtime#runFinalization()
0990 */
0991 public static void runFinalization() {
0992 Runtime.getRuntime().runFinalization();
0993 }
0994
0995 /**
0996 * Enable or disable finalization on exit; doing so specifies that the
0997 * finalizers of all objects that have finalizers that have not yet been
0998 * automatically invoked are to be run before the Java runtime exits.
0999 * By default, finalization on exit is disabled.
1000 *
1001 * <p>If there is a security manager,
1002 * its <code>checkExit</code> method is first called
1003 * with 0 as its argument to ensure the exit is allowed.
1004 * This could result in a SecurityException.
1005 *
1006 * @deprecated This method is inherently unsafe. It may result in
1007 * finalizers being called on live objects while other threads are
1008 * concurrently manipulating those objects, resulting in erratic
1009 * behavior or deadlock.
1010 * @param value indicating enabling or disabling of finalization
1011 * @throws SecurityException
1012 * if a security manager exists and its <code>checkExit</code>
1013 * method doesn't allow the exit.
1014 *
1015 * @see java.lang.Runtime#exit(int)
1016 * @see java.lang.Runtime#gc()
1017 * @see java.lang.SecurityManager#checkExit(int)
1018 * @since JDK1.1
1019 */
1020 @Deprecated
1021 public static void runFinalizersOnExit(boolean value) {
1022 Runtime.getRuntime().runFinalizersOnExit(value);
1023 }
1024
1025 /**
1026 * Loads a code file with the specified filename from the local file
1027 * system as a dynamic library. The filename
1028 * argument must be a complete path name.
1029 * <p>
1030 * The call <code>System.load(name)</code> is effectively equivalent
1031 * to the call:
1032 * <blockquote><pre>
1033 * Runtime.getRuntime().load(name)
1034 * </pre></blockquote>
1035 *
1036 * @param filename the file to load.
1037 * @exception SecurityException if a security manager exists and its
1038 * <code>checkLink</code> method doesn't allow
1039 * loading of the specified dynamic library
1040 * @exception UnsatisfiedLinkError if the file does not exist.
1041 * @exception NullPointerException if <code>filename</code> is
1042 * <code>null</code>
1043 * @see java.lang.Runtime#load(java.lang.String)
1044 * @see java.lang.SecurityManager#checkLink(java.lang.String)
1045 */
1046 public static void load(String filename) {
1047 Runtime.getRuntime().load0(getCallerClass(), filename);
1048 }
1049
1050 /**
1051 * Loads the system library specified by the <code>libname</code>
1052 * argument. The manner in which a library name is mapped to the
1053 * actual system library is system dependent.
1054 * <p>
1055 * The call <code>System.loadLibrary(name)</code> is effectively
1056 * equivalent to the call
1057 * <blockquote><pre>
1058 * Runtime.getRuntime().loadLibrary(name)
1059 * </pre></blockquote>
1060 *
1061 * @param libname the name of the library.
1062 * @exception SecurityException if a security manager exists and its
1063 * <code>checkLink</code> method doesn't allow
1064 * loading of the specified dynamic library
1065 * @exception UnsatisfiedLinkError if the library does not exist.
1066 * @exception NullPointerException if <code>libname</code> is
1067 * <code>null</code>
1068 * @see java.lang.Runtime#loadLibrary(java.lang.String)
1069 * @see java.lang.SecurityManager#checkLink(java.lang.String)
1070 */
1071 public static void loadLibrary(String libname) {
1072 Runtime.getRuntime().loadLibrary0(getCallerClass(), libname);
1073 }
1074
1075 /**
1076 * Maps a library name into a platform-specific string representing
1077 * a native library.
1078 *
1079 * @param libname the name of the library.
1080 * @return a platform-dependent native library name.
1081 * @exception NullPointerException if <code>libname</code> is
1082 * <code>null</code>
1083 * @see java.lang.System#loadLibrary(java.lang.String)
1084 * @see java.lang.ClassLoader#findLibrary(java.lang.String)
1085 * @since 1.2
1086 */
1087 public static native String mapLibraryName(String libname);
1088
1089 /**
1090 * The following two methods exist because in, out, and err must be
1091 * initialized to null. The compiler, however, cannot be permitted to
1092 * inline access to them, since they are later set to more sensible values
1093 * by initializeSystemClass().
1094 */
1095 private static InputStream nullInputStream()
1096 throws NullPointerException {
1097 if (currentTimeMillis() > 0) {
1098 return null;
1099 }
1100 throw new NullPointerException();
1101 }
1102
1103 private static PrintStream nullPrintStream()
1104 throws NullPointerException {
1105 if (currentTimeMillis() > 0) {
1106 return null;
1107 }
1108 throw new NullPointerException();
1109 }
1110
1111 /**
1112 * Initialize the system class. Called after thread initialization.
1113 */
1114 private static void initializeSystemClass() {
1115 props = new Properties();
1116 initProperties(props);
1117 sun.misc.Version.init();
1118 FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
1119 FileOutputStream fdOut = new FileOutputStream(
1120 FileDescriptor.out);
1121 FileOutputStream fdErr = new FileOutputStream(
1122 FileDescriptor.err);
1123 setIn0(new BufferedInputStream(fdIn));
1124 setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128),
1125 true));
1126 setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128),
1127 true));
1128
1129 // Load the zip library now in order to keep java.util.zip.ZipFile
1130 // from trying to use itself to load this library later.
1131 loadLibrary("zip");
1132
1133 // Setup Java signal handlers for HUP, TERM, and INT (where available).
1134 Terminator.setup();
1135
1136 // The order in with the hooks are added here is important as it
1137 // determines the order in which they are run.
1138 // (1)Console restore hook needs to be called first.
1139 // (2)Application hooks must be run before calling deleteOnExitHook.
1140 Shutdown.add(sun.misc.SharedSecrets.getJavaIOAccess()
1141 .consoleRestoreHook());
1142 Shutdown.add(ApplicationShutdownHooks.hook());
1143 Shutdown.add(sun.misc.SharedSecrets
1144 .getJavaIODeleteOnExitAccess());
1145
1146 // Initialize any miscellenous operating system settings that need to be
1147 // set for the class libraries. Currently this is no-op everywhere except
1148 // for Windows where the process-wide error mode is set before the java.io
1149 // classes are used.
1150 sun.misc.VM.initializeOSEnvironment();
1151
1152 // Set the maximum amount of direct memory. This value is controlled
1153 // by the vm option -XX:MaxDirectMemorySize=<size>. This method acts
1154 // as an initializer only if it is called before sun.misc.VM.booted().
1155 sun.misc.VM.maxDirectMemory();
1156
1157 // Set a boolean to determine whether ClassLoader.loadClass accepts
1158 // array syntax. This value is controlled by the system property
1159 // "sun.lang.ClassLoader.allowArraySyntax". This method acts as
1160 // an initializer only if it is called before sun.misc.VM.booted().
1161 sun.misc.VM.allowArraySyntax();
1162
1163 // Subsystems that are invoked during initialization can invoke
1164 // sun.misc.VM.isBooted() in order to avoid doing things that should
1165 // wait until the application class loader has been set up.
1166 sun.misc.VM.booted();
1167
1168 // The main thread is not added to its thread group in the same
1169 // way as other threads; we must do it ourselves here.
1170 Thread current = Thread.currentThread();
1171 current.getThreadGroup().add(current);
1172
1173 // Allow privileged classes outside of java.lang
1174 sun.misc.SharedSecrets
1175 .setJavaLangAccess(new sun.misc.JavaLangAccess() {
1176 public sun.reflect.ConstantPool getConstantPool(
1177 Class klass) {
1178 return klass.getConstantPool();
1179 }
1180
1181 public void setAnnotationType(Class klass,
1182 AnnotationType type) {
1183 klass.setAnnotationType(type);
1184 }
1185
1186 public AnnotationType getAnnotationType(Class klass) {
1187 return klass.getAnnotationType();
1188 }
1189
1190 public <E extends Enum<E>> E[] getEnumConstantsShared(
1191 Class<E> klass) {
1192 return klass.getEnumConstantsShared();
1193 }
1194
1195 public void blockedOn(Thread t, Interruptible b) {
1196 t.blockedOn(b);
1197 }
1198 });
1199 }
1200
1201 /* returns the class of the caller. */
1202 static Class getCallerClass() {
1203 // NOTE use of more generic Reflection.getCallerClass()
1204 return Reflection.getCallerClass(3);
1205 }
1206 }
|