author | stefank |
Mon, 29 Jan 2018 15:11:33 +0100 | |
changeset 48815 | d80d521e9cb1 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
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 |
||
28 |
import java.io.InputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.lang.ref.WeakReference; |
|
31 |
import java.util.HashMap; |
|
32 |
import java.util.Hashtable; |
|
33 |
import java.util.Map; |
|
34 |
import java.util.Properties; |
|
35 |
import java.util.StringTokenizer; |
|
36 |
import java.util.List; |
|
37 |
import java.util.ArrayList; |
|
38 |
import java.util.WeakHashMap; |
|
39 |
||
40 |
import javax.naming.*; |
|
41 |
||
42 |
/** |
|
43 |
* The ResourceManager class facilitates the reading of JNDI resource files. |
|
44 |
* |
|
45 |
* @author Rosanna Lee |
|
46 |
* @author Scott Seligman |
|
47 |
*/ |
|
48 |
||
49 |
public final class ResourceManager { |
|
50 |
||
51 |
/* |
|
52 |
* Name of provider resource files (without the package-name prefix.) |
|
53 |
*/ |
|
54 |
private static final String PROVIDER_RESOURCE_FILE_NAME = |
|
55 |
"jndiprovider.properties"; |
|
56 |
||
57 |
/* |
|
58 |
* Name of application resource files. |
|
59 |
*/ |
|
60 |
private static final String APP_RESOURCE_FILE_NAME = "jndi.properties"; |
|
61 |
||
62 |
/* |
|
27565 | 63 |
* Name of properties file in <java.home>/conf. |
2 | 64 |
*/ |
27565 | 65 |
private static final String JRE_CONF_PROPERTY_FILE_NAME = "jndi.properties"; |
2 | 66 |
|
67 |
/* |
|
22089
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
68 |
* Internal environment property, that when set to "true", disables |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
69 |
* application resource files lookup to prevent recursion issues |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
70 |
* when validating signed JARs. |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
71 |
*/ |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
72 |
private static final String DISABLE_APP_RESOURCE_FILES = |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
73 |
"com.sun.naming.disable.app.resource.files"; |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
74 |
|
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
75 |
/* |
2 | 76 |
* The standard JNDI properties that specify colon-separated lists. |
77 |
*/ |
|
78 |
private static final String[] listProperties = { |
|
79 |
Context.OBJECT_FACTORIES, |
|
80 |
Context.URL_PKG_PREFIXES, |
|
81 |
Context.STATE_FACTORIES, |
|
82 |
// The following shouldn't create a runtime dependence on ldap package. |
|
83 |
javax.naming.ldap.LdapContext.CONTROL_FACTORIES |
|
84 |
}; |
|
85 |
||
86 |
private static final VersionHelper helper = |
|
87 |
VersionHelper.getVersionHelper(); |
|
88 |
||
89 |
/* |
|
90 |
* A cache of the properties that have been constructed by |
|
91 |
* the ResourceManager. A Hashtable from a provider resource |
|
92 |
* file is keyed on a class in the resource file's package. |
|
93 |
* One from application resource files is keyed on the thread's |
|
94 |
* context class loader. |
|
95 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
96 |
// WeakHashMap<Class | ClassLoader, Hashtable> |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
97 |
private static final WeakHashMap<Object, Hashtable<? super String, Object>> |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
98 |
propertiesCache = new WeakHashMap<>(11); |
2 | 99 |
|
100 |
/* |
|
101 |
* A cache of factory objects (ObjectFactory, StateFactory, ControlFactory). |
|
102 |
* |
|
103 |
* A two-level cache keyed first on context class loader and then |
|
104 |
* on propValue. Value is a list of class or factory objects, |
|
105 |
* weakly referenced so as not to prevent GC of the class loader. |
|
106 |
* Used in getFactories(). |
|
107 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
108 |
private static final |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
109 |
WeakHashMap<ClassLoader, Map<String, List<NamedWeakReference<Object>>>> |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
110 |
factoryCache = new WeakHashMap<>(11); |
2 | 111 |
|
112 |
/* |
|
113 |
* A cache of URL factory objects (ObjectFactory). |
|
114 |
* |
|
115 |
* A two-level cache keyed first on context class loader and then |
|
116 |
* on classSuffix+propValue. Value is the factory itself (weakly |
|
117 |
* referenced so as not to prevent GC of the class loader) or |
|
118 |
* NO_FACTORY if a previous search revealed no factory. Used in |
|
119 |
* getFactory(). |
|
120 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
121 |
private static final |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
122 |
WeakHashMap<ClassLoader, Map<String, WeakReference<Object>>> |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
123 |
urlFactoryCache = new WeakHashMap<>(11); |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
124 |
private static final WeakReference<Object> NO_FACTORY = |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
125 |
new WeakReference<>(null); |
2 | 126 |
|
127 |
// There should be no instances of this class. |
|
128 |
private ResourceManager() { |
|
129 |
} |
|
130 |
||
131 |
||
132 |
// ---------- Public methods ---------- |
|
133 |
||
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
134 |
/** |
2 | 135 |
* Given the environment parameter passed to the initial context |
136 |
* constructor, returns the full environment for that initial |
|
137 |
* context (never null). This is based on the environment |
|
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
138 |
* parameter, the system properties, and all application resource files. |
2 | 139 |
* |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27565
diff
changeset
|
140 |
* <p> This method will modify {@code env} and save |
2 | 141 |
* a reference to it. The caller may no longer modify it. |
142 |
* |
|
143 |
* @param env environment passed to initial context constructor. |
|
144 |
* Null indicates an empty environment. |
|
145 |
* |
|
146 |
* @throws NamingException if an error occurs while reading a |
|
147 |
* resource file |
|
148 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
149 |
@SuppressWarnings("unchecked") |
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
150 |
public static Hashtable<?, ?> getInitialEnvironment(Hashtable<?, ?> env) |
2 | 151 |
throws NamingException |
152 |
{ |
|
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
153 |
String[] props = VersionHelper.PROPS; // system properties |
2 | 154 |
if (env == null) { |
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
155 |
env = new Hashtable<>(11); |
2 | 156 |
} |
157 |
||
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
158 |
// Merge property values from env param, and system properties. |
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
159 |
// The first value wins: there's no concatenation of |
2 | 160 |
// colon-separated lists. |
161 |
// Read system properties by first trying System.getProperties(), |
|
162 |
// and then trying System.getProperty() if that fails. The former |
|
163 |
// is more efficient due to fewer permission checks. |
|
164 |
// |
|
165 |
String[] jndiSysProps = helper.getJndiProperties(); |
|
166 |
for (int i = 0; i < props.length; i++) { |
|
167 |
Object val = env.get(props[i]); |
|
168 |
if (val == null) { |
|
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
169 |
// Read system property. |
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
170 |
val = (jndiSysProps != null) |
2 | 171 |
? jndiSysProps[i] |
172 |
: helper.getJndiProperty(i); |
|
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
173 |
} |
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
174 |
if (val != null) { |
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
175 |
((Hashtable<String, Object>)env).put(props[i], val); |
2 | 176 |
} |
177 |
} |
|
178 |
||
22089
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
179 |
// Return without merging if application resource files lookup |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
180 |
// is disabled. |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
181 |
String disableAppRes = (String)env.get(DISABLE_APP_RESOURCE_FILES); |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
182 |
if (disableAppRes != null && disableAppRes.equalsIgnoreCase("true")) { |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
183 |
return env; |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
184 |
} |
ba496138fa35
8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents:
14510
diff
changeset
|
185 |
|
2 | 186 |
// Merge the above with the values read from all application |
187 |
// resource files. Colon-separated lists are concatenated. |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
188 |
mergeTables((Hashtable<Object, Object>)env, getApplicationResources()); |
2 | 189 |
return env; |
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* Retrieves the property from the environment, or from the provider |
|
194 |
* resource file associated with the given context. The environment |
|
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
195 |
* may in turn contain values that come from system properties, |
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
196 |
* or application resource files. |
2 | 197 |
* |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27565
diff
changeset
|
198 |
* If {@code concat} is true and both the environment and the provider |
2 | 199 |
* resource file contain the property, the two values are concatenated |
200 |
* (with a ':' separator). |
|
201 |
* |
|
202 |
* Returns null if no value is found. |
|
203 |
* |
|
204 |
* @param propName The non-null property name |
|
205 |
* @param env The possibly null environment properties |
|
206 |
* @param ctx The possibly null context |
|
207 |
* @param concat True if multiple values should be concatenated |
|
208 |
* @return the property value, or null is there is none. |
|
209 |
* @throws NamingException if an error occurs while reading the provider |
|
210 |
* resource file. |
|
211 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
212 |
public static String getProperty(String propName, Hashtable<?,?> env, |
2 | 213 |
Context ctx, boolean concat) |
214 |
throws NamingException { |
|
215 |
||
216 |
String val1 = (env != null) ? (String)env.get(propName) : null; |
|
217 |
if ((ctx == null) || |
|
218 |
((val1 != null) && !concat)) { |
|
219 |
return val1; |
|
220 |
} |
|
221 |
String val2 = (String)getProviderResource(ctx).get(propName); |
|
222 |
if (val1 == null) { |
|
223 |
return val2; |
|
224 |
} else if ((val2 == null) || !concat) { |
|
225 |
return val1; |
|
226 |
} else { |
|
227 |
return (val1 + ":" + val2); |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Retrieves an enumeration of factory classes/object specified by a |
|
233 |
* property. |
|
234 |
* |
|
235 |
* The property is gotten from the environment and the provider |
|
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23010
diff
changeset
|
236 |
* resource file associated with the given context and concatenated. |
2 | 237 |
* See getProperty(). The resulting property value is a list of class names. |
238 |
*<p> |
|
239 |
* This method then loads each class using the current thread's context |
|
240 |
* class loader and keeps them in a list. Any class that cannot be loaded |
|
241 |
* is ignored. The resulting list is then cached in a two-level |
|
242 |
* hash table, keyed first by the context class loader and then by |
|
243 |
* the property's value. |
|
244 |
* The next time threads of the same context class loader call this |
|
245 |
* method, they can use the cached list. |
|
246 |
*<p> |
|
247 |
* After obtaining the list either from the cache or by creating one from |
|
248 |
* the property value, this method then creates and returns a |
|
249 |
* FactoryEnumeration using the list. As the FactoryEnumeration is |
|
250 |
* traversed, the cached Class object in the list is instantiated and |
|
251 |
* replaced by an instance of the factory object itself. Both class |
|
252 |
* objects and factories are wrapped in weak references so as not to |
|
253 |
* prevent GC of the class loader. |
|
254 |
*<p> |
|
255 |
* Note that multiple threads can be accessing the same cached list |
|
256 |
* via FactoryEnumeration, which locks the list during each next(). |
|
257 |
* The size of the list will not change, |
|
258 |
* but a cached Class object might be replaced by an instantiated factory |
|
259 |
* object. |
|
260 |
* |
|
261 |
* @param propName The non-null property name |
|
262 |
* @param env The possibly null environment properties |
|
263 |
* @param ctx The possibly null context |
|
264 |
* @return An enumeration of factory classes/objects; null if none. |
|
265 |
* @exception NamingException If encounter problem while reading the provider |
|
266 |
* property file. |
|
267 |
* @see javax.naming.spi.NamingManager#getObjectInstance |
|
268 |
* @see javax.naming.spi.NamingManager#getStateToBind |
|
269 |
* @see javax.naming.spi.DirectoryManager#getObjectInstance |
|
270 |
* @see javax.naming.spi.DirectoryManager#getStateToBind |
|
271 |
* @see javax.naming.ldap.ControlFactory#getControlInstance |
|
272 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
273 |
public static FactoryEnumeration getFactories(String propName, |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
274 |
Hashtable<?,?> env, Context ctx) throws NamingException { |
2 | 275 |
|
276 |
String facProp = getProperty(propName, env, ctx, true); |
|
277 |
if (facProp == null) |
|
278 |
return null; // no classes specified; return null |
|
279 |
||
280 |
// Cache is based on context class loader and property val |
|
281 |
ClassLoader loader = helper.getContextClassLoader(); |
|
282 |
||
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
283 |
Map<String, List<NamedWeakReference<Object>>> perLoaderCache = null; |
2 | 284 |
synchronized (factoryCache) { |
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
285 |
perLoaderCache = factoryCache.get(loader); |
2 | 286 |
if (perLoaderCache == null) { |
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
287 |
perLoaderCache = new HashMap<>(11); |
2 | 288 |
factoryCache.put(loader, perLoaderCache); |
289 |
} |
|
290 |
} |
|
291 |
||
292 |
synchronized (perLoaderCache) { |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
293 |
List<NamedWeakReference<Object>> factories = |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
294 |
perLoaderCache.get(facProp); |
2 | 295 |
if (factories != null) { |
296 |
// Cached list |
|
297 |
return factories.size() == 0 ? null |
|
298 |
: new FactoryEnumeration(factories, loader); |
|
299 |
} else { |
|
300 |
// Populate list with classes named in facProp; skipping |
|
301 |
// those that we cannot load |
|
302 |
StringTokenizer parser = new StringTokenizer(facProp, ":"); |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
303 |
factories = new ArrayList<>(5); |
2 | 304 |
while (parser.hasMoreTokens()) { |
305 |
try { |
|
306 |
// System.out.println("loading"); |
|
307 |
String className = parser.nextToken(); |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
308 |
Class<?> c = helper.loadClass(className, loader); |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
309 |
factories.add(new NamedWeakReference<Object>(c, className)); |
2 | 310 |
} catch (Exception e) { |
311 |
// ignore ClassNotFoundException, IllegalArgumentException |
|
312 |
} |
|
313 |
} |
|
314 |
// System.out.println("adding to cache: " + factories); |
|
315 |
perLoaderCache.put(facProp, factories); |
|
316 |
return new FactoryEnumeration(factories, loader); |
|
317 |
} |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
/** |
|
322 |
* Retrieves a factory from a list of packages specified in a |
|
323 |
* property. |
|
324 |
* |
|
325 |
* The property is gotten from the environment and the provider |
|
326 |
* resource file associated with the given context and concatenated. |
|
327 |
* classSuffix is added to the end of this list. |
|
328 |
* See getProperty(). The resulting property value is a list of package |
|
329 |
* prefixes. |
|
330 |
*<p> |
|
331 |
* This method then constructs a list of class names by concatenating |
|
332 |
* each package prefix with classSuffix and attempts to load and |
|
333 |
* instantiate the class until one succeeds. |
|
334 |
* Any class that cannot be loaded is ignored. |
|
335 |
* The resulting object is then cached in a two-level hash table, |
|
336 |
* keyed first by the context class loader and then by the property's |
|
337 |
* value and classSuffix. |
|
338 |
* The next time threads of the same context class loader call this |
|
339 |
* method, they use the cached factory. |
|
340 |
* If no factory can be loaded, NO_FACTORY is recorded in the table |
|
341 |
* so that next time it'll return quickly. |
|
342 |
* |
|
343 |
* @param propName The non-null property name |
|
344 |
* @param env The possibly null environment properties |
|
345 |
* @param ctx The possibly null context |
|
346 |
* @param classSuffix The non-null class name |
|
347 |
* (e.g. ".ldap.ldapURLContextFactory). |
|
348 |
* @param defaultPkgPrefix The non-null default package prefix. |
|
349 |
* (e.g., "com.sun.jndi.url"). |
|
350 |
* @return An factory object; null if none. |
|
351 |
* @exception NamingException If encounter problem while reading the provider |
|
352 |
* property file, or problem instantiating the factory. |
|
353 |
* |
|
354 |
* @see javax.naming.spi.NamingManager#getURLContext |
|
355 |
* @see javax.naming.spi.NamingManager#getURLObject |
|
356 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
357 |
public static Object getFactory(String propName, Hashtable<?,?> env, |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
358 |
Context ctx, String classSuffix, String defaultPkgPrefix) |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
359 |
throws NamingException { |
2 | 360 |
|
361 |
// Merge property with provider property and supplied default |
|
362 |
String facProp = getProperty(propName, env, ctx, true); |
|
363 |
if (facProp != null) |
|
364 |
facProp += (":" + defaultPkgPrefix); |
|
365 |
else |
|
366 |
facProp = defaultPkgPrefix; |
|
367 |
||
368 |
// Cache factory based on context class loader, class name, and |
|
369 |
// property val |
|
370 |
ClassLoader loader = helper.getContextClassLoader(); |
|
371 |
String key = classSuffix + " " + facProp; |
|
372 |
||
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
373 |
Map<String, WeakReference<Object>> perLoaderCache = null; |
2 | 374 |
synchronized (urlFactoryCache) { |
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
375 |
perLoaderCache = urlFactoryCache.get(loader); |
2 | 376 |
if (perLoaderCache == null) { |
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
377 |
perLoaderCache = new HashMap<>(11); |
2 | 378 |
urlFactoryCache.put(loader, perLoaderCache); |
379 |
} |
|
380 |
} |
|
381 |
||
382 |
synchronized (perLoaderCache) { |
|
383 |
Object factory = null; |
|
384 |
||
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
385 |
WeakReference<Object> factoryRef = perLoaderCache.get(key); |
2 | 386 |
if (factoryRef == NO_FACTORY) { |
387 |
return null; |
|
388 |
} else if (factoryRef != null) { |
|
389 |
factory = factoryRef.get(); |
|
390 |
if (factory != null) { // check if weak ref has been cleared |
|
391 |
return factory; |
|
392 |
} |
|
393 |
} |
|
394 |
||
395 |
// Not cached; find first factory and cache |
|
396 |
StringTokenizer parser = new StringTokenizer(facProp, ":"); |
|
397 |
String className; |
|
398 |
while (factory == null && parser.hasMoreTokens()) { |
|
399 |
className = parser.nextToken() + classSuffix; |
|
400 |
try { |
|
401 |
// System.out.println("loading " + className); |
|
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
32029
diff
changeset
|
402 |
@SuppressWarnings("deprecation") // Class.newInstance |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
32029
diff
changeset
|
403 |
Object tmp = helper.loadClass(className, loader).newInstance(); |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
32029
diff
changeset
|
404 |
factory = tmp; |
2 | 405 |
} catch (InstantiationException e) { |
406 |
NamingException ne = |
|
407 |
new NamingException("Cannot instantiate " + className); |
|
408 |
ne.setRootCause(e); |
|
409 |
throw ne; |
|
410 |
} catch (IllegalAccessException e) { |
|
411 |
NamingException ne = |
|
412 |
new NamingException("Cannot access " + className); |
|
413 |
ne.setRootCause(e); |
|
414 |
throw ne; |
|
415 |
} catch (Exception e) { |
|
416 |
// ignore ClassNotFoundException, IllegalArgumentException, |
|
417 |
// etc. |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
// Cache it. |
|
422 |
perLoaderCache.put(key, (factory != null) |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
423 |
? new WeakReference<>(factory) |
2 | 424 |
: NO_FACTORY); |
425 |
return factory; |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
||
430 |
// ---------- Private methods ---------- |
|
431 |
||
432 |
/* |
|
433 |
* Returns the properties contained in the provider resource file |
|
434 |
* of an object's package. Returns an empty hash table if the |
|
435 |
* object is null or the resource file cannot be found. The |
|
436 |
* results are cached. |
|
437 |
* |
|
438 |
* @throws NamingException if an error occurs while reading the file. |
|
439 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
440 |
private static Hashtable<? super String, Object> |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
441 |
getProviderResource(Object obj) |
2 | 442 |
throws NamingException |
443 |
{ |
|
444 |
if (obj == null) { |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
445 |
return (new Hashtable<>(1)); |
2 | 446 |
} |
447 |
synchronized (propertiesCache) { |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
448 |
Class<?> c = obj.getClass(); |
2 | 449 |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
450 |
Hashtable<? super String, Object> props = |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
451 |
propertiesCache.get(c); |
2 | 452 |
if (props != null) { |
453 |
return props; |
|
454 |
} |
|
455 |
props = new Properties(); |
|
456 |
||
457 |
InputStream istream = |
|
458 |
helper.getResourceAsStream(c, PROVIDER_RESOURCE_FILE_NAME); |
|
459 |
||
460 |
if (istream != null) { |
|
461 |
try { |
|
462 |
((Properties)props).load(istream); |
|
463 |
} catch (IOException e) { |
|
464 |
NamingException ne = new ConfigurationException( |
|
465 |
"Error reading provider resource file for " + c); |
|
466 |
ne.setRootCause(e); |
|
467 |
throw ne; |
|
468 |
} |
|
469 |
} |
|
470 |
propertiesCache.put(c, props); |
|
471 |
return props; |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
||
476 |
/* |
|
477 |
* Returns the Hashtable (never null) that results from merging |
|
478 |
* all application resource files available to this thread's |
|
27565 | 479 |
* context class loader. The properties file in <java.home>/conf |
2 | 480 |
* is also merged in. The results are cached. |
481 |
* |
|
482 |
* SECURITY NOTES: |
|
483 |
* 1. JNDI needs permission to read the application resource files. |
|
484 |
* 2. Any class will be able to use JNDI to view the contents of |
|
485 |
* the application resource files in its own classpath. Give |
|
486 |
* careful consideration to this before storing sensitive |
|
487 |
* information there. |
|
488 |
* |
|
489 |
* @throws NamingException if an error occurs while reading a resource |
|
490 |
* file. |
|
491 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
492 |
private static Hashtable<? super String, Object> getApplicationResources() |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
493 |
throws NamingException { |
2 | 494 |
|
495 |
ClassLoader cl = helper.getContextClassLoader(); |
|
496 |
||
497 |
synchronized (propertiesCache) { |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
498 |
Hashtable<? super String, Object> result = propertiesCache.get(cl); |
2 | 499 |
if (result != null) { |
500 |
return result; |
|
501 |
} |
|
502 |
||
503 |
try { |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
504 |
NamingEnumeration<InputStream> resources = |
2 | 505 |
helper.getResources(cl, APP_RESOURCE_FILE_NAME); |
14417
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
506 |
try { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
507 |
while (resources.hasMore()) { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
508 |
Properties props = new Properties(); |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
509 |
InputStream istream = resources.next(); |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
510 |
try { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
511 |
props.load(istream); |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
512 |
} finally { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
513 |
istream.close(); |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
514 |
} |
2 | 515 |
|
14417
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
516 |
if (result == null) { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
517 |
result = props; |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
518 |
} else { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
519 |
mergeTables(result, props); |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
520 |
} |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
521 |
} |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
522 |
} finally { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
523 |
while (resources.hasMore()) { |
14510
01a2a198d9ca
8003263: redundant cast build failure after 8003120
weijun
parents:
14417
diff
changeset
|
524 |
resources.next().close(); |
2 | 525 |
} |
526 |
} |
|
527 |
||
27565 | 528 |
// Merge in properties from file in <java.home>/conf. |
2 | 529 |
InputStream istream = |
27565 | 530 |
helper.getJavaHomeConfStream(JRE_CONF_PROPERTY_FILE_NAME); |
2 | 531 |
if (istream != null) { |
14417
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
532 |
try { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
533 |
Properties props = new Properties(); |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
534 |
props.load(istream); |
2 | 535 |
|
14417
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
536 |
if (result == null) { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
537 |
result = props; |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
538 |
} else { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
539 |
mergeTables(result, props); |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
540 |
} |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
541 |
} finally { |
4615017b9cab
8003120: ResourceManager.getApplicationResources() does not close InputStreams
andrew
parents:
10324
diff
changeset
|
542 |
istream.close(); |
2 | 543 |
} |
544 |
} |
|
545 |
||
546 |
} catch (IOException e) { |
|
547 |
NamingException ne = new ConfigurationException( |
|
548 |
"Error reading application resource file"); |
|
549 |
ne.setRootCause(e); |
|
550 |
throw ne; |
|
551 |
} |
|
552 |
if (result == null) { |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
553 |
result = new Hashtable<>(11); |
2 | 554 |
} |
555 |
propertiesCache.put(cl, result); |
|
556 |
return result; |
|
557 |
} |
|
558 |
} |
|
559 |
||
560 |
/* |
|
561 |
* Merge the properties from one hash table into another. Each |
|
562 |
* property in props2 that is not in props1 is added to props1. |
|
563 |
* For each property in both hash tables that is one of the |
|
564 |
* standard JNDI properties that specify colon-separated lists, |
|
565 |
* the values are concatenated and stored in props1. |
|
566 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
567 |
private static void mergeTables(Hashtable<? super String, Object> props1, |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
568 |
Hashtable<? super String, Object> props2) { |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
569 |
for (Object key : props2.keySet()) { |
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
570 |
String prop = (String)key; |
2 | 571 |
Object val1 = props1.get(prop); |
572 |
if (val1 == null) { |
|
573 |
props1.put(prop, props2.get(prop)); |
|
574 |
} else if (isListProperty(prop)) { |
|
575 |
String val2 = (String)props2.get(prop); |
|
576 |
props1.put(prop, ((String)val1) + ":" + val2); |
|
577 |
} |
|
578 |
} |
|
579 |
} |
|
580 |
||
581 |
/* |
|
582 |
* Is a property one of the standard JNDI properties that specify |
|
583 |
* colon-separated lists? |
|
584 |
*/ |
|
585 |
private static boolean isListProperty(String prop) { |
|
586 |
prop = prop.intern(); |
|
587 |
for (int i = 0; i < listProperties.length; i++) { |
|
588 |
if (prop == listProperties[i]) { |
|
589 |
return true; |
|
590 |
} |
|
591 |
} |
|
592 |
return false; |
|
593 |
} |
|
594 |
} |