Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/xpath/SecuritySupport.java
32285 views
/*1* Copyright (c) 2005, 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 javax.xml.xpath;2627import java.net.URL;28import java.security.*;29import java.net.*;30import java.io.*;31import java.util.*;3233/**34* This class is duplicated for each JAXP subpackage so keep it in sync.35* It is package private and therefore is not exposed as part of the JAXP36* API.37*38* Security related methods that only work on J2SE 1.2 and newer.39*/40class SecuritySupport {414243ClassLoader getContextClassLoader() {44return (ClassLoader)45AccessController.doPrivileged(new PrivilegedAction() {46public Object run() {47ClassLoader cl = null;48try {49cl = Thread.currentThread().getContextClassLoader();50} catch (SecurityException ex) { }51return cl;52}53});54}5556String getSystemProperty(final String propName) {57return (String)58AccessController.doPrivileged(new PrivilegedAction() {59public Object run() {60return System.getProperty(propName);61}62});63}6465FileInputStream getFileInputStream(final File file)66throws FileNotFoundException67{68try {69return (FileInputStream)70AccessController.doPrivileged(new PrivilegedExceptionAction() {71public Object run() throws FileNotFoundException {72return new FileInputStream(file);73}74});75} catch (PrivilegedActionException e) {76throw (FileNotFoundException)e.getException();77}78}7980InputStream getURLInputStream(final URL url)81throws IOException82{83try {84return (InputStream)85AccessController.doPrivileged(new PrivilegedExceptionAction() {86public Object run() throws IOException {87return url.openStream();88}89});90} catch (PrivilegedActionException e) {91throw (IOException)e.getException();92}93}9495URL getResourceAsURL(final ClassLoader cl,96final String name)97{98return (URL)99AccessController.doPrivileged(new PrivilegedAction() {100public Object run() {101URL url;102if (cl == null) {103url = Object.class.getResource(name);104} else {105url = cl.getResource(name);106}107return url;108}109});110}111112Enumeration getResources(final ClassLoader cl,113final String name) throws IOException114{115try{116return (Enumeration)117AccessController.doPrivileged(new PrivilegedExceptionAction() {118public Object run() throws IOException{119Enumeration enumeration;120if (cl == null) {121enumeration = ClassLoader.getSystemResources(name);122} else {123enumeration = cl.getResources(name);124}125return enumeration;126}127});128}catch(PrivilegedActionException e){129throw (IOException)e.getException();130}131}132133InputStream getResourceAsStream(final ClassLoader cl,134final String name)135{136return (InputStream)137AccessController.doPrivileged(new PrivilegedAction() {138public Object run() {139InputStream ris;140if (cl == null) {141ris = Object.class.getResourceAsStream(name);142} else {143ris = cl.getResourceAsStream(name);144}145return ris;146}147});148}149150boolean doesFileExist(final File f) {151return ((Boolean)152AccessController.doPrivileged(new PrivilegedAction() {153public Object run() {154return new Boolean(f.exists());155}156})).booleanValue();157}158159}160161162