1 7 package java.lang; 8 9 import java.io.*; 10 import java.util.Properties ; 11 import java.util.PropertyPermission ; 12 import java.util.StringTokenizer ; 13 import java.security.AccessController ; 14 import java.security.PrivilegedAction ; 15 import java.security.AllPermission ; 16 import java.nio.channels.Channel ; 17 import java.nio.channels.spi.SelectorProvider ; 18 import sun.net.InetAddressCachePolicy; 19 import sun.nio.ch.Interruptible; 20 import sun.reflect.Reflection; 21 import sun.security.util.SecurityConstants; 22 import sun.reflect.annotation.AnnotationType; 23 24 38 public final class System { 39 40 41 private static native void registerNatives(); 42 static { 43 registerNatives(); 44 } 45 46 47 private System() { 48 } 49 50 56 public final static InputStream in = nullInputStream(); 57 58 83 public final static PrintStream out = nullPrintStream(); 84 85 97 public final static PrintStream err = nullPrintStream(); 98 99 101 private static SecurityManager security = null; 102 103 123 public static void setIn(InputStream in) { 124 checkIO(); 125 setIn0(in); 126 } 127 128 147 public static void setOut(PrintStream out) { 148 checkIO(); 149 setOut0(out); 150 } 151 152 171 public static void setErr(PrintStream err) { 172 checkIO(); 173 setErr0(err); 174 } 175 176 177 202 public static Channel inheritedChannel() throws IOException { 203 return SelectorProvider.provider().inheritedChannel(); 204 } 205 206 private static void checkIO() { 207 if (security != null) 208 security.checkPermission(new RuntimePermission ("setIO")); 209 } 210 211 private static native void setIn0(InputStream in); 212 private static native void setOut0(PrintStream out); 213 private static native void setErr0(PrintStream err); 214 215 238 public static 239 void setSecurityManager(final SecurityManager s) { 240 try { 241 s.checkPackageAccess("java.lang"); 242 } catch (Exception e) { 243 } 245 setSecurityManager0(s); 246 } 247 248 private static synchronized 249 void setSecurityManager0(final SecurityManager s) { 250 if (security != null) { 251 security.checkPermission(new RuntimePermission 254 ("setSecurityManager")); 255 } 256 257 if ((s != null) && (s.getClass().getClassLoader() != null)) { 258 AccessController.doPrivileged(new PrivilegedAction () { 267 public Object run() { 268 s.getClass().getProtectionDomain().implies 269 (SecurityConstants.ALL_PERMISSION); 270 return null; 271 } 272 }); 273 } 274 275 security = s; 276 InetAddressCachePolicy.setIfNotSet(InetAddressCachePolicy.FOREVER); 277 } 278 279 287 public static SecurityManager getSecurityManager() { 288 return security; 289 } 290 291 307 public static native long currentTimeMillis(); 308 309 334 public static native long nanoTime(); 335 336 428 public static native void arraycopy(Object src, int srcPos, 429 Object dest, int destPos, 430 int length); 431 432 443 public static native int identityHashCode(Object x); 444 445 465 466 private static Properties props; 467 private static native Properties initProperties(Properties props); 468 469 559 public static Properties getProperties() { 560 if (security != null) { 561 security.checkPropertiesAccess(); 562 } 563 return props; 564 } 565 566 588 public static void setProperties(Properties props) { 589 if (security != null) { 590 security.checkPropertiesAccess(); 591 } 592 if (props == null) { 593 props = new Properties (); 594 initProperties(props); 595 } 596 System.props = props; 597 } 598 599 625 public static String getProperty(String key) { 626 checkKey(key); 627 if (security != null) { 628 security.checkPropertyAccess(key); 629 } 630 return props.getProperty(key); 631 } 632 633 659 public static String getProperty(String key, String def) { 660 checkKey(key); 661 if (security != null) { 662 security.checkPropertyAccess(key); 663 } 664 return props.getProperty(key, def); 665 } 666 667 696 public static String setProperty(String key, String value) { 697 checkKey(key); 698 if (security != null) 699 security.checkPermission(new PropertyPermission (key, 700 SecurityConstants.PROPERTY_WRITE_ACTION)); 701 return (String ) props.setProperty(key, value); 702 } 703 704 731 public static String clearProperty(String key) { 732 checkKey(key); 733 if (security != null) 734 security.checkPermission(new PropertyPermission (key, "write")); 735 return (String ) props.remove(key); 736 } 737 738 private static void checkKey(String key) { 739 if (key == null) { 740 throw new NullPointerException ("key can't be null"); 741 } 742 if (key.equals("")) { 743 throw new IllegalArgumentException ("key can't be empty"); 744 } 745 } 746 747 793 public static String getenv(String name) { 794 if (security != null) 795 security.checkPermission(new RuntimePermission ("getenv."+name)); 796 797 return ProcessEnvironment.getenv(name); 798 } 799 800 801 841 public static java.util.Map <String ,String > getenv() { 842 if (security != null) 843 security.checkPermission(new RuntimePermission ("getenv.*")); 844 845 return ProcessEnvironment.getenv(); 846 } 847 848 868 public static void exit(int status) { 869 Runtime.getRuntime().exit(status); 870 } 871 872 890 public static void gc() { 891 Runtime.getRuntime().gc(); 892 } 893 894 912 public static void runFinalization() { 913 Runtime.getRuntime().runFinalization(); 914 } 915 916 941 @Deprecated 942 public static void runFinalizersOnExit(boolean value) { 943 Runtime.getRuntime().runFinalizersOnExit(value); 944 } 945 946 967 public static void load(String filename) { 968 Runtime.getRuntime().load0(getCallerClass(), filename); 969 } 970 971 992 public static void loadLibrary(String libname) { 993 Runtime.getRuntime().loadLibrary0(getCallerClass(), libname); 994 } 995 996 1008 public static native String mapLibraryName(String libname); 1009 1010 1016 private static InputStream nullInputStream() throws NullPointerException { 1017 if (currentTimeMillis() > 0) 1018 return null; 1019 throw new NullPointerException (); 1020 } 1021 1022 private static PrintStream nullPrintStream() throws NullPointerException { 1023 if (currentTimeMillis() > 0) 1024 return null; 1025 throw new NullPointerException (); 1026 } 1027 1028 1031 private static void initializeSystemClass() { 1032 props = new Properties (); 1033 initProperties(props); 1034 sun.misc.Version.init(); 1035 FileInputStream fdIn = new FileInputStream(FileDescriptor.in); 1036 FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out); 1037 FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err); 1038 setIn0(new BufferedInputStream(fdIn)); 1039 setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true)); 1040 setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true)); 1041 1042 loadLibrary("zip"); 1045 1046 Terminator.setup(); 1053 1054 sun.misc.VM.maxDirectMemory(); 1058 1059 sun.misc.VM.allowArraySyntax(); 1064 1065 sun.misc.VM.booted(); 1069 1070 Thread current = Thread.currentThread(); 1073 current.getThreadGroup().add(current); 1074 1075 sun.misc.SharedSecrets.setJavaLangAccess(new sun.misc.JavaLangAccess(){ 1077 public sun.reflect.ConstantPool getConstantPool(Class klass) { 1078 return klass.getConstantPool(); 1079 } 1080 public void setAnnotationType(Class klass, AnnotationType type) { 1081 klass.setAnnotationType(type); 1082 } 1083 public AnnotationType getAnnotationType(Class klass) { 1084 return klass.getAnnotationType(); 1085 } 1086 public void blockedOn(Thread t, Interruptible b) { 1087 t.blockedOn(b); 1088 } 1089 }); 1090 } 1091 1092 1093 static Class getCallerClass() { 1094 return Reflection.getCallerClass(3); 1096 } 1097} 1098 | Popular Tags |