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