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 113 * @return the value of the property 114 */ 115 public static String getJAXPSystemProperty(String propName) { 116 String value = getSystemProperty(propName); 117 if (value == null) { 118 value = readJAXPProperty(propName); 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 }