author | lana |
Tue, 08 Aug 2017 22:52:41 +0000 | |
changeset 46173 | 5546b5710844 |
parent 45853 | bfa06be36a17 |
permissions | -rw-r--r-- |
16953 | 1 |
/* |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
16953 | 3 |
*/ |
4 |
/* |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
31284
diff
changeset
|
5 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
31284
diff
changeset
|
6 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
31284
diff
changeset
|
7 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
31284
diff
changeset
|
8 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
31284
diff
changeset
|
9 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
31284
diff
changeset
|
10 |
* the License. You may obtain a copy of the License at |
16953 | 11 |
* |
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
31284
diff
changeset
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
16953 | 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.lang.ClassLoader; |
|
28 |
import java.security.AccessController; |
|
29 |
import java.security.PrivilegedAction; |
|
30 |
import java.security.PrivilegedActionException; |
|
31 |
import java.security.PrivilegedExceptionAction; |
|
32 |
import java.util.ListResourceBundle; |
|
33 |
import java.util.Locale; |
|
34 |
import java.util.MissingResourceException; |
|
35 |
import java.util.ResourceBundle; |
|
36 |
||
37 |
/** |
|
38 |
* This class is duplicated for each subpackage so keep it in sync. It is |
|
39 |
* package private and therefore is not exposed as part of any API. |
|
40 |
* |
|
41 |
* @xerces.internal |
|
42 |
*/ |
|
43 |
public final class SecuritySupport { |
|
44 |
||
45 |
private static final SecuritySupport securitySupport = new SecuritySupport(); |
|
46 |
||
47 |
/** |
|
48 |
* Return an instance of this class. |
|
49 |
*/ |
|
50 |
public static SecuritySupport getInstance() { |
|
51 |
return securitySupport; |
|
52 |
} |
|
53 |
||
54 |
static ClassLoader getContextClassLoader() { |
|
55 |
return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { |
|
56 |
public Object run() { |
|
57 |
ClassLoader cl = null; |
|
58 |
try { |
|
59 |
cl = Thread.currentThread().getContextClassLoader(); |
|
60 |
} catch (SecurityException ex) { |
|
61 |
} |
|
62 |
return cl; |
|
63 |
} |
|
64 |
}); |
|
65 |
} |
|
66 |
||
67 |
static ClassLoader getSystemClassLoader() { |
|
68 |
return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { |
|
69 |
public Object run() { |
|
70 |
ClassLoader cl = null; |
|
71 |
try { |
|
72 |
cl = ClassLoader.getSystemClassLoader(); |
|
73 |
} catch (SecurityException ex) { |
|
74 |
} |
|
75 |
return cl; |
|
76 |
} |
|
77 |
}); |
|
78 |
} |
|
79 |
||
80 |
static ClassLoader getParentClassLoader(final ClassLoader cl) { |
|
81 |
return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { |
|
82 |
public Object run() { |
|
83 |
ClassLoader parent = null; |
|
84 |
try { |
|
85 |
parent = cl.getParent(); |
|
86 |
} catch (SecurityException ex) { |
|
87 |
} |
|
88 |
||
89 |
// eliminate loops in case of the boot |
|
90 |
// ClassLoader returning itself as a parent |
|
91 |
return (parent == cl) ? null : parent; |
|
92 |
} |
|
93 |
}); |
|
94 |
} |
|
95 |
||
96 |
public static String getSystemProperty(final String propName) { |
|
97 |
return (String) AccessController.doPrivileged(new PrivilegedAction() { |
|
98 |
public Object run() { |
|
99 |
return System.getProperty(propName); |
|
100 |
} |
|
101 |
}); |
|
102 |
} |
|
103 |
||
104 |
static FileInputStream getFileInputStream(final File file) |
|
105 |
throws FileNotFoundException { |
|
106 |
try { |
|
107 |
return (FileInputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() { |
|
108 |
public Object run() throws FileNotFoundException { |
|
109 |
return new FileInputStream(file); |
|
110 |
} |
|
111 |
}); |
|
112 |
} catch (PrivilegedActionException e) { |
|
113 |
throw (FileNotFoundException) e.getException(); |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Gets a resource bundle using the specified base name, the default locale, |
|
119 |
* and the caller's class loader. |
|
120 |
* |
|
121 |
* @param bundle the base name of the resource bundle, a fully qualified |
|
122 |
* class name |
|
123 |
* @return a resource bundle for the given base name and the default locale |
|
124 |
*/ |
|
125 |
public static ListResourceBundle getResourceBundle(String bundle) { |
|
126 |
return getResourceBundle(bundle, Locale.getDefault()); |
|
127 |
} |
|
128 |
||
129 |
/** |
|
130 |
* Gets a resource bundle using the specified base name and locale, and the |
|
131 |
* caller's class loader. |
|
132 |
* |
|
133 |
* @param bundle the base name of the resource bundle, a fully qualified |
|
134 |
* class name |
|
135 |
* @param locale the locale for which a resource bundle is desired |
|
136 |
* @return a resource bundle for the given base name and locale |
|
137 |
*/ |
|
138 |
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) { |
|
139 |
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() { |
|
140 |
public ListResourceBundle run() { |
|
141 |
try { |
|
142 |
return (ListResourceBundle) ResourceBundle.getBundle(bundle, locale); |
|
143 |
} catch (MissingResourceException e) { |
|
144 |
try { |
|
145 |
return (ListResourceBundle) ResourceBundle.getBundle(bundle, new Locale("en", "US")); |
|
146 |
} catch (MissingResourceException e2) { |
|
147 |
throw new MissingResourceException( |
|
148 |
"Could not load any resource bundle by " + bundle, bundle, ""); |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
}); |
|
153 |
} |
|
154 |
||
155 |
public static String[] getFileList(final File f, final FilenameFilter filter) { |
|
156 |
return ((String[]) AccessController.doPrivileged(new PrivilegedAction() { |
|
157 |
public Object run() { |
|
158 |
return f.list(filter); |
|
159 |
} |
|
160 |
})); |
|
161 |
} |
|
162 |
||
163 |
public static boolean getFileExists(final File f) { |
|
164 |
return ((Boolean) AccessController.doPrivileged(new PrivilegedAction() { |
|
165 |
public Object run() { |
|
166 |
return f.exists() ? Boolean.TRUE : Boolean.FALSE; |
|
167 |
} |
|
168 |
})).booleanValue(); |
|
169 |
} |
|
170 |
||
171 |
static long getLastModified(final File f) { |
|
172 |
return ((Long) AccessController.doPrivileged(new PrivilegedAction() { |
|
173 |
public Object run() { |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
174 |
return f.lastModified(); |
16953 | 175 |
} |
176 |
})).longValue(); |
|
177 |
} |
|
178 |
||
179 |
||
180 |
/** |
|
181 |
* Figure out which ClassLoader to use. |
|
182 |
*/ |
|
183 |
public static ClassLoader findClassLoader() |
|
184 |
{ |
|
185 |
if (System.getSecurityManager()!=null) { |
|
186 |
//this will ensure bootclassloader is used |
|
187 |
return null; |
|
188 |
} else { |
|
189 |
return SecuritySupport.class.getClassLoader(); |
|
190 |
} |
|
191 |
} // findClassLoader():ClassLoader |
|
192 |
||
193 |
private SecuritySupport() { |
|
194 |
} |
|
195 |
} |