1. /*
  2. * @(#)InitialContext.java 1.10 03/01/23
  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;
  8. import java.util.Hashtable;
  9. import javax.naming.spi.NamingManager;
  10. import com.sun.naming.internal.ResourceManager;
  11. /**
  12. * This class is the starting context for performing naming operations.
  13. *<p>
  14. * All naming operations are relative to a context.
  15. * The initial context implements the Context interface and
  16. * provides the starting point for resolution of names.
  17. *<p>
  18. * <a name=ENVIRONMENT></a>
  19. * When the initial context is constructed, its environment
  20. * is initialized with properties defined in the environment parameter
  21. * passed to the constructor, and in any
  22. * <a href=Context.html#RESOURCEFILES>application resource files</a>.
  23. * In addition, a small number of standard JNDI properties may
  24. * be specified as system properties or as applet parameters
  25. * (through the use of {@link Context#APPLET}).
  26. * These special properties are listed in the field detail sections of the
  27. * <a href=Context.html#field_detail><tt>Context</tt></a> and
  28. * <a href=ldap/LdapContext.html#field_detail><tt>LdapContext</tt></a>
  29. * interface documentation.
  30. *<p>
  31. * JNDI determines each property's value by merging
  32. * the values from the following two sources, in order:
  33. * <ol>
  34. * <li>
  35. * The first occurrence of the property from the constructor's
  36. * environment parameter and (for appropriate properties) the applet
  37. * parameters and system properties.
  38. * <li>
  39. * The application resource files (<tt>jndi.properties</tt>).
  40. * </ol>
  41. * For each property found in both of these two sources, or in
  42. * more than one application resource file, the property's value
  43. * is determined as follows. If the property is
  44. * one of the standard JNDI properties that specify a list of JNDI
  45. * factories (see <a href=Context.html#LISTPROPS><tt>Context</tt></a>),
  46. * all of the values are
  47. * concatenated into a single colon-separated list. For other
  48. * properties, only the first value found is used.
  49. *
  50. *<p>
  51. * The initial context implementation is determined at runtime.
  52. * The default policy uses the environment property
  53. * "{@link Context#INITIAL_CONTEXT_FACTORY java.naming.factory.initial}",
  54. * which contains the class name of the initial context factory.
  55. * An exception to this policy is made when resolving URL strings, as described
  56. * below.
  57. *<p>
  58. * When a URL string (a <tt>String</tt> of the form
  59. * <em>scheme_id:rest_of_name</em>) is passed as a name parameter to
  60. * any method, a URL context factory for handling that scheme is
  61. * located and used to resolve the URL. If no such factory is found,
  62. * the initial context specified by
  63. * <tt>"java.naming.factory.initial"</tt> is used. Similarly, when a
  64. * <tt>CompositeName</tt> object whose first component is a URL string is
  65. * passed as a name parameter to any method, a URL context factory is
  66. * located and used to resolve the first name component.
  67. * See {@link NamingManager#getURLContext
  68. * <tt>NamingManager.getURLContext()</tt>} for a description of how URL
  69. * context factories are located.
  70. *<p>
  71. * This default policy of locating the initial context and URL context
  72. * factories may be overridden
  73. * by calling
  74. * <tt>NamingManager.setInitialContextFactoryBuilder()</tt>.
  75. *<p>
  76. * NoInitialContextException is thrown when an initial context cannot
  77. * be instantiated. This exception can be thrown during any interaction
  78. * with the InitialContext, not only when the InitialContext is constructed.
  79. * For example, the implementation of the initial context might lazily
  80. * retrieve the context only when actual methods are invoked on it.
  81. * The application should not have any dependency on when the existence
  82. * of an initial context is determined.
  83. *<p>
  84. * When the environment property "java.naming.factory.initial" is
  85. * non-null, the InitialContext constructor will attempt to create the
  86. * initial context specified therein. At that time, the initial context factory
  87. * involved might throw an exception if a problem is encountered. However,
  88. * it is provider implementation-dependent when it verifies and indicates
  89. * to the users of the initial context any environment property- or
  90. * connection- related problems. It can do so lazily--delaying until
  91. * an operation is performed on the context, or eagerly, at the time
  92. * the context is constructed.
  93. *<p>
  94. * An InitialContext instance is not synchronized against concurrent
  95. * access by multiple threads. Multiple threads each manipulating a
  96. * different InitialContext instance need not synchronize.
  97. * Threads that need to access a single InitialContext instance
  98. * concurrently should synchronize amongst themselves and provide the
  99. * necessary locking.
  100. *
  101. * @author Rosanna Lee
  102. * @author Scott Seligman
  103. * @version 1.10 03/01/23
  104. *
  105. * @see Context
  106. * @see NamingManager#setInitialContextFactoryBuilder
  107. * NamingManager.setInitialContextFactoryBuilder
  108. * @since JNDI 1.1 / Java 2 Platform, Standard Edition, v 1.3
  109. */
  110. public class InitialContext implements Context {
  111. /**
  112. * The environment associated with this InitialContext.
  113. * It is initialized to null and is updated by the constructor
  114. * that accepts an environment or by the <tt>init()</tt> method.
  115. * @see #addToEnvironment
  116. * @see #removeFromEnvironment
  117. * @see #getEnvironment
  118. */
  119. protected Hashtable myProps = null;
  120. /**
  121. * Field holding the result of calling NamingManager.getInitialContext().
  122. * It is set by getDefaultInitCtx() the first time getDefaultInitCtx()
  123. * is called. Subsequent invocations of getDefaultInitCtx() return
  124. * the value of defaultInitCtx.
  125. * @see #getDefaultInitCtx
  126. */
  127. protected Context defaultInitCtx = null;
  128. /**
  129. * Field indicating whether the initial context has been obtained
  130. * by calling NamingManager.getInitialContext().
  131. * If true, its result is in <code>defaultInitCtx</code>.
  132. */
  133. protected boolean gotDefault = false;
  134. /**
  135. * Constructs an initial context with the option of not
  136. * initializing it. This may be used by a constructor in
  137. * a subclass when the value of the environment parameter
  138. * is not yet known at the time the <tt>InitialContext</tt>
  139. * constructor is called. The subclass's constructor will
  140. * call this constructor, compute the value of the environment,
  141. * and then call <tt>init()</tt> before returning.
  142. *
  143. * @param lazy
  144. * true means do not initialize the initial context; false
  145. * is equivalent to calling <tt>new InitialContext()</tt>
  146. * @throws NamingException if a naming exception is encountered
  147. *
  148. * @see #init(Hashtable)
  149. * @since 1.3
  150. */
  151. protected InitialContext(boolean lazy) throws NamingException {
  152. if (!lazy) {
  153. init(null);
  154. }
  155. }
  156. /**
  157. * Constructs an initial context.
  158. * No environment properties are supplied.
  159. * Equivalent to <tt>new InitialContext(null)</tt>.
  160. *
  161. * @throws NamingException if a naming exception is encountered
  162. *
  163. * @see #InitialContext(Hashtable)
  164. */
  165. public InitialContext() throws NamingException {
  166. init(null);
  167. }
  168. /**
  169. * Constructs an initial context using the supplied environment.
  170. * Environment properties are discussed in the class description.
  171. *
  172. * <p> This constructor will not modify <tt>environment</tt>
  173. * or save a reference to it, but may save a clone.
  174. *
  175. * @param environment
  176. * environment used to create the initial context.
  177. * Null indicates an empty environment.
  178. *
  179. * @throws NamingException if a naming exception is encountered
  180. */
  181. public InitialContext(Hashtable environment) throws NamingException {
  182. if (environment != null) {
  183. environment = (Hashtable)environment.clone();
  184. }
  185. init(environment);
  186. }
  187. /**
  188. * Initializes the initial context using the supplied environment.
  189. * Environment properties are discussed in the class description.
  190. *
  191. * <p> This method will modify <tt>environment</tt> and save
  192. * a reference to it. The caller may no longer modify it.
  193. *
  194. * @param environment
  195. * environment used to create the initial context.
  196. * Null indicates an empty environment.
  197. *
  198. * @throws NamingException if a naming exception is encountered
  199. *
  200. * @see #InitialContext(boolean)
  201. * @since 1.3
  202. */
  203. protected void init(Hashtable environment) throws NamingException {
  204. myProps = ResourceManager.getInitialEnvironment(environment);
  205. if (myProps.get(Context.INITIAL_CONTEXT_FACTORY) != null) {
  206. // user has specified initial context factory; try to get it
  207. getDefaultInitCtx();
  208. }
  209. }
  210. private static String getURLScheme(String str) {
  211. int colon_posn = str.indexOf(':');
  212. int slash_posn = str.indexOf('/');
  213. if (colon_posn > 0 && (slash_posn == -1 || colon_posn < slash_posn))
  214. return str.substring(0, colon_posn);
  215. return null;
  216. }
  217. /**
  218. * Retrieves the initial context by calling
  219. * <code>NamingManager.getInitialContext()</code>
  220. * and cache it in defaultInitCtx.
  221. * Set <code>gotDefault</code> so that we know we've tried this before.
  222. * @return The non-null cached initial context.
  223. * @exception NoInitialContextException If cannot find an initial context.
  224. * @exception NamingException If a naming exception was encountered.
  225. */
  226. protected Context getDefaultInitCtx() throws NamingException{
  227. if (!gotDefault) {
  228. defaultInitCtx = NamingManager.getInitialContext(myProps);
  229. gotDefault = true;
  230. }
  231. if (defaultInitCtx == null)
  232. throw new NoInitialContextException();
  233. return defaultInitCtx;
  234. }
  235. /**
  236. * Retrieves a context for resolving the string name <code>name</code>.
  237. * If <code>name</code> name is a URL string, then attempt
  238. * to find a URL context for it. If none is found, or if
  239. * <code>name</code> is not a URL string, then return
  240. * <code>getDefaultInitCtx()</code>.
  241. *<p>
  242. * See getURLOrDefaultInitCtx(Name) for description
  243. * of how a subclass should use this method.
  244. * @param name The non-null name for which to get the context.
  245. * @return A URL context for <code>name</code> or the cached
  246. * initial context. The result cannot be null.
  247. * @exception NoInitialContextException If cannot find an initial context.
  248. * @exception NamingException In a naming exception is encountered.
  249. * @see javax.naming.spi.NamingManager#getURLContext
  250. */
  251. protected Context getURLOrDefaultInitCtx(String name)
  252. throws NamingException {
  253. if (NamingManager.hasInitialContextFactoryBuilder()) {
  254. return getDefaultInitCtx();
  255. }
  256. String scheme = getURLScheme(name);
  257. if (scheme != null) {
  258. Context ctx = NamingManager.getURLContext(scheme, myProps);
  259. if (ctx != null) {
  260. return ctx;
  261. }
  262. }
  263. return getDefaultInitCtx();
  264. }
  265. /**
  266. * Retrieves a context for resolving <code>name</code>.
  267. * If the first component of <code>name</code> name is a URL string,
  268. * then attempt to find a URL context for it. If none is found, or if
  269. * the first component of <code>name</code> is not a URL string,
  270. * then return <code>getDefaultInitCtx()</code>.
  271. *<p>
  272. * When creating a subclass of InitialContext, use this method as
  273. * follows.
  274. * Define a new method that uses this method to get an initial
  275. * context of the desired subclass.
  276. * <p><blockquote><pre>
  277. * protected XXXContext getURLOrDefaultInitXXXCtx(Name name)
  278. * throws NamingException {
  279. * Context answer = getURLOrDefaultInitCtx(name);
  280. * if (!(answer instanceof XXXContext)) {
  281. * if (answer == null) {
  282. * throw new NoInitialContextException();
  283. * } else {
  284. * throw new NotContextException("Not an XXXContext");
  285. * }
  286. * }
  287. * return (XXXContext)answer;
  288. * }
  289. * </pre></blockquote>
  290. * When providing implementations for the new methods in the subclass,
  291. * use this newly defined method to get the initial context.
  292. * <p><blockquote><pre>
  293. * public Object XXXMethod1(Name name, ...) {
  294. * throws NamingException {
  295. * return getURLOrDefaultInitXXXCtx(name).XXXMethod1(name, ...);
  296. * }
  297. * </pre></blockquote>
  298. *
  299. * @param name The non-null name for which to get the context.
  300. * @return A URL context for <code>name</code> or the cached
  301. * initial context. The result cannot be null.
  302. * @exception NoInitialContextException If cannot find an initial context.
  303. * @exception NamingException In a naming exception is encountered.
  304. *
  305. * @see javax.naming.spi.NamingManager#getURLContext
  306. */
  307. protected Context getURLOrDefaultInitCtx(Name name)
  308. throws NamingException {
  309. if (NamingManager.hasInitialContextFactoryBuilder()) {
  310. return getDefaultInitCtx();
  311. }
  312. if (name.size() > 0) {
  313. String first = name.get(0);
  314. String scheme = getURLScheme(first);
  315. if (scheme != null) {
  316. Context ctx = NamingManager.getURLContext(scheme, myProps);
  317. if (ctx != null) {
  318. return ctx;
  319. }
  320. }
  321. }
  322. return getDefaultInitCtx();
  323. }
  324. // Context methods
  325. // Most Javadoc is deferred to the Context interface.
  326. public Object lookup(String name) throws NamingException {
  327. return getURLOrDefaultInitCtx(name).lookup(name);
  328. }
  329. public Object lookup(Name name) throws NamingException {
  330. return getURLOrDefaultInitCtx(name).lookup(name);
  331. }
  332. public void bind(String name, Object obj) throws NamingException {
  333. getURLOrDefaultInitCtx(name).bind(name, obj);
  334. }
  335. public void bind(Name name, Object obj) throws NamingException {
  336. getURLOrDefaultInitCtx(name).bind(name, obj);
  337. }
  338. public void rebind(String name, Object obj) throws NamingException {
  339. getURLOrDefaultInitCtx(name).rebind(name, obj);
  340. }
  341. public void rebind(Name name, Object obj) throws NamingException {
  342. getURLOrDefaultInitCtx(name).rebind(name, obj);
  343. }
  344. public void unbind(String name) throws NamingException {
  345. getURLOrDefaultInitCtx(name).unbind(name);
  346. }
  347. public void unbind(Name name) throws NamingException {
  348. getURLOrDefaultInitCtx(name).unbind(name);
  349. }
  350. public void rename(String oldName, String newName) throws NamingException {
  351. getURLOrDefaultInitCtx(oldName).rename(oldName, newName);
  352. }
  353. public void rename(Name oldName, Name newName) throws NamingException {
  354. getURLOrDefaultInitCtx(oldName).rename(oldName, newName);
  355. }
  356. public NamingEnumeration list(String name) throws NamingException {
  357. return (getURLOrDefaultInitCtx(name).list(name));
  358. }
  359. public NamingEnumeration list(Name name) throws NamingException {
  360. return (getURLOrDefaultInitCtx(name).list(name));
  361. }
  362. public NamingEnumeration listBindings(String name)
  363. throws NamingException {
  364. return getURLOrDefaultInitCtx(name).listBindings(name);
  365. }
  366. public NamingEnumeration listBindings(Name name)
  367. throws NamingException {
  368. return getURLOrDefaultInitCtx(name).listBindings(name);
  369. }
  370. public void destroySubcontext(String name) throws NamingException {
  371. getURLOrDefaultInitCtx(name).destroySubcontext(name);
  372. }
  373. public void destroySubcontext(Name name) throws NamingException {
  374. getURLOrDefaultInitCtx(name).destroySubcontext(name);
  375. }
  376. public Context createSubcontext(String name) throws NamingException {
  377. return getURLOrDefaultInitCtx(name).createSubcontext(name);
  378. }
  379. public Context createSubcontext(Name name) throws NamingException {
  380. return getURLOrDefaultInitCtx(name).createSubcontext(name);
  381. }
  382. public Object lookupLink(String name) throws NamingException {
  383. return getURLOrDefaultInitCtx(name).lookupLink(name);
  384. }
  385. public Object lookupLink(Name name) throws NamingException {
  386. return getURLOrDefaultInitCtx(name).lookupLink(name);
  387. }
  388. public NameParser getNameParser(String name) throws NamingException {
  389. return getURLOrDefaultInitCtx(name).getNameParser(name);
  390. }
  391. public NameParser getNameParser(Name name) throws NamingException {
  392. return getURLOrDefaultInitCtx(name).getNameParser(name);
  393. }
  394. /**
  395. * Composes the name of this context with a name relative to
  396. * this context.
  397. * Since an initial context may never be named relative
  398. * to any context other than itself, the value of the
  399. * <tt>prefix</tt> parameter must be an empty name (<tt>""</tt>).
  400. */
  401. public String composeName(String name, String prefix)
  402. throws NamingException {
  403. return name;
  404. }
  405. /**
  406. * Composes the name of this context with a name relative to
  407. * this context.
  408. * Since an initial context may never be named relative
  409. * to any context other than itself, the value of the
  410. * <tt>prefix</tt> parameter must be an empty name.
  411. */
  412. public Name composeName(Name name, Name prefix) throws NamingException {
  413. return (Name)name.clone();
  414. }
  415. public Object addToEnvironment(String propName, Object propVal)
  416. throws NamingException {
  417. myProps.put(propName, propVal);
  418. return getDefaultInitCtx().addToEnvironment(propName, propVal);
  419. }
  420. public Object removeFromEnvironment(String propName)
  421. throws NamingException {
  422. myProps.remove(propName);
  423. return getDefaultInitCtx().removeFromEnvironment(propName);
  424. }
  425. public Hashtable getEnvironment() throws NamingException {
  426. return getDefaultInitCtx().getEnvironment();
  427. }
  428. public void close() throws NamingException {
  429. myProps = null;
  430. if (defaultInitCtx != null) {
  431. defaultInitCtx.close();
  432. defaultInitCtx = null;
  433. }
  434. gotDefault = false;
  435. }
  436. public String getNameInNamespace() throws NamingException {
  437. return getDefaultInitCtx().getNameInNamespace();
  438. }
  439. };