1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 // $Id: SecuritySupport.java 522337 2007-03-25 20:03:27Z mrglavas $ 18 19 package javax.xml.validation; 20 21 import java.io.File; 22 import java.io.FileInputStream; 23 import java.io.FileNotFoundException; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.net.URL; 27 import java.security.AccessController; 28 import java.security.PrivilegedAction; 29 import java.security.PrivilegedActionException; 30 import java.security.PrivilegedExceptionAction; 31 import java.util.Enumeration; 32 33 /** 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 JAXP 36 * API. 37 * 38 * Security related methods that only work on J2SE 1.2 and newer. 39 */ 40 final class SecuritySupport { 41 42 private SecuritySupport() {} 43 44 static ClassLoader getContextClassLoader() { 45 return (ClassLoader) 46 AccessController.doPrivileged(new PrivilegedAction() { 47 public Object run() { 48 ClassLoader cl = null; 49 try { 50 cl = Thread.currentThread().getContextClassLoader(); 51 } catch (SecurityException ex) { } 52 return cl; 53 } 54 }); 55 } 56 57 static String getSystemProperty(final String propName) { 58 return (String) 59 AccessController.doPrivileged(new PrivilegedAction() { 60 public Object run() { 61 return System.getProperty(propName); 62 } 63 }); 64 } 65 66 static FileInputStream getFileInputStream(final File file) 67 throws FileNotFoundException 68 { 69 try { 70 return (FileInputStream) 71 AccessController.doPrivileged(new PrivilegedExceptionAction() { 72 public Object run() throws FileNotFoundException { 73 return new FileInputStream(file); 74 } 75 }); 76 } catch (PrivilegedActionException e) { 77 throw (FileNotFoundException)e.getException(); 78 } 79 } 80 81 static InputStream getURLInputStream(final URL url) 82 throws IOException 83 { 84 try { 85 return (InputStream) 86 AccessController.doPrivileged(new PrivilegedExceptionAction() { 87 public Object run() throws IOException { 88 return url.openStream(); 89 } 90 }); 91 } catch (PrivilegedActionException e) { 92 throw (IOException)e.getException(); 93 } 94 } 95 96 static URL getResourceAsURL(final ClassLoader cl, 97 final String name) 98 { 99 return (URL) 100 AccessController.doPrivileged(new PrivilegedAction() { 101 public Object run() { 102 URL url; 103 if (cl == null) { 104 url = ClassLoader.getSystemResource(name); 105 } 106 else { 107 url = cl.getResource(name); 108 } 109 return url; 110 } 111 }); 112 } 113 114 static Enumeration getResources(final ClassLoader cl, 115 final String name) throws IOException 116 { 117 try{ 118 return (Enumeration) 119 AccessController.doPrivileged(new PrivilegedExceptionAction() { 120 public Object run() throws IOException{ 121 Enumeration enumeration; 122 if (cl == null) { 123 enumeration = ClassLoader.getSystemResources(name); 124 } 125 else { 126 enumeration = cl.getResources(name); 127 } 128 return enumeration; 129 } 130 }); 131 }catch(PrivilegedActionException e){ 132 throw (IOException)e.getException(); 133 } 134 } 135 136 static InputStream getResourceAsStream(final ClassLoader cl, 137 final String name) 138 { 139 return (InputStream) 140 AccessController.doPrivileged(new PrivilegedAction() { 141 public Object run() { 142 InputStream ris; 143 if (cl == null) { 144 ris = ClassLoader.getSystemResourceAsStream(name); 145 } else { 146 ris = cl.getResourceAsStream(name); 147 } 148 return ris; 149 } 150 }); 151 } 152 153 static boolean doesFileExist(final File f) { 154 return ((Boolean) 155 AccessController.doPrivileged(new PrivilegedAction() { 156 public Object run() { 157 return f.exists() ? Boolean.TRUE : Boolean.FALSE; 158 } 159 })).booleanValue(); 160 } 161 162 } 163