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