< prev index next >

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

Print this page

  54 import java.security.AccessController;
  55 import java.security.CodeSource;
  56 import java.security.PrivilegedAction;
  57 import java.security.ProtectionDomain;
  58 import java.util.Collections;
  59 import java.util.List;
  60 import java.util.Locale;
  61 import java.util.Map;
  62 import java.util.Objects;
  63 import java.util.Properties;
  64 import java.util.PropertyPermission;
  65 import java.util.ResourceBundle;
  66 import java.util.Set;
  67 import java.util.WeakHashMap;
  68 import java.util.concurrent.Callable;
  69 import java.util.function.Supplier;
  70 import java.util.concurrent.ConcurrentHashMap;
  71 import java.util.stream.Stream;
  72 
  73 import jdk.internal.logger.LoggerFinderLoader.TemporaryLoggerFinder;

  74 import jdk.internal.misc.CarrierThreadLocal;
  75 import jdk.internal.misc.Unsafe;
  76 import jdk.internal.util.StaticProperty;
  77 import jdk.internal.module.ModuleBootstrap;
  78 import jdk.internal.module.ServicesCatalog;
  79 import jdk.internal.reflect.CallerSensitive;
  80 import jdk.internal.reflect.Reflection;
  81 import jdk.internal.access.JavaLangAccess;
  82 import jdk.internal.access.SharedSecrets;
  83 import jdk.internal.javac.PreviewFeature;
  84 import jdk.internal.logger.LoggerFinderLoader;
  85 import jdk.internal.logger.LazyLoggers;
  86 import jdk.internal.logger.LocalizedLoggerWrapper;
  87 import jdk.internal.misc.VM;
  88 import jdk.internal.util.SystemProps;
  89 import jdk.internal.vm.Continuation;
  90 import jdk.internal.vm.ContinuationScope;
  91 import jdk.internal.vm.StackableScope;
  92 import jdk.internal.vm.ThreadContainer;
  93 import jdk.internal.vm.annotation.IntrinsicCandidate;

2174         // from the properties set by the VM.  If the properties are for
2175         // internal implementation use only, these properties should be
2176         // masked from the system properties.
2177         //
2178         // Save a private copy of the system properties object that
2179         // can only be accessed by the internal implementation.
2180         VM.saveProperties(tempProps);
2181         props = createProperties(tempProps);
2182 
2183         // Check if sun.jnu.encoding is supported. If not, replace it with UTF-8.
2184         var jnuEncoding = props.getProperty("sun.jnu.encoding");
2185         if (jnuEncoding == null || !Charset.isSupported(jnuEncoding)) {
2186             notSupportedJnuEncoding = jnuEncoding == null ? "null" : jnuEncoding;
2187             props.setProperty("sun.jnu.encoding", "UTF-8");
2188         }
2189 
2190         StaticProperty.javaHome();          // Load StaticProperty to cache the property values
2191 
2192         lineSeparator = props.getProperty("line.separator");
2193 
2194         FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
2195         FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
2196         FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
2197         initialIn = new BufferedInputStream(fdIn);
2198         setIn0(initialIn);
2199         // stdout/err.encoding are set when the VM is associated with the terminal,
2200         // thus they are equivalent to Console.charset(), otherwise the encodings
2201         // of those properties default to native.encoding
2202         setOut0(newPrintStream(fdOut, props.getProperty("stdout.encoding")));
2203         setErr0(newPrintStream(fdErr, props.getProperty("stderr.encoding")));
2204 
2205         // Setup Java signal handlers for HUP, TERM, and INT (where available).
2206         Terminator.setup();
2207 
2208         // Initialize any miscellaneous operating system settings that need to be
2209         // set for the class libraries. Currently this is no-op everywhere except
2210         // for Windows where the process-wide error mode is set before the java.io
2211         // classes are used.
2212         VM.initializeOSEnvironment();
2213 
2214         // start Finalizer and Reference Handler threads
2215         SharedSecrets.getJavaLangRefAccess().startThreads();
2216 
2217         // system properties, java.lang and other core classes are now initialized
2218         VM.initLevel(1);
2219     }
2220 














































































2221     // @see #initPhase2()
2222     static ModuleLayer bootLayer;
2223 
2224     /*
2225      * Invoked by VM.  Phase 2 module system initialization.
2226      * Only classes in java.base can be loaded in this phase.
2227      *
2228      * @param printToStderr print exceptions to stderr rather than stdout
2229      * @param printStackTrace print stack trace when exception occurs
2230      *
2231      * @return JNI_OK for success, JNI_ERR for failure
2232      */
2233     private static int initPhase2(boolean printToStderr, boolean printStackTrace) {
2234 
2235         try {
2236             bootLayer = ModuleBootstrap.boot();
2237         } catch (Exception | Error e) {
2238             logInitException(printToStderr, printStackTrace,
2239                              "Error occurred during initialization of boot layer", e);
2240             return -1; // JNI_ERR

2580             public ThreadContainer threadContainer(Thread thread) {
2581                 return thread.threadContainer();
2582             }
2583 
2584             public void start(Thread thread, ThreadContainer container) {
2585                 thread.start(container);
2586             }
2587 
2588             public StackableScope headStackableScope(Thread thread) {
2589                 return thread.headStackableScopes();
2590             }
2591 
2592             public void setHeadStackableScope(StackableScope scope) {
2593                 Thread.setHeadStackableScope(scope);
2594             }
2595 
2596             public Thread currentCarrierThread() {
2597                 return Thread.currentCarrierThread();
2598             }
2599 
2600             public <V> V executeOnCarrierThread(Callable<V> task) throws Exception {
2601                 if (Thread.currentThread() instanceof VirtualThread vthread) {
2602                     return vthread.executeOnCarrierThread(task);
2603                 } else {
2604                     return task.call();
2605                 }
2606             }
2607 
2608             public <T> T getCarrierThreadLocal(CarrierThreadLocal<T> local) {
2609                 return ((ThreadLocal<T>)local).getCarrierThreadLocal();
2610             }
2611 
2612             public <T> void setCarrierThreadLocal(CarrierThreadLocal<T> local, T value) {
2613                 ((ThreadLocal<T>)local).setCarrierThreadLocal(value);
2614             }
2615 
2616             public void removeCarrierThreadLocal(CarrierThreadLocal<?> local) {
2617                 ((ThreadLocal<?>)local).removeCarrierThreadLocal();
2618             }
2619 
2620             public boolean isCarrierThreadLocalPresent(CarrierThreadLocal<?> local) {
2621                 return ((ThreadLocal<?>)local).isCarrierThreadLocalPresent();
2622             }
2623 
2624             public Object[] scopedValueCache() {
2625                 return Thread.scopedValueCache();
2626             }
2627 

  54 import java.security.AccessController;
  55 import java.security.CodeSource;
  56 import java.security.PrivilegedAction;
  57 import java.security.ProtectionDomain;
  58 import java.util.Collections;
  59 import java.util.List;
  60 import java.util.Locale;
  61 import java.util.Map;
  62 import java.util.Objects;
  63 import java.util.Properties;
  64 import java.util.PropertyPermission;
  65 import java.util.ResourceBundle;
  66 import java.util.Set;
  67 import java.util.WeakHashMap;
  68 import java.util.concurrent.Callable;
  69 import java.util.function.Supplier;
  70 import java.util.concurrent.ConcurrentHashMap;
  71 import java.util.stream.Stream;
  72 
  73 import jdk.internal.logger.LoggerFinderLoader.TemporaryLoggerFinder;
  74 import jdk.internal.misc.Blocker;
  75 import jdk.internal.misc.CarrierThreadLocal;
  76 import jdk.internal.misc.Unsafe;
  77 import jdk.internal.util.StaticProperty;
  78 import jdk.internal.module.ModuleBootstrap;
  79 import jdk.internal.module.ServicesCatalog;
  80 import jdk.internal.reflect.CallerSensitive;
  81 import jdk.internal.reflect.Reflection;
  82 import jdk.internal.access.JavaLangAccess;
  83 import jdk.internal.access.SharedSecrets;
  84 import jdk.internal.javac.PreviewFeature;
  85 import jdk.internal.logger.LoggerFinderLoader;
  86 import jdk.internal.logger.LazyLoggers;
  87 import jdk.internal.logger.LocalizedLoggerWrapper;
  88 import jdk.internal.misc.VM;
  89 import jdk.internal.util.SystemProps;
  90 import jdk.internal.vm.Continuation;
  91 import jdk.internal.vm.ContinuationScope;
  92 import jdk.internal.vm.StackableScope;
  93 import jdk.internal.vm.ThreadContainer;
  94 import jdk.internal.vm.annotation.IntrinsicCandidate;

2175         // from the properties set by the VM.  If the properties are for
2176         // internal implementation use only, these properties should be
2177         // masked from the system properties.
2178         //
2179         // Save a private copy of the system properties object that
2180         // can only be accessed by the internal implementation.
2181         VM.saveProperties(tempProps);
2182         props = createProperties(tempProps);
2183 
2184         // Check if sun.jnu.encoding is supported. If not, replace it with UTF-8.
2185         var jnuEncoding = props.getProperty("sun.jnu.encoding");
2186         if (jnuEncoding == null || !Charset.isSupported(jnuEncoding)) {
2187             notSupportedJnuEncoding = jnuEncoding == null ? "null" : jnuEncoding;
2188             props.setProperty("sun.jnu.encoding", "UTF-8");
2189         }
2190 
2191         StaticProperty.javaHome();          // Load StaticProperty to cache the property values
2192 
2193         lineSeparator = props.getProperty("line.separator");
2194 
2195         FileInputStream fdIn = new In(FileDescriptor.in);
2196         FileOutputStream fdOut = new Out(FileDescriptor.out);
2197         FileOutputStream fdErr = new Out(FileDescriptor.err);
2198         initialIn = new BufferedInputStream(fdIn);
2199         setIn0(initialIn);
2200         // stdout/err.encoding are set when the VM is associated with the terminal,
2201         // thus they are equivalent to Console.charset(), otherwise the encodings
2202         // of those properties default to native.encoding
2203         setOut0(newPrintStream(fdOut, props.getProperty("stdout.encoding")));
2204         setErr0(newPrintStream(fdErr, props.getProperty("stderr.encoding")));
2205 
2206         // Setup Java signal handlers for HUP, TERM, and INT (where available).
2207         Terminator.setup();
2208 
2209         // Initialize any miscellaneous operating system settings that need to be
2210         // set for the class libraries. Currently this is no-op everywhere except
2211         // for Windows where the process-wide error mode is set before the java.io
2212         // classes are used.
2213         VM.initializeOSEnvironment();
2214 
2215         // start Finalizer and Reference Handler threads
2216         SharedSecrets.getJavaLangRefAccess().startThreads();
2217 
2218         // system properties, java.lang and other core classes are now initialized
2219         VM.initLevel(1);
2220     }
2221 
2222     /**
2223      * System.in.
2224      */
2225     private static class In extends FileInputStream {
2226         In(FileDescriptor fd) {
2227             super(fd);
2228         }
2229 
2230         @Override
2231         public int read() throws IOException {
2232             boolean attempted = Blocker.begin();
2233             try {
2234                 return super.read();
2235             } finally {
2236                 Blocker.end(attempted);
2237             }
2238         }
2239 
2240         @Override
2241         public int read(byte[] b) throws IOException {
2242             boolean attempted = Blocker.begin();
2243             try {
2244                 return super.read(b);
2245             } finally {
2246                 Blocker.end(attempted);
2247             }
2248         }
2249 
2250         @Override
2251         public int read(byte[] b, int off, int len) throws IOException {
2252             boolean attempted = Blocker.begin();
2253             try {
2254                 return super.read(b, off, len);
2255             } finally {
2256                 Blocker.end(attempted);
2257             }
2258         }
2259     }
2260 
2261     /**
2262      * System.out/System.err wrap this output stream.
2263      */
2264     private static class Out extends FileOutputStream {
2265         Out(FileDescriptor fd) {
2266             super(fd);
2267         }
2268 
2269         @Override
2270         public void write(int b) throws IOException {
2271             boolean attempted = Blocker.begin();
2272             try {
2273                 super.write(b);
2274             } finally {
2275                 Blocker.end(attempted);
2276             }
2277         }
2278 
2279         @Override
2280         public void write(byte[] b) throws IOException {
2281             boolean attempted = Blocker.begin();
2282             try {
2283                 super.write(b);
2284             } finally {
2285                 Blocker.end(attempted);
2286             }
2287         }
2288 
2289         @Override
2290         public void write(byte[] b, int off, int len) throws IOException {
2291             boolean attempted = Blocker.begin();
2292             try {
2293                 super.write(b, off, len);
2294             } finally {
2295                 Blocker.end(attempted);
2296             }
2297         }
2298     }
2299 
2300     // @see #initPhase2()
2301     static ModuleLayer bootLayer;
2302 
2303     /*
2304      * Invoked by VM.  Phase 2 module system initialization.
2305      * Only classes in java.base can be loaded in this phase.
2306      *
2307      * @param printToStderr print exceptions to stderr rather than stdout
2308      * @param printStackTrace print stack trace when exception occurs
2309      *
2310      * @return JNI_OK for success, JNI_ERR for failure
2311      */
2312     private static int initPhase2(boolean printToStderr, boolean printStackTrace) {
2313 
2314         try {
2315             bootLayer = ModuleBootstrap.boot();
2316         } catch (Exception | Error e) {
2317             logInitException(printToStderr, printStackTrace,
2318                              "Error occurred during initialization of boot layer", e);
2319             return -1; // JNI_ERR

2659             public ThreadContainer threadContainer(Thread thread) {
2660                 return thread.threadContainer();
2661             }
2662 
2663             public void start(Thread thread, ThreadContainer container) {
2664                 thread.start(container);
2665             }
2666 
2667             public StackableScope headStackableScope(Thread thread) {
2668                 return thread.headStackableScopes();
2669             }
2670 
2671             public void setHeadStackableScope(StackableScope scope) {
2672                 Thread.setHeadStackableScope(scope);
2673             }
2674 
2675             public Thread currentCarrierThread() {
2676                 return Thread.currentCarrierThread();
2677             }
2678 








2679             public <T> T getCarrierThreadLocal(CarrierThreadLocal<T> local) {
2680                 return ((ThreadLocal<T>)local).getCarrierThreadLocal();
2681             }
2682 
2683             public <T> void setCarrierThreadLocal(CarrierThreadLocal<T> local, T value) {
2684                 ((ThreadLocal<T>)local).setCarrierThreadLocal(value);
2685             }
2686 
2687             public void removeCarrierThreadLocal(CarrierThreadLocal<?> local) {
2688                 ((ThreadLocal<?>)local).removeCarrierThreadLocal();
2689             }
2690 
2691             public boolean isCarrierThreadLocalPresent(CarrierThreadLocal<?> local) {
2692                 return ((ThreadLocal<?>)local).isCarrierThreadLocalPresent();
2693             }
2694 
2695             public Object[] scopedValueCache() {
2696                 return Thread.scopedValueCache();
2697             }
2698 
< prev index next >