< prev index next >

src/java.base/share/classes/java/security/Security.java

Print this page




  49 public final class Security {
  50 
  51     /* Are we debugging? -- for developers */
  52     private static final Debug sdebug =
  53                         Debug.getInstance("properties");
  54 
  55     /* The java.security properties */
  56     private static Properties props;
  57 
  58     // An element in the cache
  59     private static class ProviderProperty {
  60         String className;
  61         Provider provider;
  62     }
  63 
  64     static {
  65         // doPrivileged here because there are multiple
  66         // things in initialize that might require privs.
  67         // (the FileInputStream call and the File.exists call,
  68         // the securityPropFile call, etc)
  69         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  70             public Void run() {
  71                 initialize();
  72                 return null;
  73             }
  74         });
  75     }
  76 
  77     private static void initialize() {
  78         props = new Properties();
  79         boolean loadedProps = false;
  80         boolean overrideAll = false;
  81 
  82         // first load the system properties file
  83         // to determine the value of security.overridePropertiesFile
  84         File propFile = securityPropFile("java.security");
  85         if (propFile.exists()) {
  86             InputStream is = null;
  87             try {
  88                 FileInputStream fis = new FileInputStream(propFile);
  89                 is = new BufferedInputStream(fis);


 793         props.put(key, datum);
 794         invalidateSMCache(key);  /* See below. */
 795     }
 796 
 797     /*
 798      * Implementation detail:  If the property we just set in
 799      * setProperty() was either "package.access" or
 800      * "package.definition", we need to signal to the SecurityManager
 801      * class that the value has just changed, and that it should
 802      * invalidate it's local cache values.
 803      *
 804      * Rather than create a new API entry for this function,
 805      * we use reflection to set a private variable.
 806      */
 807     private static void invalidateSMCache(String key) {
 808 
 809         final boolean pa = key.equals("package.access");
 810         final boolean pd = key.equals("package.definition");
 811 
 812         if (pa || pd) {
 813             AccessController.doPrivileged(new PrivilegedAction<Void>() {
 814                 public Void run() {
 815                     try {
 816                         /* Get the class via the bootstrap class loader. */
 817                         Class<?> cl = Class.forName(
 818                             "java.lang.SecurityManager", false, null);
 819                         Field f = null;
 820                         boolean accessible = false;
 821 
 822                         if (pa) {
 823                             f = cl.getDeclaredField("packageAccessValid");
 824                             accessible = f.isAccessible();
 825                             f.setAccessible(true);
 826                         } else {
 827                             f = cl.getDeclaredField("packageDefinitionValid");
 828                             accessible = f.isAccessible();
 829                             f.setAccessible(true);
 830                         }
 831                         f.setBoolean(f, false);
 832                         f.setAccessible(accessible);
 833                     }




  49 public final class Security {
  50 
  51     /* Are we debugging? -- for developers */
  52     private static final Debug sdebug =
  53                         Debug.getInstance("properties");
  54 
  55     /* The java.security properties */
  56     private static Properties props;
  57 
  58     // An element in the cache
  59     private static class ProviderProperty {
  60         String className;
  61         Provider provider;
  62     }
  63 
  64     static {
  65         // doPrivileged here because there are multiple
  66         // things in initialize that might require privs.
  67         // (the FileInputStream call and the File.exists call,
  68         // the securityPropFile call, etc)
  69         AccessController.doPrivileged(new PrivilegedAction<>() {
  70             public Void run() {
  71                 initialize();
  72                 return null;
  73             }
  74         });
  75     }
  76 
  77     private static void initialize() {
  78         props = new Properties();
  79         boolean loadedProps = false;
  80         boolean overrideAll = false;
  81 
  82         // first load the system properties file
  83         // to determine the value of security.overridePropertiesFile
  84         File propFile = securityPropFile("java.security");
  85         if (propFile.exists()) {
  86             InputStream is = null;
  87             try {
  88                 FileInputStream fis = new FileInputStream(propFile);
  89                 is = new BufferedInputStream(fis);


 793         props.put(key, datum);
 794         invalidateSMCache(key);  /* See below. */
 795     }
 796 
 797     /*
 798      * Implementation detail:  If the property we just set in
 799      * setProperty() was either "package.access" or
 800      * "package.definition", we need to signal to the SecurityManager
 801      * class that the value has just changed, and that it should
 802      * invalidate it's local cache values.
 803      *
 804      * Rather than create a new API entry for this function,
 805      * we use reflection to set a private variable.
 806      */
 807     private static void invalidateSMCache(String key) {
 808 
 809         final boolean pa = key.equals("package.access");
 810         final boolean pd = key.equals("package.definition");
 811 
 812         if (pa || pd) {
 813             AccessController.doPrivileged(new PrivilegedAction<>() {
 814                 public Void run() {
 815                     try {
 816                         /* Get the class via the bootstrap class loader. */
 817                         Class<?> cl = Class.forName(
 818                             "java.lang.SecurityManager", false, null);
 819                         Field f = null;
 820                         boolean accessible = false;
 821 
 822                         if (pa) {
 823                             f = cl.getDeclaredField("packageAccessValid");
 824                             accessible = f.isAccessible();
 825                             f.setAccessible(true);
 826                         } else {
 827                             f = cl.getDeclaredField("packageDefinitionValid");
 828                             accessible = f.isAccessible();
 829                             f.setAccessible(true);
 830                         }
 831                         f.setBoolean(f, false);
 832                         f.setAccessible(accessible);
 833                     }


< prev index next >