author | sspitsyn |
Wed, 07 Sep 2016 03:35:45 +0000 | |
changeset 40963 | c7c990c4ec68 |
parent 37782 | ad8fe7507ecc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36511 | 2 |
* Copyright (c) 1999, 2015, 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 javax.naming.spi; |
|
27 |
||
36511 | 28 |
import java.net.MalformedURLException; |
27090 | 29 |
import java.util.*; |
36511 | 30 |
|
2 | 31 |
|
32 |
import javax.naming.*; |
|
33 |
import com.sun.naming.internal.VersionHelper; |
|
34 |
import com.sun.naming.internal.ResourceManager; |
|
35 |
import com.sun.naming.internal.FactoryEnumeration; |
|
36 |
||
37 |
/** |
|
38 |
* This class contains methods for creating context objects |
|
39 |
* and objects referred to by location information in the naming |
|
40 |
* or directory service. |
|
41 |
*<p> |
|
42 |
* This class cannot be instantiated. It has only static methods. |
|
43 |
*<p> |
|
44 |
* The mention of URL in the documentation for this class refers to |
|
45 |
* a URL string as defined by RFC 1738 and its related RFCs. It is |
|
46 |
* any string that conforms to the syntax described therein, and |
|
47 |
* may not always have corresponding support in the java.net.URL |
|
48 |
* class or Web browsers. |
|
49 |
*<p> |
|
50 |
* NamingManager is safe for concurrent access by multiple threads. |
|
51 |
*<p> |
|
52 |
* Except as otherwise noted, |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
53 |
* a {@code Name} or environment parameter |
2 | 54 |
* passed to any method is owned by the caller. |
55 |
* The implementation will not modify the object or keep a reference |
|
56 |
* to it, although it may keep a reference to a clone or copy. |
|
57 |
* |
|
58 |
* @author Rosanna Lee |
|
59 |
* @author Scott Seligman |
|
60 |
* @since 1.3 |
|
61 |
*/ |
|
62 |
||
63 |
public class NamingManager { |
|
64 |
||
65 |
/* |
|
66 |
* Disallow anyone from creating one of these. |
|
67 |
* Made package private so that DirectoryManager can subclass. |
|
68 |
*/ |
|
69 |
||
70 |
NamingManager() {} |
|
71 |
||
72 |
// should be protected and package private |
|
73 |
static final VersionHelper helper = VersionHelper.getVersionHelper(); |
|
74 |
||
75 |
// --------- object factory stuff |
|
76 |
||
77 |
/** |
|
78 |
* Package-private; used by DirectoryManager and NamingManager. |
|
79 |
*/ |
|
80 |
private static ObjectFactoryBuilder object_factory_builder = null; |
|
81 |
||
82 |
/** |
|
83 |
* The ObjectFactoryBuilder determines the policy used when |
|
84 |
* trying to load object factories. |
|
85 |
* See getObjectInstance() and class ObjectFactory for a description |
|
86 |
* of the default policy. |
|
87 |
* setObjectFactoryBuilder() overrides this default policy by installing |
|
88 |
* an ObjectFactoryBuilder. Subsequent object factories will |
|
89 |
* be loaded and created using the installed builder. |
|
90 |
*<p> |
|
91 |
* The builder can only be installed if the executing thread is allowed |
|
92 |
* (by the security manager's checkSetFactory() method) to do so. |
|
93 |
* Once installed, the builder cannot be replaced. |
|
23712 | 94 |
* |
2 | 95 |
* @param builder The factory builder to install. If null, no builder |
96 |
* is installed. |
|
97 |
* @exception SecurityException builder cannot be installed |
|
98 |
* for security reasons. |
|
99 |
* @exception NamingException builder cannot be installed for |
|
100 |
* a non-security-related reason. |
|
101 |
* @exception IllegalStateException If a factory has already been installed. |
|
102 |
* @see #getObjectInstance |
|
103 |
* @see ObjectFactory |
|
104 |
* @see ObjectFactoryBuilder |
|
105 |
* @see java.lang.SecurityManager#checkSetFactory |
|
106 |
*/ |
|
107 |
public static synchronized void setObjectFactoryBuilder( |
|
108 |
ObjectFactoryBuilder builder) throws NamingException { |
|
109 |
if (object_factory_builder != null) |
|
110 |
throw new IllegalStateException("ObjectFactoryBuilder already set"); |
|
111 |
||
112 |
SecurityManager security = System.getSecurityManager(); |
|
113 |
if (security != null) { |
|
114 |
security.checkSetFactory(); |
|
115 |
} |
|
116 |
object_factory_builder = builder; |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Used for accessing object factory builder. |
|
121 |
*/ |
|
122 |
static synchronized ObjectFactoryBuilder getObjectFactoryBuilder() { |
|
123 |
return object_factory_builder; |
|
124 |
} |
|
125 |
||
126 |
||
127 |
/** |
|
128 |
* Retrieves the ObjectFactory for the object identified by a reference, |
|
129 |
* using the reference's factory class name and factory codebase |
|
130 |
* to load in the factory's class. |
|
131 |
* @param ref The non-null reference to use. |
|
132 |
* @param factoryName The non-null class name of the factory. |
|
133 |
* @return The object factory for the object identified by ref; null |
|
134 |
* if unable to load the factory. |
|
135 |
*/ |
|
136 |
static ObjectFactory getObjectFactoryFromReference( |
|
137 |
Reference ref, String factoryName) |
|
138 |
throws IllegalAccessException, |
|
139 |
InstantiationException, |
|
140 |
MalformedURLException { |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
141 |
Class<?> clas = null; |
2 | 142 |
|
143 |
// Try to use current class loader |
|
144 |
try { |
|
145 |
clas = helper.loadClass(factoryName); |
|
146 |
} catch (ClassNotFoundException e) { |
|
147 |
// ignore and continue |
|
148 |
// e.printStackTrace(); |
|
149 |
} |
|
150 |
// All other exceptions are passed up. |
|
151 |
||
152 |
// Not in class path; try to use codebase |
|
153 |
String codebase; |
|
154 |
if (clas == null && |
|
155 |
(codebase = ref.getFactoryClassLocation()) != null) { |
|
156 |
try { |
|
157 |
clas = helper.loadClass(factoryName, codebase); |
|
158 |
} catch (ClassNotFoundException e) { |
|
159 |
} |
|
160 |
} |
|
161 |
||
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
162 |
@SuppressWarnings("deprecation") // Class.newInstance |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
163 |
ObjectFactory result = (clas != null) ? (ObjectFactory) clas.newInstance() : null; |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
164 |
return result; |
2 | 165 |
} |
166 |
||
167 |
||
168 |
/** |
|
169 |
* Creates an object using the factories specified in the |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
170 |
* {@code Context.OBJECT_FACTORIES} property of the environment |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
171 |
* or of the provider resource file associated with {@code nameCtx}. |
2 | 172 |
* |
173 |
* @return factory created; null if cannot create |
|
174 |
*/ |
|
175 |
private static Object createObjectFromFactories(Object obj, Name name, |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
176 |
Context nameCtx, Hashtable<?,?> environment) throws Exception { |
2 | 177 |
|
178 |
FactoryEnumeration factories = ResourceManager.getFactories( |
|
179 |
Context.OBJECT_FACTORIES, environment, nameCtx); |
|
180 |
||
181 |
if (factories == null) |
|
182 |
return null; |
|
183 |
||
184 |
// Try each factory until one succeeds |
|
185 |
ObjectFactory factory; |
|
186 |
Object answer = null; |
|
187 |
while (answer == null && factories.hasMore()) { |
|
188 |
factory = (ObjectFactory)factories.next(); |
|
189 |
answer = factory.getObjectInstance(obj, name, nameCtx, environment); |
|
190 |
} |
|
191 |
return answer; |
|
192 |
} |
|
193 |
||
194 |
private static String getURLScheme(String str) { |
|
195 |
int colon_posn = str.indexOf(':'); |
|
196 |
int slash_posn = str.indexOf('/'); |
|
197 |
||
198 |
if (colon_posn > 0 && (slash_posn == -1 || colon_posn < slash_posn)) |
|
199 |
return str.substring(0, colon_posn); |
|
200 |
return null; |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Creates an instance of an object for the specified object |
|
205 |
* and environment. |
|
206 |
* <p> |
|
207 |
* If an object factory builder has been installed, it is used to |
|
208 |
* create a factory for creating the object. |
|
209 |
* Otherwise, the following rules are used to create the object: |
|
210 |
*<ol> |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
211 |
* <li>If {@code refInfo} is a {@code Reference} |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
212 |
* or {@code Referenceable} containing a factory class name, |
2 | 213 |
* use the named factory to create the object. |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
214 |
* Return {@code refInfo} if the factory cannot be created. |
2 | 215 |
* Under JDK 1.1, if the factory class must be loaded from a location |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
216 |
* specified in the reference, a {@code SecurityManager} must have |
2 | 217 |
* been installed or the factory creation will fail. |
218 |
* If an exception is encountered while creating the factory, |
|
219 |
* it is passed up to the caller. |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
220 |
* <li>If {@code refInfo} is a {@code Reference} or |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
221 |
* {@code Referenceable} with no factory class name, |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
222 |
* and the address or addresses are {@code StringRefAddr}s with |
2 | 223 |
* address type "URL", |
224 |
* try the URL context factory corresponding to each URL's scheme id |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
225 |
* to create the object (see {@code getURLContext()}). |
2 | 226 |
* If that fails, continue to the next step. |
227 |
* <li> Use the object factories specified in |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
228 |
* the {@code Context.OBJECT_FACTORIES} property of the environment, |
2 | 229 |
* and of the provider resource file associated with |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
230 |
* {@code nameCtx}, in that order. |
2 | 231 |
* The value of this property is a colon-separated list of factory |
232 |
* class names that are tried in order, and the first one that succeeds |
|
233 |
* in creating an object is the one used. |
|
234 |
* If none of the factories can be loaded, |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
235 |
* return {@code refInfo}. |
2 | 236 |
* If an exception is encountered while creating the object, the |
237 |
* exception is passed up to the caller. |
|
238 |
*</ol> |
|
239 |
*<p> |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
240 |
* Service providers that implement the {@code DirContext} |
2 | 241 |
* interface should use |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
242 |
* {@code DirectoryManager.getObjectInstance()}, not this method. |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
243 |
* Service providers that implement only the {@code Context} |
2 | 244 |
* interface should use this method. |
245 |
* <p> |
|
246 |
* Note that an object factory (an object that implements the ObjectFactory |
|
247 |
* interface) must be public and must have a public constructor that |
|
248 |
* accepts no arguments. |
|
36511 | 249 |
* In cases where the factory is in a named module then it must be in a |
250 |
* package which is exported by that module to the {@code java.naming} |
|
251 |
* module. |
|
2 | 252 |
* <p> |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
253 |
* The {@code name} and {@code nameCtx} parameters may |
2 | 254 |
* optionally be used to specify the name of the object being created. |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
255 |
* {@code name} is the name of the object, relative to context |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
256 |
* {@code nameCtx}. This information could be useful to the object |
2 | 257 |
* factory or to the object implementation. |
258 |
* If there are several possible contexts from which the object |
|
259 |
* could be named -- as will often be the case -- it is up to |
|
260 |
* the caller to select one. A good rule of thumb is to select the |
|
261 |
* "deepest" context available. |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
262 |
* If {@code nameCtx} is null, {@code name} is relative |
2 | 263 |
* to the default initial context. If no name is being specified, the |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
264 |
* {@code name} parameter should be null. |
2 | 265 |
* |
266 |
* @param refInfo The possibly null object for which to create an object. |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
267 |
* @param name The name of this object relative to {@code nameCtx}. |
2 | 268 |
* Specifying a name is optional; if it is |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
269 |
* omitted, {@code name} should be null. |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
270 |
* @param nameCtx The context relative to which the {@code name} |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
271 |
* parameter is specified. If null, {@code name} is |
2 | 272 |
* relative to the default initial context. |
273 |
* @param environment The possibly null environment to |
|
274 |
* be used in the creation of the object factory and the object. |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
275 |
* @return An object created using {@code refInfo}; or |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
276 |
* {@code refInfo} if an object cannot be created using |
2 | 277 |
* the algorithm described above. |
278 |
* @exception NamingException if a naming exception was encountered |
|
279 |
* while attempting to get a URL context, or if one of the |
|
280 |
* factories accessed throws a NamingException. |
|
281 |
* @exception Exception if one of the factories accessed throws an |
|
282 |
* exception, or if an error was encountered while loading |
|
283 |
* and instantiating the factory and object classes. |
|
284 |
* A factory should only throw an exception if it does not want |
|
285 |
* other factories to be used in an attempt to create an object. |
|
286 |
* See ObjectFactory.getObjectInstance(). |
|
287 |
* @see #getURLContext |
|
288 |
* @see ObjectFactory |
|
289 |
* @see ObjectFactory#getObjectInstance |
|
290 |
*/ |
|
291 |
public static Object |
|
292 |
getObjectInstance(Object refInfo, Name name, Context nameCtx, |
|
293 |
Hashtable<?,?> environment) |
|
294 |
throws Exception |
|
295 |
{ |
|
296 |
||
297 |
ObjectFactory factory; |
|
298 |
||
299 |
// Use builder if installed |
|
300 |
ObjectFactoryBuilder builder = getObjectFactoryBuilder(); |
|
301 |
if (builder != null) { |
|
302 |
// builder must return non-null factory |
|
303 |
factory = builder.createObjectFactory(refInfo, environment); |
|
304 |
return factory.getObjectInstance(refInfo, name, nameCtx, |
|
305 |
environment); |
|
306 |
} |
|
307 |
||
308 |
// Use reference if possible |
|
309 |
Reference ref = null; |
|
310 |
if (refInfo instanceof Reference) { |
|
311 |
ref = (Reference) refInfo; |
|
312 |
} else if (refInfo instanceof Referenceable) { |
|
313 |
ref = ((Referenceable)(refInfo)).getReference(); |
|
314 |
} |
|
315 |
||
316 |
Object answer; |
|
317 |
||
318 |
if (ref != null) { |
|
319 |
String f = ref.getFactoryClassName(); |
|
320 |
if (f != null) { |
|
321 |
// if reference identifies a factory, use exclusively |
|
322 |
||
323 |
factory = getObjectFactoryFromReference(ref, f); |
|
324 |
if (factory != null) { |
|
325 |
return factory.getObjectInstance(ref, name, nameCtx, |
|
326 |
environment); |
|
327 |
} |
|
328 |
// No factory found, so return original refInfo. |
|
329 |
// Will reach this point if factory class is not in |
|
330 |
// class path and reference does not contain a URL for it |
|
331 |
return refInfo; |
|
332 |
||
333 |
} else { |
|
334 |
// if reference has no factory, check for addresses |
|
335 |
// containing URLs |
|
336 |
||
337 |
answer = processURLAddrs(ref, name, nameCtx, environment); |
|
338 |
if (answer != null) { |
|
339 |
return answer; |
|
340 |
} |
|
341 |
} |
|
342 |
} |
|
343 |
||
344 |
// try using any specified factories |
|
345 |
answer = |
|
346 |
createObjectFromFactories(refInfo, name, nameCtx, environment); |
|
347 |
return (answer != null) ? answer : refInfo; |
|
348 |
} |
|
349 |
||
350 |
/* |
|
351 |
* Ref has no factory. For each address of type "URL", try its URL |
|
352 |
* context factory. Returns null if unsuccessful in creating and |
|
353 |
* invoking a factory. |
|
354 |
*/ |
|
355 |
static Object processURLAddrs(Reference ref, Name name, Context nameCtx, |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
356 |
Hashtable<?,?> environment) |
2 | 357 |
throws NamingException { |
358 |
||
359 |
for (int i = 0; i < ref.size(); i++) { |
|
360 |
RefAddr addr = ref.get(i); |
|
361 |
if (addr instanceof StringRefAddr && |
|
362 |
addr.getType().equalsIgnoreCase("URL")) { |
|
363 |
||
364 |
String url = (String)addr.getContent(); |
|
365 |
Object answer = processURL(url, name, nameCtx, environment); |
|
366 |
if (answer != null) { |
|
367 |
return answer; |
|
368 |
} |
|
369 |
} |
|
370 |
} |
|
371 |
return null; |
|
372 |
} |
|
373 |
||
374 |
private static Object processURL(Object refInfo, Name name, |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
375 |
Context nameCtx, Hashtable<?,?> environment) |
2 | 376 |
throws NamingException { |
377 |
Object answer; |
|
378 |
||
379 |
// If refInfo is a URL string, try to use its URL context factory |
|
380 |
// If no context found, continue to try object factories. |
|
381 |
if (refInfo instanceof String) { |
|
382 |
String url = (String)refInfo; |
|
383 |
String scheme = getURLScheme(url); |
|
384 |
if (scheme != null) { |
|
385 |
answer = getURLObject(scheme, refInfo, name, nameCtx, |
|
386 |
environment); |
|
387 |
if (answer != null) { |
|
388 |
return answer; |
|
389 |
} |
|
390 |
} |
|
391 |
} |
|
392 |
||
393 |
// If refInfo is an array of URL strings, |
|
394 |
// try to find a context factory for any one of its URLs. |
|
395 |
// If no context found, continue to try object factories. |
|
396 |
if (refInfo instanceof String[]) { |
|
397 |
String[] urls = (String[])refInfo; |
|
398 |
for (int i = 0; i <urls.length; i++) { |
|
399 |
String scheme = getURLScheme(urls[i]); |
|
400 |
if (scheme != null) { |
|
401 |
answer = getURLObject(scheme, refInfo, name, nameCtx, |
|
402 |
environment); |
|
403 |
if (answer != null) |
|
404 |
return answer; |
|
405 |
} |
|
406 |
} |
|
407 |
} |
|
408 |
return null; |
|
409 |
} |
|
410 |
||
411 |
||
412 |
/** |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
413 |
* Retrieves a context identified by {@code obj}, using the specified |
2 | 414 |
* environment. |
415 |
* Used by ContinuationContext. |
|
416 |
* |
|
417 |
* @param obj The object identifying the context. |
|
418 |
* @param name The name of the context being returned, relative to |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
419 |
* {@code nameCtx}, or null if no name is being |
2 | 420 |
* specified. |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
421 |
* See the {@code getObjectInstance} method for |
2 | 422 |
* details. |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
423 |
* @param nameCtx The context relative to which {@code name} is |
2 | 424 |
* specified, or null for the default initial context. |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
425 |
* See the {@code getObjectInstance} method for |
2 | 426 |
* details. |
427 |
* @param environment Environment specifying characteristics of the |
|
428 |
* resulting context. |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
429 |
* @return A context identified by {@code obj}. |
2 | 430 |
* |
431 |
* @see #getObjectInstance |
|
432 |
*/ |
|
433 |
static Context getContext(Object obj, Name name, Context nameCtx, |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
434 |
Hashtable<?,?> environment) throws NamingException { |
2 | 435 |
Object answer; |
436 |
||
437 |
if (obj instanceof Context) { |
|
438 |
// %%% Ignore environment for now. OK since method not public. |
|
439 |
return (Context)obj; |
|
440 |
} |
|
441 |
||
442 |
try { |
|
443 |
answer = getObjectInstance(obj, name, nameCtx, environment); |
|
444 |
} catch (NamingException e) { |
|
445 |
throw e; |
|
446 |
} catch (Exception e) { |
|
447 |
NamingException ne = new NamingException(); |
|
448 |
ne.setRootCause(e); |
|
449 |
throw ne; |
|
450 |
} |
|
451 |
||
452 |
return (answer instanceof Context) |
|
453 |
? (Context)answer |
|
454 |
: null; |
|
455 |
} |
|
456 |
||
457 |
// Used by ContinuationContext |
|
458 |
static Resolver getResolver(Object obj, Name name, Context nameCtx, |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
459 |
Hashtable<?,?> environment) throws NamingException { |
2 | 460 |
Object answer; |
461 |
||
462 |
if (obj instanceof Resolver) { |
|
463 |
// %%% Ignore environment for now. OK since method not public. |
|
464 |
return (Resolver)obj; |
|
465 |
} |
|
466 |
||
467 |
try { |
|
468 |
answer = getObjectInstance(obj, name, nameCtx, environment); |
|
469 |
} catch (NamingException e) { |
|
470 |
throw e; |
|
471 |
} catch (Exception e) { |
|
472 |
NamingException ne = new NamingException(); |
|
473 |
ne.setRootCause(e); |
|
474 |
throw ne; |
|
475 |
} |
|
476 |
||
477 |
return (answer instanceof Resolver) |
|
478 |
? (Resolver)answer |
|
479 |
: null; |
|
480 |
} |
|
481 |
||
482 |
||
483 |
/***************** URL Context implementations ***************/ |
|
484 |
||
485 |
/** |
|
486 |
* Creates a context for the given URL scheme id. |
|
487 |
* <p> |
|
488 |
* The resulting context is for resolving URLs of the |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
489 |
* scheme {@code scheme}. The resulting context is not tied |
2 | 490 |
* to a specific URL. It is able to handle arbitrary URLs with |
491 |
* the specified scheme. |
|
492 |
*<p> |
|
493 |
* The class name of the factory that creates the resulting context |
|
494 |
* has the naming convention <i>scheme-id</i>URLContextFactory |
|
495 |
* (e.g. "ftpURLContextFactory" for the "ftp" scheme-id), |
|
496 |
* in the package specified as follows. |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
497 |
* The {@code Context.URL_PKG_PREFIXES} environment property (which |
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23712
diff
changeset
|
498 |
* may contain values taken from system properties, |
2 | 499 |
* or application resource files) |
500 |
* contains a colon-separated list of package prefixes. |
|
501 |
* Each package prefix in |
|
502 |
* the property is tried in the order specified to load the factory class. |
|
503 |
* The default package prefix is "com.sun.jndi.url" (if none of the |
|
504 |
* specified packages work, this default is tried). |
|
505 |
* The complete package name is constructed using the package prefix, |
|
506 |
* concatenated with the scheme id. |
|
507 |
*<p> |
|
508 |
* For example, if the scheme id is "ldap", and the |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
509 |
* {@code Context.URL_PKG_PREFIXES} property |
2 | 510 |
* contains "com.widget:com.wiz.jndi", |
511 |
* the naming manager would attempt to load the following classes |
|
512 |
* until one is successfully instantiated: |
|
513 |
*<ul> |
|
514 |
* <li>com.widget.ldap.ldapURLContextFactory |
|
515 |
* <li>com.wiz.jndi.ldap.ldapURLContextFactory |
|
516 |
* <li>com.sun.jndi.url.ldap.ldapURLContextFactory |
|
517 |
*</ul> |
|
518 |
* If none of the package prefixes work, null is returned. |
|
519 |
*<p> |
|
520 |
* If a factory is instantiated, it is invoked with the following |
|
521 |
* parameters to produce the resulting context. |
|
522 |
* <p> |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
523 |
* {@code factory.getObjectInstance(null, environment);} |
2 | 524 |
* <p> |
525 |
* For example, invoking getObjectInstance() as shown above |
|
526 |
* on a LDAP URL context factory would return a |
|
527 |
* context that can resolve LDAP urls |
|
528 |
* (e.g. "ldap://ldap.wiz.com/o=wiz,c=us", |
|
529 |
* "ldap://ldap.umich.edu/o=umich,c=us", ...). |
|
530 |
*<p> |
|
531 |
* Note that an object factory (an object that implements the ObjectFactory |
|
532 |
* interface) must be public and must have a public constructor that |
|
533 |
* accepts no arguments. |
|
36511 | 534 |
* In cases where the factory is in a named module then it must be in a |
535 |
* package which is exported by that module to the {@code java.naming} |
|
536 |
* module. |
|
2 | 537 |
* |
538 |
* @param scheme The non-null scheme-id of the URLs supported by the context. |
|
539 |
* @param environment The possibly null environment properties to be |
|
540 |
* used in the creation of the object factory and the context. |
|
541 |
* @return A context for resolving URLs with the |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
542 |
* scheme id {@code scheme}; |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
543 |
* {@code null} if the factory for creating the |
2 | 544 |
* context is not found. |
545 |
* @exception NamingException If a naming exception occurs while creating |
|
546 |
* the context. |
|
547 |
* @see #getObjectInstance |
|
548 |
* @see ObjectFactory#getObjectInstance |
|
549 |
*/ |
|
550 |
public static Context getURLContext(String scheme, |
|
551 |
Hashtable<?,?> environment) |
|
552 |
throws NamingException |
|
553 |
{ |
|
554 |
// pass in 'null' to indicate creation of generic context for scheme |
|
555 |
// (i.e. not specific to a URL). |
|
556 |
||
557 |
Object answer = getURLObject(scheme, null, null, null, environment); |
|
558 |
if (answer instanceof Context) { |
|
559 |
return (Context)answer; |
|
560 |
} else { |
|
561 |
return null; |
|
562 |
} |
|
563 |
} |
|
564 |
||
565 |
private static final String defaultPkgPrefix = "com.sun.jndi.url"; |
|
566 |
||
567 |
/** |
|
568 |
* Creates an object for the given URL scheme id using |
|
569 |
* the supplied urlInfo. |
|
570 |
* <p> |
|
571 |
* If urlInfo is null, the result is a context for resolving URLs |
|
572 |
* with the scheme id 'scheme'. |
|
573 |
* If urlInfo is a URL, the result is a context named by the URL. |
|
574 |
* Names passed to this context is assumed to be relative to this |
|
575 |
* context (i.e. not a URL). For example, if urlInfo is |
|
576 |
* "ldap://ldap.wiz.com/o=Wiz,c=us", the resulting context will |
|
577 |
* be that pointed to by "o=Wiz,c=us" on the server 'ldap.wiz.com'. |
|
578 |
* Subsequent names that can be passed to this context will be |
|
579 |
* LDAP names relative to this context (e.g. cn="Barbs Jensen"). |
|
580 |
* If urlInfo is an array of URLs, the URLs are assumed |
|
581 |
* to be equivalent in terms of the context to which they refer. |
|
582 |
* The resulting context is like that of the single URL case. |
|
583 |
* If urlInfo is of any other type, that is handled by the |
|
584 |
* context factory for the URL scheme. |
|
585 |
* @param scheme the URL scheme id for the context |
|
586 |
* @param urlInfo information used to create the context |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
587 |
* @param name name of this object relative to {@code nameCtx} |
2 | 588 |
* @param nameCtx Context whose provider resource file will be searched |
589 |
* for package prefix values (or null if none) |
|
590 |
* @param environment Environment properties for creating the context |
|
591 |
* @see javax.naming.InitialContext |
|
592 |
*/ |
|
593 |
private static Object getURLObject(String scheme, Object urlInfo, |
|
594 |
Name name, Context nameCtx, |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
595 |
Hashtable<?,?> environment) |
2 | 596 |
throws NamingException { |
597 |
||
598 |
// e.g. "ftpURLContextFactory" |
|
599 |
ObjectFactory factory = (ObjectFactory)ResourceManager.getFactory( |
|
600 |
Context.URL_PKG_PREFIXES, environment, nameCtx, |
|
601 |
"." + scheme + "." + scheme + "URLContextFactory", defaultPkgPrefix); |
|
602 |
||
603 |
if (factory == null) |
|
604 |
return null; |
|
605 |
||
606 |
// Found object factory |
|
607 |
try { |
|
608 |
return factory.getObjectInstance(urlInfo, name, nameCtx, environment); |
|
609 |
} catch (NamingException e) { |
|
610 |
throw e; |
|
611 |
} catch (Exception e) { |
|
612 |
NamingException ne = new NamingException(); |
|
613 |
ne.setRootCause(e); |
|
614 |
throw ne; |
|
615 |
} |
|
616 |
||
617 |
} |
|
618 |
||
619 |
||
620 |
// ------------ Initial Context Factory Stuff |
|
621 |
private static InitialContextFactoryBuilder initctx_factory_builder = null; |
|
622 |
||
623 |
/** |
|
624 |
* Use this method for accessing initctx_factory_builder while |
|
625 |
* inside an unsynchronized method. |
|
626 |
*/ |
|
627 |
private static synchronized InitialContextFactoryBuilder |
|
628 |
getInitialContextFactoryBuilder() { |
|
629 |
return initctx_factory_builder; |
|
630 |
} |
|
631 |
||
632 |
/** |
|
633 |
* Creates an initial context using the specified environment |
|
634 |
* properties. |
|
27090 | 635 |
* <p> |
636 |
* This is done as follows: |
|
637 |
* <ul> |
|
638 |
* <li>If an InitialContextFactoryBuilder has been installed, |
|
639 |
* it is used to create the factory for creating the initial |
|
640 |
* context</li> |
|
641 |
* <li>Otherwise, the class specified in the |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
642 |
* {@code Context.INITIAL_CONTEXT_FACTORY} environment property |
27090 | 643 |
* is used |
644 |
* <ul> |
|
645 |
* <li>First, the {@linkplain java.util.ServiceLoader ServiceLoader} |
|
646 |
* mechanism tries to locate an {@code InitialContextFactory} |
|
647 |
* provider using the current thread's context class loader</li> |
|
648 |
* <li>Failing that, this implementation tries to locate a suitable |
|
649 |
* {@code InitialContextFactory} using a built-in mechanism |
|
650 |
* <br> |
|
651 |
* (Note that an initial context factory (an object that implements |
|
652 |
* the InitialContextFactory interface) must be public and must have |
|
36511 | 653 |
* a public constructor that accepts no arguments. |
654 |
* In cases where the factory is in a named module then it must |
|
655 |
* be in a package which is exported by that module to the |
|
656 |
* {@code java.naming} module.)</li> |
|
27090 | 657 |
* </ul> |
658 |
* </li> |
|
659 |
* </ul> |
|
2 | 660 |
* @param env The possibly null environment properties used when |
661 |
* creating the context. |
|
662 |
* @return A non-null initial context. |
|
663 |
* @exception NoInitialContextException If the |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
664 |
* {@code Context.INITIAL_CONTEXT_FACTORY} property |
2 | 665 |
* is not found or names a nonexistent |
666 |
* class or a class that cannot be instantiated, |
|
667 |
* or if the initial context could not be created for some other |
|
668 |
* reason. |
|
669 |
* @exception NamingException If some other naming exception was encountered. |
|
670 |
* @see javax.naming.InitialContext |
|
671 |
* @see javax.naming.directory.InitialDirContext |
|
672 |
*/ |
|
673 |
public static Context getInitialContext(Hashtable<?,?> env) |
|
674 |
throws NamingException { |
|
27090 | 675 |
InitialContextFactory factory = null; |
2 | 676 |
|
677 |
InitialContextFactoryBuilder builder = getInitialContextFactoryBuilder(); |
|
678 |
if (builder == null) { |
|
27090 | 679 |
// No builder installed, use property |
2 | 680 |
// Get initial context factory class name |
681 |
||
682 |
String className = env != null ? |
|
683 |
(String)env.get(Context.INITIAL_CONTEXT_FACTORY) : null; |
|
684 |
if (className == null) { |
|
685 |
NoInitialContextException ne = new NoInitialContextException( |
|
686 |
"Need to specify class name in environment or system " + |
|
25752
8df7ddac1cb3
8051422: Remove JNDI dependency on java.applet.Applet
prappo
parents:
23712
diff
changeset
|
687 |
"property, or in an application resource file: " + |
2 | 688 |
Context.INITIAL_CONTEXT_FACTORY); |
689 |
throw ne; |
|
690 |
} |
|
691 |
||
27090 | 692 |
ServiceLoader<InitialContextFactory> loader = |
693 |
ServiceLoader.load(InitialContextFactory.class); |
|
694 |
||
695 |
Iterator<InitialContextFactory> iterator = loader.iterator(); |
|
2 | 696 |
try { |
27090 | 697 |
while (iterator.hasNext()) { |
698 |
InitialContextFactory f = iterator.next(); |
|
699 |
if (f.getClass().getName().equals(className)) { |
|
700 |
factory = f; |
|
701 |
break; |
|
702 |
} |
|
703 |
} |
|
704 |
} catch (ServiceConfigurationError e) { |
|
2 | 705 |
NoInitialContextException ne = |
27090 | 706 |
new NoInitialContextException( |
707 |
"Cannot load initial context factory " |
|
708 |
+ "'" + className + "'"); |
|
2 | 709 |
ne.setRootCause(e); |
710 |
throw ne; |
|
711 |
} |
|
27090 | 712 |
|
713 |
if (factory == null) { |
|
714 |
try { |
|
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
715 |
@SuppressWarnings("deprecation") |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
716 |
Object o = helper.loadClass(className).newInstance(); |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
717 |
factory = (InitialContextFactory) o; |
27090 | 718 |
} catch (Exception e) { |
719 |
NoInitialContextException ne = |
|
720 |
new NoInitialContextException( |
|
721 |
"Cannot instantiate class: " + className); |
|
722 |
ne.setRootCause(e); |
|
723 |
throw ne; |
|
724 |
} |
|
725 |
} |
|
2 | 726 |
} else { |
727 |
factory = builder.createInitialContextFactory(env); |
|
728 |
} |
|
729 |
||
730 |
return factory.getInitialContext(env); |
|
731 |
} |
|
732 |
||
733 |
||
734 |
/** |
|
735 |
* Sets the InitialContextFactory builder to be builder. |
|
736 |
* |
|
737 |
*<p> |
|
738 |
* The builder can only be installed if the executing thread is allowed by |
|
739 |
* the security manager to do so. Once installed, the builder cannot |
|
740 |
* be replaced. |
|
741 |
* @param builder The initial context factory builder to install. If null, |
|
742 |
* no builder is set. |
|
743 |
* @exception SecurityException builder cannot be installed for security |
|
744 |
* reasons. |
|
745 |
* @exception NamingException builder cannot be installed for |
|
746 |
* a non-security-related reason. |
|
747 |
* @exception IllegalStateException If a builder was previous installed. |
|
748 |
* @see #hasInitialContextFactoryBuilder |
|
749 |
* @see java.lang.SecurityManager#checkSetFactory |
|
750 |
*/ |
|
751 |
public static synchronized void setInitialContextFactoryBuilder( |
|
752 |
InitialContextFactoryBuilder builder) |
|
753 |
throws NamingException { |
|
754 |
if (initctx_factory_builder != null) |
|
755 |
throw new IllegalStateException( |
|
756 |
"InitialContextFactoryBuilder already set"); |
|
757 |
||
758 |
SecurityManager security = System.getSecurityManager(); |
|
759 |
if (security != null) { |
|
760 |
security.checkSetFactory(); |
|
761 |
} |
|
762 |
initctx_factory_builder = builder; |
|
763 |
} |
|
764 |
||
765 |
/** |
|
766 |
* Determines whether an initial context factory builder has |
|
767 |
* been set. |
|
768 |
* @return true if an initial context factory builder has |
|
769 |
* been set; false otherwise. |
|
770 |
* @see #setInitialContextFactoryBuilder |
|
771 |
*/ |
|
772 |
public static boolean hasInitialContextFactoryBuilder() { |
|
773 |
return (getInitialContextFactoryBuilder() != null); |
|
774 |
} |
|
775 |
||
776 |
// ----- Continuation Context Stuff |
|
777 |
||
778 |
/** |
|
779 |
* Constant that holds the name of the environment property into |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
780 |
* which {@code getContinuationContext()} stores the value of its |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
781 |
* {@code CannotProceedException} parameter. |
2 | 782 |
* This property is inherited by the continuation context, and may |
783 |
* be used by that context's service provider to inspect the |
|
784 |
* fields of the exception. |
|
785 |
*<p> |
|
786 |
* The value of this constant is "java.naming.spi.CannotProceedException". |
|
787 |
* |
|
788 |
* @see #getContinuationContext |
|
789 |
* @since 1.3 |
|
790 |
*/ |
|
791 |
public static final String CPE = "java.naming.spi.CannotProceedException"; |
|
792 |
||
793 |
/** |
|
794 |
* Creates a context in which to continue a context operation. |
|
795 |
*<p> |
|
796 |
* In performing an operation on a name that spans multiple |
|
797 |
* namespaces, a context from one naming system may need to pass |
|
798 |
* the operation on to the next naming system. The context |
|
799 |
* implementation does this by first constructing a |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
800 |
* {@code CannotProceedException} containing information |
2 | 801 |
* pinpointing how far it has proceeded. It then obtains a |
802 |
* continuation context from JNDI by calling |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
803 |
* {@code getContinuationContext}. The context |
2 | 804 |
* implementation should then resume the context operation by |
805 |
* invoking the same operation on the continuation context, using |
|
806 |
* the remainder of the name that has not yet been resolved. |
|
807 |
*<p> |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
808 |
* Before making use of the {@code cpe} parameter, this method |
2 | 809 |
* updates the environment associated with that object by setting |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
810 |
* the value of the property <a href="#CPE">{@code CPE}</a> |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
811 |
* to {@code cpe}. This property will be inherited by the |
2 | 812 |
* continuation context, and may be used by that context's |
813 |
* service provider to inspect the fields of this exception. |
|
814 |
* |
|
815 |
* @param cpe |
|
816 |
* The non-null exception that triggered this continuation. |
|
817 |
* @return A non-null Context object for continuing the operation. |
|
818 |
* @exception NamingException If a naming exception occurred. |
|
819 |
*/ |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
820 |
@SuppressWarnings("unchecked") |
2 | 821 |
public static Context getContinuationContext(CannotProceedException cpe) |
822 |
throws NamingException { |
|
823 |
||
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
824 |
Hashtable<Object,Object> env = (Hashtable<Object,Object>)cpe.getEnvironment(); |
2 | 825 |
if (env == null) { |
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
826 |
env = new Hashtable<>(7); |
2 | 827 |
} else { |
828 |
// Make a (shallow) copy of the environment. |
|
10324
e28265130e4f
7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
829 |
env = (Hashtable<Object,Object>)env.clone(); |
2 | 830 |
} |
831 |
env.put(CPE, cpe); |
|
832 |
||
833 |
ContinuationContext cctx = new ContinuationContext(cpe, env); |
|
834 |
return cctx.getTargetContext(); |
|
835 |
} |
|
836 |
||
837 |
// ------------ State Factory Stuff |
|
838 |
||
839 |
/** |
|
840 |
* Retrieves the state of an object for binding. |
|
841 |
* <p> |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
842 |
* Service providers that implement the {@code DirContext} interface |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
843 |
* should use {@code DirectoryManager.getStateToBind()}, not this method. |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
844 |
* Service providers that implement only the {@code Context} interface |
2 | 845 |
* should use this method. |
846 |
*<p> |
|
847 |
* This method uses the specified state factories in |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
848 |
* the {@code Context.STATE_FACTORIES} property from the environment |
2 | 849 |
* properties, and from the provider resource file associated with |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
850 |
* {@code nameCtx}, in that order. |
2 | 851 |
* The value of this property is a colon-separated list of factory |
852 |
* class names that are tried in order, and the first one that succeeds |
|
853 |
* in returning the object's state is the one used. |
|
854 |
* If no object's state can be retrieved in this way, return the |
|
855 |
* object itself. |
|
856 |
* If an exception is encountered while retrieving the state, the |
|
857 |
* exception is passed up to the caller. |
|
858 |
* <p> |
|
859 |
* Note that a state factory |
|
860 |
* (an object that implements the StateFactory |
|
861 |
* interface) must be public and must have a public constructor that |
|
862 |
* accepts no arguments. |
|
36511 | 863 |
* In cases where the factory is in a named module then it must be in a |
864 |
* package which is exported by that module to the {@code java.naming} |
|
865 |
* module. |
|
2 | 866 |
* <p> |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
867 |
* The {@code name} and {@code nameCtx} parameters may |
2 | 868 |
* optionally be used to specify the name of the object being created. |
869 |
* See the description of "Name and Context Parameters" in |
|
870 |
* {@link ObjectFactory#getObjectInstance |
|
871 |
* ObjectFactory.getObjectInstance()} |
|
872 |
* for details. |
|
873 |
* <p> |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
874 |
* This method may return a {@code Referenceable} object. The |
2 | 875 |
* service provider obtaining this object may choose to store it |
876 |
* directly, or to extract its reference (using |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
877 |
* {@code Referenceable.getReference()}) and store that instead. |
2 | 878 |
* |
879 |
* @param obj The non-null object for which to get state to bind. |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
880 |
* @param name The name of this object relative to {@code nameCtx}, |
2 | 881 |
* or null if no name is specified. |
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
882 |
* @param nameCtx The context relative to which the {@code name} |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
883 |
* parameter is specified, or null if {@code name} is |
2 | 884 |
* relative to the default initial context. |
885 |
* @param environment The possibly null environment to |
|
886 |
* be used in the creation of the state factory and |
|
887 |
* the object's state. |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
888 |
* @return The non-null object representing {@code obj}'s state for |
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
889 |
* binding. It could be the object ({@code obj}) itself. |
2 | 890 |
* @exception NamingException If one of the factories accessed throws an |
891 |
* exception, or if an error was encountered while loading |
|
892 |
* and instantiating the factory and object classes. |
|
893 |
* A factory should only throw an exception if it does not want |
|
894 |
* other factories to be used in an attempt to create an object. |
|
32029
a5538163e144
8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents:
27090
diff
changeset
|
895 |
* See {@code StateFactory.getStateToBind()}. |
2 | 896 |
* @see StateFactory |
897 |
* @see StateFactory#getStateToBind |
|
898 |
* @see DirectoryManager#getStateToBind |
|
899 |
* @since 1.3 |
|
900 |
*/ |
|
901 |
public static Object |
|
902 |
getStateToBind(Object obj, Name name, Context nameCtx, |
|
903 |
Hashtable<?,?> environment) |
|
904 |
throws NamingException |
|
905 |
{ |
|
906 |
||
907 |
FactoryEnumeration factories = ResourceManager.getFactories( |
|
908 |
Context.STATE_FACTORIES, environment, nameCtx); |
|
909 |
||
910 |
if (factories == null) { |
|
911 |
return obj; |
|
912 |
} |
|
913 |
||
914 |
// Try each factory until one succeeds |
|
915 |
StateFactory factory; |
|
916 |
Object answer = null; |
|
917 |
while (answer == null && factories.hasMore()) { |
|
918 |
factory = (StateFactory)factories.next(); |
|
919 |
answer = factory.getStateToBind(obj, name, nameCtx, environment); |
|
920 |
} |
|
921 |
||
922 |
return (answer != null) ? answer : obj; |
|
923 |
} |
|
924 |
} |