1. /*
  2. * @(#)NamingManager.java 1.18 03/05/09
  3. *
  4. * Copyright 2003 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.18 03/05/09
  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 getObjectInstance(Object refInfo, Name name,
  245. Context nameCtx, Hashtable environment) throws Exception {
  246. ObjectFactory factory;
  247. // Use builder if installed
  248. ObjectFactoryBuilder builder = getObjectFactoryBuilder();
  249. if (builder != null) {
  250. // builder must return non-null factory
  251. factory = builder.createObjectFactory(refInfo, environment);
  252. return factory.getObjectInstance(refInfo, name, nameCtx,
  253. environment);
  254. }
  255. // Use reference if possible
  256. Reference ref = null;
  257. if (refInfo instanceof Reference) {
  258. ref = (Reference) refInfo;
  259. } else if (refInfo instanceof Referenceable) {
  260. ref = ((Referenceable)(refInfo)).getReference();
  261. }
  262. Object answer;
  263. if (ref != null) {
  264. String f = ref.getFactoryClassName();
  265. if (f != null) {
  266. // if reference identifies a factory, use exclusively
  267. factory = getObjectFactoryFromReference(ref, f);
  268. if (factory != null) {
  269. return factory.getObjectInstance(ref, name, nameCtx,
  270. environment);
  271. }
  272. // No factory found, so return original refInfo.
  273. // Will reach this point if factory class is not in
  274. // class path and reference does not contain a URL for it
  275. return refInfo;
  276. } else {
  277. // if reference has no factory, check for addresses
  278. // containing URLs
  279. answer = processURLAddrs(ref, name, nameCtx, environment);
  280. if (answer != null) {
  281. return answer;
  282. }
  283. }
  284. }
  285. // try using any specified factories
  286. answer =
  287. createObjectFromFactories(refInfo, name, nameCtx, environment);
  288. return (answer != null) ? answer : refInfo;
  289. }
  290. /*
  291. * Ref has no factory. For each address of type "URL", try its URL
  292. * context factory. Returns null if unsuccessful in creating and
  293. * invoking a factory.
  294. */
  295. static Object processURLAddrs(Reference ref, Name name, Context nameCtx,
  296. Hashtable environment)
  297. throws NamingException {
  298. for (int i = 0; i < ref.size(); i++) {
  299. RefAddr addr = ref.get(i);
  300. if (addr instanceof StringRefAddr &&
  301. addr.getType().equalsIgnoreCase("URL")) {
  302. String url = (String)addr.getContent();
  303. Object answer = processURL(url, name, nameCtx, environment);
  304. if (answer != null) {
  305. return answer;
  306. }
  307. }
  308. }
  309. return null;
  310. }
  311. private static Object processURL(Object refInfo, Name name,
  312. Context nameCtx, Hashtable environment)
  313. throws NamingException {
  314. Object answer;
  315. // If refInfo is a URL string, try to use its URL context factory
  316. // If no context found, continue to try object factories.
  317. if (refInfo instanceof String) {
  318. String url = (String)refInfo;
  319. String scheme = getURLScheme(url);
  320. if (scheme != null) {
  321. answer = getURLObject(scheme, refInfo, name, nameCtx,
  322. environment);
  323. if (answer != null) {
  324. return answer;
  325. }
  326. }
  327. }
  328. // If refInfo is an array of URL strings,
  329. // try to find a context factory for any one of its URLs.
  330. // If no context found, continue to try object factories.
  331. if (refInfo instanceof String[]) {
  332. String[] urls = (String[])refInfo;
  333. for (int i = 0; i <urls.length; i++) {
  334. String scheme = getURLScheme(urls[i]);
  335. if (scheme != null) {
  336. answer = getURLObject(scheme, refInfo, name, nameCtx,
  337. environment);
  338. if (answer != null)
  339. return answer;
  340. }
  341. }
  342. }
  343. return null;
  344. }
  345. /**
  346. * Retrieves a context identified by <code>obj</code>, using the specified
  347. * environment.
  348. * Used by ContinuationContext.
  349. *
  350. * @param obj The object identifying the context.
  351. * @param name The name of the context being returned, relative to
  352. * <code>nameCtx</code>, or null if no name is being
  353. * specified.
  354. * See the <code>getObjectInstance</code> method for
  355. * details.
  356. * @param ctx The context relative to which <code>name</code> is
  357. * specified, or null for the default initial context.
  358. * See the <code>getObjectInstance</code> method for
  359. * details.
  360. * @param environment Environment specifying characteristics of the
  361. * resulting context.
  362. * @return A context identified by <code>obj</code>.
  363. *
  364. * @see #getObjectInstance
  365. */
  366. static Context getContext(Object obj, Name name, Context nameCtx,
  367. Hashtable environment) throws NamingException {
  368. Object answer;
  369. if (obj instanceof Context) {
  370. // %%% Ignore environment for now. OK since method not public.
  371. return (Context)obj;
  372. }
  373. try {
  374. answer = getObjectInstance(obj, name, nameCtx, environment);
  375. } catch (NamingException e) {
  376. throw e;
  377. } catch (Exception e) {
  378. NamingException ne = new NamingException();
  379. ne.setRootCause(e);
  380. throw ne;
  381. }
  382. return (answer instanceof Context)
  383. ? (Context)answer
  384. : null;
  385. }
  386. // Used by ContinuationContext
  387. static Resolver getResolver(Object obj, Name name, Context nameCtx,
  388. Hashtable environment) throws NamingException {
  389. Object answer;
  390. if (obj instanceof Resolver) {
  391. // %%% Ignore environment for now. OK since method not public.
  392. return (Resolver)obj;
  393. }
  394. try {
  395. answer = getObjectInstance(obj, name, nameCtx, environment);
  396. } catch (NamingException e) {
  397. throw e;
  398. } catch (Exception e) {
  399. NamingException ne = new NamingException();
  400. ne.setRootCause(e);
  401. throw ne;
  402. }
  403. return (answer instanceof Resolver)
  404. ? (Resolver)answer
  405. : null;
  406. }
  407. /***************** URL Context implementations ***************/
  408. /**
  409. * Creates a context for the given URL scheme id.
  410. * <p>
  411. * The resulting context is for resolving URLs of the
  412. * scheme <code>scheme</code>. The resulting context is not tied
  413. * to a specific URL. It is able to handle arbitrary URLs with
  414. * the specified scheme.
  415. *<p>
  416. * The class name of the factory that creates the resulting context
  417. * has the naming convention <i>scheme-id</i>URLContextFactory
  418. * (e.g. "ftpURLContextFactory" for the "ftp" scheme-id),
  419. * in the package specified as follows.
  420. * The <tt>Context.URL_PKG_PREFIXES</tt> environment property (which
  421. * may contain values taken from applet parameters, system properties,
  422. * or application resource files)
  423. * contains a colon-separated list of package prefixes.
  424. * Each package prefix in
  425. * the property is tried in the order specified to load the factory class.
  426. * The default package prefix is "com.sun.jndi.url" (if none of the
  427. * specified packages work, this default is tried).
  428. * The complete package name is constructed using the package prefix,
  429. * concatenated with the scheme id.
  430. *<p>
  431. * For example, if the scheme id is "ldap", and the
  432. * <tt>Context.URL_PKG_PREFIXES</tt> property
  433. * contains "com.widget:com.wiz.jndi",
  434. * the naming manager would attempt to load the following classes
  435. * until one is successfully instantiated:
  436. *<ul>
  437. * <li>com.widget.ldap.ldapURLContextFactory
  438. * <li>com.wiz.jndi.ldap.ldapURLContextFactory
  439. * <li>com.sun.jndi.url.ldap.ldapURLContextFactory
  440. *</ul>
  441. * If none of the package prefixes work, null is returned.
  442. *<p>
  443. * If a factory is instantiated, it is invoked with the following
  444. * parameters to produce the resulting context.
  445. * <p>
  446. * <code>factory.getObjectInstance(null, environment);</code>
  447. * <p>
  448. * For example, invoking getObjectInstance() as shown above
  449. * on a LDAP URL context factory would return a
  450. * context that can resolve LDAP urls
  451. * (e.g. "ldap://ldap.wiz.com/o=wiz,c=us",
  452. * "ldap://ldap.umich.edu/o=umich,c=us", ...).
  453. *<p>
  454. * Note that an object factory (an object that implements the ObjectFactory
  455. * interface) must be public and must have a public constructor that
  456. * accepts no arguments.
  457. *
  458. * @param scheme The non-null scheme-id of the URLs supported by the context.
  459. * @param environment The possibly null environment properties to be
  460. * used in the creation of the object factory and the context.
  461. * @return A context for resolving URLs with the
  462. * scheme id <code>scheme</code>
  463. * <code>null</code> if the factory for creating the
  464. * context is not found.
  465. * @exception NamingException If a naming exception occurs while creating
  466. * the context.
  467. * @see #getObjectInstance
  468. * @see ObjectFactory#getObjectInstance
  469. */
  470. public static Context getURLContext(String scheme,
  471. Hashtable environment) throws NamingException {
  472. // pass in 'null' to indicate creation of generic context for scheme
  473. // (i.e. not specific to a URL).
  474. Object answer = getURLObject(scheme, null, null, null, environment);
  475. if (answer instanceof Context) {
  476. return (Context)answer;
  477. } else {
  478. return null;
  479. }
  480. }
  481. private static final String defaultPkgPrefix = "com.sun.jndi.url";
  482. /**
  483. * Creates an object for the given URL scheme id using
  484. * the supplied urlInfo.
  485. * <p>
  486. * If urlInfo is null, the result is a context for resolving URLs
  487. * with the scheme id 'scheme'.
  488. * If urlInfo is a URL, the result is a context named by the URL.
  489. * Names passed to this context is assumed to be relative to this
  490. * context (i.e. not a URL). For example, if urlInfo is
  491. * "ldap://ldap.wiz.com/o=Wiz,c=us", the resulting context will
  492. * be that pointed to by "o=Wiz,c=us" on the server 'ldap.wiz.com'.
  493. * Subsequent names that can be passed to this context will be
  494. * LDAP names relative to this context (e.g. cn="Barbs Jensen").
  495. * If urlInfo is an array of URLs, the URLs are assumed
  496. * to be equivalent in terms of the context to which they refer.
  497. * The resulting context is like that of the single URL case.
  498. * If urlInfo is of any other type, that is handled by the
  499. * context factory for the URL scheme.
  500. * @param scheme the URL scheme id for the context
  501. * @param urlInfo information used to create the context
  502. * @param name name of this object relative to <code>nameCtx</code>
  503. * @param nameCtx Context whose provider resource file will be searched
  504. * for package prefix values (or null if none)
  505. * @param environment Environment properties for creating the context
  506. * @see javax.naming.InitialContext
  507. */
  508. private static Object getURLObject(String scheme, Object urlInfo,
  509. Name name, Context nameCtx,
  510. Hashtable environment)
  511. throws NamingException {
  512. // e.g. "ftpURLContextFactory"
  513. ObjectFactory factory = (ObjectFactory)ResourceManager.getFactory(
  514. Context.URL_PKG_PREFIXES, environment, nameCtx,
  515. "." + scheme + "." + scheme + "URLContextFactory", defaultPkgPrefix);
  516. if (factory == null)
  517. return null;
  518. // Found object factory
  519. try {
  520. return factory.getObjectInstance(urlInfo, name, nameCtx, environment);
  521. } catch (NamingException e) {
  522. throw e;
  523. } catch (Exception e) {
  524. NamingException ne = new NamingException();
  525. ne.setRootCause(e);
  526. throw ne;
  527. }
  528. }
  529. // ------------ Initial Context Factory Stuff
  530. private static InitialContextFactoryBuilder initctx_factory_builder = null;
  531. /**
  532. * Use this method for accessing initctx_factory_builder while
  533. * inside an unsynchronized method.
  534. */
  535. private static synchronized InitialContextFactoryBuilder
  536. getInitialContextFactoryBuilder() {
  537. return initctx_factory_builder;
  538. }
  539. /**
  540. * Creates an initial context using the specified environment
  541. * properties.
  542. *<p>
  543. * If an InitialContextFactoryBuilder has been installed,
  544. * it is used to create the factory for creating the initial context.
  545. * Otherwise, the class specified in the
  546. * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> environment property is used.
  547. * Note that an initial context factory (an object that implements the
  548. * InitialContextFactory interface) must be public and must have a
  549. * public constructor that accepts no arguments.
  550. *
  551. * @param env The possibly null environment properties used when
  552. * creating the context.
  553. * @return A non-null initial context.
  554. * @exception NoInitialContextException If the
  555. * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> property
  556. * is not found or names a nonexistent
  557. * class or a class that cannot be instantiated,
  558. * or if the initial context could not be created for some other
  559. * reason.
  560. * @exception NamingException If some other naming exception was encountered.
  561. * @see javax.naming.InitialContext
  562. * @see javax.naming.directory.InitialDirContext
  563. */
  564. public static Context getInitialContext(Hashtable env)
  565. throws NamingException {
  566. InitialContextFactory factory;
  567. InitialContextFactoryBuilder builder = getInitialContextFactoryBuilder();
  568. if (builder == null) {
  569. // No factory installed, use property
  570. // Get initial context factory class name
  571. String className = env != null ?
  572. (String)env.get(Context.INITIAL_CONTEXT_FACTORY) : null;
  573. if (className == null) {
  574. NoInitialContextException ne = new NoInitialContextException(
  575. "Need to specify class name in environment or system " +
  576. "property, or as an applet parameter, or in an " +
  577. "application resource file: " +
  578. Context.INITIAL_CONTEXT_FACTORY);
  579. throw ne;
  580. }
  581. try {
  582. factory = (InitialContextFactory)
  583. helper.loadClass(className).newInstance();
  584. } catch(Exception e) {
  585. NoInitialContextException ne =
  586. new NoInitialContextException(
  587. "Cannot instantiate class: " + className);
  588. ne.setRootCause(e);
  589. throw ne;
  590. }
  591. } else {
  592. factory = builder.createInitialContextFactory(env);
  593. }
  594. return factory.getInitialContext(env);
  595. }
  596. /**
  597. * Sets the InitialContextFactory builder to be builder.
  598. *
  599. *<p>
  600. * The builder can only be installed if the executing thread is allowed by
  601. * the security manager to do so. Once installed, the builder cannot
  602. * be replaced.
  603. * @param builder The initial context factory builder to install. If null,
  604. * no builder is set.
  605. * @exception SecurityException builder cannot be installed for security
  606. * reasons.
  607. * @exception NamingException builder cannot be installed for
  608. * a non-security-related reason.
  609. * @exception IllegalStateException If a builder was previous installed.
  610. * @see #hasInitialContextFactoryBuilder
  611. * @see java.lang.SecurityManager#checkSetFactory
  612. */
  613. public static synchronized void setInitialContextFactoryBuilder(
  614. InitialContextFactoryBuilder builder)
  615. throws NamingException {
  616. if (initctx_factory_builder != null)
  617. throw new IllegalStateException(
  618. "InitialContextFactoryBuilder already set");
  619. SecurityManager security = System.getSecurityManager();
  620. if (security != null) {
  621. security.checkSetFactory();
  622. }
  623. initctx_factory_builder = builder;
  624. }
  625. /**
  626. * Determines whether an initial context factory builder has
  627. * been set.
  628. * @return true if an initial context factory builder has
  629. * been set; false otherwise.
  630. * @see #setInitialContextFactoryBuilder
  631. */
  632. public static boolean hasInitialContextFactoryBuilder() {
  633. return (getInitialContextFactoryBuilder() != null);
  634. }
  635. // ----- Continuation Context Stuff
  636. /**
  637. * Constant that holds the name of the environment property into
  638. * which <tt>getContinuationContext()</tt> stores the value of its
  639. * <tt>CannotProceedException</tt> parameter.
  640. * This property is inherited by the continuation context, and may
  641. * be used by that context's service provider to inspect the
  642. * fields of the exception.
  643. *<p>
  644. * The value of this constant is "java.naming.spi.CannotProceedException".
  645. *
  646. * @see #getContinuationContext
  647. * @since 1.3
  648. */
  649. public static final String CPE = "java.naming.spi.CannotProceedException";
  650. /**
  651. * Creates a context in which to continue a context operation.
  652. *<p>
  653. * In performing an operation on a name that spans multiple
  654. * namespaces, a context from one naming system may need to pass
  655. * the operation on to the next naming system. The context
  656. * implementation does this by first constructing a
  657. * <code>CannotProceedException</code> containing information
  658. * pinpointing how far it has proceeded. It then obtains a
  659. * continuation context from JNDI by calling
  660. * <code>getContinuationContext</code>. The context
  661. * implementation should then resume the context operation by
  662. * invoking the same operation on the continuation context, using
  663. * the remainder of the name that has not yet been resolved.
  664. *<p>
  665. * Before making use of the <tt>cpe</tt> parameter, this method
  666. * updates the environment associated with that object by setting
  667. * the value of the property <a href="#CPE"><tt>CPE</tt></a>
  668. * to <tt>cpe</tt>. This property will be inherited by the
  669. * continuation context, and may be used by that context's
  670. * service provider to inspect the fields of this exception.
  671. *
  672. * @param cpe
  673. * The non-null exception that triggered this continuation.
  674. * @return A non-null Context object for continuing the operation.
  675. * @exception NamingException If a naming exception occurred.
  676. */
  677. public static Context getContinuationContext(CannotProceedException cpe)
  678. throws NamingException {
  679. Hashtable env = cpe.getEnvironment();
  680. if (env == null) {
  681. env = new Hashtable(7);
  682. } else {
  683. // Make a (shallow) copy of the environment.
  684. env = (Hashtable) env.clone();
  685. }
  686. env.put(CPE, cpe);
  687. ContinuationContext cctx = new ContinuationContext(cpe, env);
  688. return cctx.getTargetContext();
  689. }
  690. // ------------ State Factory Stuff
  691. /**
  692. * Retrieves the state of an object for binding.
  693. * <p>
  694. * Service providers that implement the <tt>DirContext</tt> interface
  695. * should use <tt>DirectoryManager.getStateToBind()</tt>, not this method.
  696. * Service providers that implement only the <tt>Context</tt> interface
  697. * should use this method.
  698. *<p>
  699. * This method uses the specified state factories in
  700. * the <tt>Context.STATE_FACTORIES</tt> property from the environment
  701. * properties, and from the provider resource file associated with
  702. * <tt>nameCtx</tt>, in that order.
  703. * The value of this property is a colon-separated list of factory
  704. * class names that are tried in order, and the first one that succeeds
  705. * in returning the object's state is the one used.
  706. * If no object's state can be retrieved in this way, return the
  707. * object itself.
  708. * If an exception is encountered while retrieving the state, the
  709. * exception is passed up to the caller.
  710. * <p>
  711. * Note that a state factory
  712. * (an object that implements the StateFactory
  713. * interface) must be public and must have a public constructor that
  714. * accepts no arguments.
  715. * <p>
  716. * The <code>name</code> and <code>nameCtx</code> parameters may
  717. * optionally be used to specify the name of the object being created.
  718. * See the description of "Name and Context Parameters" in
  719. * {@link ObjectFactory#getObjectInstance
  720. * ObjectFactory.getObjectInstance()}
  721. * for details.
  722. * <p>
  723. * This method may return a <tt>Referenceable</tt> object. The
  724. * service provider obtaining this object may choose to store it
  725. * directly, or to extract its reference (using
  726. * <tt>Referenceable.getReference()</tt>) and store that instead.
  727. *
  728. * @param obj The non-null object for which to get state to bind.
  729. * @param name The name of this object relative to <code>nameCtx</code>,
  730. * or null if no name is specified.
  731. * @param nameCtx The context relative to which the <code>name</code>
  732. * parameter is specified, or null if <code>name</code> is
  733. * relative to the default initial context.
  734. * @param environment The possibly null environment to
  735. * be used in the creation of the state factory and
  736. * the object's state.
  737. * @return The non-null object representing <tt>obj</tt>'s state for
  738. * binding. It could be the object (<tt>obj</tt>) itself.
  739. * @exception NamingException If one of the factories accessed throws an
  740. * exception, or if an error was encountered while loading
  741. * and instantiating the factory and object classes.
  742. * A factory should only throw an exception if it does not want
  743. * other factories to be used in an attempt to create an object.
  744. * See <tt>StateFactory.getStateToBind()</tt>.
  745. * @see StateFactory
  746. * @see StateFactory#getStateToBind
  747. * @see DirectoryManager#getStateToBind
  748. * @since 1.3
  749. */
  750. public static Object getStateToBind(Object obj, Name name, Context nameCtx,
  751. Hashtable environment) throws NamingException {
  752. FactoryEnumeration factories = ResourceManager.getFactories(
  753. Context.STATE_FACTORIES, environment, nameCtx);
  754. if (factories == null) {
  755. return obj;
  756. }
  757. // Try each factory until one succeeds
  758. StateFactory factory;
  759. Object answer = null;
  760. while (answer == null && factories.hasMore()) {
  761. factory = (StateFactory)factories.next();
  762. answer = factory.getStateToBind(obj, name, nameCtx, environment);
  763. }
  764. return (answer != null) ? answer : obj;
  765. }
  766. }