1. /*
  2. * @(#)NamingManager.java 1.15 01/02/09
  3. *
  4. * Copyright 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.naming.spi;
  11. import java.util.Enumeration;
  12. import java.util.Hashtable;
  13. import java.util.StringTokenizer;
  14. import java.net.MalformedURLException;
  15. import javax.naming.*;
  16. import com.sun.naming.internal.VersionHelper;
  17. import com.sun.naming.internal.ResourceManager;
  18. import com.sun.naming.internal.FactoryEnumeration;
  19. /**
  20. * This class contains methods for creating context objects
  21. * and objects referred to by location information in the naming
  22. * or directory service.
  23. *<p>
  24. * This class cannot be instantiated. It has only static methods.
  25. *<p>
  26. * The mention of URL in the documentation for this class refers to
  27. * a URL string as defined by RFC 1738 and its related RFCs. It is
  28. * any string that conforms to the syntax described therein, and
  29. * may not always have corresponding support in the java.net.URL
  30. * class or Web browsers.
  31. *<p>
  32. * NamingManager is safe for concurrent access by multiple threads.
  33. *<p>
  34. * Except as otherwise noted,
  35. * a <tt>Name</tt> or environment parameter
  36. * passed to any method is owned by the caller.
  37. * The implementation will not modify the object or keep a reference
  38. * to it, although it may keep a reference to a clone or copy.
  39. *
  40. * @author Rosanna Lee
  41. * @author Scott Seligman
  42. * @version 1.15 01/02/09
  43. * @since 1.3
  44. */
  45. public class NamingManager {
  46. /*
  47. * Disallow anyone from creating one of these.
  48. * Made package private so that DirectoryManager can subclass.
  49. */
  50. NamingManager() {}
  51. // should be protected and package private
  52. static final VersionHelper helper = VersionHelper.getVersionHelper();
  53. // --------- object factory stuff
  54. /**
  55. * Package-private; used by DirectoryManager and NamingManager.
  56. */
  57. private static ObjectFactoryBuilder object_factory_builder = null;
  58. /**
  59. * The ObjectFactoryBuilder determines the policy used when
  60. * trying to load object factories.
  61. * See getObjectInstance() and class ObjectFactory for a description
  62. * of the default policy.
  63. * setObjectFactoryBuilder() overrides this default policy by installing
  64. * an ObjectFactoryBuilder. Subsequent object factories will
  65. * be loaded and created using the installed builder.
  66. *<p>
  67. * The builder can only be installed if the executing thread is allowed
  68. * (by the security manager's checkSetFactory() method) to do so.
  69. * Once installed, the builder cannot be replaced.
  70. *<p>
  71. * @param builder The factory builder to install. If null, no builder
  72. * is installed.
  73. * @exception SecurityException builder cannot be installed
  74. * for security reasons.
  75. * @exception NamingException builder cannot be installed for
  76. * a non-security-related reason.
  77. * @exception IllegalStateException If a factory has already been installed.
  78. * @see #getObjectInstance
  79. * @see ObjectFactory
  80. * @see ObjectFactoryBuilder
  81. * @see java.lang.SecurityManager#checkSetFactory
  82. */
  83. public static synchronized void setObjectFactoryBuilder(
  84. ObjectFactoryBuilder builder) throws NamingException {
  85. if (object_factory_builder != null)
  86. throw new IllegalStateException("ObjectFactoryBuilder already set");
  87. SecurityManager security = System.getSecurityManager();
  88. if (security != null) {
  89. security.checkSetFactory();
  90. }
  91. object_factory_builder = builder;
  92. }
  93. /**
  94. * Used for accessing object factory builder.
  95. */
  96. static synchronized ObjectFactoryBuilder getObjectFactoryBuilder() {
  97. return object_factory_builder;
  98. }
  99. /**
  100. * Retrieves the ObjectFactory for the object identified by a reference,
  101. * using the reference's factory class name and factory codebase
  102. * to load in the factory's class.
  103. * @param ref The non-null reference to use.
  104. * @param factoryName The non-null class name of the factory.
  105. * @return The object factory for the object identified by ref; null
  106. * if unable to load the factory.
  107. */
  108. static ObjectFactory getObjectFactoryFromReference(
  109. Reference ref, String factoryName)
  110. throws IllegalAccessException,
  111. InstantiationException,
  112. MalformedURLException {
  113. Class clas = null;
  114. // Try to use current class loader
  115. try {
  116. clas = helper.loadClass(factoryName);
  117. } catch (ClassNotFoundException e) {
  118. // ignore and continue
  119. // e.printStackTrace();
  120. }
  121. // All other exceptions are passed up.
  122. // Not in class path; try to use codebase
  123. String codebase;
  124. if (clas == null &&
  125. (codebase = ref.getFactoryClassLocation()) != null) {
  126. try {
  127. clas = helper.loadClass(factoryName, codebase);
  128. } catch (ClassNotFoundException e) {
  129. }
  130. }
  131. return (clas != null) ? (ObjectFactory) clas.newInstance() : null;
  132. }
  133. /**
  134. * Creates an object using the factories specified in the
  135. * <tt>Context.OBJECT_FACTORIES</tt> property of the environment
  136. * or of the provider resource file associated with <tt>nameCtx</tt>.
  137. *
  138. * @return factory created; null if cannot create
  139. */
  140. private static Object createObjectFromFactories(Object obj, Name name,
  141. Context nameCtx, Hashtable environment) throws Exception {
  142. FactoryEnumeration factories = ResourceManager.getFactories(
  143. Context.OBJECT_FACTORIES, environment, nameCtx);
  144. if (factories == null)
  145. return null;
  146. // Try each factory until one succeeds
  147. ObjectFactory factory;
  148. Object answer = null;
  149. while (answer == null && factories.hasMore()) {
  150. factory = (ObjectFactory)factories.next();
  151. answer = factory.getObjectInstance(obj, name, nameCtx, environment);
  152. }
  153. return answer;
  154. }
  155. private static String getURLScheme(String str) {
  156. int colon_posn = str.indexOf(':');
  157. int slash_posn = str.indexOf('/');
  158. if (colon_posn > 0 && (slash_posn == -1 || colon_posn < slash_posn))
  159. return str.substring(0, colon_posn);
  160. return null;
  161. }
  162. /**
  163. * Creates an instance of an object for the specified object
  164. * and environment.
  165. * <p>
  166. * If an object factory builder has been installed, it is used to
  167. * create a factory for creating the object.
  168. * Otherwise, the following rules are used to create the object:
  169. *<ol>
  170. * <li>If <code>refInfo</code> is a <code>Reference</code>
  171. * or <code>Referenceable</code> containing a factory class name,
  172. * use the named factory to create the object.
  173. * Return <code>refInfo</code> if the factory cannot be created.
  174. * Under JDK 1.1, if the factory class must be loaded from a location
  175. * specified in the reference, a <tt>SecurityManager</tt> must have
  176. * been installed or the factory creation will fail.
  177. * If an exception is encountered while creating the factory,
  178. * it is passed up to the caller.
  179. * <li>If <tt>refInfo</tt> is a <tt>Reference</tt> or
  180. * <tt>Referenceable</tt> with no factory class name,
  181. * and the address or addresses are <tt>StringRefAddr</tt>s with
  182. * address type "URL",
  183. * try the URL context factory corresponding to each URL's scheme id
  184. * to create the object (see <tt>getURLContext()</tt>).
  185. * If that fails, continue to the next step.
  186. * <li> Use the object factories specified in
  187. * the <tt>Context.OBJECT_FACTORIES</tt> property of the environment,
  188. * and of the provider resource file associated with
  189. * <tt>nameCtx</tt>, in that order.
  190. * The value of this property is a colon-separated list of factory
  191. * class names that are tried in order, and the first one that succeeds
  192. * in creating an object is the one used.
  193. * If none of the factories can be loaded,
  194. * return <code>refInfo</code>.
  195. * If an exception is encountered while creating the object, the
  196. * exception is passed up to the caller.
  197. *</ol>
  198. *<p>
  199. * Service providers that implement the <tt>DirContext</tt>
  200. * interface should use
  201. * <tt>DirectoryManager.getObjectInstance()</tt>, not this method.
  202. * Service providers that implement only the <tt>Context</tt>
  203. * interface should use this method.
  204. * <p>
  205. * Note that an object factory (an object that implements the ObjectFactory
  206. * interface) must be public and must have a public constructor that
  207. * accepts no arguments.
  208. * <p>
  209. * The <code>name</code> and <code>nameCtx</code> parameters may
  210. * optionally be used to specify the name of the object being created.
  211. * <code>name</code> is the name of the object, relative to context
  212. * <code>nameCtx</code>. This information could be useful to the object
  213. * factory or to the object implementation.
  214. * If there are several possible contexts from which the object
  215. * could be named -- as will often be the case -- it is up to
  216. * the caller to select one. A good rule of thumb is to select the
  217. * "deepest" context available.
  218. * If <code>nameCtx</code> is null, <code>name</code> is relative
  219. * to the default initial context. If no name is being specified, the
  220. * <code>name</code> parameter should be null.
  221. *
  222. * @param refInfo The possibly null object for which to create an object.
  223. * @param name The name of this object relative to <code>nameCtx</code>.
  224. * Specifying a name is optional; if it is
  225. * omitted, <code>name</code> should be null.
  226. * @param nameCtx The context relative to which the <code>name</code>
  227. * parameter is specified. If null, <code>name</code> is
  228. * relative to the default initial context.
  229. * @param environment The possibly null environment to
  230. * be used in the creation of the object factory and the object.
  231. * @return An object created using <code>refInfo</code> or
  232. * <code>refInfo</code> if an object cannot be created using
  233. * the algorithm described above.
  234. * @exception NamingException if a naming exception was encountered
  235. * while attempting to get a URL context, or if one of the
  236. * factories accessed throws a NamingException.
  237. * @exception Exception if one of the factories accessed throws an
  238. * exception, or if an error was encountered while loading
  239. * and instantiating the factory and object classes.
  240. * A factory should only throw an exception if it does not want
  241. * other factories to be used in an attempt to create an object.
  242. * See ObjectFactory.getObjectInstance().
  243. * @see #getURLContext
  244. * @see ObjectFactory
  245. * @see ObjectFactory#getObjectInstance
  246. */
  247. public static Object getObjectInstance(Object refInfo, Name name,
  248. Context nameCtx, Hashtable environment) throws Exception {
  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) throws NamingException {
  475. // pass in 'null' to indicate creation of generic context for scheme
  476. // (i.e. not specific to a URL).
  477. Object answer = getURLObject(scheme, null, null, null, environment);
  478. if (answer instanceof Context) {
  479. return (Context)answer;
  480. } else {
  481. return null;
  482. }
  483. }
  484. private static final String defaultPkgPrefix = "com.sun.jndi.url";
  485. /**
  486. * Creates an object for the given URL scheme id using
  487. * the supplied urlInfo.
  488. * <p>
  489. * If urlInfo is null, the result is a context for resolving URLs
  490. * with the scheme id 'scheme'.
  491. * If urlInfo is a URL, the result is a context named by the URL.
  492. * Names passed to this context is assumed to be relative to this
  493. * context (i.e. not a URL). For example, if urlInfo is
  494. * "ldap://ldap.wiz.com/o=Wiz,c=us", the resulting context will
  495. * be that pointed to by "o=Wiz,c=us" on the server 'ldap.wiz.com'.
  496. * Subsequent names that can be passed to this context will be
  497. * LDAP names relative to this context (e.g. cn="Barbs Jensen").
  498. * If urlInfo is an array of URLs, the URLs are assumed
  499. * to be equivalent in terms of the context to which they refer.
  500. * The resulting context is like that of the single URL case.
  501. * If urlInfo is of any other type, that is handled by the
  502. * context factory for the URL scheme.
  503. * @param scheme the URL scheme id for the context
  504. * @param urlInfo information used to create the context
  505. * @param name name of this object relative to <code>nameCtx</code>
  506. * @param nameCtx Context whose provider resource file will be searched
  507. * for package prefix values (or null if none)
  508. * @param environment Environment properties for creating the context
  509. * @see javax.naming.InitialContext
  510. */
  511. private static Object getURLObject(String scheme, Object urlInfo,
  512. Name name, Context nameCtx,
  513. Hashtable environment)
  514. throws NamingException {
  515. // e.g. "ftpURLContextFactory"
  516. ObjectFactory factory = (ObjectFactory)ResourceManager.getFactory(
  517. Context.URL_PKG_PREFIXES, environment, nameCtx,
  518. "." + scheme + "." + scheme + "URLContextFactory", defaultPkgPrefix);
  519. if (factory == null)
  520. return null;
  521. // Found object factory
  522. try {
  523. return factory.getObjectInstance(urlInfo, name, nameCtx, environment);
  524. } catch (NamingException e) {
  525. throw e;
  526. } catch (Exception e) {
  527. NamingException ne = new NamingException();
  528. ne.setRootCause(e);
  529. throw ne;
  530. }
  531. }
  532. // ------------ Initial Context Factory Stuff
  533. private static InitialContextFactoryBuilder initctx_factory_builder = null;
  534. /**
  535. * Use this method for accessing initctx_factory_builder while
  536. * inside an unsychronized method.
  537. */
  538. private static synchronized InitialContextFactoryBuilder
  539. getInitialContextFactoryBuilder() {
  540. return initctx_factory_builder;
  541. }
  542. /**
  543. * Creates an initial context using the specified environment
  544. * properties.
  545. *<p>
  546. * If an InitialContextFactoryBuilder has been installed,
  547. * it is used to create the factory for creating the initial context.
  548. * Otherwise, the class specified in the
  549. * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> environment property is used.
  550. * Note that an initial context factory (an object that implements the
  551. * InitialContextFactory interface) must be public and must have a
  552. * public constructor that accepts no arguments.
  553. *
  554. * @param env The possibly null environment properties used when
  555. * creating the context.
  556. * @return A non-null initial context.
  557. * @exception NoInitialContextException If the
  558. * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> property
  559. * is not found or names a nonexistent
  560. * class or a class that cannot be instantiated,
  561. * or if the initial context could not be created for some other
  562. * reason.
  563. * @exception NamingException If some other naming exception was encountered.
  564. * @see javax.naming.InitialContext
  565. * @see javax.naming.directory.InitialDirContext
  566. */
  567. public static Context getInitialContext(Hashtable env)
  568. throws NamingException {
  569. InitialContextFactory factory;
  570. InitialContextFactoryBuilder builder = getInitialContextFactoryBuilder();
  571. if (builder == null) {
  572. // No factory installed, use property
  573. // Get initial context factory class name
  574. String className = env != null ?
  575. (String)env.get(Context.INITIAL_CONTEXT_FACTORY) : null;
  576. if (className == null) {
  577. NoInitialContextException ne = new NoInitialContextException(
  578. "Need to specify class name in environment or system " +
  579. "property, or as an applet parameter, or in an " +
  580. "application resource file: " +
  581. Context.INITIAL_CONTEXT_FACTORY);
  582. throw ne;
  583. }
  584. try {
  585. factory = (InitialContextFactory)
  586. helper.loadClass(className).newInstance();
  587. } catch(Exception e) {
  588. NoInitialContextException ne =
  589. new NoInitialContextException(
  590. "Cannot instantiate class: " + className);
  591. ne.setRootCause(e);
  592. throw ne;
  593. }
  594. } else {
  595. factory = builder.createInitialContextFactory(env);
  596. }
  597. return factory.getInitialContext(env);
  598. }
  599. /**
  600. * Sets the InitialContextFactory builder to be builder.
  601. *
  602. *<p>
  603. * The builder can only be installed if the executing thread is allowed by
  604. * the security manager to do so. Once installed, the builder cannot
  605. * be replaced.
  606. * @param builder The initial context factory builder to install. If null,
  607. * no builder is set.
  608. * @exception SecurityException builder cannot be installed for security
  609. * reasons.
  610. * @exception NamingException builder cannot be installed for
  611. * a non-security-related reason.
  612. * @exception IllegalStateException If a builder was previous installed.
  613. * @see #hasInitialContextFactoryBuilder
  614. * @see java.lang.SecurityManager#checkSetFactory
  615. */
  616. public static synchronized void setInitialContextFactoryBuilder(
  617. InitialContextFactoryBuilder builder)
  618. throws NamingException {
  619. if (initctx_factory_builder != null)
  620. throw new IllegalStateException(
  621. "InitialContextFactoryBuilder already set");
  622. SecurityManager security = System.getSecurityManager();
  623. if (security != null) {
  624. security.checkSetFactory();
  625. }
  626. initctx_factory_builder = builder;
  627. }
  628. /**
  629. * Determines whether an initial context factory builder has
  630. * been set.
  631. * @return true if an initial context factory builder has
  632. * been set; false otherwise.
  633. * @see #setInitialContextFactoryBuilder
  634. */
  635. public static boolean hasInitialContextFactoryBuilder() {
  636. return (getInitialContextFactoryBuilder() != null);
  637. }
  638. // ----- Continuation Context Stuff
  639. /**
  640. * Constant that holds the name of the environment property into
  641. * which <tt>getContinuationContext()</tt> stores the value of its
  642. * <tt>CannotProceedException</tt> parameter.
  643. * This property is inherited by the continuation context, and may
  644. * be used by that context's service provider to inspect the
  645. * fields of the exception.
  646. *<p>
  647. * The value of this constant is "java.naming.spi.CannotProceedException".
  648. *
  649. * @see #getContinuationContext
  650. * @since 1.3
  651. */
  652. public static final String CPE = "java.naming.spi.CannotProceedException";
  653. /**
  654. * Creates a context in which to continue a context operation.
  655. *<p>
  656. * In performing an operation on a name that spans multiple
  657. * namespaces, a context from one naming system may need to pass
  658. * the operation on to the next naming system. The context
  659. * implementation does this by first constructing a
  660. * <code>CannotProceedException</code> containing information
  661. * pinpointing how far it has proceeded. It then obtains a
  662. * continuation context from JNDI by calling
  663. * <code>getContinuationContext</code>. The context
  664. * implementation should then resume the context operation by
  665. * invoking the same operation on the continuation context, using
  666. * the remainder of the name that has not yet been resolved.
  667. *<p>
  668. * Before making use of the <tt>cpe</tt> parameter, this method
  669. * updates the environment associated with that object by setting
  670. * the value of the property <a href="#CPE"><tt>CPE</tt></a>
  671. * to <tt>cpe</tt>. This property will be inherited by the
  672. * continuation context, and may be used by that context's
  673. * service provider to inspect the fields of this exception.
  674. *
  675. * @param cpe
  676. * The non-null exception that triggered this continuation.
  677. * @return A non-null Context object for continuing the operation.
  678. * @exception NamingException If a naming exception occurred.
  679. */
  680. public static Context getContinuationContext(CannotProceedException cpe)
  681. throws NamingException
  682. {
  683. Hashtable env = cpe.getEnvironment();
  684. if (env == null) {
  685. env = new Hashtable(7);
  686. cpe.setEnvironment(env);
  687. }
  688. env.put(CPE, cpe);
  689. ContinuationContext cctx = new ContinuationContext(cpe);
  690. return cctx.getTargetContext();
  691. }
  692. // ------------ State Factory Stuff
  693. /**
  694. * Retrieves the state of an object for binding.
  695. * <p>
  696. * Service providers that implement the <tt>DirContext</tt> interface
  697. * should use <tt>DirectoryManager.getStateToBind()</tt>, not this method.
  698. * Service providers that implement only the <tt>Context</tt> interface
  699. * should use this method.
  700. *<p>
  701. * This method uses the specified state factories in
  702. * the <tt>Context.STATE_FACTORIES</tt> property from the environment
  703. * properties, and from the provider resource file associated with
  704. * <tt>nameCtx</tt>, in that order.
  705. * The value of this property is a colon-separated list of factory
  706. * class names that are tried in order, and the first one that succeeds
  707. * in returning the object's state is the one used.
  708. * If no object's state can be retrieved in this way, return the
  709. * object itself.
  710. * If an exception is encountered while retrieving the state, the
  711. * exception is passed up to the caller.
  712. * <p>
  713. * Note that a state factory
  714. * (an object that implements the StateFactory
  715. * interface) must be public and must have a public constructor that
  716. * accepts no arguments.
  717. * <p>
  718. * The <code>name</code> and <code>nameCtx</code> parameters may
  719. * optionally be used to specify the name of the object being created.
  720. * See the description of "Name and Context Parameters" in
  721. * {@link ObjectFactory#getObjectInstance
  722. * ObjectFactory.getObjectInstance()}
  723. * for details.
  724. * <p>
  725. * This method may return a <tt>Referenceable</tt> object. The
  726. * service provider obtaining this object may choose to store it
  727. * directly, or to extract its reference (using
  728. * <tt>Referenceable.getReference()</tt>) and store that instead.
  729. *
  730. * @param obj The non-null object for which to get state to bind.
  731. * @param name The name of this object relative to <code>nameCtx</code>,
  732. * or null if no name is specified.
  733. * @param nameCtx The context relative to which the <code>name</code>
  734. * parameter is specified, or null if <code>name</code> is
  735. * relative to the default initial context.
  736. * @param environment The possibly null environment to
  737. * be used in the creation of the state factory and
  738. * the object's state.
  739. * @return The non-null object representing <tt>obj</tt>'s state for
  740. * binding. It could be the object (<tt>obj</tt>) itself.
  741. * @exception NamingException If one of the factories accessed throws an
  742. * exception, or if an error was encountered while loading
  743. * and instantiating the factory and object classes.
  744. * A factory should only throw an exception if it does not want
  745. * other factories to be used in an attempt to create an object.
  746. * See <tt>StateFactory.getStateToBind()</tt>.
  747. * @see StateFactory
  748. * @see StateFactory#getStateToBind
  749. * @see DirectoryManager#getStateToBind
  750. * @since 1.3
  751. */
  752. public static Object getStateToBind(Object obj, Name name, Context nameCtx,
  753. Hashtable environment) throws NamingException {
  754. FactoryEnumeration factories = ResourceManager.getFactories(
  755. Context.STATE_FACTORIES, environment, nameCtx);
  756. if (factories == null) {
  757. return obj;
  758. }
  759. // Try each factory until one succeeds
  760. StateFactory factory;
  761. Object answer = null;
  762. while (answer == null && factories.hasMore()) {
  763. factory = (StateFactory)factories.next();
  764. answer = factory.getStateToBind(obj, name, nameCtx, environment);
  765. }
  766. return (answer != null) ? answer : obj;
  767. }
  768. }