1. /*
  2. * @(#)ResourceManager.java 1.6 00/02/02
  3. *
  4. * Copyright 1999, 2000 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 com.sun.naming.internal;
  11. import java.applet.Applet;
  12. import java.io.InputStream;
  13. import java.io.IOException;
  14. import java.net.URL;
  15. import java.util.Enumeration;
  16. import java.util.Hashtable;
  17. import java.util.Properties;
  18. import java.util.StringTokenizer;
  19. import java.util.Vector;
  20. import javax.naming.*;
  21. /**
  22. * The ResourceManager class facilitates the reading of JNDI resource files.
  23. *
  24. * @author Rosanna Lee
  25. * @author Scott Seligman
  26. * @version 1.6 00/02/02
  27. */
  28. public final class ResourceManager {
  29. /*
  30. * Name of provider resource files (without the package-name prefix.)
  31. */
  32. private static final String PROVIDER_RESOURCE_FILE_NAME =
  33. "jndiprovider.properties";
  34. /*
  35. * Name of application resource files.
  36. */
  37. private static final String APP_RESOURCE_FILE_NAME = "jndi.properties";
  38. /*
  39. * Name of properties file in <java.home>/lib.
  40. */
  41. private static final String JRELIB_PROPERTY_FILE_NAME = "jndi.properties";
  42. /*
  43. * The standard JNDI properties that specify colon-separated lists.
  44. */
  45. private static final String[] listProperties = {
  46. Context.OBJECT_FACTORIES,
  47. Context.URL_PKG_PREFIXES,
  48. Context.STATE_FACTORIES,
  49. // The following shouldn't create a runtime dependence on ldap package.
  50. javax.naming.ldap.LdapContext.CONTROL_FACTORIES
  51. };
  52. private static final VersionHelper helper =
  53. VersionHelper.getVersionHelper();
  54. /*
  55. * A cache of the properties that have been constructed by
  56. * the ResourceManager. A Hashtable from a provider resource
  57. * file is keyed on an object in the resource file's package.
  58. * One from application resource files is keyed on the thread's
  59. * context class loader (or the string "bootstrap" if that is null).
  60. */
  61. private static final Hashtable propertiesCache = new Hashtable(11);
  62. /*
  63. * A cache of factory objects (ObjectFactory, StateFactory, ControlFactory).
  64. *
  65. * Key is loader+propValue; value is a Vector of class name/factory objects.
  66. * Used in getFactories().
  67. */
  68. private static final Hashtable factoryCache = new Hashtable(11);
  69. /*
  70. * A cache of URL factory objects (ObjectFactory)
  71. *
  72. * Key is loader+className+propValue; value is factory itself, or "none" if
  73. * a previous search revealed no factory for the key.
  74. * Used in getFactory().
  75. */
  76. private static final Hashtable urlFactoryCache = new Hashtable(11);
  77. // There should be no instances of this class.
  78. private ResourceManager() {
  79. }
  80. // ---------- Public methods ----------
  81. /*
  82. * Given the environment parameter passed to the initial context
  83. * constructor, returns the full environment for that initial
  84. * context (never null). This is based on the environment
  85. * parameter, the applet parameters (where appropriate), the
  86. * system properties, and all application resource files.
  87. *
  88. * <p> This method will modify <tt>env</tt> and save
  89. * a reference to it. The caller may no longer modify it.
  90. *
  91. * @param env environment passed to initial context constructor.
  92. * Null indicates an empty environment.
  93. *
  94. * @throws NamingException if an error occurs while reading a
  95. * resource file
  96. */
  97. public static Hashtable getInitialEnvironment(Hashtable env)
  98. throws NamingException
  99. {
  100. String[] props = VersionHelper.PROPS; // system/applet properties
  101. if (env == null) {
  102. env = new Hashtable(11);
  103. }
  104. Applet applet = (Applet)env.get(Context.APPLET);
  105. // Merge property values from env param, applet params, and system
  106. // properties. The first value wins: there's no concatenation of
  107. // colon-separated lists.
  108. // Read system properties by first trying System.getProperties(),
  109. // and then trying System.getProperty() if that fails. The former
  110. // is more efficient due to fewer permission checks.
  111. //
  112. String[] jndiSysProps = helper.getJndiProperties();
  113. for (int i = 0; i < props.length; i++) {
  114. Object val = env.get(props[i]);
  115. if (val == null) {
  116. if (applet != null) {
  117. val = applet.getParameter(props[i]);
  118. }
  119. if (val == null) {
  120. // Read system property.
  121. val = (jndiSysProps != null)
  122. ? jndiSysProps[i]
  123. : helper.getJndiProperty(i);
  124. }
  125. if (val != null) {
  126. env.put(props[i], val);
  127. }
  128. }
  129. }
  130. // Merge the above with the values read from all application
  131. // resource files. Colon-separated lists are concatenated.
  132. mergeTables(env, getApplicationResources());
  133. return env;
  134. }
  135. /**
  136. * Retrieves the property from the environment, or from the provider
  137. * resource file associated with the given context. The environment
  138. * may in turn contain values that come from applet parameters,
  139. * system properties, or application resource files.
  140. *
  141. * If <tt>concat</tt> is true and both the environment and the provider
  142. * resource file contain the property, the two values are concatenated
  143. * (with a ':' separator).
  144. *
  145. * Returns null if no value is found.
  146. *
  147. * @param propName The non-null property name
  148. * @param env The possibly null environment properties
  149. * @param ctx The possibly null context
  150. * @param concat True if multiple values should be concatenated
  151. * @return the property value, or null is there is none.
  152. * @throws NamingException if an error occurs while reading the provider
  153. * resource file.
  154. */
  155. public static String getProperty(String propName, Hashtable env,
  156. Context ctx, boolean concat)
  157. throws NamingException {
  158. String val1 = (env != null) ? (String)env.get(propName) : null;
  159. if ((ctx == null) ||
  160. ((val1 != null) && !concat)) {
  161. return val1;
  162. }
  163. String val2 = (String)getProviderResource(ctx).get(propName);
  164. if (val1 == null) {
  165. return val2;
  166. } else if ((val2 == null) || !concat) {
  167. return val1;
  168. } else {
  169. return (val1 + ":" + val2);
  170. }
  171. }
  172. /**
  173. * Retreives an enumeration of factory classes/object specified by a
  174. * property.
  175. *
  176. * The property is gotten from the environment and the provider
  177. * resource file associated with the given context and concantenated.
  178. * See getProperty(). The resulting property value is a list of class names.
  179. *<p>
  180. * This method then loads each class using the current thread's context
  181. * class loader and keeps them in a vector. Any class that cannot be loaded
  182. * is ignored. The resulting vector is then cached in
  183. * a hash table, keyed by the context class loader and the property's
  184. * value. The next time threads of the same context class loader call this
  185. * method, they can use the cached vector.
  186. *<p>
  187. * After obtaining the vector either from the cache or by creating one from
  188. * the property value, this method then creates and returns a
  189. * FactoryEnumeration using the vector. As the FactoryEnumeration is
  190. * traversed, the cached Class object in the vector is instantiated and
  191. * replaced by an instance of the factory object itself.
  192. *<p>
  193. * Note that multiple threads can be accessing the same cached vector
  194. * via FactoryEnumeration, which locks the vector during each next().
  195. * The size of the vector will not change,
  196. * but a cached Class object might be replaced by an instantiated factory
  197. * object.
  198. *
  199. * @param propName The non-null property name
  200. * @param env The possibly null environment properties
  201. * @param ctx The possibly null context
  202. * @return An enumeration of factory classes/objects; null if none.
  203. * @exception NamingException If encounter problem while reading the provider
  204. * property file.
  205. * @see javax.naming.spi.NamingManager#getObjectInstance
  206. * @see javax.naming.spi.NamingManager#getStateToBind
  207. * @see javax.naming.spi.DirectoryManager#getObjectInstance
  208. * @see javax.naming.spi.DirectoryManager#getStateToBind
  209. * @see javax.naming.ldap.ControlFactory#getControlInstance
  210. */
  211. public static FactoryEnumeration getFactories(String propName, Hashtable env,
  212. Context ctx) throws NamingException {
  213. String facProp = getProperty(propName, env, ctx, true);
  214. if (facProp == null)
  215. return null; // no classes specified; return null
  216. // Construct key based on context class loader and property val
  217. FactoryKey key = new FactoryKey(facProp);
  218. synchronized (factoryCache) {
  219. Vector v = (Vector)factoryCache.get(key);
  220. if (v != null) {
  221. // Cached vector
  222. return v.size() == 0 ? null : new FactoryEnumeration(v);
  223. } else {
  224. // Populate Vector with classes named in facProp; skipping
  225. // those that we cannot load
  226. StringTokenizer parser = new StringTokenizer(facProp, ":");
  227. v = new Vector(5);
  228. while (parser.hasMoreTokens()) {
  229. try {
  230. // System.out.println("loading");
  231. v.addElement(
  232. helper.loadClass(parser.nextToken(), key.loader));
  233. } catch (Exception e) {
  234. // ignore ClassNotFoundException, IllegalArgumentException
  235. }
  236. }
  237. // System.out.println("adding to cache: " + v);
  238. factoryCache.put(key, v);
  239. return new FactoryEnumeration(v);
  240. }
  241. }
  242. }
  243. /**
  244. * Retreives a factory from a list of packages specified in a
  245. * property.
  246. *
  247. * The property is gotten from the environment and the provider
  248. * resource file associated with the given context and concantenated.
  249. * propValSuffix is added to the end of this list.
  250. * See getProperty(). The resulting property value is a list of package
  251. * prefixes.
  252. *<p>
  253. * This method then constructs a list of class names by concatenating
  254. * each package prefix with classSuffix and attempts to load and
  255. * instantiate the class until one succeeds.
  256. * Any class that cannot be loaded is ignored.
  257. * The resulting object is then cached in a hash table, keyed by the
  258. * context class loader and the property's value, and classSuffix.
  259. * The next time threads of the same context class loader call this
  260. * method, they use the cached factory..
  261. * If no factory can be loaded, "none" is recorded in the hashtable
  262. * so that next time it'll return null quickly.
  263. *
  264. * @param propName The non-null property name
  265. * @param env The possibly null environment properties
  266. * @param ctx The possibly null context
  267. * @param classSuffix The non-null class name (e.g. ".ldap.ldapURLContextFactory).
  268. * @param defaultPkgPrefix The non-null default package prefix.
  269. * (e.g., "com.sun.jndi.url").
  270. * @return An factory object; null if none.
  271. * @exception NamingException If encounter problem while reading the provider
  272. * property file, or problem instantiating the factory.
  273. *
  274. * @see javax.naming.spi.NamingManager#getURLContext
  275. * @see javax.naming.spi.NamingManager#getURLObject
  276. */
  277. public static Object getFactory(String propName, Hashtable env, Context ctx,
  278. String classSuffix, String defaultPkgPrefix) throws NamingException {
  279. // Merge property with provider property and supplied default
  280. String facProp = getProperty(propName, env, ctx, true);
  281. if (facProp != null)
  282. facProp += (":" + defaultPkgPrefix);
  283. else
  284. facProp = defaultPkgPrefix;
  285. // Construct key based on context class loader, class name, and property val
  286. FactoryKey key = new FactoryKey(classSuffix + facProp);
  287. synchronized (urlFactoryCache) {
  288. Object factory = urlFactoryCache.get(key);
  289. if (factory != null) {
  290. // already in cache, return
  291. return factory.equals("none") ? null : factory;
  292. }
  293. // Not previously cached; find first factory and cache
  294. StringTokenizer parser = new StringTokenizer(facProp, ":");
  295. String className;
  296. while (factory == null && parser.hasMoreTokens()) {
  297. className = parser.nextToken() + classSuffix;
  298. try {
  299. // System.out.println("loading " + className);
  300. factory = helper.loadClass(className, key.loader).newInstance();
  301. } catch (InstantiationException e) {
  302. NamingException ne =
  303. new NamingException("Cannot instantiate " + className);
  304. ne.setRootCause(e);
  305. throw ne;
  306. } catch (IllegalAccessException e) {
  307. NamingException ne =
  308. new NamingException("Cannot access " + className);
  309. ne.setRootCause(e);
  310. throw ne;
  311. } catch (Exception e) {
  312. // ignore ClassNotFoundException, IllegalArgumentException, etc
  313. }
  314. }
  315. if (factory != null) {
  316. urlFactoryCache.put(key, factory); // cache
  317. } else {
  318. urlFactoryCache.put(key, "none"); // to indicate none will be found
  319. }
  320. return factory;
  321. }
  322. }
  323. // ---------- Private methods ----------
  324. /*
  325. * Returns the properties contained in the provider resource file
  326. * of an object's package. Returns an empty hash table if the
  327. * object is null or the resource file cannot be found. The
  328. * results are cached.
  329. *
  330. * @throws NamingException if an error occurs while reading the file.
  331. */
  332. private static Hashtable getProviderResource(Object obj)
  333. throws NamingException
  334. {
  335. if (obj == null) {
  336. return (new Hashtable(1));
  337. }
  338. synchronized (propertiesCache) {
  339. Class c = obj.getClass();
  340. Hashtable props = (Hashtable)propertiesCache.get(c);
  341. if (props != null) {
  342. return props;
  343. }
  344. props = new Properties();
  345. InputStream istream =
  346. helper.getResourceAsStream(c, PROVIDER_RESOURCE_FILE_NAME);
  347. if (istream != null) {
  348. try {
  349. ((Properties)props).load(istream);
  350. } catch (IOException e) {
  351. NamingException ne = new ConfigurationException(
  352. "Error reading provider resource file for " + c);
  353. ne.setRootCause(e);
  354. throw ne;
  355. }
  356. }
  357. propertiesCache.put(c, props);
  358. return props;
  359. }
  360. }
  361. /*
  362. * Returns the Hashtable (never null) that results from merging
  363. * all application resource files available to this thread's
  364. * context class loader. The properties file in <java.home>/lib
  365. * is also merged in. The results are cached.
  366. *
  367. * When running under JDK 1.1, the Hashtable returned is always empty.
  368. *
  369. * SECURITY NOTES:
  370. * 1. JNDI needs permission to read the application resource files.
  371. * 2. Any class will be able to use JNDI to view the contents of
  372. * the application resource files in its own classpath. Give
  373. * careful consideration to this before storing sensitive
  374. * information there.
  375. *
  376. * @throws NamingException if an error occurs while reading a resource
  377. * file.
  378. */
  379. private static Hashtable getApplicationResources() throws NamingException {
  380. ClassLoader cl;
  381. try {
  382. cl = helper.getContextClassLoader();
  383. } catch (SecurityException e) {
  384. return (new Hashtable(1));
  385. }
  386. Object key = (cl != null) ? (Object)cl : (Object)"bootstrap";
  387. synchronized (propertiesCache) {
  388. Hashtable result = (Hashtable)propertiesCache.get(key);
  389. if (result != null) {
  390. return result;
  391. }
  392. try {
  393. NamingEnumeration resources =
  394. helper.getResources(cl, APP_RESOURCE_FILE_NAME);
  395. while (resources.hasMore()) {
  396. Properties props = new Properties();
  397. props.load((InputStream)resources.next());
  398. if (result == null) {
  399. result = props;
  400. } else {
  401. mergeTables(result, props);
  402. }
  403. }
  404. // Merge in properties from file in <java.home>/lib.
  405. InputStream istream =
  406. helper.getJavaHomeLibStream(JRELIB_PROPERTY_FILE_NAME);
  407. if (istream != null) {
  408. Properties props = new Properties();
  409. props.load(istream);
  410. if (result == null) {
  411. result = props;
  412. } else {
  413. mergeTables(result, props);
  414. }
  415. }
  416. } catch (IOException e) {
  417. NamingException ne = new ConfigurationException(
  418. "Error reading application resource file");
  419. ne.setRootCause(e);
  420. throw ne;
  421. }
  422. if (result == null) {
  423. result = new Hashtable(11);
  424. }
  425. propertiesCache.put(key, result);
  426. return result;
  427. }
  428. }
  429. /*
  430. * Merge the properties from one hash table into another. Each
  431. * property in props2 that is not in props1 is added to props1.
  432. * For each property in both hash tables that is one of the
  433. * standard JNDI properties that specify colon-separated lists,
  434. * the values are concatenated and stored in props1.
  435. */
  436. private static void mergeTables(Hashtable props1, Hashtable props2) {
  437. Enumeration keys = props2.keys();
  438. while (keys.hasMoreElements()) {
  439. String prop = (String)keys.nextElement();
  440. Object val1 = props1.get(prop);
  441. if (val1 == null) {
  442. props1.put(prop, props2.get(prop));
  443. } else if (isListProperty(prop)) {
  444. String val2 = (String)props2.get(prop);
  445. props1.put(prop, ((String)val1) + ":" + val2);
  446. }
  447. }
  448. }
  449. /*
  450. * Is a property one of the standard JNDI properties that specify
  451. * colon-separated lists?
  452. */
  453. private static boolean isListProperty(String prop) {
  454. prop = prop.intern();
  455. for (int i = 0; i < listProperties.length; i++) {
  456. if (prop == listProperties[i]) {
  457. return true;
  458. }
  459. }
  460. return false;
  461. }
  462. // --------------- factory loading --------------------
  463. /**
  464. * The key used in the factoryCache and urlFactoryCache hash tables.
  465. */
  466. final static private class FactoryKey {
  467. private String propVal;
  468. private ClassLoader loader;
  469. FactoryKey(String propVal) {
  470. try {
  471. loader = helper.getContextClassLoader();
  472. } catch (SecurityException e) {
  473. }
  474. this.propVal = propVal;
  475. }
  476. public boolean equals(Object obj) {
  477. if (!(obj instanceof FactoryKey)) {
  478. return false;
  479. }
  480. FactoryKey other = (FactoryKey)obj;
  481. return (propVal.equals(other.propVal) &&
  482. ((loader == other.loader) ||
  483. (loader != null && loader.equals(other.loader))));
  484. }
  485. public int hashCode() {
  486. return propVal.hashCode() + (loader != null? loader.hashCode() : 0);
  487. }
  488. /*
  489. public String toString() {
  490. return (propVal + loader);
  491. }
  492. */
  493. }
  494. }