1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.naming.internal;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.lang.ref.WeakReference;
  31 import java.lang.reflect.Method;
  32 import java.lang.reflect.InvocationTargetException;
  33 import java.util.HashMap;
  34 import java.util.Hashtable;
  35 import java.util.Map;
  36 import java.util.Properties;
  37 import java.util.StringTokenizer;
  38 import java.util.List;
  39 import java.util.ArrayList;
  40 import java.util.WeakHashMap;
  41 
  42 import javax.naming.*;
  43 
  44 /**
  45   * The ResourceManager class facilitates the reading of JNDI resource files.
  46   *
  47   * @author Rosanna Lee
  48   * @author Scott Seligman
  49   */
  50 
  51 public final class ResourceManager {
  52 
  53     /*
  54      * Name of provider resource files (without the package-name prefix.)
  55      */
  56     private static final String PROVIDER_RESOURCE_FILE_NAME =
  57             "jndiprovider.properties";
  58 
  59     /*
  60      * Name of application resource files.
  61      */
  62     private static final String APP_RESOURCE_FILE_NAME = "jndi.properties";
  63 
  64     /*
  65      * Name of properties file in <java.home>/lib.
  66      */
  67     private static final String JRELIB_PROPERTY_FILE_NAME = "jndi.properties";
  68 
  69     /*
  70      * The standard JNDI properties that specify colon-separated lists.
  71      */
  72     private static final String[] listProperties = {
  73         Context.OBJECT_FACTORIES,
  74         Context.URL_PKG_PREFIXES,
  75         Context.STATE_FACTORIES,
  76         // The following shouldn't create a runtime dependence on ldap package.
  77         javax.naming.ldap.LdapContext.CONTROL_FACTORIES
  78     };
  79 
  80     private static final VersionHelper helper =
  81             VersionHelper.getVersionHelper();
  82 
  83     /*
  84      * A cache of the properties that have been constructed by
  85      * the ResourceManager.  A Hashtable from a provider resource
  86      * file is keyed on a class in the resource file's package.
  87      * One from application resource files is keyed on the thread's
  88      * context class loader.
  89      */
  90     // WeakHashMap<Class | ClassLoader, Hashtable>
  91     private static final WeakHashMap<Object, Hashtable<? super String, Object>>
  92             propertiesCache = new WeakHashMap<>(11);
  93 
  94     /*
  95      * A cache of factory objects (ObjectFactory, StateFactory, ControlFactory).
  96      *
  97      * A two-level cache keyed first on context class loader and then
  98      * on propValue.  Value is a list of class or factory objects,
  99      * weakly referenced so as not to prevent GC of the class loader.
 100      * Used in getFactories().
 101      */
 102     private static final
 103         WeakHashMap<ClassLoader, Map<String, List<NamedWeakReference<Object>>>>
 104             factoryCache = new WeakHashMap<>(11);
 105 
 106     /*
 107      * A cache of URL factory objects (ObjectFactory).
 108      *
 109      * A two-level cache keyed first on context class loader and then
 110      * on classSuffix+propValue.  Value is the factory itself (weakly
 111      * referenced so as not to prevent GC of the class loader) or
 112      * NO_FACTORY if a previous search revealed no factory.  Used in
 113      * getFactory().
 114      */
 115     private static final
 116         WeakHashMap<ClassLoader, Map<String, WeakReference<Object>>>
 117             urlFactoryCache = new WeakHashMap<>(11);
 118     private static final WeakReference<Object> NO_FACTORY =
 119             new WeakReference<>(null);
 120 
 121     /**
 122      * A class to allow JNDI properties be specified as applet parameters
 123      * without creating a static dependency on java.applet.
 124      */
 125     private static class AppletParameter {
 126         private static final Class<?> clazz = getClass("java.applet.Applet");
 127         private static final Method getMethod =
 128             getMethod(clazz, "getParameter", String.class);
 129         private static Class<?> getClass(String name) {
 130             try {
 131                 return Class.forName(name, true, null);
 132             } catch (ClassNotFoundException e) {
 133                 return null;
 134             }
 135         }
 136         private static Method getMethod(Class<?> clazz,
 137                                         String name,
 138                                         Class<?>... paramTypes)
 139         {
 140             if (clazz != null) {
 141                 try {
 142                     return clazz.getMethod(name, paramTypes);
 143                 } catch (NoSuchMethodException e) {
 144                     throw new AssertionError(e);
 145                 }
 146             } else {
 147                 return null;
 148             }
 149         }
 150 
 151         /**
 152          * Returns the value of the applet's named parameter.
 153          */
 154         static Object get(Object applet, String name) {
 155             // if clazz is null then applet cannot be an Applet.
 156             if (clazz == null || !clazz.isInstance(applet))
 157                 throw new ClassCastException(applet.getClass().getName());
 158             try {
 159                 return getMethod.invoke(applet, name);
 160             } catch (InvocationTargetException |
 161                      IllegalAccessException e) {
 162                 throw new AssertionError(e);
 163             }
 164         }
 165     }
 166 
 167     // There should be no instances of this class.
 168     private ResourceManager() {
 169     }
 170 
 171 
 172     // ---------- Public methods ----------
 173 
 174     /*
 175      * Given the environment parameter passed to the initial context
 176      * constructor, returns the full environment for that initial
 177      * context (never null).  This is based on the environment
 178      * parameter, the applet parameters (where appropriate), the
 179      * system properties, and all application resource files.
 180      *
 181      * <p> This method will modify <tt>env</tt> and save
 182      * a reference to it.  The caller may no longer modify it.
 183      *
 184      * @param env       environment passed to initial context constructor.
 185      *                  Null indicates an empty environment.
 186      *
 187      * @throws NamingException if an error occurs while reading a
 188      *          resource file
 189      */
 190     @SuppressWarnings("unchecked")
 191     public static Hashtable<?, ?> getInitialEnvironment(
 192             Hashtable<?, ?> env)
 193             throws NamingException
 194     {
 195         String[] props = VersionHelper.PROPS;   // system/applet properties
 196         if (env == null) {
 197             env = new Hashtable<>(11);
 198         }
 199         Object applet = env.get(Context.APPLET);
 200 
 201         // Merge property values from env param, applet params, and system
 202         // properties.  The first value wins:  there's no concatenation of
 203         // colon-separated lists.
 204         // Read system properties by first trying System.getProperties(),
 205         // and then trying System.getProperty() if that fails.  The former
 206         // is more efficient due to fewer permission checks.
 207         //
 208         String[] jndiSysProps = helper.getJndiProperties();
 209         for (int i = 0; i < props.length; i++) {
 210             Object val = env.get(props[i]);
 211             if (val == null) {
 212                 if (applet != null) {
 213                     val = AppletParameter.get(applet, props[i]);
 214                 }
 215                 if (val == null) {
 216                     // Read system property.
 217                     val = (jndiSysProps != null)
 218                         ? jndiSysProps[i]
 219                         : helper.getJndiProperty(i);
 220                 }
 221                 if (val != null) {
 222                     ((Hashtable<String, Object>)env).put(props[i], val);
 223                 }
 224             }
 225         }
 226 
 227         // Merge the above with the values read from all application
 228         // resource files.  Colon-separated lists are concatenated.
 229         mergeTables((Hashtable<Object, Object>)env, getApplicationResources());
 230         return env;
 231     }
 232 
 233     /**
 234       * Retrieves the property from the environment, or from the provider
 235       * resource file associated with the given context.  The environment
 236       * may in turn contain values that come from applet parameters,
 237       * system properties, or application resource files.
 238       *
 239       * If <tt>concat</tt> is true and both the environment and the provider
 240       * resource file contain the property, the two values are concatenated
 241       * (with a ':' separator).
 242       *
 243       * Returns null if no value is found.
 244       *
 245       * @param propName The non-null property name
 246       * @param env      The possibly null environment properties
 247       * @param ctx      The possibly null context
 248       * @param concat   True if multiple values should be concatenated
 249       * @return the property value, or null is there is none.
 250       * @throws NamingException if an error occurs while reading the provider
 251       * resource file.
 252       */
 253     public static String getProperty(String propName, Hashtable<?,?> env,
 254         Context ctx, boolean concat)
 255             throws NamingException {
 256 
 257         String val1 = (env != null) ? (String)env.get(propName) : null;
 258         if ((ctx == null) ||
 259             ((val1 != null) && !concat)) {
 260             return val1;
 261         }
 262         String val2 = (String)getProviderResource(ctx).get(propName);
 263         if (val1 == null) {
 264             return val2;
 265         } else if ((val2 == null) || !concat) {
 266             return val1;
 267         } else {
 268             return (val1 + ":" + val2);
 269         }
 270     }
 271 
 272     /**
 273      * Retrieves an enumeration of factory classes/object specified by a
 274      * property.
 275      *
 276      * The property is gotten from the environment and the provider
 277      * resource file associated with the given context and concantenated.
 278      * See getProperty(). The resulting property value is a list of class names.
 279      *<p>
 280      * This method then loads each class using the current thread's context
 281      * class loader and keeps them in a list. Any class that cannot be loaded
 282      * is ignored. The resulting list is then cached in a two-level
 283      * hash table, keyed first by the context class loader and then by
 284      * the property's value.
 285      * The next time threads of the same context class loader call this
 286      * method, they can use the cached list.
 287      *<p>
 288      * After obtaining the list either from the cache or by creating one from
 289      * the property value, this method then creates and returns a
 290      * FactoryEnumeration using the list. As the FactoryEnumeration is
 291      * traversed, the cached Class object in the list is instantiated and
 292      * replaced by an instance of the factory object itself.  Both class
 293      * objects and factories are wrapped in weak references so as not to
 294      * prevent GC of the class loader.
 295      *<p>
 296      * Note that multiple threads can be accessing the same cached list
 297      * via FactoryEnumeration, which locks the list during each next().
 298      * The size of the list will not change,
 299      * but a cached Class object might be replaced by an instantiated factory
 300      * object.
 301      *
 302      * @param propName  The non-null property name
 303      * @param env       The possibly null environment properties
 304      * @param ctx       The possibly null context
 305      * @return An enumeration of factory classes/objects; null if none.
 306      * @exception NamingException If encounter problem while reading the provider
 307      * property file.
 308      * @see javax.naming.spi.NamingManager#getObjectInstance
 309      * @see javax.naming.spi.NamingManager#getStateToBind
 310      * @see javax.naming.spi.DirectoryManager#getObjectInstance
 311      * @see javax.naming.spi.DirectoryManager#getStateToBind
 312      * @see javax.naming.ldap.ControlFactory#getControlInstance
 313      */
 314     public static FactoryEnumeration getFactories(String propName,
 315         Hashtable<?,?> env, Context ctx) throws NamingException {
 316 
 317         String facProp = getProperty(propName, env, ctx, true);
 318         if (facProp == null)
 319             return null;  // no classes specified; return null
 320 
 321         // Cache is based on context class loader and property val
 322         ClassLoader loader = helper.getContextClassLoader();
 323 
 324         Map<String, List<NamedWeakReference<Object>>> perLoaderCache = null;
 325         synchronized (factoryCache) {
 326             perLoaderCache = factoryCache.get(loader);
 327             if (perLoaderCache == null) {
 328                 perLoaderCache = new HashMap<>(11);
 329                 factoryCache.put(loader, perLoaderCache);
 330             }
 331         }
 332 
 333         synchronized (perLoaderCache) {
 334             List<NamedWeakReference<Object>> factories =
 335                     perLoaderCache.get(facProp);
 336             if (factories != null) {
 337                 // Cached list
 338                 return factories.size() == 0 ? null
 339                     : new FactoryEnumeration(factories, loader);
 340             } else {
 341                 // Populate list with classes named in facProp; skipping
 342                 // those that we cannot load
 343                 StringTokenizer parser = new StringTokenizer(facProp, ":");
 344                 factories = new ArrayList<>(5);
 345                 while (parser.hasMoreTokens()) {
 346                     try {
 347                         // System.out.println("loading");
 348                         String className = parser.nextToken();
 349                         Class<?> c = helper.loadClass(className, loader);
 350                         factories.add(new NamedWeakReference<Object>(c, className));
 351                     } catch (Exception e) {
 352                         // ignore ClassNotFoundException, IllegalArgumentException
 353                     }
 354                 }
 355                 // System.out.println("adding to cache: " + factories);
 356                 perLoaderCache.put(facProp, factories);
 357                 return new FactoryEnumeration(factories, loader);
 358             }
 359         }
 360     }
 361 
 362     /**
 363      * Retrieves a factory from a list of packages specified in a
 364      * property.
 365      *
 366      * The property is gotten from the environment and the provider
 367      * resource file associated with the given context and concatenated.
 368      * classSuffix is added to the end of this list.
 369      * See getProperty(). The resulting property value is a list of package
 370      * prefixes.
 371      *<p>
 372      * This method then constructs a list of class names by concatenating
 373      * each package prefix with classSuffix and attempts to load and
 374      * instantiate the class until one succeeds.
 375      * Any class that cannot be loaded is ignored.
 376      * The resulting object is then cached in a two-level hash table,
 377      * keyed first by the context class loader and then by the property's
 378      * value and classSuffix.
 379      * The next time threads of the same context class loader call this
 380      * method, they use the cached factory.
 381      * If no factory can be loaded, NO_FACTORY is recorded in the table
 382      * so that next time it'll return quickly.
 383      *
 384      * @param propName  The non-null property name
 385      * @param env       The possibly null environment properties
 386      * @param ctx       The possibly null context
 387      * @param classSuffix The non-null class name
 388      *                  (e.g. ".ldap.ldapURLContextFactory).
 389      * @param defaultPkgPrefix The non-null default package prefix.
 390      *        (e.g., "com.sun.jndi.url").
 391      * @return An factory object; null if none.
 392      * @exception NamingException If encounter problem while reading the provider
 393      * property file, or problem instantiating the factory.
 394      *
 395      * @see javax.naming.spi.NamingManager#getURLContext
 396      * @see javax.naming.spi.NamingManager#getURLObject
 397      */
 398     public static Object getFactory(String propName, Hashtable<?,?> env,
 399             Context ctx, String classSuffix, String defaultPkgPrefix)
 400             throws NamingException {
 401 
 402         // Merge property with provider property and supplied default
 403         String facProp = getProperty(propName, env, ctx, true);
 404         if (facProp != null)
 405             facProp += (":" + defaultPkgPrefix);
 406         else
 407             facProp = defaultPkgPrefix;
 408 
 409         // Cache factory based on context class loader, class name, and
 410         // property val
 411         ClassLoader loader = helper.getContextClassLoader();
 412         String key = classSuffix + " " + facProp;
 413 
 414         Map<String, WeakReference<Object>> perLoaderCache = null;
 415         synchronized (urlFactoryCache) {
 416             perLoaderCache = urlFactoryCache.get(loader);
 417             if (perLoaderCache == null) {
 418                 perLoaderCache = new HashMap<>(11);
 419                 urlFactoryCache.put(loader, perLoaderCache);
 420             }
 421         }
 422 
 423         synchronized (perLoaderCache) {
 424             Object factory = null;
 425 
 426             WeakReference<Object> factoryRef = perLoaderCache.get(key);
 427             if (factoryRef == NO_FACTORY) {
 428                 return null;
 429             } else if (factoryRef != null) {
 430                 factory = factoryRef.get();
 431                 if (factory != null) {  // check if weak ref has been cleared
 432                     return factory;
 433                 }
 434             }
 435 
 436             // Not cached; find first factory and cache
 437             StringTokenizer parser = new StringTokenizer(facProp, ":");
 438             String className;
 439             while (factory == null && parser.hasMoreTokens()) {
 440                 className = parser.nextToken() + classSuffix;
 441                 try {
 442                     // System.out.println("loading " + className);
 443                     factory = helper.loadClass(className, loader).newInstance();
 444                 } catch (InstantiationException e) {
 445                     NamingException ne =
 446                         new NamingException("Cannot instantiate " + className);
 447                     ne.setRootCause(e);
 448                     throw ne;
 449                 } catch (IllegalAccessException e) {
 450                     NamingException ne =
 451                         new NamingException("Cannot access " + className);
 452                     ne.setRootCause(e);
 453                     throw ne;
 454                 } catch (Exception e) {
 455                     // ignore ClassNotFoundException, IllegalArgumentException,
 456                     // etc.
 457                 }
 458             }
 459 
 460             // Cache it.
 461             perLoaderCache.put(key, (factory != null)
 462                                         ? new WeakReference<>(factory)
 463                                         : NO_FACTORY);
 464             return factory;
 465         }
 466     }
 467 
 468 
 469     // ---------- Private methods ----------
 470 
 471     /*
 472      * Returns the properties contained in the provider resource file
 473      * of an object's package.  Returns an empty hash table if the
 474      * object is null or the resource file cannot be found.  The
 475      * results are cached.
 476      *
 477      * @throws NamingException if an error occurs while reading the file.
 478      */
 479     private static Hashtable<? super String, Object>
 480         getProviderResource(Object obj)
 481             throws NamingException
 482     {
 483         if (obj == null) {
 484             return (new Hashtable<>(1));
 485         }
 486         synchronized (propertiesCache) {
 487             Class<?> c = obj.getClass();
 488 
 489             Hashtable<? super String, Object> props =
 490                     propertiesCache.get(c);
 491             if (props != null) {
 492                 return props;
 493             }
 494             props = new Properties();
 495 
 496             InputStream istream =
 497                 helper.getResourceAsStream(c, PROVIDER_RESOURCE_FILE_NAME);
 498 
 499             if (istream != null) {
 500                 try {
 501                     ((Properties)props).load(istream);
 502                 } catch (IOException e) {
 503                     NamingException ne = new ConfigurationException(
 504                             "Error reading provider resource file for " + c);
 505                     ne.setRootCause(e);
 506                     throw ne;
 507                 }
 508             }
 509             propertiesCache.put(c, props);
 510             return props;
 511         }
 512     }
 513 
 514 
 515     /*
 516      * Returns the Hashtable (never null) that results from merging
 517      * all application resource files available to this thread's
 518      * context class loader.  The properties file in <java.home>/lib
 519      * is also merged in.  The results are cached.
 520      *
 521      * SECURITY NOTES:
 522      * 1.  JNDI needs permission to read the application resource files.
 523      * 2.  Any class will be able to use JNDI to view the contents of
 524      * the application resource files in its own classpath.  Give
 525      * careful consideration to this before storing sensitive
 526      * information there.
 527      *
 528      * @throws NamingException if an error occurs while reading a resource
 529      *  file.
 530      */
 531     private static Hashtable<? super String, Object> getApplicationResources()
 532             throws NamingException {
 533 
 534         ClassLoader cl = helper.getContextClassLoader();
 535 
 536         synchronized (propertiesCache) {
 537             Hashtable<? super String, Object> result = propertiesCache.get(cl);
 538             if (result != null) {
 539                 return result;
 540             }
 541 
 542             try {
 543                 NamingEnumeration<InputStream> resources =
 544                     helper.getResources(cl, APP_RESOURCE_FILE_NAME);
 545                 while (resources.hasMore()) {
 546                     Properties props = new Properties();
 547                     props.load(resources.next());
 548 
 549                     if (result == null) {
 550                         result = props;
 551                     } else {
 552                         mergeTables(result, props);
 553                     }
 554                 }
 555 
 556                 // Merge in properties from file in <java.home>/lib.
 557                 InputStream istream =
 558                     helper.getJavaHomeLibStream(JRELIB_PROPERTY_FILE_NAME);
 559                 if (istream != null) {
 560                     Properties props = new Properties();
 561                     props.load(istream);
 562 
 563                     if (result == null) {
 564                         result = props;
 565                     } else {
 566                         mergeTables(result, props);
 567                     }
 568                 }
 569 
 570             } catch (IOException e) {
 571                 NamingException ne = new ConfigurationException(
 572                         "Error reading application resource file");
 573                 ne.setRootCause(e);
 574                 throw ne;
 575             }
 576             if (result == null) {
 577                 result = new Hashtable<>(11);
 578             }
 579             propertiesCache.put(cl, result);
 580             return result;
 581         }
 582     }
 583 
 584     /*
 585      * Merge the properties from one hash table into another.  Each
 586      * property in props2 that is not in props1 is added to props1.
 587      * For each property in both hash tables that is one of the
 588      * standard JNDI properties that specify colon-separated lists,
 589      * the values are concatenated and stored in props1.
 590      */
 591     private static void mergeTables(Hashtable<? super String, Object> props1,
 592                                     Hashtable<? super String, Object> props2) {
 593         for (Object key : props2.keySet()) {
 594             String prop = (String)key;
 595             Object val1 = props1.get(prop);
 596             if (val1 == null) {
 597                 props1.put(prop, props2.get(prop));
 598             } else if (isListProperty(prop)) {
 599                 String val2 = (String)props2.get(prop);
 600                 props1.put(prop, ((String)val1) + ":" + val2);
 601             }
 602         }
 603     }
 604 
 605     /*
 606      * Is a property one of the standard JNDI properties that specify
 607      * colon-separated lists?
 608      */
 609     private static boolean isListProperty(String prop) {
 610         prop = prop.intern();
 611         for (int i = 0; i < listProperties.length; i++) {
 612             if (prop == listProperties[i]) {
 613                 return true;
 614             }
 615         }
 616         return false;
 617     }
 618 }