1 /*
2  * Copyright (c) 2005, 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 
26 package javax.xml.xpath;
27 
28 import java.net.URL;
29 import java.security.*;
30 import java.net.*;
31 import java.io.*;
32 import java.util.*;
33 
34 /**
35  * This class is duplicated for each JAXP subpackage so keep it in sync.
36  * It is package private and therefore is not exposed as part of the JAXP
37  * API.
38  *
39  * Security related methods that only work on J2SE 1.2 and newer.
40  */
41 class SecuritySupport  {
42 
43 
getContextClassLoader()44     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     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     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     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     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 = Object.class.getResource(name);
105                     } else {
106                         url = cl.getResource(name);
107                     }
108                     return url;
109                 }
110             });
111     }
112 
113     Enumeration getResources(final ClassLoader cl,
114                                            final String name) throws IOException
115     {
116         try{
117         return (Enumeration)
118             AccessController.doPrivileged(new PrivilegedExceptionAction() {
119                 public Object run() throws IOException{
120                     Enumeration enumeration;
121                     if (cl == null) {
122                         enumeration = ClassLoader.getSystemResources(name);
123                     } else {
124                         enumeration = cl.getResources(name);
125                     }
126                     return enumeration;
127                 }
128             });
129         }catch(PrivilegedActionException e){
130             throw (IOException)e.getException();
131         }
132     }
133 
134     InputStream getResourceAsStream(final ClassLoader cl,
135                                            final String name)
136     {
137         return (InputStream)
138             AccessController.doPrivileged(new PrivilegedAction() {
139                 public Object run() {
140                     InputStream ris;
141                     if (cl == null) {
142                         ris = Object.class.getResourceAsStream(name);
143                     } else {
144                         ris = cl.getResourceAsStream(name);
145                     }
146                     return ris;
147                 }
148             });
149     }
150 
151     boolean doesFileExist(final File f) {
152     return ((Boolean)
153             AccessController.doPrivileged(new PrivilegedAction() {
154                 public Object run() {
155                     return new Boolean(f.exists());
156                 }
157             })).booleanValue();
158     }
159 
160 }
161