System.java 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /* System.java -- useful methods to interface with the system
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.lang;
  33. import gnu.classpath.SystemProperties;
  34. import java.io.BufferedInputStream;
  35. import java.io.BufferedOutputStream;
  36. import java.io.FileDescriptor;
  37. import java.io.FileInputStream;
  38. import java.io.FileOutputStream;
  39. import java.io.IOException;
  40. import java.io.InputStream;
  41. import java.io.PrintStream;
  42. import java.nio.channels.Channel;
  43. import java.nio.channels.spi.SelectorProvider;
  44. import java.util.AbstractCollection;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.HashMap;
  49. import java.util.Iterator;
  50. import java.util.List;
  51. import java.util.Map;
  52. import java.util.Set;
  53. import java.util.Properties;
  54. import java.util.PropertyPermission;
  55. /**
  56. * System represents system-wide resources; things that represent the
  57. * general environment. As such, all methods are static.
  58. *
  59. * @author John Keiser
  60. * @author Eric Blake (ebb9@email.byu.edu)
  61. * @since 1.0
  62. * @status still missing 1.4 functionality
  63. */
  64. public final class System
  65. {
  66. // WARNING: System is a CORE class in the bootstrap cycle. See the comments
  67. // in vm/reference/java/lang/Runtime for implications of this fact.
  68. /**
  69. * The standard InputStream. This is assigned at startup and starts its
  70. * life perfectly valid. Although it is marked final, you can change it
  71. * using {@link #setIn(InputStream)} through some hefty VM magic.
  72. *
  73. * <p>This corresponds to the C stdin and C++ cin variables, which
  74. * typically input from the keyboard, but may be used to pipe input from
  75. * other processes or files. That should all be transparent to you,
  76. * however.
  77. */
  78. public static final InputStream in
  79. = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
  80. /**
  81. * The standard output PrintStream. This is assigned at startup and
  82. * starts its life perfectly valid. Although it is marked final, you can
  83. * change it using {@link #setOut(PrintStream)} through some hefty VM magic.
  84. *
  85. * <p>This corresponds to the C stdout and C++ cout variables, which
  86. * typically output normal messages to the screen, but may be used to pipe
  87. * output to other processes or files. That should all be transparent to
  88. * you, however.
  89. */
  90. public static final PrintStream out
  91. = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), true);
  92. /**
  93. * The standard output PrintStream. This is assigned at startup and
  94. * starts its life perfectly valid. Although it is marked final, you can
  95. * change it using {@link #setErr(PrintStream)} through some hefty VM magic.
  96. *
  97. * <p>This corresponds to the C stderr and C++ cerr variables, which
  98. * typically output error messages to the screen, but may be used to pipe
  99. * output to other processes or files. That should all be transparent to
  100. * you, however.
  101. */
  102. public static final PrintStream err
  103. = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.err)), true);
  104. /**
  105. * A cached copy of the environment variable map.
  106. */
  107. private static Map<String,String> environmentMap;
  108. /**
  109. * This class is uninstantiable.
  110. */
  111. private System()
  112. {
  113. }
  114. /**
  115. * Set {@link #in} to a new InputStream. This uses some VM magic to change
  116. * a "final" variable, so naturally there is a security check,
  117. * <code>RuntimePermission("setIO")</code>.
  118. *
  119. * @param in the new InputStream
  120. * @throws SecurityException if permission is denied
  121. * @since 1.1
  122. */
  123. public static void setIn(InputStream in)
  124. {
  125. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  126. if (sm != null)
  127. sm.checkPermission(new RuntimePermission("setIO"));
  128. setIn0(in);
  129. }
  130. /**
  131. * Set {@link #out} to a new PrintStream. This uses some VM magic to change
  132. * a "final" variable, so naturally there is a security check,
  133. * <code>RuntimePermission("setIO")</code>.
  134. *
  135. * @param out the new PrintStream
  136. * @throws SecurityException if permission is denied
  137. * @since 1.1
  138. */
  139. public static void setOut(PrintStream out)
  140. {
  141. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  142. if (sm != null)
  143. sm.checkPermission(new RuntimePermission("setIO"));
  144. setOut0(out);
  145. }
  146. /**
  147. * Set {@link #err} to a new PrintStream. This uses some VM magic to change
  148. * a "final" variable, so naturally there is a security check,
  149. * <code>RuntimePermission("setIO")</code>.
  150. *
  151. * @param err the new PrintStream
  152. * @throws SecurityException if permission is denied
  153. * @since 1.1
  154. */
  155. public static void setErr(PrintStream err)
  156. {
  157. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  158. if (sm != null)
  159. sm.checkPermission(new RuntimePermission("setIO"));
  160. setErr0(err);
  161. }
  162. /**
  163. * Set the current SecurityManager. If a security manager already exists,
  164. * then <code>RuntimePermission("setSecurityManager")</code> is checked
  165. * first. Since this permission is denied by the default security manager,
  166. * setting the security manager is often an irreversible action.
  167. *
  168. * @param sm the new SecurityManager
  169. * @throws SecurityException if permission is denied
  170. */
  171. public static synchronized void setSecurityManager(SecurityManager sm)
  172. {
  173. // Implementation note: the field lives in SecurityManager because of
  174. // bootstrap initialization issues. This method is synchronized so that
  175. // no other thread changes it to null before this thread makes the change.
  176. if (SecurityManager.current != null)
  177. SecurityManager.current.checkPermission
  178. (new RuntimePermission("setSecurityManager"));
  179. SecurityManager.current = sm;
  180. }
  181. /**
  182. * Get the current SecurityManager. If the SecurityManager has not been
  183. * set yet, then this method returns null.
  184. *
  185. * @return the current SecurityManager, or null
  186. */
  187. public static SecurityManager getSecurityManager()
  188. {
  189. return SecurityManager.current;
  190. }
  191. /**
  192. * Get the current time, measured in the number of milliseconds from the
  193. * beginning of Jan. 1, 1970. This is gathered from the system clock, with
  194. * any attendant incorrectness (it may be timezone dependent).
  195. *
  196. * @return the current time
  197. * @see java.util.Date
  198. */
  199. public static native long currentTimeMillis();
  200. /**
  201. * Get the current time, measured in nanoseconds. The result is as
  202. * precise as possible, and is measured against a fixed epoch.
  203. * However, unlike currentTimeMillis(), the epoch chosen is
  204. * arbitrary and may vary by platform, etc.
  205. * @since 1.5
  206. */
  207. public static native long nanoTime();
  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 native void arraycopy(Object src, int srcStart,
  232. Object dest, int destStart, int len);
  233. /**
  234. * Get a hash code computed by the VM for the Object. This hash code will
  235. * be the same as Object's hashCode() method. It is usually some
  236. * convolution of the pointer to the Object internal to the VM. It
  237. * follows standard hash code rules, in that it will remain the same for a
  238. * given Object for the lifetime of that Object.
  239. *
  240. * @param o the Object to get the hash code for
  241. * @return the VM-dependent hash code for this Object
  242. * @since 1.1
  243. */
  244. public static native int identityHashCode(Object o);
  245. /**
  246. * Get all the system properties at once. A security check may be performed,
  247. * <code>checkPropertiesAccess</code>. Note that a security manager may
  248. * allow getting a single property, but not the entire group.
  249. *
  250. * <p>The required properties include:
  251. * <dl>
  252. * <dt>java.version</dt> <dd>Java version number</dd>
  253. * <dt>java.vendor</dt> <dd>Java vendor specific string</dd>
  254. * <dt>java.vendor.url</dt> <dd>Java vendor URL</dd>
  255. * <dt>java.home</dt> <dd>Java installation directory</dd>
  256. * <dt>java.vm.specification.version</dt> <dd>VM Spec version</dd>
  257. * <dt>java.vm.specification.vendor</dt> <dd>VM Spec vendor</dd>
  258. * <dt>java.vm.specification.name</dt> <dd>VM Spec name</dd>
  259. * <dt>java.vm.version</dt> <dd>VM implementation version</dd>
  260. * <dt>java.vm.vendor</dt> <dd>VM implementation vendor</dd>
  261. * <dt>java.vm.name</dt> <dd>VM implementation name</dd>
  262. * <dt>java.specification.version</dt> <dd>Java Runtime Environment version</dd>
  263. * <dt>java.specification.vendor</dt> <dd>Java Runtime Environment vendor</dd>
  264. * <dt>java.specification.name</dt> <dd>Java Runtime Environment name</dd>
  265. * <dt>java.class.version</dt> <dd>Java class version number</dd>
  266. * <dt>java.class.path</dt> <dd>Java classpath</dd>
  267. * <dt>java.library.path</dt> <dd>Path for finding Java libraries</dd>
  268. * <dt>java.io.tmpdir</dt> <dd>Default temp file path</dd>
  269. * <dt>java.compiler</dt> <dd>Name of JIT to use</dd>
  270. * <dt>java.ext.dirs</dt> <dd>Java extension path</dd>
  271. * <dt>os.name</dt> <dd>Operating System Name</dd>
  272. * <dt>os.arch</dt> <dd>Operating System Architecture</dd>
  273. * <dt>os.version</dt> <dd>Operating System Version</dd>
  274. * <dt>file.separator</dt> <dd>File separator ("/" on Unix)</dd>
  275. * <dt>path.separator</dt> <dd>Path separator (":" on Unix)</dd>
  276. * <dt>line.separator</dt> <dd>Line separator ("\n" on Unix)</dd>
  277. * <dt>user.name</dt> <dd>User account name</dd>
  278. * <dt>user.home</dt> <dd>User home directory</dd>
  279. * <dt>user.dir</dt> <dd>User's current working directory</dd>
  280. * </dl>
  281. *
  282. * In addition, gnu defines several other properties, where ? stands for
  283. * each character in '0' through '9':
  284. * <dl>
  285. * <dt>gnu.classpath.home</dt> <dd>Path to the classpath libraries.</dd>
  286. * <dt>gnu.classpath.version</dt> <dd>Version of the classpath libraries.</dd>
  287. * <dt>gnu.classpath.vm.shortname</dt> <dd>Succinct version of the VM name;
  288. * used for finding property files in file system</dd>
  289. * <dt>gnu.classpath.home.url</dt> <dd> Base URL; used for finding
  290. * property files in file system</dd>
  291. * <dt>gnu.cpu.endian</dt> <dd>big or little</dd>
  292. * <dt>gnu.java.io.encoding_scheme_alias.ISO-8859-?</dt> <dd>8859_?</dd>
  293. * <dt>gnu.java.io.encoding_scheme_alias.iso-8859-?</dt> <dd>8859_?</dd>
  294. * <dt>gnu.java.io.encoding_scheme_alias.iso8859_?</dt> <dd>8859_?</dd>
  295. * <dt>gnu.java.io.encoding_scheme_alias.iso-latin-_?</dt> <dd>8859_?</dd>
  296. * <dt>gnu.java.io.encoding_scheme_alias.latin?</dt> <dd>8859_?</dd>
  297. * <dt>gnu.java.io.encoding_scheme_alias.UTF-8</dt> <dd>UTF8</dd>
  298. * <dt>gnu.java.io.encoding_scheme_alias.utf-8</dt> <dd>UTF8</dd>
  299. * <dt>gnu.java.util.zoneinfo.dir</dt> <dd>Root of zoneinfo tree</dd>
  300. * </dl>
  301. *
  302. * @return the system properties, will never be null
  303. * @throws SecurityException if permission is denied
  304. */
  305. public static Properties getProperties()
  306. {
  307. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  308. if (sm != null)
  309. sm.checkPropertiesAccess();
  310. return SystemProperties.getProperties();
  311. }
  312. /**
  313. * Set all the system properties at once. A security check may be performed,
  314. * <code>checkPropertiesAccess</code>. Note that a security manager may
  315. * allow setting a single property, but not the entire group. An argument
  316. * of null resets the properties to the startup default.
  317. *
  318. * @param properties the new set of system properties
  319. * @throws SecurityException if permission is denied
  320. */
  321. public static void setProperties(Properties properties)
  322. {
  323. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  324. if (sm != null)
  325. sm.checkPropertiesAccess();
  326. SystemProperties.setProperties(properties);
  327. }
  328. /**
  329. * Get a single system property by name. A security check may be performed,
  330. * <code>checkPropertyAccess(key)</code>.
  331. *
  332. * @param key the name of the system property to get
  333. * @return the property, or null if not found
  334. * @throws SecurityException if permission is denied
  335. * @throws NullPointerException if key is null
  336. * @throws IllegalArgumentException if key is ""
  337. */
  338. public static String getProperty(String key)
  339. {
  340. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  341. if (sm != null)
  342. sm.checkPropertyAccess(key);
  343. else if (key.length() == 0)
  344. throw new IllegalArgumentException("key can't be empty");
  345. return SystemProperties.getProperty(key);
  346. }
  347. /**
  348. * Get a single system property by name. A security check may be performed,
  349. * <code>checkPropertyAccess(key)</code>.
  350. *
  351. * @param key the name of the system property to get
  352. * @param def the default
  353. * @return the property, or def if not found
  354. * @throws SecurityException if permission is denied
  355. * @throws NullPointerException if key is null
  356. * @throws IllegalArgumentException if key is ""
  357. */
  358. public static String getProperty(String key, String def)
  359. {
  360. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  361. if (sm != null)
  362. sm.checkPropertyAccess(key);
  363. return SystemProperties.getProperty(key, def);
  364. }
  365. /**
  366. * Set a single system property by name. A security check may be performed,
  367. * <code>checkPropertyAccess(key, "write")</code>.
  368. *
  369. * @param key the name of the system property to set
  370. * @param value the new value
  371. * @return the previous value, or null
  372. * @throws SecurityException if permission is denied
  373. * @throws NullPointerException if key is null
  374. * @throws IllegalArgumentException if key is ""
  375. * @since 1.2
  376. */
  377. public static String setProperty(String key, String value)
  378. {
  379. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  380. if (sm != null)
  381. sm.checkPermission(new PropertyPermission(key, "write"));
  382. return SystemProperties.setProperty(key, value);
  383. }
  384. /**
  385. * Remove a single system property by name. A security check may be
  386. * performed, <code>checkPropertyAccess(key, "write")</code>.
  387. *
  388. * @param key the name of the system property to remove
  389. * @return the previous value, or null
  390. * @throws SecurityException if permission is denied
  391. * @throws NullPointerException if key is null
  392. * @throws IllegalArgumentException if key is ""
  393. * @since 1.5
  394. */
  395. public static String clearProperty(String key)
  396. {
  397. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  398. if (sm != null)
  399. sm.checkPermission(new PropertyPermission(key, "write"));
  400. // This handles both the null pointer exception and the illegal
  401. // argument exception.
  402. if (key.length() == 0)
  403. throw new IllegalArgumentException("key can't be empty");
  404. return SystemProperties.remove(key);
  405. }
  406. /**
  407. * Gets the value of an environment variable.
  408. *
  409. * @param name the name of the environment variable
  410. * @return the string value of the variable or null when the
  411. * environment variable is not defined.
  412. * @throws NullPointerException
  413. * @throws SecurityException if permission is denied
  414. * @since 1.5
  415. * @specnote This method was deprecated in some JDK releases, but
  416. * was restored in 1.5.
  417. */
  418. public static String getenv(String name)
  419. {
  420. if (name == null)
  421. throw new NullPointerException();
  422. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  423. if (sm != null)
  424. sm.checkPermission(new RuntimePermission("getenv." + name));
  425. return getenv0(name);
  426. }
  427. /**
  428. * <p>
  429. * Returns an unmodifiable view of the system environment variables.
  430. * If the underlying system does not support environment variables,
  431. * an empty map is returned.
  432. * </p>
  433. * <p>
  434. * The returned map is read-only and does not accept queries using
  435. * null keys or values, or those of a type other than <code>String</code>.
  436. * Attempts to modify the map will throw an
  437. * <code>UnsupportedOperationException</code>, while attempts
  438. * to pass in a null value will throw a
  439. * <code>NullPointerException</code>. Types other than <code>String</code>
  440. * throw a <code>ClassCastException</code>.
  441. * </p>
  442. * <p>
  443. * As the returned map is generated using data from the underlying
  444. * platform, it may not comply with the <code>equals()</code>
  445. * and <code>hashCode()</code> contracts. It is also likely that
  446. * the keys of this map will be case-sensitive.
  447. * </p>
  448. * <p>
  449. * Use of this method may require a security check for the
  450. * RuntimePermission "getenv.*".
  451. * </p>
  452. *
  453. * @return a map of the system environment variables.
  454. * @throws SecurityException if the checkPermission method of
  455. * an installed security manager prevents access to
  456. * the system environment variables.
  457. * @since 1.5
  458. */
  459. public static Map<String, String> getenv()
  460. {
  461. SecurityManager sm = SecurityManager.current; // Be thread-safe.
  462. if (sm != null)
  463. sm.checkPermission(new RuntimePermission("getenv.*"));
  464. if (environmentMap == null)
  465. {
  466. // List<String> environ = (List<String>)VMSystem.environ();
  467. // FIXME
  468. List<String> environ = new ArrayList<String>();
  469. Map<String,String> variables = new EnvironmentMap();
  470. for (String pair : environ)
  471. {
  472. String[] parts = pair.split("=");
  473. variables.put(parts[0], parts[1]);
  474. }
  475. environmentMap = Collections.unmodifiableMap(variables);
  476. }
  477. return environmentMap;
  478. }
  479. /**
  480. * Terminate the Virtual Machine. This just calls
  481. * <code>Runtime.getRuntime().exit(status)</code>, and never returns.
  482. * Obviously, a security check is in order, <code>checkExit</code>.
  483. *
  484. * @param status the exit status; by convention non-zero is abnormal
  485. * @throws SecurityException if permission is denied
  486. * @see Runtime#exit(int)
  487. */
  488. public static void exit(int status)
  489. {
  490. Runtime.getRuntime().exit(status);
  491. }
  492. /**
  493. * Calls the garbage collector. This is only a hint, and it is up to the
  494. * implementation what this hint suggests, but it usually causes a
  495. * best-effort attempt to reclaim unused memory from discarded objects.
  496. * This calls <code>Runtime.getRuntime().gc()</code>.
  497. *
  498. * @see Runtime#gc()
  499. */
  500. public static void gc()
  501. {
  502. Runtime.getRuntime().gc();
  503. }
  504. /**
  505. * Runs object finalization on pending objects. This is only a hint, and
  506. * it is up to the implementation what this hint suggests, but it usually
  507. * causes a best-effort attempt to run finalizers on all objects ready
  508. * to be reclaimed. This calls
  509. * <code>Runtime.getRuntime().runFinalization()</code>.
  510. *
  511. * @see Runtime#runFinalization()
  512. */
  513. public static void runFinalization()
  514. {
  515. Runtime.getRuntime().runFinalization();
  516. }
  517. /**
  518. * Tell the Runtime whether to run finalization before exiting the
  519. * JVM. This is inherently unsafe in multi-threaded applications,
  520. * since it can force initialization on objects which are still in use
  521. * by live threads, leading to deadlock; therefore this is disabled by
  522. * default. There may be a security check, <code>checkExit(0)</code>. This
  523. * calls <code>Runtime.getRuntime().runFinalizersOnExit()</code>.
  524. *
  525. * @param finalizeOnExit whether to run finalizers on exit
  526. * @throws SecurityException if permission is denied
  527. * @see Runtime#runFinalizersOnExit()
  528. * @since 1.1
  529. * @deprecated never rely on finalizers to do a clean, thread-safe,
  530. * mop-up from your code
  531. */
  532. public static void runFinalizersOnExit(boolean finalizeOnExit)
  533. {
  534. Runtime.getRuntime().runFinalizersOnExit(finalizeOnExit);
  535. }
  536. /**
  537. * Load a code file using its explicit system-dependent filename. A security
  538. * check may be performed, <code>checkLink</code>. This just calls
  539. * <code>Runtime.getRuntime().load(filename)</code>.
  540. *
  541. * <p>
  542. * The library is loaded using the class loader associated with the
  543. * class associated with the invoking method.
  544. *
  545. * @param filename the code file to load
  546. * @throws SecurityException if permission is denied
  547. * @throws UnsatisfiedLinkError if the file cannot be loaded
  548. * @see Runtime#load(String)
  549. */
  550. public static void load(String filename)
  551. {
  552. Runtime.getRuntime().load(filename);
  553. }
  554. /**
  555. * Load a library using its explicit system-dependent filename. A security
  556. * check may be performed, <code>checkLink</code>. This just calls
  557. * <code>Runtime.getRuntime().load(filename)</code>.
  558. *
  559. * <p>
  560. * The library is loaded using the class loader associated with the
  561. * class associated with the invoking method.
  562. *
  563. * @param libname the library file to load
  564. * @throws SecurityException if permission is denied
  565. * @throws UnsatisfiedLinkError if the file cannot be loaded
  566. * @see Runtime#load(String)
  567. */
  568. public static void loadLibrary(String libname)
  569. {
  570. Runtime.getRuntime().loadLibrary(libname);
  571. }
  572. /**
  573. * Convert a library name to its platform-specific variant.
  574. *
  575. * @param libname the library name, as used in <code>loadLibrary</code>
  576. * @return the platform-specific mangling of the name
  577. * @since 1.2
  578. */
  579. public static String mapLibraryName(String libname)
  580. {
  581. // XXX Fix this!!!!
  582. return Runtime.nativeGetLibname("", libname);
  583. }
  584. /**
  585. * Set {@link #in} to a new InputStream.
  586. *
  587. * @param in the new InputStream
  588. * @see #setIn(InputStream)
  589. */
  590. private static native void setIn0(InputStream in);
  591. /**
  592. * Set {@link #out} to a new PrintStream.
  593. *
  594. * @param out the new PrintStream
  595. * @see #setOut(PrintStream)
  596. */
  597. private static native void setOut0(PrintStream out);
  598. /**
  599. * Set {@link #err} to a new PrintStream.
  600. *
  601. * @param err the new PrintStream
  602. * @see #setErr(PrintStream)
  603. */
  604. private static native void setErr0(PrintStream err);
  605. /**
  606. * Gets the value of an environment variable.
  607. *
  608. * @see #getenv(String)
  609. */
  610. static native String getenv0(String name);
  611. /**
  612. * Returns the inherited channel of the VM.
  613. *
  614. * This wraps the inheritedChannel() call of the system's default
  615. * {@link SelectorProvider}.
  616. *
  617. * @return the inherited channel of the VM
  618. *
  619. * @throws IOException If an I/O error occurs
  620. * @throws SecurityException If an installed security manager denies access
  621. * to RuntimePermission("inheritedChannel")
  622. *
  623. * @since 1.5
  624. */
  625. public static Channel inheritedChannel()
  626. throws IOException
  627. {
  628. return SelectorProvider.provider().inheritedChannel();
  629. }
  630. /**
  631. * This is a specialised <code>Collection</code>, providing
  632. * the necessary provisions for the collections used by the
  633. * environment variable map. Namely, it prevents
  634. * querying anything but <code>String</code>s.
  635. *
  636. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  637. */
  638. private static class EnvironmentCollection
  639. extends AbstractCollection<String>
  640. {
  641. /**
  642. * The wrapped collection.
  643. */
  644. protected Collection<String> c;
  645. /**
  646. * Constructs a new environment collection, which
  647. * wraps the elements of the supplied collection.
  648. *
  649. * @param coll the collection to use as a base for
  650. * this collection.
  651. */
  652. public EnvironmentCollection(Collection<String> coll)
  653. {
  654. c = coll;
  655. }
  656. /**
  657. * Blocks queries containing a null object or an object which
  658. * isn't of type <code>String</code>. All other queries
  659. * are forwarded to the underlying collection.
  660. *
  661. * @param obj the object to look for.
  662. * @return true if the object exists in the collection.
  663. * @throws NullPointerException if the specified object is null.
  664. * @throws ClassCastException if the specified object is not a String.
  665. */
  666. public boolean contains(Object obj)
  667. {
  668. if (obj == null)
  669. throw new
  670. NullPointerException("This collection does not support " +
  671. "null values.");
  672. if (!(obj instanceof String))
  673. throw new
  674. ClassCastException("This collection only supports Strings.");
  675. return c.contains(obj);
  676. }
  677. /**
  678. * Blocks queries where the collection contains a null object or
  679. * an object which isn't of type <code>String</code>. All other
  680. * queries are forwarded to the underlying collection.
  681. *
  682. * @param coll the collection of objects to look for.
  683. * @return true if the collection contains all elements in the collection.
  684. * @throws NullPointerException if the collection is null.
  685. * @throws NullPointerException if any collection entry is null.
  686. * @throws ClassCastException if any collection entry is not a String.
  687. */
  688. public boolean containsAll(Collection<?> coll)
  689. {
  690. for (Object o: coll)
  691. {
  692. if (o == null)
  693. throw new
  694. NullPointerException("This collection does not support " +
  695. "null values.");
  696. if (!(o instanceof String))
  697. throw new
  698. ClassCastException("This collection only supports Strings.");
  699. }
  700. return c.containsAll(coll);
  701. }
  702. /**
  703. * This returns an iterator over the map elements, with the
  704. * same provisions as for the collection and underlying map.
  705. *
  706. * @return an iterator over the map elements.
  707. */
  708. public Iterator<String> iterator()
  709. {
  710. return c.iterator();
  711. }
  712. /**
  713. * Blocks the removal of elements from the collection.
  714. *
  715. * @return true if the removal was sucessful.
  716. * @throws NullPointerException if the collection is null.
  717. * @throws NullPointerException if any collection entry is null.
  718. * @throws ClassCastException if any collection entry is not a String.
  719. */
  720. public boolean remove(Object key)
  721. {
  722. if (key == null)
  723. throw new
  724. NullPointerException("This collection does not support " +
  725. "null values.");
  726. if (!(key instanceof String))
  727. throw new
  728. ClassCastException("This collection only supports Strings.");
  729. return c.contains(key);
  730. }
  731. /**
  732. * Blocks the removal of all elements in the specified
  733. * collection from the collection.
  734. *
  735. * @param coll the collection of elements to remove.
  736. * @return true if the elements were removed.
  737. * @throws NullPointerException if the collection is null.
  738. * @throws NullPointerException if any collection entry is null.
  739. * @throws ClassCastException if any collection entry is not a String.
  740. */
  741. public boolean removeAll(Collection<?> coll)
  742. {
  743. for (Object o: coll)
  744. {
  745. if (o == null)
  746. throw new
  747. NullPointerException("This collection does not support " +
  748. "null values.");
  749. if (!(o instanceof String))
  750. throw new
  751. ClassCastException("This collection only supports Strings.");
  752. }
  753. return c.removeAll(coll);
  754. }
  755. /**
  756. * Blocks the retention of all elements in the specified
  757. * collection from the collection.
  758. *
  759. * @param c the collection of elements to retain.
  760. * @return true if the other elements were removed.
  761. * @throws NullPointerException if the collection is null.
  762. * @throws NullPointerException if any collection entry is null.
  763. * @throws ClassCastException if any collection entry is not a String.
  764. */
  765. public boolean retainAll(Collection<?> coll)
  766. {
  767. for (Object o: coll)
  768. {
  769. if (o == null)
  770. throw new
  771. NullPointerException("This collection does not support " +
  772. "null values.");
  773. if (!(o instanceof String))
  774. throw new
  775. ClassCastException("This collection only supports Strings.");
  776. }
  777. return c.containsAll(coll);
  778. }
  779. /**
  780. * This simply calls the same method on the wrapped
  781. * collection.
  782. *
  783. * @return the size of the underlying collection.
  784. */
  785. public int size()
  786. {
  787. return c.size();
  788. }
  789. } // class EnvironmentCollection<String>
  790. /**
  791. * This is a specialised <code>HashMap</code>, which
  792. * prevents the addition or querying of anything other than
  793. * <code>String</code> objects.
  794. *
  795. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  796. */
  797. static class EnvironmentMap
  798. extends HashMap<String,String>
  799. {
  800. /**
  801. * Cache the entry set.
  802. */
  803. private transient Set<Map.Entry<String,String>> entries;
  804. /**
  805. * Cache the key set.
  806. */
  807. private transient Set<String> keys;
  808. /**
  809. * Cache the value collection.
  810. */
  811. private transient Collection<String> values;
  812. /**
  813. * Constructs a new empty <code>EnvironmentMap</code>.
  814. */
  815. EnvironmentMap()
  816. {
  817. super();
  818. }
  819. /**
  820. * Constructs a new <code>EnvironmentMap</code> containing
  821. * the contents of the specified map.
  822. *
  823. * @param m the map to be added to this.
  824. * @throws NullPointerException if a key or value is null.
  825. * @throws ClassCastException if a key or value is not a String.
  826. */
  827. EnvironmentMap(Map<String,String> m)
  828. {
  829. super(m);
  830. }
  831. /**
  832. * Blocks queries containing a null key or one which is not
  833. * of type <code>String</code>. All other queries
  834. * are forwarded to the superclass.
  835. *
  836. * @param key the key to look for in the map.
  837. * @return true if the key exists in the map.
  838. * @throws NullPointerException if the specified key is null.
  839. */
  840. public boolean containsKey(Object key)
  841. {
  842. if (key == null)
  843. throw new
  844. NullPointerException("This map does not support null keys.");
  845. if (!(key instanceof String))
  846. throw new
  847. ClassCastException("This map only allows queries using Strings.");
  848. return super.containsKey(key);
  849. }
  850. /**
  851. * Blocks queries using a null or non-<code>String</code> value.
  852. * All other queries are forwarded to the superclass.
  853. *
  854. * @param value the value to look for in the map.
  855. * @return true if the value exists in the map.
  856. * @throws NullPointerException if the specified value is null.
  857. */
  858. public boolean containsValue(Object value)
  859. {
  860. if (value == null)
  861. throw new
  862. NullPointerException("This map does not support null values.");
  863. if (!(value instanceof String))
  864. throw new
  865. ClassCastException("This map only allows queries using Strings.");
  866. return super.containsValue(value);
  867. }
  868. /**
  869. * Returns a set view of the map entries, with the same
  870. * provisions as for the underlying map.
  871. *
  872. * @return a set containing the map entries.
  873. */
  874. public Set<Map.Entry<String,String>> entrySet()
  875. {
  876. if (entries == null)
  877. entries = super.entrySet();
  878. return entries;
  879. }
  880. /**
  881. * Blocks queries containing a null or non-<code>String</code> key.
  882. * All other queries are passed on to the superclass.
  883. *
  884. * @param key the key to retrieve the value for.
  885. * @return the value associated with the given key.
  886. * @throws NullPointerException if the specified key is null.
  887. * @throws ClassCastException if the specified key is not a String.
  888. */
  889. public String get(Object key)
  890. {
  891. if (key == null)
  892. throw new
  893. NullPointerException("This map does not support null keys.");
  894. if (!(key instanceof String))
  895. throw new
  896. ClassCastException("This map only allows queries using Strings.");
  897. return super.get(key);
  898. }
  899. /**
  900. * Returns a set view of the keys, with the same
  901. * provisions as for the underlying map.
  902. *
  903. * @return a set containing the keys.
  904. */
  905. public Set<String> keySet()
  906. {
  907. if (keys == null)
  908. keys = new EnvironmentSet(super.keySet());
  909. return keys;
  910. }
  911. /**
  912. * Associates the given key to the given value. If the
  913. * map already contains the key, its value is replaced.
  914. * The map does not accept null keys or values, or keys
  915. * and values not of type {@link String}.
  916. *
  917. * @param key the key to map.
  918. * @param value the value to be mapped.
  919. * @return the previous value of the key, or null if there was no mapping
  920. * @throws NullPointerException if a key or value is null.
  921. * @throws ClassCastException if a key or value is not a String.
  922. */
  923. public String put(String key, String value)
  924. {
  925. if (key == null)
  926. throw new NullPointerException("A new key is null.");
  927. if (value == null)
  928. throw new NullPointerException("A new value is null.");
  929. if (!(key instanceof String))
  930. throw new ClassCastException("A new key is not a String.");
  931. if (!(value instanceof String))
  932. throw new ClassCastException("A new value is not a String.");
  933. return super.put(key, value);
  934. }
  935. /**
  936. * Removes a key-value pair from the map. The queried key may not
  937. * be null or of a type other than a <code>String</code>.
  938. *
  939. * @param key the key of the entry to remove.
  940. * @return the removed value.
  941. * @throws NullPointerException if the specified key is null.
  942. * @throws ClassCastException if the specified key is not a String.
  943. */
  944. public String remove(Object key)
  945. {
  946. if (key == null)
  947. throw new
  948. NullPointerException("This map does not support null keys.");
  949. if (!(key instanceof String))
  950. throw new
  951. ClassCastException("This map only allows queries using Strings.");
  952. return super.remove(key);
  953. }
  954. /**
  955. * Returns a collection view of the values, with the same
  956. * provisions as for the underlying map.
  957. *
  958. * @return a collection containing the values.
  959. */
  960. public Collection<String> values()
  961. {
  962. if (values == null)
  963. values = new EnvironmentCollection(super.values());
  964. return values;
  965. }
  966. }
  967. /**
  968. * This is a specialised <code>Set</code>, providing
  969. * the necessary provisions for the collections used by the
  970. * environment variable map. Namely, it prevents
  971. * modifications and the use of queries with null
  972. * or non-<code>String</code> values.
  973. *
  974. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  975. */
  976. private static class EnvironmentSet
  977. extends EnvironmentCollection
  978. implements Set<String>
  979. {
  980. /**
  981. * Constructs a new environment set, which
  982. * wraps the elements of the supplied set.
  983. *
  984. * @param set the set to use as a base for
  985. * this set.
  986. */
  987. public EnvironmentSet(Set<String> set)
  988. {
  989. super(set);
  990. }
  991. /**
  992. * This simply calls the same method on the wrapped
  993. * collection.
  994. *
  995. * @param obj the object to compare with.
  996. * @return true if the two objects are equal.
  997. */
  998. public boolean equals(Object obj)
  999. {
  1000. return c.equals(obj);
  1001. }
  1002. /**
  1003. * This simply calls the same method on the wrapped
  1004. * collection.
  1005. *
  1006. * @return the hashcode of the collection.
  1007. */
  1008. public int hashCode()
  1009. {
  1010. return c.hashCode();
  1011. }
  1012. } // class EnvironmentSet<String>
  1013. } // class System