author | stefank |
Mon, 29 Jan 2018 15:11:33 +0100 | |
changeset 48815 | d80d521e9cb1 |
parent 47216 | 71c04702a3d5 |
child 52165 | 6c014b7762a2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
25803 | 2 |
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.naming.internal; |
|
27 |
||
25803 | 28 |
import javax.naming.NamingEnumeration; |
29 |
import java.io.File; |
|
30 |
import java.io.FileInputStream; |
|
31 |
import java.io.IOException; |
|
2 | 32 |
import java.io.InputStream; |
33 |
import java.net.MalformedURLException; |
|
34 |
import java.net.URL; |
|
25803 | 35 |
import java.net.URLClassLoader; |
36 |
import java.security.AccessController; |
|
37 |
import java.security.PrivilegedAction; |
|
38 |
import java.security.PrivilegedActionException; |
|
39 |
import java.security.PrivilegedExceptionAction; |
|
40 |
import java.util.*; |
|
2 | 41 |
|
42 |
/** |
|
43 |
* VersionHelper was used by JNDI to accommodate differences between |
|
44 |
* JDK 1.1.x and the Java 2 platform. As this is no longer necessary |
|
45 |
* since JNDI's inclusion in the platform, this class currently |
|
46 |
* serves as a set of utilities for performing system-level things, |
|
47 |
* such as class-loading and reading system properties. |
|
48 |
* |
|
49 |
* @author Rosanna Lee |
|
50 |
* @author Scott Seligman |
|
51 |
*/ |
|
52 |
||
25803 | 53 |
public final class VersionHelper { |
54 |
private static final VersionHelper helper = new VersionHelper(); |
|
2 | 55 |
|
25803 | 56 |
final static String[] PROPS = new String[]{ |
2 | 57 |
javax.naming.Context.INITIAL_CONTEXT_FACTORY, |
58 |
javax.naming.Context.OBJECT_FACTORIES, |
|
59 |
javax.naming.Context.URL_PKG_PREFIXES, |
|
60 |
javax.naming.Context.STATE_FACTORIES, |
|
61 |
javax.naming.Context.PROVIDER_URL, |
|
62 |
javax.naming.Context.DNS_URL, |
|
63 |
// The following shouldn't create a runtime dependence on ldap package. |
|
64 |
javax.naming.ldap.LdapContext.CONTROL_FACTORIES |
|
65 |
}; |
|
66 |
||
67 |
public final static int INITIAL_CONTEXT_FACTORY = 0; |
|
68 |
public final static int OBJECT_FACTORIES = 1; |
|
69 |
public final static int URL_PKG_PREFIXES = 2; |
|
70 |
public final static int STATE_FACTORIES = 3; |
|
71 |
public final static int PROVIDER_URL = 4; |
|
72 |
public final static int DNS_URL = 5; |
|
73 |
public final static int CONTROL_FACTORIES = 6; |
|
74 |
||
25803 | 75 |
private VersionHelper() {} // Disallow anyone from creating one of these. |
2 | 76 |
|
77 |
public static VersionHelper getVersionHelper() { |
|
78 |
return helper; |
|
79 |
} |
|
80 |
||
25803 | 81 |
public Class<?> loadClass(String className) throws ClassNotFoundException { |
82 |
return loadClass(className, getContextClassLoader()); |
|
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* @param className A non-null fully qualified class name. |
|
87 |
* @param codebase A non-null, space-separated list of URL strings. |
|
88 |
*/ |
|
89 |
public Class<?> loadClass(String className, String codebase) |
|
90 |
throws ClassNotFoundException, MalformedURLException { |
|
91 |
||
92 |
ClassLoader parent = getContextClassLoader(); |
|
93 |
ClassLoader cl = |
|
94 |
URLClassLoader.newInstance(getUrlArray(codebase), parent); |
|
2 | 95 |
|
25803 | 96 |
return loadClass(className, cl); |
97 |
} |
|
2 | 98 |
|
25803 | 99 |
/** |
100 |
* Package private. |
|
101 |
* <p> |
|
102 |
* This internal method is used with Thread Context Class Loader (TCCL), |
|
103 |
* please don't expose this method as public. |
|
104 |
*/ |
|
105 |
Class<?> loadClass(String className, ClassLoader cl) |
|
106 |
throws ClassNotFoundException { |
|
107 |
Class<?> cls = Class.forName(className, true, cl); |
|
108 |
return cls; |
|
109 |
} |
|
2 | 110 |
|
111 |
/* |
|
25803 | 112 |
* Returns a JNDI property from the system properties. Returns |
2 | 113 |
* null if the property is not set, or if there is no permission |
114 |
* to read it. |
|
115 |
*/ |
|
25803 | 116 |
String getJndiProperty(int i) { |
117 |
PrivilegedAction<String> act = () -> { |
|
118 |
try { |
|
119 |
return System.getProperty(PROPS[i]); |
|
120 |
} catch (SecurityException e) { |
|
121 |
return null; |
|
122 |
} |
|
123 |
}; |
|
124 |
return AccessController.doPrivileged(act); |
|
125 |
} |
|
2 | 126 |
|
127 |
/* |
|
128 |
* Reads each property in PROPS from the system properties, and |
|
129 |
* returns their values -- in order -- in an array. For each |
|
130 |
* unset property, the corresponding array element is set to null. |
|
131 |
* Returns null if there is no permission to call System.getProperties(). |
|
132 |
*/ |
|
25803 | 133 |
String[] getJndiProperties() { |
134 |
PrivilegedAction<Properties> act = () -> { |
|
135 |
try { |
|
136 |
return System.getProperties(); |
|
137 |
} catch (SecurityException e) { |
|
138 |
return null; |
|
139 |
} |
|
140 |
}; |
|
141 |
Properties sysProps = AccessController.doPrivileged(act); |
|
142 |
if (sysProps == null) { |
|
143 |
return null; |
|
144 |
} |
|
145 |
String[] jProps = new String[PROPS.length]; |
|
146 |
for (int i = 0; i < PROPS.length; i++) { |
|
147 |
jProps[i] = sysProps.getProperty(PROPS[i]); |
|
148 |
} |
|
149 |
return jProps; |
|
150 |
} |
|
2 | 151 |
|
36511 | 152 |
private static String resolveName(Class<?> c, String name) { |
153 |
if (name == null) { |
|
154 |
return name; |
|
155 |
} |
|
156 |
if (!name.startsWith("/")) { |
|
157 |
while (c.isArray()) { |
|
158 |
c = c.getComponentType(); |
|
159 |
} |
|
160 |
String baseName = c.getName(); |
|
161 |
int index = baseName.lastIndexOf('.'); |
|
162 |
if (index != -1) { |
|
163 |
name = baseName.substring(0, index).replace('.', '/') |
|
164 |
+"/"+name; |
|
165 |
} |
|
166 |
} else { |
|
167 |
name = name.substring(1); |
|
168 |
} |
|
169 |
return name; |
|
170 |
} |
|
171 |
||
2 | 172 |
/* |
173 |
* Returns the resource of a given name associated with a particular |
|
174 |
* class (never null), or null if none can be found. |
|
175 |
*/ |
|
25803 | 176 |
InputStream getResourceAsStream(Class<?> c, String name) { |
36511 | 177 |
PrivilegedAction<InputStream> act = () -> { |
178 |
try { |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
36511
diff
changeset
|
179 |
Module m = c.getModule(); |
36511 | 180 |
return c.getModule().getResourceAsStream(resolveName(c,name)); |
181 |
} catch (IOException x) { |
|
182 |
return null; |
|
183 |
} |
|
184 |
}; |
|
25803 | 185 |
return AccessController.doPrivileged(act); |
186 |
} |
|
2 | 187 |
|
188 |
/* |
|
27565 | 189 |
* Returns an input stream for a file in <java.home>/conf, |
2 | 190 |
* or null if it cannot be located or opened. |
191 |
* |
|
192 |
* @param filename The file name, sans directory. |
|
193 |
*/ |
|
27565 | 194 |
InputStream getJavaHomeConfStream(String filename) { |
25803 | 195 |
PrivilegedAction<InputStream> act = () -> { |
196 |
try { |
|
197 |
String javahome = System.getProperty("java.home"); |
|
198 |
if (javahome == null) { |
|
199 |
return null; |
|
200 |
} |
|
201 |
String pathname = javahome + File.separator + |
|
27565 | 202 |
"conf" + File.separator + filename; |
25803 | 203 |
return new FileInputStream(pathname); |
204 |
} catch (Exception e) { |
|
205 |
return null; |
|
206 |
} |
|
207 |
}; |
|
208 |
return AccessController.doPrivileged(act); |
|
209 |
} |
|
2 | 210 |
|
211 |
/* |
|
212 |
* Returns an enumeration (never null) of InputStreams of the |
|
213 |
* resources of a given name associated with a particular class |
|
214 |
* loader. Null represents the bootstrap class loader in some |
|
215 |
* Java implementations. |
|
216 |
*/ |
|
25803 | 217 |
NamingEnumeration<InputStream> getResources(ClassLoader cl, |
218 |
String name) throws IOException { |
|
219 |
Enumeration<URL> urls; |
|
220 |
PrivilegedExceptionAction<Enumeration<URL>> act = () -> |
|
221 |
(cl == null) |
|
222 |
? ClassLoader.getSystemResources(name) |
|
223 |
: cl.getResources(name); |
|
224 |
try { |
|
225 |
urls = AccessController.doPrivileged(act); |
|
226 |
} catch (PrivilegedActionException e) { |
|
227 |
throw (IOException) e.getException(); |
|
228 |
} |
|
229 |
return new InputStreamEnumeration(urls); |
|
230 |
} |
|
231 |
||
2 | 232 |
|
25803 | 233 |
/** |
234 |
* Package private. |
|
235 |
* <p> |
|
236 |
* This internal method returns Thread Context Class Loader (TCCL), |
|
237 |
* if null, returns the system Class Loader. |
|
238 |
* <p> |
|
239 |
* Please don't expose this method as public. |
|
240 |
* @throws SecurityException if the class loader is not accessible |
|
2 | 241 |
*/ |
25803 | 242 |
ClassLoader getContextClassLoader() { |
2 | 243 |
|
25803 | 244 |
PrivilegedAction<ClassLoader> act = () -> { |
245 |
ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
|
246 |
if (loader == null) { |
|
247 |
// Don't use bootstrap class loader directly! |
|
248 |
loader = ClassLoader.getSystemClassLoader(); |
|
249 |
} |
|
250 |
return loader; |
|
251 |
}; |
|
252 |
return AccessController.doPrivileged(act); |
|
253 |
} |
|
254 |
||
255 |
private static URL[] getUrlArray(String codebase) |
|
256 |
throws MalformedURLException { |
|
2 | 257 |
// Parse codebase into separate URLs |
258 |
StringTokenizer parser = new StringTokenizer(codebase); |
|
25803 | 259 |
List<String> list = new ArrayList<>(); |
2 | 260 |
while (parser.hasMoreTokens()) { |
25803 | 261 |
list.add(parser.nextToken()); |
2 | 262 |
} |
25803 | 263 |
String[] url = new String[list.size()]; |
2 | 264 |
for (int i = 0; i < url.length; i++) { |
25803 | 265 |
url[i] = list.get(i); |
2 | 266 |
} |
267 |
||
268 |
URL[] urlArray = new URL[url.length]; |
|
269 |
for (int i = 0; i < urlArray.length; i++) { |
|
270 |
urlArray[i] = new URL(url[i]); |
|
271 |
} |
|
272 |
return urlArray; |
|
273 |
} |
|
25803 | 274 |
|
275 |
/** |
|
276 |
* Given an enumeration of URLs, an instance of this class represents |
|
277 |
* an enumeration of their InputStreams. Each operation on the URL |
|
278 |
* enumeration is performed within a doPrivileged block. |
|
279 |
* This is used to enumerate the resources under a foreign codebase. |
|
280 |
* This class is not MT-safe. |
|
281 |
*/ |
|
282 |
private class InputStreamEnumeration implements |
|
283 |
NamingEnumeration<InputStream> { |
|
284 |
||
285 |
private final Enumeration<URL> urls; |
|
286 |
||
287 |
private InputStream nextElement; |
|
288 |
||
289 |
InputStreamEnumeration(Enumeration<URL> urls) { |
|
290 |
this.urls = urls; |
|
291 |
} |
|
292 |
||
293 |
/* |
|
294 |
* Returns the next InputStream, or null if there are no more. |
|
295 |
* An InputStream that cannot be opened is skipped. |
|
296 |
*/ |
|
297 |
private InputStream getNextElement() { |
|
298 |
PrivilegedAction<InputStream> act = () -> { |
|
299 |
while (urls.hasMoreElements()) { |
|
300 |
try { |
|
301 |
return urls.nextElement().openStream(); |
|
302 |
} catch (IOException e) { |
|
303 |
// skip this URL |
|
304 |
} |
|
305 |
} |
|
306 |
return null; |
|
307 |
}; |
|
308 |
return AccessController.doPrivileged(act); |
|
309 |
} |
|
310 |
||
311 |
public boolean hasMore() { |
|
312 |
if (nextElement != null) { |
|
313 |
return true; |
|
314 |
} |
|
315 |
nextElement = getNextElement(); |
|
316 |
return (nextElement != null); |
|
317 |
} |
|
318 |
||
319 |
public boolean hasMoreElements() { |
|
320 |
return hasMore(); |
|
321 |
} |
|
322 |
||
323 |
public InputStream next() { |
|
324 |
if (hasMore()) { |
|
325 |
InputStream res = nextElement; |
|
326 |
nextElement = null; |
|
327 |
return res; |
|
328 |
} else { |
|
329 |
throw new NoSuchElementException(); |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
public InputStream nextElement() { |
|
334 |
return next(); |
|
335 |
} |
|
336 |
||
337 |
public void close() { |
|
338 |
} |
|
339 |
} |
|
2 | 340 |
} |