< prev index next >

src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.xml.internal;
  26 
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import java.io.FileNotFoundException;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 import java.security.PrivilegedActionException;
  35 import java.security.PrivilegedExceptionAction;
  36 import java.text.MessageFormat;

  37 import java.util.Locale;

  38 import java.util.Properties;
  39 import java.util.ResourceBundle;
  40 
  41 /**
  42  * This class contains utility methods for reading resources in the JAXP packages
  43  */
  44 public class SecuritySupport {


  45     /**
  46      * Cache for properties in java.home/conf/jaxp.properties
  47      */
  48     static final Properties cacheProps = new Properties();
  49 
  50     /**
  51      * Flag indicating whether java.home/conf/jaxp.properties has been read
  52      */
  53     static volatile boolean firstTime = true;
  54 
  55     private SecuritySupport() {}
  56 
  57     public static String getErrorMessage(Locale locale, String bundle, String key,
  58             Object[] arguments) {
  59         ResourceBundle rb;
  60         if (locale != null) {
  61             rb = ResourceBundle.getBundle(bundle,locale);
  62         } else {
  63             rb = ResourceBundle.getBundle(bundle);
  64         }
  65 
  66         String msg = rb.getString(key);
  67         if (arguments != null) {
  68             msg = MessageFormat.format(msg, arguments);
  69         }
  70         return msg;
  71     }
  72 
  73     /**
  74      * Reads JAXP system property with privilege
  75      *
  76      * @param propName the name of the property
  77      * @return the value of the property
  78      */
  79     public static String getSystemProperty(final String propName) {
  80         return
  81         AccessController.doPrivileged(
  82                 (PrivilegedAction<String>) () -> (String)System.getProperty(propName));
  83     }
  84 
  85     /**
  86      * Reads a system property.






































  87      *
  88      * @param <T> the type of the property value
  89      * @param type the type of the property value
  90      * @param propName the name of the property
  91      * @param defValue the default value
  92      * @return the value of the property, or the default value of no system
  93      * property is found
  94      */
  95     public static <T> T getJAXPSystemProperty(Class<T> type, String propName, String defValue) {
  96         String value = getJAXPSystemProperty(propName);
  97         if (value == null) {
  98             value = defValue;
  99         }
 100         if (Integer.class.isAssignableFrom(type)) {
 101             return type.cast(Integer.parseInt(value));
 102         } else if (Boolean.class.isAssignableFrom(type)) {
 103             return type.cast(Boolean.parseBoolean(value));
 104         }
 105         return type.cast(value);
 106     }
 107 
 108     /**
 109      * Reads JAXP system property in this order: system property,
 110      * $java.home/conf/jaxp.properties if the system property is not specified
 111      *
 112      * @param propName the name of the property


 119         }
 120         return value;
 121     }
 122 
 123     /**
 124      * Reads the specified property from $java.home/conf/jaxp.properties
 125      *
 126      * @param propName the name of the property
 127      * @return the value of the property
 128      */
 129     public static String readJAXPProperty(String propName) {
 130         String value = null;
 131         InputStream is = null;
 132         try {
 133             if (firstTime) {
 134                 synchronized (cacheProps) {
 135                     if (firstTime) {
 136                         String configFile = getSystemProperty("java.home") + File.separator
 137                                 + "conf" + File.separator + "jaxp.properties";
 138                         File f = new File(configFile);
 139                         if (getFileExists(f)) {
 140                             is = getFileInputStream(f);
 141                             cacheProps.load(is);
 142                         }
 143                         firstTime = false;
 144                     }
 145                 }
 146             }
 147             value = cacheProps.getProperty(propName);
 148 
 149         } catch (IOException ex) {
 150         } finally {
 151             if (is != null) {
 152                 try {
 153                     is.close();
 154                 } catch (IOException ex) {}
 155             }
 156         }
 157 
 158         return value;
 159     }
 160 
 161 //------------------- private methods ---------------------------
 162     static boolean getFileExists(final File f) {




 163         return (AccessController.doPrivileged((PrivilegedAction<Boolean>) ()
 164                 -> f.exists() ? Boolean.TRUE : Boolean.FALSE));
 165     }
 166 
 167     static FileInputStream getFileInputStream(final File file)











 168             throws FileNotFoundException {
 169         try {
 170             return AccessController.doPrivileged((PrivilegedExceptionAction<FileInputStream>) ()
 171                     -> new FileInputStream(file));
 172         } catch (PrivilegedActionException e) {
 173             throw (FileNotFoundException) e.getException();
 174         }






























 175     }
 176 }
   1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.xml.internal;
  26 
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import java.io.FileNotFoundException;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 import java.security.PrivilegedActionException;
  35 import java.security.PrivilegedExceptionAction;
  36 import java.text.MessageFormat;
  37 import java.util.ListResourceBundle;
  38 import java.util.Locale;
  39 import java.util.MissingResourceException;
  40 import java.util.Properties;
  41 import java.util.ResourceBundle;
  42 
  43 /**
  44  * This class contains utility methods for reading resources in the JAXP packages
  45  */
  46 public class SecuritySupport {
  47     public final static String NEWLINE = getSystemProperty("line.separator", "\n");
  48 
  49     /**
  50      * Cache for properties in java.home/conf/jaxp.properties
  51      */
  52     static final Properties cacheProps = new Properties();
  53 
  54     /**
  55      * Flag indicating whether java.home/conf/jaxp.properties has been read
  56      */
  57     static volatile boolean firstTime = true;
  58 
  59     private SecuritySupport() {}
  60 
  61     public static String getErrorMessage(Locale locale, String bundle, String key,
  62             Object[] arguments) {
  63         ResourceBundle rb;
  64         if (locale != null) {
  65             rb = ResourceBundle.getBundle(bundle,locale);
  66         } else {
  67             rb = ResourceBundle.getBundle(bundle);
  68         }
  69 
  70         String msg = rb.getString(key);
  71         if (arguments != null) {
  72             msg = MessageFormat.format(msg, arguments);
  73         }
  74         return msg;
  75     }
  76 
  77     /**
  78      * Reads a system property with privilege
  79      *
  80      * @param propName the name of the property
  81      * @return the value of the property
  82      */
  83     public static String getSystemProperty(final String propName) {
  84         return
  85         AccessController.doPrivileged(
  86                 (PrivilegedAction<String>) () -> (String)System.getProperty(propName));
  87     }
  88 
  89     /**
  90      * Reads a system property with privilege
  91      *
  92      * @param propName the name of the property
  93      * @return the value of the property
  94      */
  95     public static String getSystemProperty(final String propName, String defValue) {
  96         String value = getSystemProperty(propName);
  97         if (value == null) {
  98             return defValue;
  99         }
 100         return value;
 101     }
 102 
 103     /**
 104      * Reads a system property with specified type.
 105      *
 106      * @param <T> the type of the property value
 107      * @param type the type of the property value
 108      * @param propName the name of the property
 109      * @param defValue the default value
 110      * @return the value of the property, or the default value if no system
 111      * property is found
 112      */
 113     public static <T> T getSystemProperty(Class<T> type, String propName, String defValue) {
 114         String value = getSystemProperty(propName);
 115         if (value == null) {
 116             value = defValue;
 117         }
 118         if (Integer.class.isAssignableFrom(type)) {
 119             return type.cast(Integer.parseInt(value));
 120         } else if (Boolean.class.isAssignableFrom(type)) {
 121             return type.cast(Boolean.parseBoolean(value));
 122         }
 123         return type.cast(value);
 124     }
 125 
 126     /**
 127      * Reads JAXP system property in this order: system property,
 128      * $java.home/conf/jaxp.properties if the system property is not specified
 129      *
 130      * @param <T> the type of the property value
 131      * @param type the type of the property value
 132      * @param propName the name of the property
 133      * @param defValue the default value
 134      * @return the value of the property, or the default value if no system
 135      * property is found
 136      */
 137     public static <T> T getJAXPSystemProperty(Class<T> type, String propName, String defValue) {
 138         String value = getJAXPSystemProperty(propName);
 139         if (value == null) {
 140             value = defValue;
 141         }
 142         if (Integer.class.isAssignableFrom(type)) {
 143             return type.cast(Integer.parseInt(value));
 144         } else if (Boolean.class.isAssignableFrom(type)) {
 145             return type.cast(Boolean.parseBoolean(value));
 146         }
 147         return type.cast(value);
 148     }
 149 
 150     /**
 151      * Reads JAXP system property in this order: system property,
 152      * $java.home/conf/jaxp.properties if the system property is not specified
 153      *
 154      * @param propName the name of the property


 161         }
 162         return value;
 163     }
 164 
 165     /**
 166      * Reads the specified property from $java.home/conf/jaxp.properties
 167      *
 168      * @param propName the name of the property
 169      * @return the value of the property
 170      */
 171     public static String readJAXPProperty(String propName) {
 172         String value = null;
 173         InputStream is = null;
 174         try {
 175             if (firstTime) {
 176                 synchronized (cacheProps) {
 177                     if (firstTime) {
 178                         String configFile = getSystemProperty("java.home") + File.separator
 179                                 + "conf" + File.separator + "jaxp.properties";
 180                         File f = new File(configFile);
 181                         if (isFileExists(f)) {
 182                             is = getFileInputStream(f);
 183                             cacheProps.load(is);
 184                         }
 185                         firstTime = false;
 186                     }
 187                 }
 188             }
 189             value = cacheProps.getProperty(propName);
 190 
 191         } catch (IOException ex) {
 192         } finally {
 193             if (is != null) {
 194                 try {
 195                     is.close();
 196                 } catch (IOException ex) {}
 197             }
 198         }
 199 
 200         return value;
 201     }
 202 
 203     /**
 204      * Tests whether the file denoted by this abstract pathname is a directory.
 205      * @param f the file to be tested
 206      * @return true if it is a directory, false otherwise
 207      */
 208     public static boolean isDirectory(final File f) {
 209         return (AccessController.doPrivileged((PrivilegedAction<Boolean>) ()
 210                 -> f.isDirectory()));
 211     }
 212 
 213     /**
 214      * Tests whether the file exists.
 215      *
 216      * @param f the file to be tested
 217      * @return true if the file exists, false otherwise
 218      */
 219     public static boolean isFileExists(final File f) {
 220         return (AccessController.doPrivileged((PrivilegedAction<Boolean>) ()
 221                 -> f.exists()));
 222     }
 223 
 224     public static FileInputStream getFileInputStream(final File file)
 225             throws FileNotFoundException {
 226         try {
 227             return AccessController.doPrivileged((PrivilegedExceptionAction<FileInputStream>) ()
 228                     -> new FileInputStream(file));
 229         } catch (PrivilegedActionException e) {
 230             throw (FileNotFoundException) e.getException();
 231         }
 232     }
 233 
 234     /**
 235      * Gets a resource bundle using the specified base name, the default locale, and the caller's class loader.
 236      * @param bundle the base name of the resource bundle, a fully qualified class name
 237      * @return a resource bundle for the given base name and the default locale
 238      */
 239     public static ListResourceBundle getResourceBundle(String bundle) {
 240         return getResourceBundle(bundle, Locale.getDefault());
 241     }
 242 
 243     /**
 244      * Gets a resource bundle using the specified base name and locale, and the caller's class loader.
 245      * @param bundle the base name of the resource bundle, a fully qualified class name
 246      * @param locale the locale for which a resource bundle is desired
 247      * @return a resource bundle for the given base name and locale
 248      */
 249     public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
 250         return AccessController.doPrivileged((PrivilegedAction<ListResourceBundle>) () -> {
 251             try {
 252                 return (ListResourceBundle)ResourceBundle.getBundle(bundle, locale);
 253             } catch (MissingResourceException e) {
 254                 try {
 255                     return (ListResourceBundle)ResourceBundle.getBundle(bundle, new Locale("en", "US"));
 256                 } catch (MissingResourceException e2) {
 257                     throw new MissingResourceException(
 258                             "Could not load any resource bundle by " + bundle, bundle, "");
 259                 }
 260             }
 261         });
 262     }
 263 }
< prev index next >