< prev index next >

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

Print this page

     336      * <p> Otherwise, the argument is established as the current
     337      * security manager. If the argument is {@code null} and no
     338      * security manager has been established, then no action is taken and
     339      * the method simply returns.
     340      *
     341      * @implNote In the JDK implementation, if the Java virtual machine is
     342      * started with the system property {@code java.security.manager} set to
     343      * the special token "{@code disallow}" then the {@code setSecurityManager}
     344      * method cannot be used to set a security manager.
     345      *
     346      * @param  sm the security manager or {@code null}
     347      * @throws SecurityException
     348      *         if the security manager has already been set and its {@code
     349      *         checkPermission} method doesn't allow it to be replaced
     350      * @throws UnsupportedOperationException
     351      *         if {@code sm} is non-null and a security manager is not allowed
     352      *         to be set dynamically
     353      * @see #getSecurityManager
     354      * @see SecurityManager#checkPermission
     355      * @see java.lang.RuntimePermission

     356      */

     357     public static void setSecurityManager(SecurityManager sm) {
     358         if (allowSecurityManager()) {
     359             if (security == null) {
     360                 // ensure image reader is initialized
     361                 Object.class.getResource("java/lang/ANY");
     362                 // ensure the default file system is initialized
     363                 DefaultFileSystemProvider.theFileSystem();
     364             }
     365             if (sm != null) {
     366                 try {
     367                     // pre-populates the SecurityManager.packageAccess cache
     368                     // to avoid recursive permission checking issues with custom
     369                     // SecurityManager implementations
     370                     sm.checkPackageAccess("java.lang");
     371                 } catch (Exception e) {
     372                     // no-op
     373                 }
     374             }
     375             setSecurityManager0(sm);
     376         } else {
     402             // (in this case: the new security manager class) on the stack).
     403             AccessController.doPrivileged(new PrivilegedAction<>() {
     404                 public Object run() {
     405                     s.getClass().getProtectionDomain().implies
     406                         (SecurityConstants.ALL_PERMISSION);
     407                     return null;
     408                 }
     409             });
     410         }
     411
     412         security = s;
     413     }
     414
     415     /**
     416      * Gets the system-wide security manager.
     417      *
     418      * @return  if a security manager has already been established for the
     419      *          current application, then that security manager is returned;
     420      *          otherwise, {@code null} is returned.
     421      * @see     #setSecurityManager

     422      */

     423     public static SecurityManager getSecurityManager() {
     424         if (allowSecurityManager()) {
     425             return security;
     426         } else {
     427             return null;
     428         }
     429     }
     430
     431     /**
     432      * Returns the current time in milliseconds.  Note that
     433      * while the unit of time of the return value is a millisecond,
     434      * the granularity of the value depends on the underlying
     435      * operating system and may be larger.  For example, many
     436      * operating systems measure time in units of tens of
     437      * milliseconds.
     438      *
     439      * <p> See the description of the class {@code Date} for
     440      * a discussion of slight discrepancies that may arise between
     441      * "computer time" and coordinated universal time (UTC).
     442      *
    2131                         Class<?> c = Class.forName(smProp, false, cl);
    2132                         Constructor<?> ctor = c.getConstructor();
    2133                         // Must be a public subclass of SecurityManager with
    2134                         // a public no-arg constructor
    2135                         if (!SecurityManager.class.isAssignableFrom(c) ||
    2136                             !Modifier.isPublic(c.getModifiers()) ||
    2137                             !Modifier.isPublic(ctor.getModifiers())) {
    2138                             throw new Error("Could not create SecurityManager: "
    2139                                              + ctor.toString());
    2140                         }
    2141                         // custom security manager may be in non-exported package
    2142                         ctor.setAccessible(true);
    2143                         SecurityManager sm = (SecurityManager) ctor.newInstance();
    2144                         setSecurityManager(sm);
    2145                     } catch (Exception e) {
    2146                         throw new InternalError("Could not create SecurityManager", e);
    2147                     }
    2148                     allowSecurityManager = MAYBE;
    2149             }
    2150         } else {
    2151             allowSecurityManager = MAYBE;

    2152         }
    2153
    2154         // initializing the system class loader
    2155         VM.initLevel(3);
    2156
    2157         // system class loader initialized
    2158         ClassLoader scl = ClassLoader.initSystemClassLoader();
    2159
    2160         // set TCCL
    2161         Thread.currentThread().setContextClassLoader(scl);
    2162
    2163         // system is fully initialized
    2164         VM.initLevel(4);
    2165     }
    2166
    2167     private static void setJavaLangAccess() {
    2168         // Allow privileged classes outside of java.lang
    2169         SharedSecrets.setJavaLangAccess(new JavaLangAccess() {
    2170             public List<Method> getDeclaredPublicMethods(Class<?> klass, String name, Class<?>... parameterTypes) {
    2171                 return klass.getDeclaredPublicMethods(name, parameterTypes);
     336      * <p> Otherwise, the argument is established as the current
     337      * security manager. If the argument is {@code null} and no
     338      * security manager has been established, then no action is taken and
     339      * the method simply returns.
     340      *
     341      * @implNote In the JDK implementation, if the Java virtual machine is
     342      * started with the system property {@code java.security.manager} set to
     343      * the special token "{@code disallow}" then the {@code setSecurityManager}
     344      * method cannot be used to set a security manager.
     345      *
     346      * @param  sm the security manager or {@code null}
     347      * @throws SecurityException
     348      *         if the security manager has already been set and its {@code
     349      *         checkPermission} method doesn't allow it to be replaced
     350      * @throws UnsupportedOperationException
     351      *         if {@code sm} is non-null and a security manager is not allowed
     352      *         to be set dynamically
     353      * @see #getSecurityManager
     354      * @see SecurityManager#checkPermission
     355      * @see java.lang.RuntimePermission
     356      * @deprecated This method is only useful in conjunction with
     357      *       {@linkplain SecurityManager the Security Manager}, which is
     358      *       deprecated and subject to removal in a future release.
     359      *       Consequently, this method is also deprecated and subject to
     360      *       removal. There is no replacement for the Security Manager or this
     361      *       method.
     362      */
     363     @Deprecated(since="17", forRemoval=true)
     364     public static void setSecurityManager(SecurityManager sm) {
     365         if (allowSecurityManager()) {
     366             if (security == null) {
     367                 // ensure image reader is initialized
     368                 Object.class.getResource("java/lang/ANY");
     369                 // ensure the default file system is initialized
     370                 DefaultFileSystemProvider.theFileSystem();
     371             }
     372             if (sm != null) {
     373                 try {
     374                     // pre-populates the SecurityManager.packageAccess cache
     375                     // to avoid recursive permission checking issues with custom
     376                     // SecurityManager implementations
     377                     sm.checkPackageAccess("java.lang");
     378                 } catch (Exception e) {
     379                     // no-op
     380                 }
     381             }
     382             setSecurityManager0(sm);
     383         } else {
     409             // (in this case: the new security manager class) on the stack).
     410             AccessController.doPrivileged(new PrivilegedAction<>() {
     411                 public Object run() {
     412                     s.getClass().getProtectionDomain().implies
     413                         (SecurityConstants.ALL_PERMISSION);
     414                     return null;
     415                 }
     416             });
     417         }
     418
     419         security = s;
     420     }
     421
     422     /**
     423      * Gets the system-wide security manager.
     424      *
     425      * @return  if a security manager has already been established for the
     426      *          current application, then that security manager is returned;
     427      *          otherwise, {@code null} is returned.
     428      * @see     #setSecurityManager
     429      * @deprecated This method is only useful in conjunction with
     430      *       {@linkplain SecurityManager the Security Manager}, which is
     431      *       deprecated and subject to removal in a future release.
     432      *       Consequently, this method is also deprecated and subject to
     433      *       removal. There is no replacement for the Security Manager or this
     434      *       method.
     435      */
     436     @Deprecated(since="17", forRemoval=true)
     437     public static SecurityManager getSecurityManager() {
     438         if (allowSecurityManager()) {
     439             return security;
     440         } else {
     441             return null;
     442         }
     443     }
     444
     445     /**
     446      * Returns the current time in milliseconds.  Note that
     447      * while the unit of time of the return value is a millisecond,
     448      * the granularity of the value depends on the underlying
     449      * operating system and may be larger.  For example, many
     450      * operating systems measure time in units of tens of
     451      * milliseconds.
     452      *
     453      * <p> See the description of the class {@code Date} for
     454      * a discussion of slight discrepancies that may arise between
     455      * "computer time" and coordinated universal time (UTC).
     456      *
    2145                         Class<?> c = Class.forName(smProp, false, cl);
    2146                         Constructor<?> ctor = c.getConstructor();
    2147                         // Must be a public subclass of SecurityManager with
    2148                         // a public no-arg constructor
    2149                         if (!SecurityManager.class.isAssignableFrom(c) ||
    2150                             !Modifier.isPublic(c.getModifiers()) ||
    2151                             !Modifier.isPublic(ctor.getModifiers())) {
    2152                             throw new Error("Could not create SecurityManager: "
    2153                                              + ctor.toString());
    2154                         }
    2155                         // custom security manager may be in non-exported package
    2156                         ctor.setAccessible(true);
    2157                         SecurityManager sm = (SecurityManager) ctor.newInstance();
    2158                         setSecurityManager(sm);
    2159                     } catch (Exception e) {
    2160                         throw new InternalError("Could not create SecurityManager", e);
    2161                     }
    2162                     allowSecurityManager = MAYBE;
    2163             }
    2164         } else {
    2165             allowSecurityManager = NEVER;
    2166         }
    2167
    2168         if (allowSecurityManager != NEVER) {
    2169             System.err.println("WARNING: The Security Manager is deprecated" +
    2170                     " and will be removed in a future release.");
    2171         }
    2172
    2173         // initializing the system class loader
    2174         VM.initLevel(3);
    2175
    2176         // system class loader initialized
    2177         ClassLoader scl = ClassLoader.initSystemClassLoader();
    2178
    2179         // set TCCL
    2180         Thread.currentThread().setContextClassLoader(scl);
    2181
    2182         // system is fully initialized
    2183         VM.initLevel(4);
    2184     }
    2185
    2186     private static void setJavaLangAccess() {
    2187         // Allow privileged classes outside of java.lang
    2188         SharedSecrets.setJavaLangAccess(new JavaLangAccess() {
    2189             public List<Method> getDeclaredPublicMethods(Class<?> klass, String name, Class<?>... parameterTypes) {
    2190                 return klass.getDeclaredPublicMethods(name, parameterTypes);

< prev index next >