16953
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 2002-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
|
|
21 |
package com.sun.org.apache.bcel.internal.util;
|
|
22 |
|
|
23 |
import java.io.File;
|
|
24 |
import java.io.FileInputStream;
|
|
25 |
import java.io.FileNotFoundException;
|
|
26 |
import java.io.FilenameFilter;
|
|
27 |
import java.io.InputStream;
|
|
28 |
import java.lang.ClassLoader;
|
|
29 |
import java.security.AccessController;
|
|
30 |
import java.security.PrivilegedAction;
|
|
31 |
import java.security.PrivilegedActionException;
|
|
32 |
import java.security.PrivilegedExceptionAction;
|
|
33 |
import java.util.ListResourceBundle;
|
|
34 |
import java.util.Locale;
|
|
35 |
import java.util.MissingResourceException;
|
|
36 |
import java.util.ResourceBundle;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* This class is duplicated for each subpackage so keep it in sync. It is
|
|
40 |
* package private and therefore is not exposed as part of any API.
|
|
41 |
*
|
|
42 |
* @xerces.internal
|
|
43 |
*/
|
|
44 |
public final class SecuritySupport {
|
|
45 |
|
|
46 |
private static final SecuritySupport securitySupport = new SecuritySupport();
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Return an instance of this class.
|
|
50 |
*/
|
|
51 |
public static SecuritySupport getInstance() {
|
|
52 |
return securitySupport;
|
|
53 |
}
|
|
54 |
|
|
55 |
static ClassLoader getContextClassLoader() {
|
|
56 |
return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
|
|
57 |
public Object run() {
|
|
58 |
ClassLoader cl = null;
|
|
59 |
try {
|
|
60 |
cl = Thread.currentThread().getContextClassLoader();
|
|
61 |
} catch (SecurityException ex) {
|
|
62 |
}
|
|
63 |
return cl;
|
|
64 |
}
|
|
65 |
});
|
|
66 |
}
|
|
67 |
|
|
68 |
static ClassLoader getSystemClassLoader() {
|
|
69 |
return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
|
|
70 |
public Object run() {
|
|
71 |
ClassLoader cl = null;
|
|
72 |
try {
|
|
73 |
cl = ClassLoader.getSystemClassLoader();
|
|
74 |
} catch (SecurityException ex) {
|
|
75 |
}
|
|
76 |
return cl;
|
|
77 |
}
|
|
78 |
});
|
|
79 |
}
|
|
80 |
|
|
81 |
static ClassLoader getParentClassLoader(final ClassLoader cl) {
|
|
82 |
return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
|
|
83 |
public Object run() {
|
|
84 |
ClassLoader parent = null;
|
|
85 |
try {
|
|
86 |
parent = cl.getParent();
|
|
87 |
} catch (SecurityException ex) {
|
|
88 |
}
|
|
89 |
|
|
90 |
// eliminate loops in case of the boot
|
|
91 |
// ClassLoader returning itself as a parent
|
|
92 |
return (parent == cl) ? null : parent;
|
|
93 |
}
|
|
94 |
});
|
|
95 |
}
|
|
96 |
|
|
97 |
public static String getSystemProperty(final String propName) {
|
|
98 |
return (String) AccessController.doPrivileged(new PrivilegedAction() {
|
|
99 |
public Object run() {
|
|
100 |
return System.getProperty(propName);
|
|
101 |
}
|
|
102 |
});
|
|
103 |
}
|
|
104 |
|
|
105 |
static FileInputStream getFileInputStream(final File file)
|
|
106 |
throws FileNotFoundException {
|
|
107 |
try {
|
|
108 |
return (FileInputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() {
|
|
109 |
public Object run() throws FileNotFoundException {
|
|
110 |
return new FileInputStream(file);
|
|
111 |
}
|
|
112 |
});
|
|
113 |
} catch (PrivilegedActionException e) {
|
|
114 |
throw (FileNotFoundException) e.getException();
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Return resource using the same classloader for the ObjectFactory by
|
|
120 |
* default or bootclassloader when Security Manager is in place
|
|
121 |
*/
|
|
122 |
public static InputStream getResourceAsStream(final String name) {
|
|
123 |
if (System.getSecurityManager() != null) {
|
|
124 |
return getResourceAsStream(null, name);
|
|
125 |
} else {
|
|
126 |
return getResourceAsStream(findClassLoader(), name);
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
public static InputStream getResourceAsStream(final ClassLoader cl,
|
|
131 |
final String name) {
|
|
132 |
return (InputStream) AccessController.doPrivileged(new PrivilegedAction() {
|
|
133 |
public Object run() {
|
|
134 |
InputStream ris;
|
|
135 |
if (cl == null) {
|
|
136 |
ris = Object.class.getResourceAsStream("/" + name);
|
|
137 |
} else {
|
|
138 |
ris = cl.getResourceAsStream(name);
|
|
139 |
}
|
|
140 |
return ris;
|
|
141 |
}
|
|
142 |
});
|
|
143 |
}
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Gets a resource bundle using the specified base name, the default locale,
|
|
147 |
* and the caller's class loader.
|
|
148 |
*
|
|
149 |
* @param bundle the base name of the resource bundle, a fully qualified
|
|
150 |
* class name
|
|
151 |
* @return a resource bundle for the given base name and the default locale
|
|
152 |
*/
|
|
153 |
public static ListResourceBundle getResourceBundle(String bundle) {
|
|
154 |
return getResourceBundle(bundle, Locale.getDefault());
|
|
155 |
}
|
|
156 |
|
|
157 |
/**
|
|
158 |
* Gets a resource bundle using the specified base name and locale, and the
|
|
159 |
* caller's class loader.
|
|
160 |
*
|
|
161 |
* @param bundle the base name of the resource bundle, a fully qualified
|
|
162 |
* class name
|
|
163 |
* @param locale the locale for which a resource bundle is desired
|
|
164 |
* @return a resource bundle for the given base name and locale
|
|
165 |
*/
|
|
166 |
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
|
|
167 |
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
|
|
168 |
public ListResourceBundle run() {
|
|
169 |
try {
|
|
170 |
return (ListResourceBundle) ResourceBundle.getBundle(bundle, locale);
|
|
171 |
} catch (MissingResourceException e) {
|
|
172 |
try {
|
|
173 |
return (ListResourceBundle) ResourceBundle.getBundle(bundle, new Locale("en", "US"));
|
|
174 |
} catch (MissingResourceException e2) {
|
|
175 |
throw new MissingResourceException(
|
|
176 |
"Could not load any resource bundle by " + bundle, bundle, "");
|
|
177 |
}
|
|
178 |
}
|
|
179 |
}
|
|
180 |
});
|
|
181 |
}
|
|
182 |
|
|
183 |
public static String[] getFileList(final File f, final FilenameFilter filter) {
|
|
184 |
return ((String[]) AccessController.doPrivileged(new PrivilegedAction() {
|
|
185 |
public Object run() {
|
|
186 |
return f.list(filter);
|
|
187 |
}
|
|
188 |
}));
|
|
189 |
}
|
|
190 |
|
|
191 |
public static boolean getFileExists(final File f) {
|
|
192 |
return ((Boolean) AccessController.doPrivileged(new PrivilegedAction() {
|
|
193 |
public Object run() {
|
|
194 |
return f.exists() ? Boolean.TRUE : Boolean.FALSE;
|
|
195 |
}
|
|
196 |
})).booleanValue();
|
|
197 |
}
|
|
198 |
|
|
199 |
static long getLastModified(final File f) {
|
|
200 |
return ((Long) AccessController.doPrivileged(new PrivilegedAction() {
|
|
201 |
public Object run() {
|
|
202 |
return new Long(f.lastModified());
|
|
203 |
}
|
|
204 |
})).longValue();
|
|
205 |
}
|
|
206 |
|
|
207 |
|
|
208 |
/**
|
|
209 |
* Figure out which ClassLoader to use.
|
|
210 |
*/
|
|
211 |
public static ClassLoader findClassLoader()
|
|
212 |
{
|
|
213 |
if (System.getSecurityManager()!=null) {
|
|
214 |
//this will ensure bootclassloader is used
|
|
215 |
return null;
|
|
216 |
} else {
|
|
217 |
return SecuritySupport.class.getClassLoader();
|
|
218 |
}
|
|
219 |
} // findClassLoader():ClassLoader
|
|
220 |
|
|
221 |
private SecuritySupport() {
|
|
222 |
}
|
|
223 |
}
|