1 |
/* |
2 |
* @(#)System.java 1.125 01/12/03 |
3 |
* |
4 |
* Copyright 2002 Sun Microsystems, Inc. All rights reserved. |
5 |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
*/ |
7 |
|
8 |
package java.lang; |
9 |
|
10 |
import java.io.*; |
11 |
import java.util.Properties; |
12 |
import java.util.PropertyPermission; |
13 |
import java.util.StringTokenizer; |
14 |
import java.security.AccessController; |
15 |
import java.security.PrivilegedAction; |
16 |
import java.security.AllPermission; |
17 |
import sun.net.InetAddressCachePolicy; |
18 |
import sun.reflect.Reflection; |
19 |
|
20 |
/** |
21 |
* <b>JSR166: Add <tt>currentTimeNanos</tt><br> |
22 |
* The <code>System</code> class contains several useful class fields |
23 |
* and methods. It cannot be instantiated. |
24 |
* <p> |
25 |
* Among the facilities provided by the <code>System</code> class |
26 |
* are standard input, standard output, and error output streams; |
27 |
* access to externally defined "properties"; a means of |
28 |
* loading files and libraries; and a utility method for quickly |
29 |
* copying a portion of an array. |
30 |
* |
31 |
* @author Arthur van Hoff |
32 |
* @version 1.125, 12/03/01 |
33 |
* @since JDK1.0 |
34 |
*/ |
35 |
public final class System { |
36 |
|
37 |
/* First thing---register the natives */ |
38 |
private static native void registerNatives(); |
39 |
static { |
40 |
registerNatives(); |
41 |
} |
42 |
|
43 |
/** Don't let anyone instantiate this class */ |
44 |
private System() { |
45 |
} |
46 |
|
47 |
/** |
48 |
* The "standard" input stream. This stream is already |
49 |
* open and ready to supply input data. Typically this stream |
50 |
* corresponds to keyboard input or another input source specified by |
51 |
* the host environment or user. |
52 |
*/ |
53 |
public final static InputStream in = nullInputStream(); |
54 |
|
55 |
/** |
56 |
* The "standard" output stream. This stream is already |
57 |
* open and ready to accept output data. Typically this stream |
58 |
* corresponds to display output or another output destination |
59 |
* specified by the host environment or user. |
60 |
* <p> |
61 |
* For simple stand-alone Java applications, a typical way to write |
62 |
* a line of output data is: |
63 |
* <blockquote><pre> |
64 |
* System.out.println(data) |
65 |
* </pre></blockquote> |
66 |
* <p> |
67 |
* See the <code>println</code> methods in class <code>PrintStream</code>. |
68 |
* |
69 |
* @see java.io.PrintStream#println() |
70 |
* @see java.io.PrintStream#println(boolean) |
71 |
* @see java.io.PrintStream#println(char) |
72 |
* @see java.io.PrintStream#println(char[]) |
73 |
* @see java.io.PrintStream#println(double) |
74 |
* @see java.io.PrintStream#println(float) |
75 |
* @see java.io.PrintStream#println(int) |
76 |
* @see java.io.PrintStream#println(long) |
77 |
* @see java.io.PrintStream#println(java.lang.Object) |
78 |
* @see java.io.PrintStream#println(java.lang.String) |
79 |
*/ |
80 |
public final static PrintStream out = nullPrintStream(); |
81 |
|
82 |
/** |
83 |
* The "standard" error output stream. This stream is already |
84 |
* open and ready to accept output data. |
85 |
* <p> |
86 |
* Typically this stream corresponds to display output or another |
87 |
* output destination specified by the host environment or user. By |
88 |
* convention, this output stream is used to display error messages |
89 |
* or other information that should come to the immediate attention |
90 |
* of a user even if the principal output stream, the value of the |
91 |
* variable <code>out</code>, has been redirected to a file or other |
92 |
* destination that is typically not continuously monitored. |
93 |
*/ |
94 |
public final static PrintStream err = nullPrintStream(); |
95 |
|
96 |
/* The security manager for the system. |
97 |
*/ |
98 |
private static SecurityManager security = null; |
99 |
|
100 |
/** |
101 |
* Reassigns the "standard" input stream. |
102 |
* |
103 |
* <p>First, if there is a security manager, its <code>checkPermission</code> |
104 |
* method is called with a <code>RuntimePermission("setIO")</code> permission |
105 |
* to see if it's ok to reassign the "standard" input stream. |
106 |
* <p> |
107 |
* |
108 |
* @param in the new standard input stream. |
109 |
* |
110 |
* @throws SecurityException |
111 |
* if a security manager exists and its |
112 |
* <code>checkPermission</code> method doesn't allow |
113 |
* reassigning of the standard input stream. |
114 |
* |
115 |
* @see SecurityManager#checkPermission |
116 |
* @see java.lang.RuntimePermission |
117 |
* |
118 |
* @since JDK1.1 |
119 |
*/ |
120 |
public static void setIn(InputStream in) { |
121 |
checkIO(); |
122 |
setIn0(in); |
123 |
} |
124 |
|
125 |
/** |
126 |
* Reassigns the "standard" output stream. |
127 |
* |
128 |
* <p>First, if there is a security manager, its <code>checkPermission</code> |
129 |
* method is called with a <code>RuntimePermission("setIO")</code> permission |
130 |
* to see if it's ok to reassign the "standard" output stream. |
131 |
* |
132 |
* @param out the new standard output stream |
133 |
* |
134 |
* @throws SecurityException |
135 |
* if a security manager exists and its |
136 |
* <code>checkPermission</code> method doesn't allow |
137 |
* reassigning of the standard output stream. |
138 |
* |
139 |
* @see SecurityManager#checkPermission |
140 |
* @see java.lang.RuntimePermission |
141 |
* |
142 |
* @since JDK1.1 |
143 |
*/ |
144 |
public static void setOut(PrintStream out) { |
145 |
checkIO(); |
146 |
setOut0(out); |
147 |
} |
148 |
|
149 |
/** |
150 |
* Reassigns the "standard" error output stream. |
151 |
* |
152 |
* <p>First, if there is a security manager, its <code>checkPermission</code> |
153 |
* method is called with a <code>RuntimePermission("setIO")</code> permission |
154 |
* to see if it's ok to reassign the "standard" error output stream. |
155 |
* |
156 |
* @param err the new standard error output stream. |
157 |
* |
158 |
* @throws SecurityException |
159 |
* if a security manager exists and its |
160 |
* <code>checkPermission</code> method doesn't allow |
161 |
* reassigning of the standard error output stream. |
162 |
* |
163 |
* @see SecurityManager#checkPermission |
164 |
* @see java.lang.RuntimePermission |
165 |
* |
166 |
* @since JDK1.1 |
167 |
*/ |
168 |
public static void setErr(PrintStream err) { |
169 |
checkIO(); |
170 |
setErr0(err); |
171 |
} |
172 |
|
173 |
private static void checkIO() { |
174 |
if (security != null) |
175 |
security.checkPermission(new RuntimePermission("setIO")); |
176 |
} |
177 |
|
178 |
private static native void setIn0(InputStream in); |
179 |
private static native void setOut0(PrintStream out); |
180 |
private static native void setErr0(PrintStream err); |
181 |
|
182 |
/** |
183 |
* Sets the System security. |
184 |
* |
185 |
* <p> If there is a security manager already installed, this method first |
186 |
* calls the security manager's <code>checkPermission</code> method |
187 |
* with a <code>RuntimePermission("setSecurityManager")</code> |
188 |
* permission to ensure it's ok to replace the existing |
189 |
* security manager. |
190 |
* This may result in throwing a <code>SecurityException</code>. |
191 |
* |
192 |
* <p> Otherwise, the argument is established as the current |
193 |
* security manager. If the argument is <code>null</code> and no |
194 |
* security manager has been established, then no action is taken and |
195 |
* the method simply returns. |
196 |
* |
197 |
* @param s the security manager. |
198 |
* @exception SecurityException if the security manager has already |
199 |
* been set and its <code>checkPermission</code> method |
200 |
* doesn't allow it to be replaced. |
201 |
* @see #getSecurityManager |
202 |
* @see SecurityManager#checkPermission |
203 |
* @see java.lang.RuntimePermission |
204 |
*/ |
205 |
public static |
206 |
void setSecurityManager(final SecurityManager s) { |
207 |
try { |
208 |
s.checkPackageAccess("java.lang"); |
209 |
} catch (Exception e) { |
210 |
// no-op |
211 |
} |
212 |
setSecurityManager0(s); |
213 |
} |
214 |
|
215 |
private static synchronized |
216 |
void setSecurityManager0(final SecurityManager s) { |
217 |
if (security != null) { |
218 |
// ask the currently installed security manager if we |
219 |
// can replace it. |
220 |
security.checkPermission(new RuntimePermission |
221 |
("setSecurityManager")); |
222 |
} |
223 |
|
224 |
if ((s != null) && (s.getClass().getClassLoader() != null)) { |
225 |
// New security manager class is not on bootstrap classpath. |
226 |
// Cause policy to get initialized before we install the new |
227 |
// security manager, in order to prevent infinite loops when |
228 |
// trying to initialize the policy (which usually involves |
229 |
// accessing some security and/or system properties, which in turn |
230 |
// calls the installed security manager's checkPermission method |
231 |
// which will loop infinitely if there is a non-system class |
232 |
// (in this case: the new security manager class) on the stack). |
233 |
AccessController.doPrivileged(new PrivilegedAction() { |
234 |
public Object run() { |
235 |
s.getClass().getProtectionDomain().implies |
236 |
(new AllPermission()); |
237 |
return null; |
238 |
} |
239 |
}); |
240 |
} |
241 |
|
242 |
security = s; |
243 |
InetAddressCachePolicy.setIfNotSet(InetAddressCachePolicy.FOREVER); |
244 |
} |
245 |
|
246 |
/** |
247 |
* Gets the system security interface. |
248 |
* |
249 |
* @return if a security manager has already been established for the |
250 |
* current application, then that security manager is returned; |
251 |
* otherwise, <code>null</code> is returned. |
252 |
* @see #setSecurityManager |
253 |
*/ |
254 |
public static SecurityManager getSecurityManager() { |
255 |
return security; |
256 |
} |
257 |
|
258 |
/** |
259 |
* Returns the current time in milliseconds. Note that |
260 |
* while the unit of time of the return value is a millisecond, |
261 |
* the granularity of the value depends on the underlying |
262 |
* operating system and may be larger. For example, many |
263 |
* operating systems measure time in units of tens of |
264 |
* milliseconds. |
265 |
* |
266 |
* <p> See the description of the class <code>Date</code> for |
267 |
* a discussion of slight discrepancies that may arise between |
268 |
* "computer time" and coordinated universal time (UTC). |
269 |
* |
270 |
* @return the difference, measured in milliseconds, between |
271 |
* the current time and midnight, January 1, 1970 UTC. |
272 |
* @see java.util.Date |
273 |
*/ |
274 |
public static native long currentTimeMillis(); |
275 |
|
276 |
/** |
277 |
* Returns the number of nanoseconds between the current time and |
278 |
* some arbitrary point of time (possibly in the future, so values |
279 |
* may be negative). Note that while the unit of time of the |
280 |
* return value is a nanosecond, the granularity of the value |
281 |
* depends on the underlying operating system and may be larger. |
282 |
* The time values returned by this method may be completely |
283 |
* uncoordinated with those from <code>currentTimeMillis</code> or |
284 |
* those used by class <code>Date</code>. |
285 |
* |
286 |
* @return the difference, measured in nanoseconds, between |
287 |
* the current time and an arbitrary time. |
288 |
*/ |
289 |
|
290 |
public static native long currentTimeNanos(); |
291 |
|
292 |
/** |
293 |
* Copies an array from the specified source array, beginning at the |
294 |
* specified position, to the specified position of the destination array. |
295 |
* A subsequence of array components are copied from the source |
296 |
* array referenced by <code>src</code> to the destination array |
297 |
* referenced by <code>dest</code>. The number of components copied is |
298 |
* equal to the <code>length</code> argument. The components at |
299 |
* positions <code>srcPos</code> through |
300 |
* <code>srcPos+length-1</code> in the source array are copied into |
301 |
* positions <code>destPos</code> through |
302 |
* <code>destPos+length-1</code>, respectively, of the destination |
303 |
* array. |
304 |
* <p> |
305 |
* If the <code>src</code> and <code>dest</code> arguments refer to the |
306 |
* same array object, then the copying is performed as if the |
307 |
* components at positions <code>srcPos</code> through |
308 |
* <code>srcPos+length-1</code> were first copied to a temporary |
309 |
* array with <code>length</code> components and then the contents of |
310 |
* the temporary array were copied into positions |
311 |
* <code>destPos</code> through <code>destPos+length-1</code> of the |
312 |
* destination array. |
313 |
* <p> |
314 |
* If <code>dest</code> is <code>null</code>, then a |
315 |
* <code>NullPointerException</code> is thrown. |
316 |
* <p> |
317 |
* If <code>src</code> is <code>null</code>, then a |
318 |
* <code>NullPointerException</code> is thrown and the destination |
319 |
* array is not modified. |
320 |
* <p> |
321 |
* Otherwise, if any of the following is true, an |
322 |
* <code>ArrayStoreException</code> is thrown and the destination is |
323 |
* not modified: |
324 |
* <ul> |
325 |
* <li>The <code>src</code> argument refers to an object that is not an |
326 |
* array. |
327 |
* <li>The <code>dest</code> argument refers to an object that is not an |
328 |
* array. |
329 |
* <li>The <code>src</code> argument and <code>dest</code> argument refer |
330 |
* to arrays whose component types are different primitive types. |
331 |
* <li>The <code>src</code> argument refers to an array with a primitive |
332 |
* component type and the <code>dest</code> argument refers to an array |
333 |
* with a reference component type. |
334 |
* <li>The <code>src</code> argument refers to an array with a reference |
335 |
* component type and the <code>dest</code> argument refers to an array |
336 |
* with a primitive component type. |
337 |
* </ul> |
338 |
* <p> |
339 |
* Otherwise, if any of the following is true, an |
340 |
* <code>IndexOutOfBoundsException</code> is |
341 |
* thrown and the destination is not modified: |
342 |
* <ul> |
343 |
* <li>The <code>srcPos</code> argument is negative. |
344 |
* <li>The <code>destPos</code> argument is negative. |
345 |
* <li>The <code>length</code> argument is negative. |
346 |
* <li><code>srcPos+length</code> is greater than |
347 |
* <code>src.length</code>, the length of the source array. |
348 |
* <li><code>destPos+length</code> is greater than |
349 |
* <code>dest.length</code>, the length of the destination array. |
350 |
* </ul> |
351 |
* <p> |
352 |
* Otherwise, if any actual component of the source array from |
353 |
* position <code>srcPos</code> through |
354 |
* <code>srcPos+length-1</code> cannot be converted to the component |
355 |
* type of the destination array by assignment conversion, an |
356 |
* <code>ArrayStoreException</code> is thrown. In this case, let |
357 |
* <b><i>k</i></b> be the smallest nonnegative integer less than |
358 |
* length such that <code>src[srcPos+</code><i>k</i><code>]</code> |
359 |
* cannot be converted to the component type of the destination |
360 |
* array; when the exception is thrown, source array components from |
361 |
* positions <code>srcPos</code> through |
362 |
* <code>srcPos+</code><i>k</i><code>-1</code> |
363 |
* will already have been copied to destination array positions |
364 |
* <code>destPos</code> through |
365 |
* <code>destPos+</code><i>k</I><code>-1</code> and no other |
366 |
* positions of the destination array will have been modified. |
367 |
* (Because of the restrictions already itemized, this |
368 |
* paragraph effectively applies only to the situation where both |
369 |
* arrays have component types that are reference types.) |
370 |
* |
371 |
* @param src the source array. |
372 |
* @param srcPos starting position in the source array. |
373 |
* @param dest the destination array. |
374 |
* @param destPos starting position in the destination data. |
375 |
* @param length the number of array elements to be copied. |
376 |
* @exception IndexOutOfBoundsException if copying would cause |
377 |
* access of data outside array bounds. |
378 |
* @exception ArrayStoreException if an element in the <code>src</code> |
379 |
* array could not be stored into the <code>dest</code> array |
380 |
* because of a type mismatch. |
381 |
* @exception NullPointerException if either <code>src</code> or |
382 |
* <code>dest</code> is <code>null</code>. |
383 |
*/ |
384 |
public static native void arraycopy(Object src, int srcPos, |
385 |
Object dest, int destPos, |
386 |
int length); |
387 |
|
388 |
/** |
389 |
* Returns the same hash code for the given object as |
390 |
* would be returned by the default method hashCode(), |
391 |
* whether or not the given object's class overrides |
392 |
* hashCode(). |
393 |
* The hash code for the null reference is zero. |
394 |
* |
395 |
* @param x object for which the hashCode is to be calculated |
396 |
* @return the hashCode |
397 |
* @since JDK1.1 |
398 |
*/ |
399 |
public static native int identityHashCode(Object x); |
400 |
|
401 |
/** |
402 |
* System properties. The following properties are guaranteed to be defined: |
403 |
* <dl> |
404 |
* <dt>java.version <dd>Java version number |
405 |
* <dt>java.vendor <dd>Java vendor specific string |
406 |
* <dt>java.vendor.url <dd>Java vendor URL |
407 |
* <dt>java.home <dd>Java installation directory |
408 |
* <dt>java.class.version <dd>Java class version number |
409 |
* <dt>java.class.path <dd>Java classpath |
410 |
* <dt>os.name <dd>Operating System Name |
411 |
* <dt>os.arch <dd>Operating System Architecture |
412 |
* <dt>os.version <dd>Operating System Version |
413 |
* <dt>file.separator <dd>File separator ("/" on Unix) |
414 |
* <dt>path.separator <dd>Path separator (":" on Unix) |
415 |
* <dt>line.separator <dd>Line separator ("\n" on Unix) |
416 |
* <dt>user.name <dd>User account name |
417 |
* <dt>user.home <dd>User home directory |
418 |
* <dt>user.dir <dd>User's current working directory |
419 |
* </dl> |
420 |
*/ |
421 |
|
422 |
private static Properties props; |
423 |
private static native Properties initProperties(Properties props); |
424 |
|
425 |
/** |
426 |
* Determines the current system properties. |
427 |
* <p> |
428 |
* First, if there is a security manager, its |
429 |
* <code>checkPropertiesAccess</code> method is called with no |
430 |
* arguments. This may result in a security exception. |
431 |
* <p> |
432 |
* The current set of system properties for use by the |
433 |
* {@link #getProperty(String)} method is returned as a |
434 |
* <code>Properties</code> object. If there is no current set of |
435 |
* system properties, a set of system properties is first created and |
436 |
* initialized. This set of system properties always includes values |
437 |
* for the following keys: |
438 |
* <table> |
439 |
* <tr><th>Key</th> |
440 |
* <th>Description of Associated Value</th></tr> |
441 |
* <tr><td><code>java.version</code></td> |
442 |
* <td>Java Runtime Environment version</td></tr> |
443 |
* <tr><td><code>java.vendor</code></td> |
444 |
* <td>Java Runtime Environment vendor</td></tr |
445 |
* <tr><td><code>java.vendor.url</code></td> |
446 |
* <td>Java vendor URL</td></tr> |
447 |
* <tr><td><code>java.home</code></td> |
448 |
* <td>Java installation directory</td></tr> |
449 |
* <tr><td><code>java.vm.specification.version</code></td> |
450 |
* <td>Java Virtual Machine specification version</td></tr> |
451 |
* <tr><td><code>java.vm.specification.vendor</code></td> |
452 |
* <td>Java Virtual Machine specification vendor</td></tr> |
453 |
* <tr><td><code>java.vm.specification.name</code></td> |
454 |
* <td>Java Virtual Machine specification name</td></tr> |
455 |
* <tr><td><code>java.vm.version</code></td> |
456 |
* <td>Java Virtual Machine implementation version</td></tr> |
457 |
* <tr><td><code>java.vm.vendor</code></td> |
458 |
* <td>Java Virtual Machine implementation vendor</td></tr> |
459 |
* <tr><td><code>java.vm.name</code></td> |
460 |
* <td>Java Virtual Machine implementation name</td></tr> |
461 |
* <tr><td><code>java.specification.version</code></td> |
462 |
* <td>Java Runtime Environment specification version</td></tr> |
463 |
* <tr><td><code>java.specification.vendor</code></td> |
464 |
* <td>Java Runtime Environment specification vendor</td></tr> |
465 |
* <tr><td><code>java.specification.name</code></td> |
466 |
* <td>Java Runtime Environment specification name</td></tr> |
467 |
* <tr><td><code>java.class.version</code></td> |
468 |
* <td>Java class format version number</td></tr> |
469 |
* <tr><td><code>java.class.path</code></td> |
470 |
* <td>Java class path</td></tr> |
471 |
* <tr><td><code>java.library.path</code></td> |
472 |
* <td>List of paths to search when loading libraries</td></tr> |
473 |
* <tr><td><code>java.io.tmpdir</code></td> |
474 |
* <td>Default temp file path</td></tr> |
475 |
* <tr><td><code>java.compiler</code></td> |
476 |
* <td>Name of JIT compiler to use</td></tr> |
477 |
* <tr><td><code>java.ext.dirs</code></td> |
478 |
* <td>Path of extension directory or directories</td></tr> |
479 |
* <tr><td><code>os.name</code></td> |
480 |
* <td>Operating system name</td></tr> |
481 |
* <tr><td><code>os.arch</code></td> |
482 |
* <td>Operating system architecture</td></tr> |
483 |
* <tr><td><code>os.version</code></td> |
484 |
* <td>Operating system version</td></tr> |
485 |
* <tr><td><code>file.separator</code></td> |
486 |
* <td>File separator ("/" on UNIX)</td></tr> |
487 |
* <tr><td><code>path.separator</code></td> |
488 |
* <td>Path separator (":" on UNIX)</td></tr> |
489 |
* <tr><td><code>line.separator</code></td> |
490 |
* <td>Line separator ("\n" on UNIX)</td></tr> |
491 |
* <tr><td><code>user.name</code></td> |
492 |
* <td>User's account name</td></tr> |
493 |
* <tr><td><code>user.home</code></td> |
494 |
* <td>User's home directory</td></tr> |
495 |
* <tr><td><code>user.dir</code></td> |
496 |
* <td>User's current working directory</td></tr> |
497 |
* </table> |
498 |
* <p> |
499 |
* Multiple paths in a system property value are separated by the path |
500 |
* separator character of the platform. |
501 |
* <p> |
502 |
* Note that even if the security manager does not permit the |
503 |
* <code>getProperties</code> operation, it may choose to permit the |
504 |
* {@link #getProperty(String)} operation. |
505 |
* |
506 |
* @return the system properties |
507 |
* @exception SecurityException if a security manager exists and its |
508 |
* <code>checkPropertiesAccess</code> method doesn't allow access |
509 |
* to the system properties. |
510 |
* @see #setProperties |
511 |
* @see java.lang.SecurityException |
512 |
* @see java.lang.SecurityManager#checkPropertiesAccess() |
513 |
* @see java.util.Properties |
514 |
*/ |
515 |
public static Properties getProperties() { |
516 |
if (security != null) { |
517 |
security.checkPropertiesAccess(); |
518 |
} |
519 |
return props; |
520 |
} |
521 |
|
522 |
/** |
523 |
* Sets the system properties to the <code>Properties</code> |
524 |
* argument. |
525 |
* <p> |
526 |
* First, if there is a security manager, its |
527 |
* <code>checkPropertiesAccess</code> method is called with no |
528 |
* arguments. This may result in a security exception. |
529 |
* <p> |
530 |
* The argument becomes the current set of system properties for use |
531 |
* by the {@link #getProperty(String)} method. If the argument is |
532 |
* <code>null</code>, then the current set of system properties is |
533 |
* forgotten. |
534 |
* |
535 |
* @param props the new system properties. |
536 |
* @exception SecurityException if a security manager exists and its |
537 |
* <code>checkPropertiesAccess</code> method doesn't allow access |
538 |
* to the system properties. |
539 |
* @see #getProperties |
540 |
* @see java.util.Properties |
541 |
* @see java.lang.SecurityException |
542 |
* @see java.lang.SecurityManager#checkPropertiesAccess() |
543 |
*/ |
544 |
public static void setProperties(Properties props) { |
545 |
if (security != null) { |
546 |
security.checkPropertiesAccess(); |
547 |
} |
548 |
if (props == null) { |
549 |
props = new Properties(); |
550 |
initProperties(props); |
551 |
} |
552 |
System.props = props; |
553 |
} |
554 |
|
555 |
/** |
556 |
* Gets the system property indicated by the specified key. |
557 |
* <p> |
558 |
* First, if there is a security manager, its |
559 |
* <code>checkPropertyAccess</code> method is called with the key as |
560 |
* its argument. This may result in a SecurityException. |
561 |
* <p> |
562 |
* If there is no current set of system properties, a set of system |
563 |
* properties is first created and initialized in the same manner as |
564 |
* for the <code>getProperties</code> method. |
565 |
* |
566 |
* @param key the name of the system property. |
567 |
* @return the string value of the system property, |
568 |
* or <code>null</code> if there is no property with that key. |
569 |
* |
570 |
* @exception SecurityException if a security manager exists and its |
571 |
* <code>checkPropertyAccess</code> method doesn't allow |
572 |
* access to the specified system property. |
573 |
* @exception NullPointerException if <code>key</code> is |
574 |
* <code>null</code>. |
575 |
* @exception IllegalArgumentException if <code>key</code> is empty. |
576 |
* @see #setProperty |
577 |
* @see java.lang.SecurityException |
578 |
* @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String) |
579 |
* @see java.lang.System#getProperties() |
580 |
*/ |
581 |
public static String getProperty(String key) { |
582 |
if (key == null) { |
583 |
throw new NullPointerException("key can't be null"); |
584 |
} |
585 |
if (key.equals("")) { |
586 |
throw new IllegalArgumentException("key can't be empty"); |
587 |
} |
588 |
if (security != null) { |
589 |
security.checkPropertyAccess(key); |
590 |
} |
591 |
return props.getProperty(key); |
592 |
} |
593 |
|
594 |
/** |
595 |
* Gets the system property indicated by the specified key. |
596 |
* <p> |
597 |
* First, if there is a security manager, its |
598 |
* <code>checkPropertyAccess</code> method is called with the |
599 |
* <code>key</code> as its argument. |
600 |
* <p> |
601 |
* If there is no current set of system properties, a set of system |
602 |
* properties is first created and initialized in the same manner as |
603 |
* for the <code>getProperties</code> method. |
604 |
* |
605 |
* @param key the name of the system property. |
606 |
* @param def a default value. |
607 |
* @return the string value of the system property, |
608 |
* or the default value if there is no property with that key. |
609 |
* |
610 |
* @exception SecurityException if a security manager exists and its |
611 |
* <code>checkPropertyAccess</code> method doesn't allow |
612 |
* access to the specified system property. |
613 |
* @exception NullPointerException if <code>key</code> is |
614 |
* <code>null</code>. |
615 |
* @exception IllegalArgumentException if <code>key</code> is empty. |
616 |
* @see #setProperty |
617 |
* @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String) |
618 |
* @see java.lang.System#getProperties() |
619 |
*/ |
620 |
public static String getProperty(String key, String def) { |
621 |
if (key == null) { |
622 |
throw new NullPointerException("key can't be null"); |
623 |
} |
624 |
if (key.equals("")) { |
625 |
throw new IllegalArgumentException("key can't be empty"); |
626 |
} |
627 |
if (security != null) { |
628 |
security.checkPropertyAccess(key); |
629 |
} |
630 |
return props.getProperty(key, def); |
631 |
} |
632 |
|
633 |
/** |
634 |
* Sets the system property indicated by the specified key. |
635 |
* <p> |
636 |
* First, if a security manager exists, its |
637 |
* <code>SecurityManager.checkPermission</code> method |
638 |
* is called with a <code>PropertyPermission(key, "write")</code> |
639 |
* permission. This may result in a SecurityException being thrown. |
640 |
* If no exception is thrown, the specified property is set to the given |
641 |
* value. |
642 |
* <p> |
643 |
* |
644 |
* @param key the name of the system property. |
645 |
* @param value the value of the system property. |
646 |
* @return the previous value of the system property, |
647 |
* or <code>null</code> if it did not have one. |
648 |
* |
649 |
* @exception SecurityException if a security manager exists and its |
650 |
* <code>checkPermission</code> method doesn't allow |
651 |
* setting of the specified property. |
652 |
* @exception NullPointerException if <code>key</code> is |
653 |
* <code>null</code>. |
654 |
* @exception IllegalArgumentException if <code>key</code> is empty. |
655 |
* @see #getProperty |
656 |
* @see java.lang.System#getProperty(java.lang.String) |
657 |
* @see java.lang.System#getProperty(java.lang.String, java.lang.String) |
658 |
* @see java.util.PropertyPermission |
659 |
* @see SecurityManager#checkPermission |
660 |
* @since 1.2 |
661 |
*/ |
662 |
public static String setProperty(String key, String value) { |
663 |
if (key == null) { |
664 |
throw new NullPointerException("key can't be null"); |
665 |
} |
666 |
if (key.equals("")) { |
667 |
throw new IllegalArgumentException("key can't be empty"); |
668 |
} |
669 |
if (security != null) |
670 |
security.checkPermission(new PropertyPermission(key, "write")); |
671 |
return (String) props.setProperty(key, value); |
672 |
} |
673 |
|
674 |
/** |
675 |
* Gets an environment variable. An environment variable is a |
676 |
* system-dependent external variable that has a string value. |
677 |
* |
678 |
* @deprecated The preferred way to extract system-dependent information |
679 |
* is the system properties of the |
680 |
* <code>java.lang.System.getProperty</code> methods and the |
681 |
* corresponding <code>get</code><em>TypeName</em> methods of |
682 |
* the <code>Boolean</code>, <code>Integer</code>, and |
683 |
* <code>Long</code> primitive types. For example: |
684 |
* <blockquote><pre> |
685 |
* String classPath = System.getProperty("java.class.path","."); |
686 |
* <br> |
687 |
* if (Boolean.getBoolean("myapp.exper.mode")) |
688 |
* enableExpertCommands(); |
689 |
* </pre></blockquote> |
690 |
* |
691 |
* @param name of the environment variable |
692 |
* @return the value of the variable, or <code>null</code> if the variable |
693 |
* is not defined. |
694 |
* @see java.lang.Boolean#getBoolean(java.lang.String) |
695 |
* @see java.lang.Integer#getInteger(java.lang.String) |
696 |
* @see java.lang.Integer#getInteger(java.lang.String, int) |
697 |
* @see java.lang.Integer#getInteger(java.lang.String, java.lang.Integer) |
698 |
* @see java.lang.Long#getLong(java.lang.String) |
699 |
* @see java.lang.Long#getLong(java.lang.String, long) |
700 |
* @see java.lang.Long#getLong(java.lang.String, java.lang.Long) |
701 |
* @see java.lang.System#getProperties() |
702 |
* @see java.lang.System#getProperty(java.lang.String) |
703 |
* @see java.lang.System#getProperty(java.lang.String, java.lang.String) |
704 |
*/ |
705 |
public static String getenv(String name) { |
706 |
throw new Error("getenv no longer supported, use properties and -D instead: " + name); |
707 |
} |
708 |
|
709 |
/** |
710 |
* Terminates the currently running Java Virtual Machine. The |
711 |
* argument serves as a status code; by convention, a nonzero status |
712 |
* code indicates abnormal termination. |
713 |
* <p> |
714 |
* This method calls the <code>exit</code> method in class |
715 |
* <code>Runtime</code>. This method never returns normally. |
716 |
* <p> |
717 |
* The call <code>System.exit(n)</code> is effectively equivalent to |
718 |
* the call: |
719 |
* <blockquote><pre> |
720 |
* Runtime.getRuntime().exit(n) |
721 |
* </pre></blockquote> |
722 |
* |
723 |
* @param status exit status. |
724 |
* @throws SecurityException |
725 |
* if a security manager exists and its <code>checkExit</code> |
726 |
* method doesn't allow exit with the specified status. |
727 |
* @see java.lang.Runtime#exit(int) |
728 |
*/ |
729 |
public static void exit(int status) { |
730 |
Runtime.getRuntime().exit(status); |
731 |
} |
732 |
|
733 |
/** |
734 |
* Runs the garbage collector. |
735 |
* <p> |
736 |
* Calling the <code>gc</code> method suggests that the Java Virtual |
737 |
* Machine expend effort toward recycling unused objects in order to |
738 |
* make the memory they currently occupy available for quick reuse. |
739 |
* When control returns from the method call, the Java Virtual |
740 |
* Machine has made a best effort to reclaim space from all discarded |
741 |
* objects. |
742 |
* <p> |
743 |
* The call <code>System.gc()</code> is effectively equivalent to the |
744 |
* call: |
745 |
* <blockquote><pre> |
746 |
* Runtime.getRuntime().gc() |
747 |
* </pre></blockquote> |
748 |
* |
749 |
* @see java.lang.Runtime#gc() |
750 |
*/ |
751 |
public static void gc() { |
752 |
Runtime.getRuntime().gc(); |
753 |
} |
754 |
|
755 |
/** |
756 |
* Runs the finalization methods of any objects pending finalization. |
757 |
* <p> |
758 |
* Calling this method suggests that the Java Virtual Machine expend |
759 |
* effort toward running the <code>finalize</code> methods of objects |
760 |
* that have been found to be discarded but whose <code>finalize</code> |
761 |
* methods have not yet been run. When control returns from the |
762 |
* method call, the Java Virtual Machine has made a best effort to |
763 |
* complete all outstanding finalizations. |
764 |
* <p> |
765 |
* The call <code>System.runFinalization()</code> is effectively |
766 |
* equivalent to the call: |
767 |
* <blockquote><pre> |
768 |
* Runtime.getRuntime().runFinalization() |
769 |
* </pre></blockquote> |
770 |
* |
771 |
* @see java.lang.Runtime#runFinalization() |
772 |
*/ |
773 |
public static void runFinalization() { |
774 |
Runtime.getRuntime().runFinalization(); |
775 |
} |
776 |
|
777 |
/** |
778 |
* Enable or disable finalization on exit; doing so specifies that the |
779 |
* finalizers of all objects that have finalizers that have not yet been |
780 |
* automatically invoked are to be run before the Java runtime exits. |
781 |
* By default, finalization on exit is disabled. |
782 |
* |
783 |
* <p>If there is a security manager, |
784 |
* its <code>checkExit</code> method is first called |
785 |
* with 0 as its argument to ensure the exit is allowed. |
786 |
* This could result in a SecurityException. |
787 |
* |
788 |
* @deprecated This method is inherently unsafe. It may result in |
789 |
* finalizers being called on live objects while other threads are |
790 |
* concurrently manipulating those objects, resulting in erratic |
791 |
* behavior or deadlock. |
792 |
* @param value indicating enabling or disabling of finalization |
793 |
* @throws SecurityException |
794 |
* if a security manager exists and its <code>checkExit</code> |
795 |
* method doesn't allow the exit. |
796 |
* |
797 |
* @see java.lang.Runtime#exit(int) |
798 |
* @see java.lang.Runtime#gc() |
799 |
* @see java.lang.SecurityManager#checkExit(int) |
800 |
* @since JDK1.1 |
801 |
*/ |
802 |
public static void runFinalizersOnExit(boolean value) { |
803 |
Runtime.getRuntime().runFinalizersOnExit(value); |
804 |
} |
805 |
|
806 |
/** |
807 |
* Loads a code file with the specified filename from the local file |
808 |
* system as a dynamic library. The filename |
809 |
* argument must be a complete path name. |
810 |
* <p> |
811 |
* The call <code>System.load(name)</code> is effectively equivalent |
812 |
* to the call: |
813 |
* <blockquote><pre> |
814 |
* Runtime.getRuntime().load(name) |
815 |
* </pre></blockquote> |
816 |
* |
817 |
* @param filename the file to load. |
818 |
* @exception SecurityException if a security manager exists and its |
819 |
* <code>checkLink</code> method doesn't allow |
820 |
* loading of the specified dynamic library |
821 |
* @exception UnsatisfiedLinkError if the file does not exist. |
822 |
* @see java.lang.Runtime#load(java.lang.String) |
823 |
* @see java.lang.SecurityManager#checkLink(java.lang.String) |
824 |
*/ |
825 |
public static void load(String filename) { |
826 |
Runtime.getRuntime().load0(getCallerClass(), filename); |
827 |
} |
828 |
|
829 |
/** |
830 |
* Loads the system library specified by the <code>libname</code> |
831 |
* argument. The manner in which a library name is mapped to the |
832 |
* actual system library is system dependent. |
833 |
* <p> |
834 |
* The call <code>System.loadLibrary(name)</code> is effectively |
835 |
* equivalent to the call |
836 |
* <blockquote><pre> |
837 |
* Runtime.getRuntime().loadLibrary(name) |
838 |
* </pre></blockquote> |
839 |
* |
840 |
* @param libname the name of the library. |
841 |
* @exception SecurityException if a security manager exists and its |
842 |
* <code>checkLink</code> method doesn't allow |
843 |
* loading of the specified dynamic library |
844 |
* @exception UnsatisfiedLinkError if the library does not exist. |
845 |
* @see java.lang.Runtime#loadLibrary(java.lang.String) |
846 |
* @see java.lang.SecurityManager#checkLink(java.lang.String) |
847 |
*/ |
848 |
public static void loadLibrary(String libname) { |
849 |
Runtime.getRuntime().loadLibrary0(getCallerClass(), libname); |
850 |
} |
851 |
|
852 |
/** |
853 |
* Maps a library name into a platform-specific string representing |
854 |
* a native library. |
855 |
* |
856 |
* @param libname the name of the library. |
857 |
* @return a platform-dependent native library name. |
858 |
* @see java.lang.System#loadLibrary(java.lang.String) |
859 |
* @see java.lang.ClassLoader#findLibrary(java.lang.String) |
860 |
* @since 1.2 |
861 |
*/ |
862 |
public static native String mapLibraryName(String libname); |
863 |
|
864 |
/** |
865 |
* The following two methods exist because in, out, and err must be |
866 |
* initialized to null. The compiler, however, cannot be permitted to |
867 |
* inline access to them, since they are later set to more sensible values |
868 |
* by initializeSystemClass(). |
869 |
*/ |
870 |
private static InputStream nullInputStream() throws NullPointerException { |
871 |
if (currentTimeMillis() > 0) |
872 |
return null; |
873 |
throw new NullPointerException(); |
874 |
} |
875 |
|
876 |
private static PrintStream nullPrintStream() throws NullPointerException { |
877 |
if (currentTimeMillis() > 0) |
878 |
return null; |
879 |
throw new NullPointerException(); |
880 |
} |
881 |
|
882 |
/** |
883 |
* Initialize the system class. Called after thread initialization. |
884 |
*/ |
885 |
private static void initializeSystemClass() { |
886 |
props = new Properties(); |
887 |
initProperties(props); |
888 |
sun.misc.Version.init(); |
889 |
FileInputStream fdIn = new FileInputStream(FileDescriptor.in); |
890 |
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out); |
891 |
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err); |
892 |
setIn0(new BufferedInputStream(fdIn)); |
893 |
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true)); |
894 |
setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true)); |
895 |
|
896 |
// Enough of the world is now in place that we can risk |
897 |
// initializing the logging configuration. |
898 |
try { |
899 |
java.util.logging.LogManager.getLogManager().readConfiguration(); |
900 |
} catch (Exception ex) { |
901 |
// System.err.println("Can't read logging configuration:"); |
902 |
// ex.printStackTrace(); |
903 |
} |
904 |
|
905 |
// Load the zip library now in order to keep java.util.zip.ZipFile |
906 |
// from trying to use itself to load this library later. |
907 |
loadLibrary("zip"); |
908 |
|
909 |
// Subsystems that are invoked during initialization can invoke |
910 |
// sun.misc.VM.isBooted() in order to avoid doing things that should |
911 |
// wait until the application class loader has been set up. |
912 |
sun.misc.VM.booted(); |
913 |
} |
914 |
|
915 |
/* returns the class of the caller. */ |
916 |
static Class getCallerClass() { |
917 |
// NOTE use of more generic Reflection.getCallerClass() |
918 |
return Reflection.getCallerClass(3); |
919 |
} |
920 |
} |