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 |
|
|
152 |
/*
|
|
153 |
* Returns the resource of a given name associated with a particular
|
|
154 |
* class (never null), or null if none can be found.
|
|
155 |
*/
|
25803
|
156 |
InputStream getResourceAsStream(Class<?> c, String name) {
|
|
157 |
PrivilegedAction<InputStream> act = () -> c.getResourceAsStream(name);
|
|
158 |
return AccessController.doPrivileged(act);
|
|
159 |
}
|
2
|
160 |
|
|
161 |
/*
|
27565
|
162 |
* Returns an input stream for a file in <java.home>/conf,
|
2
|
163 |
* or null if it cannot be located or opened.
|
|
164 |
*
|
|
165 |
* @param filename The file name, sans directory.
|
|
166 |
*/
|
27565
|
167 |
InputStream getJavaHomeConfStream(String filename) {
|
25803
|
168 |
PrivilegedAction<InputStream> act = () -> {
|
|
169 |
try {
|
|
170 |
String javahome = System.getProperty("java.home");
|
|
171 |
if (javahome == null) {
|
|
172 |
return null;
|
|
173 |
}
|
|
174 |
String pathname = javahome + File.separator +
|
27565
|
175 |
"conf" + File.separator + filename;
|
25803
|
176 |
return new FileInputStream(pathname);
|
|
177 |
} catch (Exception e) {
|
|
178 |
return null;
|
|
179 |
}
|
|
180 |
};
|
|
181 |
return AccessController.doPrivileged(act);
|
|
182 |
}
|
2
|
183 |
|
|
184 |
/*
|
|
185 |
* Returns an enumeration (never null) of InputStreams of the
|
|
186 |
* resources of a given name associated with a particular class
|
|
187 |
* loader. Null represents the bootstrap class loader in some
|
|
188 |
* Java implementations.
|
|
189 |
*/
|
25803
|
190 |
NamingEnumeration<InputStream> getResources(ClassLoader cl,
|
|
191 |
String name) throws IOException {
|
|
192 |
Enumeration<URL> urls;
|
|
193 |
PrivilegedExceptionAction<Enumeration<URL>> act = () ->
|
|
194 |
(cl == null)
|
|
195 |
? ClassLoader.getSystemResources(name)
|
|
196 |
: cl.getResources(name);
|
|
197 |
try {
|
|
198 |
urls = AccessController.doPrivileged(act);
|
|
199 |
} catch (PrivilegedActionException e) {
|
|
200 |
throw (IOException) e.getException();
|
|
201 |
}
|
|
202 |
return new InputStreamEnumeration(urls);
|
|
203 |
}
|
|
204 |
|
2
|
205 |
|
25803
|
206 |
/**
|
|
207 |
* Package private.
|
|
208 |
* <p>
|
|
209 |
* This internal method returns Thread Context Class Loader (TCCL),
|
|
210 |
* if null, returns the system Class Loader.
|
|
211 |
* <p>
|
|
212 |
* Please don't expose this method as public.
|
|
213 |
* @throws SecurityException if the class loader is not accessible
|
2
|
214 |
*/
|
25803
|
215 |
ClassLoader getContextClassLoader() {
|
2
|
216 |
|
25803
|
217 |
PrivilegedAction<ClassLoader> act = () -> {
|
|
218 |
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
|
219 |
if (loader == null) {
|
|
220 |
// Don't use bootstrap class loader directly!
|
|
221 |
loader = ClassLoader.getSystemClassLoader();
|
|
222 |
}
|
|
223 |
return loader;
|
|
224 |
};
|
|
225 |
return AccessController.doPrivileged(act);
|
|
226 |
}
|
|
227 |
|
|
228 |
private static URL[] getUrlArray(String codebase)
|
|
229 |
throws MalformedURLException {
|
2
|
230 |
// Parse codebase into separate URLs
|
|
231 |
StringTokenizer parser = new StringTokenizer(codebase);
|
25803
|
232 |
List<String> list = new ArrayList<>();
|
2
|
233 |
while (parser.hasMoreTokens()) {
|
25803
|
234 |
list.add(parser.nextToken());
|
2
|
235 |
}
|
25803
|
236 |
String[] url = new String[list.size()];
|
2
|
237 |
for (int i = 0; i < url.length; i++) {
|
25803
|
238 |
url[i] = list.get(i);
|
2
|
239 |
}
|
|
240 |
|
|
241 |
URL[] urlArray = new URL[url.length];
|
|
242 |
for (int i = 0; i < urlArray.length; i++) {
|
|
243 |
urlArray[i] = new URL(url[i]);
|
|
244 |
}
|
|
245 |
return urlArray;
|
|
246 |
}
|
25803
|
247 |
|
|
248 |
/**
|
|
249 |
* Given an enumeration of URLs, an instance of this class represents
|
|
250 |
* an enumeration of their InputStreams. Each operation on the URL
|
|
251 |
* enumeration is performed within a doPrivileged block.
|
|
252 |
* This is used to enumerate the resources under a foreign codebase.
|
|
253 |
* This class is not MT-safe.
|
|
254 |
*/
|
|
255 |
private class InputStreamEnumeration implements
|
|
256 |
NamingEnumeration<InputStream> {
|
|
257 |
|
|
258 |
private final Enumeration<URL> urls;
|
|
259 |
|
|
260 |
private InputStream nextElement;
|
|
261 |
|
|
262 |
InputStreamEnumeration(Enumeration<URL> urls) {
|
|
263 |
this.urls = urls;
|
|
264 |
}
|
|
265 |
|
|
266 |
/*
|
|
267 |
* Returns the next InputStream, or null if there are no more.
|
|
268 |
* An InputStream that cannot be opened is skipped.
|
|
269 |
*/
|
|
270 |
private InputStream getNextElement() {
|
|
271 |
PrivilegedAction<InputStream> act = () -> {
|
|
272 |
while (urls.hasMoreElements()) {
|
|
273 |
try {
|
|
274 |
return urls.nextElement().openStream();
|
|
275 |
} catch (IOException e) {
|
|
276 |
// skip this URL
|
|
277 |
}
|
|
278 |
}
|
|
279 |
return null;
|
|
280 |
};
|
|
281 |
return AccessController.doPrivileged(act);
|
|
282 |
}
|
|
283 |
|
|
284 |
public boolean hasMore() {
|
|
285 |
if (nextElement != null) {
|
|
286 |
return true;
|
|
287 |
}
|
|
288 |
nextElement = getNextElement();
|
|
289 |
return (nextElement != null);
|
|
290 |
}
|
|
291 |
|
|
292 |
public boolean hasMoreElements() {
|
|
293 |
return hasMore();
|
|
294 |
}
|
|
295 |
|
|
296 |
public InputStream next() {
|
|
297 |
if (hasMore()) {
|
|
298 |
InputStream res = nextElement;
|
|
299 |
nextElement = null;
|
|
300 |
return res;
|
|
301 |
} else {
|
|
302 |
throw new NoSuchElementException();
|
|
303 |
}
|
|
304 |
}
|
|
305 |
|
|
306 |
public InputStream nextElement() {
|
|
307 |
return next();
|
|
308 |
}
|
|
309 |
|
|
310 |
public void close() {
|
|
311 |
}
|
|
312 |
}
|
2
|
313 |
}
|