< prev index next >

src/java.base/share/classes/java/lang/System.java

Print this page




  26 
  27 import java.io.BufferedInputStream;
  28 import java.io.BufferedOutputStream;
  29 import java.io.Console;
  30 import java.io.FileDescriptor;
  31 import java.io.FileInputStream;
  32 import java.io.FileOutputStream;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.PrintStream;
  36 import java.io.UnsupportedEncodingException;
  37 import java.lang.annotation.Annotation;
  38 import java.lang.reflect.Constructor;
  39 import java.lang.reflect.Executable;
  40 import java.lang.reflect.Layer;
  41 import java.lang.reflect.Method;
  42 import java.lang.reflect.Modifier;
  43 import java.lang.reflect.Module;
  44 import java.net.URL;
  45 import java.security.AccessControlContext;

  46 import java.util.Properties;
  47 import java.util.PropertyPermission;
  48 import java.util.Map;
  49 import java.security.AccessController;
  50 import java.security.PrivilegedAction;
  51 import java.nio.channels.Channel;
  52 import java.nio.channels.spi.SelectorProvider;
  53 import java.util.concurrent.ConcurrentHashMap;
  54 import java.util.stream.Stream;
  55 
  56 import java.util.Objects;
  57 import java.util.ResourceBundle;
  58 import java.util.function.Supplier;
  59 import sun.nio.ch.Interruptible;
  60 import jdk.internal.reflect.CallerSensitive;
  61 import jdk.internal.reflect.Reflection;
  62 import sun.security.util.SecurityConstants;
  63 import sun.reflect.annotation.AnnotationType;
  64 import jdk.internal.HotSpotIntrinsicCandidate;
  65 import jdk.internal.misc.JavaLangAccess;;


1828      *             <code>null</code>
1829      * @see        java.lang.System#loadLibrary(java.lang.String)
1830      * @see        java.lang.ClassLoader#findLibrary(java.lang.String)
1831      * @since      1.2
1832      */
1833     public static native String mapLibraryName(String libname);
1834 
1835     /**
1836      * Create PrintStream for stdout/err based on encoding.
1837      */
1838     private static PrintStream newPrintStream(FileOutputStream fos, String enc) {
1839        if (enc != null) {
1840             try {
1841                 return new PrintStream(new BufferedOutputStream(fos, 128), true, enc);
1842             } catch (UnsupportedEncodingException uee) {}
1843         }
1844         return new PrintStream(new BufferedOutputStream(fos, 128), true);
1845     }
1846 
1847     /**

































1848      * Initialize the system class.  Called after thread initialization.
1849      */
1850     private static void initPhase1() {
1851 
1852         // VM might invoke JNU_NewStringPlatform() to set those encoding
1853         // sensitive properties (user.home, user.name, boot.class.path, etc.)
1854         // during "props" initialization, in which it may need access, via
1855         // System.getProperty(), to the related system encoding property that
1856         // have been initialized (put into "props") at early stage of the
1857         // initialization. So make sure the "props" is available at the
1858         // very beginning of the initialization and all system properties to
1859         // be put into it directly.
1860         props = new Properties();
1861         initProperties(props);  // initialized by the VM
1862 
1863         // There are certain system configurations that may be controlled by
1864         // VM options such as the maximum amount of direct memory and
1865         // Integer cache size used to support the object identity semantics
1866         // of autoboxing.  Typically, the library will obtain these values
1867         // from the properties set by the VM.  If the properties are for


1905         current.getThreadGroup().add(current);
1906 
1907         // register shared secrets
1908         setJavaLangAccess();
1909 
1910         // Subsystems that are invoked during initialization can invoke
1911         // VM.isBooted() in order to avoid doing things that should
1912         // wait until the VM is fully initialized. The initialization level
1913         // is incremented from 0 to 1 here to indicate the first phase of
1914         // initialization has completed.
1915         // IMPORTANT: Ensure that this remains the last initialization action!
1916         VM.initLevel(1);
1917     }
1918 
1919     // @see #initPhase2()
1920     private static Layer bootLayer;
1921 
1922     /*
1923      * Invoked by VM.  Phase 2 module system initialization.
1924      * Only classes in java.base can be loaded in this phase.





1925      */
1926     private static void initPhase2() {
1927         // initialize the module system
1928         System.bootLayer = ModuleBootstrap.boot();





1929 
1930         // module system initialized
1931         VM.initLevel(2);


1932     }
1933 
1934     /*
1935      * Invoked by VM.  Phase 3 is the final system initialization:
1936      * 1. set security manager
1937      * 2. set system class loader
1938      * 3. set TCCL
1939      *
1940      * This method must be called after the module system initialization.
1941      * The security manager and system class loader may be custom class from
1942      * the application classpath or modulepath.
1943      */
1944     private static void initPhase3() {
1945         // set security manager
1946         String cn = System.getProperty("java.security.manager");
1947         if (cn != null) {
1948             if (cn.isEmpty() || "default".equals(cn)) {
1949                 System.setSecurityManager(new SecurityManager());
1950             } else {
1951                 try {


2017                 t.blockedOn(b);
2018             }
2019             public void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook) {
2020                 Shutdown.add(slot, registerShutdownInProgress, hook);
2021             }
2022             public String newStringUnsafe(char[] chars) {
2023                 return new String(chars, true);
2024             }
2025             public Thread newThreadWithAcc(Runnable target, AccessControlContext acc) {
2026                 return new Thread(target, acc);
2027             }
2028             public void invokeFinalize(Object o) throws Throwable {
2029                 o.finalize();
2030             }
2031             public Layer getBootLayer() {
2032                 return bootLayer;
2033             }
2034             public ConcurrentHashMap<?, ?> createOrGetClassLoaderValueMap(ClassLoader cl) {
2035                 return cl.createOrGetClassLoaderValueMap();
2036             }



2037             public Class<?> findBootstrapClassOrNull(ClassLoader cl, String name) {
2038                 return cl.findBootstrapClassOrNull(name);
2039             }
2040             public URL findResource(ClassLoader cl, String mn, String name) throws IOException {
2041                 return cl.findResource(mn, name);
2042             }
2043             public Stream<Package> packages(ClassLoader cl) {
2044                 return cl.packages();
2045             }
2046             public Package definePackage(ClassLoader cl, String name, Module module) {
2047                 return cl.definePackage(name, module);
2048             }
2049             public String fastUUID(long lsb, long msb) {
2050                 return Long.fastUUID(lsb, msb);
2051             }
2052             public void invalidatePackageAccessCache() {
2053                 SecurityManager.invalidatePackageAccessCache();
2054             }
2055         });
2056     }


  26 
  27 import java.io.BufferedInputStream;
  28 import java.io.BufferedOutputStream;
  29 import java.io.Console;
  30 import java.io.FileDescriptor;
  31 import java.io.FileInputStream;
  32 import java.io.FileOutputStream;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.PrintStream;
  36 import java.io.UnsupportedEncodingException;
  37 import java.lang.annotation.Annotation;
  38 import java.lang.reflect.Constructor;
  39 import java.lang.reflect.Executable;
  40 import java.lang.reflect.Layer;
  41 import java.lang.reflect.Method;
  42 import java.lang.reflect.Modifier;
  43 import java.lang.reflect.Module;
  44 import java.net.URL;
  45 import java.security.AccessControlContext;
  46 import java.security.ProtectionDomain;
  47 import java.util.Properties;
  48 import java.util.PropertyPermission;
  49 import java.util.Map;
  50 import java.security.AccessController;
  51 import java.security.PrivilegedAction;
  52 import java.nio.channels.Channel;
  53 import java.nio.channels.spi.SelectorProvider;
  54 import java.util.concurrent.ConcurrentHashMap;
  55 import java.util.stream.Stream;
  56 
  57 import java.util.Objects;
  58 import java.util.ResourceBundle;
  59 import java.util.function.Supplier;
  60 import sun.nio.ch.Interruptible;
  61 import jdk.internal.reflect.CallerSensitive;
  62 import jdk.internal.reflect.Reflection;
  63 import sun.security.util.SecurityConstants;
  64 import sun.reflect.annotation.AnnotationType;
  65 import jdk.internal.HotSpotIntrinsicCandidate;
  66 import jdk.internal.misc.JavaLangAccess;;


1829      *             <code>null</code>
1830      * @see        java.lang.System#loadLibrary(java.lang.String)
1831      * @see        java.lang.ClassLoader#findLibrary(java.lang.String)
1832      * @since      1.2
1833      */
1834     public static native String mapLibraryName(String libname);
1835 
1836     /**
1837      * Create PrintStream for stdout/err based on encoding.
1838      */
1839     private static PrintStream newPrintStream(FileOutputStream fos, String enc) {
1840        if (enc != null) {
1841             try {
1842                 return new PrintStream(new BufferedOutputStream(fos, 128), true, enc);
1843             } catch (UnsupportedEncodingException uee) {}
1844         }
1845         return new PrintStream(new BufferedOutputStream(fos, 128), true);
1846     }
1847 
1848     /**
1849      * Logs an exception/error at initialization time to stdout or stderr.
1850      *
1851      * @param printToStderr to print to stderr rather than stdout
1852      * @param printStackTrace to print the stack trace
1853      * @param msg the message to print before the exception, can be {@code null}
1854      * @param e the exception or error
1855      */
1856     private static void logInitException(boolean printToStderr,
1857                                          boolean printStackTrace,
1858                                          String msg,
1859                                          Throwable e) {
1860         if (VM.initLevel() < 1) {
1861             throw new InternalError("system classes not initialized");
1862         }
1863         PrintStream log = (printToStderr) ? err : out;
1864         if (msg != null) {
1865             log.println(msg);
1866         }
1867         if (printStackTrace) {
1868             e.printStackTrace(log);
1869         } else {
1870             log.println(e);
1871             for (Throwable suppressed : e.getSuppressed()) {
1872                 log.println("Suppressed: " + suppressed);
1873             }
1874             Throwable cause = e.getCause();
1875             if (cause != null) {
1876                 log.println("Caused by: " + cause);
1877             }
1878         }
1879     }
1880 
1881     /**
1882      * Initialize the system class.  Called after thread initialization.
1883      */
1884     private static void initPhase1() {
1885 
1886         // VM might invoke JNU_NewStringPlatform() to set those encoding
1887         // sensitive properties (user.home, user.name, boot.class.path, etc.)
1888         // during "props" initialization, in which it may need access, via
1889         // System.getProperty(), to the related system encoding property that
1890         // have been initialized (put into "props") at early stage of the
1891         // initialization. So make sure the "props" is available at the
1892         // very beginning of the initialization and all system properties to
1893         // be put into it directly.
1894         props = new Properties();
1895         initProperties(props);  // initialized by the VM
1896 
1897         // There are certain system configurations that may be controlled by
1898         // VM options such as the maximum amount of direct memory and
1899         // Integer cache size used to support the object identity semantics
1900         // of autoboxing.  Typically, the library will obtain these values
1901         // from the properties set by the VM.  If the properties are for


1939         current.getThreadGroup().add(current);
1940 
1941         // register shared secrets
1942         setJavaLangAccess();
1943 
1944         // Subsystems that are invoked during initialization can invoke
1945         // VM.isBooted() in order to avoid doing things that should
1946         // wait until the VM is fully initialized. The initialization level
1947         // is incremented from 0 to 1 here to indicate the first phase of
1948         // initialization has completed.
1949         // IMPORTANT: Ensure that this remains the last initialization action!
1950         VM.initLevel(1);
1951     }
1952 
1953     // @see #initPhase2()
1954     private static Layer bootLayer;
1955 
1956     /*
1957      * Invoked by VM.  Phase 2 module system initialization.
1958      * Only classes in java.base can be loaded in this phase.
1959      *
1960      * @param printToStderr print exceptions to stderr rather than stdout
1961      * @param printStackTrace print stack trace when exception occurs
1962      *
1963      * @return JNI_OK for success, JNI_ERR for failure
1964      */
1965     private static int initPhase2(boolean printToStderr, boolean printStackTrace) {
1966         try {
1967             bootLayer = ModuleBootstrap.boot();
1968         } catch (Exception | Error e) {
1969             logInitException(printToStderr, printStackTrace,
1970                              "Error occurred during initialization of boot layer", e);
1971             return -1; // JNI_ERR
1972         }
1973 
1974         // module system initialized
1975         VM.initLevel(2);
1976 
1977         return 0; // JNI_OK
1978     }
1979 
1980     /*
1981      * Invoked by VM.  Phase 3 is the final system initialization:
1982      * 1. set security manager
1983      * 2. set system class loader
1984      * 3. set TCCL
1985      *
1986      * This method must be called after the module system initialization.
1987      * The security manager and system class loader may be custom class from
1988      * the application classpath or modulepath.
1989      */
1990     private static void initPhase3() {
1991         // set security manager
1992         String cn = System.getProperty("java.security.manager");
1993         if (cn != null) {
1994             if (cn.isEmpty() || "default".equals(cn)) {
1995                 System.setSecurityManager(new SecurityManager());
1996             } else {
1997                 try {


2063                 t.blockedOn(b);
2064             }
2065             public void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook) {
2066                 Shutdown.add(slot, registerShutdownInProgress, hook);
2067             }
2068             public String newStringUnsafe(char[] chars) {
2069                 return new String(chars, true);
2070             }
2071             public Thread newThreadWithAcc(Runnable target, AccessControlContext acc) {
2072                 return new Thread(target, acc);
2073             }
2074             public void invokeFinalize(Object o) throws Throwable {
2075                 o.finalize();
2076             }
2077             public Layer getBootLayer() {
2078                 return bootLayer;
2079             }
2080             public ConcurrentHashMap<?, ?> createOrGetClassLoaderValueMap(ClassLoader cl) {
2081                 return cl.createOrGetClassLoaderValueMap();
2082             }
2083             public Class<?> defineClass(ClassLoader loader, String name, byte[] b, ProtectionDomain pd, String source) {
2084                 return ClassLoader.defineClass1(loader, name, b, 0, b.length, pd, source);
2085             }
2086             public Class<?> findBootstrapClassOrNull(ClassLoader cl, String name) {
2087                 return cl.findBootstrapClassOrNull(name);
2088             }
2089             public URL findResource(ClassLoader cl, String mn, String name) throws IOException {
2090                 return cl.findResource(mn, name);
2091             }
2092             public Stream<Package> packages(ClassLoader cl) {
2093                 return cl.packages();
2094             }
2095             public Package definePackage(ClassLoader cl, String name, Module module) {
2096                 return cl.definePackage(name, module);
2097             }
2098             public String fastUUID(long lsb, long msb) {
2099                 return Long.fastUUID(lsb, msb);
2100             }
2101             public void invalidatePackageAccessCache() {
2102                 SecurityManager.invalidatePackageAccessCache();
2103             }
2104         });
2105     }
< prev index next >