Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/jdk/xml/internal/SecuritySupport.java
38813 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.xml.internal;2627import java.io.File;28import java.io.FileInputStream;29import java.io.FileNotFoundException;30import java.io.IOException;31import java.io.InputStream;32import java.security.AccessController;33import java.security.PrivilegedAction;34import java.security.PrivilegedActionException;35import java.security.PrivilegedExceptionAction;36import java.util.Properties;3738/**39* This class contains utility methods for reading resources in the JAXP packages40*/41class SecuritySupport {42/**43* Cache for properties in java.home/lib/jaxp.properties44*/45static final Properties cacheProps = new Properties();4647/**48* Flag indicating whether java.home/lib/jaxp.properties has been read49*/50static volatile boolean firstTime = true;5152private SecuritySupport() {}5354/**55* Reads JAXP system property with privilege56*57* @param propName the name of the property58* @return the value of the property59*/60public static String getSystemProperty(final String propName) {61return AccessController.doPrivileged(new PrivilegedAction<String>() {62@Override63public String run() {64return System.getProperty(propName);65}66});67}6869/**70* Reads a system property.71*72* @param <T> the type of the property value73* @param type the type of the property value74* @param propName the name of the property75* @param defValue the default value76* @return the value of the property, or the default value of no system77* property is found78*/79public static <T> T getJAXPSystemProperty(Class<T> type, String propName, String defValue) {80String value = getJAXPSystemProperty(propName);81if (value == null) {82value = defValue;83}84if (Integer.class.isAssignableFrom(type)) {85return type.cast(Integer.parseInt(value));86} else if (Boolean.class.isAssignableFrom(type)) {87return type.cast(Boolean.parseBoolean(value));88}89return type.cast(value);90}9192/**93* Reads JAXP system property in this order: system property,94* $java.home/lib/jaxp.properties if the system property is not specified95*96* @param propName the name of the property97* @return the value of the property98*/99public static String getJAXPSystemProperty(String propName) {100String value = getSystemProperty(propName);101if (value == null) {102value = readJAXPProperty(propName);103}104return value;105}106107/**108* Reads the specified property from $java.home/lib/jaxp.properties109*110* @param propName the name of the property111* @return the value of the property112*/113public static String readJAXPProperty(String propName) {114String value = null;115InputStream is = null;116try {117if (firstTime) {118synchronized (cacheProps) {119if (firstTime) {120String configFile = getSystemProperty("java.home") + File.separator121+ "lib" + File.separator + "jaxp.properties";122File f = new File(configFile);123if (getFileExists(f)) {124is = getFileInputStream(f);125cacheProps.load(is);126}127firstTime = false;128}129}130}131value = cacheProps.getProperty(propName);132133} catch (IOException ex) {134} finally {135if (is != null) {136try {137is.close();138} catch (IOException ex) {}139}140}141142return value;143}144145//------------------- private methods ---------------------------146static boolean getFileExists(final File f) {147return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {148@Override149public Boolean run() {150return f.exists() ? Boolean.TRUE : Boolean.FALSE;151}152});153}154155static FileInputStream getFileInputStream(final File file) throws FileNotFoundException {156try {157return AccessController.doPrivileged(new PrivilegedExceptionAction<FileInputStream>() {158@Override159public FileInputStream run() throws Exception {160return new FileInputStream(file);161}162});163} catch (PrivilegedActionException e) {164throw (FileNotFoundException) e.getException();165}166}167}168169170