0N/A/*
2362N/A * Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.naming.internal;
0N/A
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.net.URL;
0N/Aimport java.lang.ref.WeakReference;
1747N/Aimport java.lang.reflect.Method;
1747N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Map;
0N/Aimport java.util.Properties;
0N/Aimport java.util.StringTokenizer;
0N/Aimport java.util.List;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.WeakHashMap;
0N/A
0N/Aimport javax.naming.*;
0N/A
0N/A/**
0N/A * The ResourceManager class facilitates the reading of JNDI resource files.
0N/A *
0N/A * @author Rosanna Lee
0N/A * @author Scott Seligman
0N/A */
0N/A
0N/Apublic final class ResourceManager {
0N/A
0N/A /*
0N/A * Name of provider resource files (without the package-name prefix.)
0N/A */
0N/A private static final String PROVIDER_RESOURCE_FILE_NAME =
0N/A "jndiprovider.properties";
0N/A
0N/A /*
0N/A * Name of application resource files.
0N/A */
0N/A private static final String APP_RESOURCE_FILE_NAME = "jndi.properties";
0N/A
0N/A /*
0N/A * Name of properties file in <java.home>/lib.
0N/A */
0N/A private static final String JRELIB_PROPERTY_FILE_NAME = "jndi.properties";
0N/A
0N/A /*
0N/A * The standard JNDI properties that specify colon-separated lists.
0N/A */
0N/A private static final String[] listProperties = {
0N/A Context.OBJECT_FACTORIES,
0N/A Context.URL_PKG_PREFIXES,
0N/A Context.STATE_FACTORIES,
0N/A // The following shouldn't create a runtime dependence on ldap package.
0N/A javax.naming.ldap.LdapContext.CONTROL_FACTORIES
0N/A };
0N/A
0N/A private static final VersionHelper helper =
0N/A VersionHelper.getVersionHelper();
0N/A
0N/A /*
0N/A * A cache of the properties that have been constructed by
0N/A * the ResourceManager. A Hashtable from a provider resource
0N/A * file is keyed on a class in the resource file's package.
0N/A * One from application resource files is keyed on the thread's
0N/A * context class loader.
0N/A */
0N/A private static final WeakHashMap propertiesCache = new WeakHashMap(11);
0N/A
0N/A /*
0N/A * A cache of factory objects (ObjectFactory, StateFactory, ControlFactory).
0N/A *
0N/A * A two-level cache keyed first on context class loader and then
0N/A * on propValue. Value is a list of class or factory objects,
0N/A * weakly referenced so as not to prevent GC of the class loader.
0N/A * Used in getFactories().
0N/A */
0N/A private static final WeakHashMap factoryCache = new WeakHashMap(11);
0N/A
0N/A /*
0N/A * A cache of URL factory objects (ObjectFactory).
0N/A *
0N/A * A two-level cache keyed first on context class loader and then
0N/A * on classSuffix+propValue. Value is the factory itself (weakly
0N/A * referenced so as not to prevent GC of the class loader) or
0N/A * NO_FACTORY if a previous search revealed no factory. Used in
0N/A * getFactory().
0N/A */
0N/A private static final WeakHashMap urlFactoryCache = new WeakHashMap(11);
0N/A private static final WeakReference NO_FACTORY = new WeakReference(null);
0N/A
1747N/A /**
1747N/A * A class to allow JNDI properties be specified as applet parameters
1747N/A * without creating a static dependency on java.applet.
1747N/A */
1747N/A private static class AppletParameter {
1747N/A private static final Class<?> clazz = getClass("java.applet.Applet");
1747N/A private static final Method getMethod =
1747N/A getMethod(clazz, "getParameter", String.class);
1747N/A private static Class<?> getClass(String name) {
1747N/A try {
1747N/A return Class.forName(name, true, null);
1747N/A } catch (ClassNotFoundException e) {
1747N/A return null;
1747N/A }
1747N/A }
1747N/A private static Method getMethod(Class<?> clazz,
1747N/A String name,
1747N/A Class<?>... paramTypes)
1747N/A {
1747N/A if (clazz != null) {
1747N/A try {
1747N/A return clazz.getMethod(name, paramTypes);
1747N/A } catch (NoSuchMethodException e) {
1747N/A throw new AssertionError(e);
1747N/A }
1747N/A } else {
1747N/A return null;
1747N/A }
1747N/A }
1747N/A
1747N/A /**
1747N/A * Returns the value of the applet's named parameter.
1747N/A */
1747N/A static Object get(Object applet, String name) {
1747N/A // if clazz is null then applet cannot be an Applet.
1747N/A if (clazz == null || !clazz.isInstance(applet))
1747N/A throw new ClassCastException(applet.getClass().getName());
1747N/A try {
1747N/A return getMethod.invoke(applet, name);
1747N/A } catch (InvocationTargetException e) {
1747N/A throw new AssertionError(e);
1747N/A } catch (IllegalAccessException iae) {
1747N/A throw new AssertionError(iae);
1747N/A }
1747N/A }
1747N/A }
0N/A
0N/A // There should be no instances of this class.
0N/A private ResourceManager() {
0N/A }
0N/A
0N/A
0N/A // ---------- Public methods ----------
0N/A
0N/A /*
0N/A * Given the environment parameter passed to the initial context
0N/A * constructor, returns the full environment for that initial
0N/A * context (never null). This is based on the environment
0N/A * parameter, the applet parameters (where appropriate), the
0N/A * system properties, and all application resource files.
0N/A *
0N/A * <p> This method will modify <tt>env</tt> and save
0N/A * a reference to it. The caller may no longer modify it.
0N/A *
0N/A * @param env environment passed to initial context constructor.
0N/A * Null indicates an empty environment.
0N/A *
0N/A * @throws NamingException if an error occurs while reading a
0N/A * resource file
0N/A */
0N/A public static Hashtable getInitialEnvironment(Hashtable env)
0N/A throws NamingException
0N/A {
0N/A String[] props = VersionHelper.PROPS; // system/applet properties
0N/A if (env == null) {
0N/A env = new Hashtable(11);
0N/A }
1747N/A Object applet = env.get(Context.APPLET);
0N/A
0N/A // Merge property values from env param, applet params, and system
0N/A // properties. The first value wins: there's no concatenation of
0N/A // colon-separated lists.
0N/A // Read system properties by first trying System.getProperties(),
0N/A // and then trying System.getProperty() if that fails. The former
0N/A // is more efficient due to fewer permission checks.
0N/A //
0N/A String[] jndiSysProps = helper.getJndiProperties();
0N/A for (int i = 0; i < props.length; i++) {
0N/A Object val = env.get(props[i]);
0N/A if (val == null) {
0N/A if (applet != null) {
1747N/A val = AppletParameter.get(applet, props[i]);
0N/A }
0N/A if (val == null) {
0N/A // Read system property.
0N/A val = (jndiSysProps != null)
0N/A ? jndiSysProps[i]
0N/A : helper.getJndiProperty(i);
0N/A }
0N/A if (val != null) {
0N/A env.put(props[i], val);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Merge the above with the values read from all application
0N/A // resource files. Colon-separated lists are concatenated.
0N/A mergeTables(env, getApplicationResources());
0N/A return env;
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the property from the environment, or from the provider
0N/A * resource file associated with the given context. The environment
0N/A * may in turn contain values that come from applet parameters,
0N/A * system properties, or application resource files.
0N/A *
0N/A * If <tt>concat</tt> is true and both the environment and the provider
0N/A * resource file contain the property, the two values are concatenated
0N/A * (with a ':' separator).
0N/A *
0N/A * Returns null if no value is found.
0N/A *
0N/A * @param propName The non-null property name
0N/A * @param env The possibly null environment properties
0N/A * @param ctx The possibly null context
0N/A * @param concat True if multiple values should be concatenated
0N/A * @return the property value, or null is there is none.
0N/A * @throws NamingException if an error occurs while reading the provider
0N/A * resource file.
0N/A */
0N/A public static String getProperty(String propName, Hashtable env,
0N/A Context ctx, boolean concat)
0N/A throws NamingException {
0N/A
0N/A String val1 = (env != null) ? (String)env.get(propName) : null;
0N/A if ((ctx == null) ||
0N/A ((val1 != null) && !concat)) {
0N/A return val1;
0N/A }
0N/A String val2 = (String)getProviderResource(ctx).get(propName);
0N/A if (val1 == null) {
0N/A return val2;
0N/A } else if ((val2 == null) || !concat) {
0N/A return val1;
0N/A } else {
0N/A return (val1 + ":" + val2);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Retrieves an enumeration of factory classes/object specified by a
0N/A * property.
0N/A *
0N/A * The property is gotten from the environment and the provider
0N/A * resource file associated with the given context and concantenated.
0N/A * See getProperty(). The resulting property value is a list of class names.
0N/A *<p>
0N/A * This method then loads each class using the current thread's context
0N/A * class loader and keeps them in a list. Any class that cannot be loaded
0N/A * is ignored. The resulting list is then cached in a two-level
0N/A * hash table, keyed first by the context class loader and then by
0N/A * the property's value.
0N/A * The next time threads of the same context class loader call this
0N/A * method, they can use the cached list.
0N/A *<p>
0N/A * After obtaining the list either from the cache or by creating one from
0N/A * the property value, this method then creates and returns a
0N/A * FactoryEnumeration using the list. As the FactoryEnumeration is
0N/A * traversed, the cached Class object in the list is instantiated and
0N/A * replaced by an instance of the factory object itself. Both class
0N/A * objects and factories are wrapped in weak references so as not to
0N/A * prevent GC of the class loader.
0N/A *<p>
0N/A * Note that multiple threads can be accessing the same cached list
0N/A * via FactoryEnumeration, which locks the list during each next().
0N/A * The size of the list will not change,
0N/A * but a cached Class object might be replaced by an instantiated factory
0N/A * object.
0N/A *
0N/A * @param propName The non-null property name
0N/A * @param env The possibly null environment properties
0N/A * @param ctx The possibly null context
0N/A * @return An enumeration of factory classes/objects; null if none.
0N/A * @exception NamingException If encounter problem while reading the provider
0N/A * property file.
0N/A * @see javax.naming.spi.NamingManager#getObjectInstance
0N/A * @see javax.naming.spi.NamingManager#getStateToBind
0N/A * @see javax.naming.spi.DirectoryManager#getObjectInstance
0N/A * @see javax.naming.spi.DirectoryManager#getStateToBind
0N/A * @see javax.naming.ldap.ControlFactory#getControlInstance
0N/A */
0N/A public static FactoryEnumeration getFactories(String propName, Hashtable env,
0N/A Context ctx) throws NamingException {
0N/A
0N/A String facProp = getProperty(propName, env, ctx, true);
0N/A if (facProp == null)
0N/A return null; // no classes specified; return null
0N/A
0N/A // Cache is based on context class loader and property val
0N/A ClassLoader loader = helper.getContextClassLoader();
0N/A
0N/A Map perLoaderCache = null;
0N/A synchronized (factoryCache) {
0N/A perLoaderCache = (Map) factoryCache.get(loader);
0N/A if (perLoaderCache == null) {
0N/A perLoaderCache = new HashMap(11);
0N/A factoryCache.put(loader, perLoaderCache);
0N/A }
0N/A }
0N/A
0N/A synchronized (perLoaderCache) {
0N/A List factories = (List) perLoaderCache.get(facProp);
0N/A if (factories != null) {
0N/A // Cached list
0N/A return factories.size() == 0 ? null
0N/A : new FactoryEnumeration(factories, loader);
0N/A } else {
0N/A // Populate list with classes named in facProp; skipping
0N/A // those that we cannot load
0N/A StringTokenizer parser = new StringTokenizer(facProp, ":");
0N/A factories = new ArrayList(5);
0N/A while (parser.hasMoreTokens()) {
0N/A try {
0N/A // System.out.println("loading");
0N/A String className = parser.nextToken();
0N/A Class c = helper.loadClass(className, loader);
0N/A factories.add(new NamedWeakReference(c, className));
0N/A } catch (Exception e) {
0N/A // ignore ClassNotFoundException, IllegalArgumentException
0N/A }
0N/A }
0N/A // System.out.println("adding to cache: " + factories);
0N/A perLoaderCache.put(facProp, factories);
0N/A return new FactoryEnumeration(factories, loader);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Retrieves a factory from a list of packages specified in a
0N/A * property.
0N/A *
0N/A * The property is gotten from the environment and the provider
0N/A * resource file associated with the given context and concatenated.
0N/A * classSuffix is added to the end of this list.
0N/A * See getProperty(). The resulting property value is a list of package
0N/A * prefixes.
0N/A *<p>
0N/A * This method then constructs a list of class names by concatenating
0N/A * each package prefix with classSuffix and attempts to load and
0N/A * instantiate the class until one succeeds.
0N/A * Any class that cannot be loaded is ignored.
0N/A * The resulting object is then cached in a two-level hash table,
0N/A * keyed first by the context class loader and then by the property's
0N/A * value and classSuffix.
0N/A * The next time threads of the same context class loader call this
0N/A * method, they use the cached factory.
0N/A * If no factory can be loaded, NO_FACTORY is recorded in the table
0N/A * so that next time it'll return quickly.
0N/A *
0N/A * @param propName The non-null property name
0N/A * @param env The possibly null environment properties
0N/A * @param ctx The possibly null context
0N/A * @param classSuffix The non-null class name
0N/A * (e.g. ".ldap.ldapURLContextFactory).
0N/A * @param defaultPkgPrefix The non-null default package prefix.
0N/A * (e.g., "com.sun.jndi.url").
0N/A * @return An factory object; null if none.
0N/A * @exception NamingException If encounter problem while reading the provider
0N/A * property file, or problem instantiating the factory.
0N/A *
0N/A * @see javax.naming.spi.NamingManager#getURLContext
0N/A * @see javax.naming.spi.NamingManager#getURLObject
0N/A */
0N/A public static Object getFactory(String propName, Hashtable env, Context ctx,
0N/A String classSuffix, String defaultPkgPrefix) throws NamingException {
0N/A
0N/A // Merge property with provider property and supplied default
0N/A String facProp = getProperty(propName, env, ctx, true);
0N/A if (facProp != null)
0N/A facProp += (":" + defaultPkgPrefix);
0N/A else
0N/A facProp = defaultPkgPrefix;
0N/A
0N/A // Cache factory based on context class loader, class name, and
0N/A // property val
0N/A ClassLoader loader = helper.getContextClassLoader();
0N/A String key = classSuffix + " " + facProp;
0N/A
0N/A Map perLoaderCache = null;
0N/A synchronized (urlFactoryCache) {
0N/A perLoaderCache = (Map) urlFactoryCache.get(loader);
0N/A if (perLoaderCache == null) {
0N/A perLoaderCache = new HashMap(11);
0N/A urlFactoryCache.put(loader, perLoaderCache);
0N/A }
0N/A }
0N/A
0N/A synchronized (perLoaderCache) {
0N/A Object factory = null;
0N/A
0N/A WeakReference factoryRef = (WeakReference) perLoaderCache.get(key);
0N/A if (factoryRef == NO_FACTORY) {
0N/A return null;
0N/A } else if (factoryRef != null) {
0N/A factory = factoryRef.get();
0N/A if (factory != null) { // check if weak ref has been cleared
0N/A return factory;
0N/A }
0N/A }
0N/A
0N/A // Not cached; find first factory and cache
0N/A StringTokenizer parser = new StringTokenizer(facProp, ":");
0N/A String className;
0N/A while (factory == null && parser.hasMoreTokens()) {
0N/A className = parser.nextToken() + classSuffix;
0N/A try {
0N/A // System.out.println("loading " + className);
0N/A factory = helper.loadClass(className, loader).newInstance();
0N/A } catch (InstantiationException e) {
0N/A NamingException ne =
0N/A new NamingException("Cannot instantiate " + className);
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A } catch (IllegalAccessException e) {
0N/A NamingException ne =
0N/A new NamingException("Cannot access " + className);
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A } catch (Exception e) {
0N/A // ignore ClassNotFoundException, IllegalArgumentException,
0N/A // etc.
0N/A }
0N/A }
0N/A
0N/A // Cache it.
0N/A perLoaderCache.put(key, (factory != null)
0N/A ? new WeakReference(factory)
0N/A : NO_FACTORY);
0N/A return factory;
0N/A }
0N/A }
0N/A
0N/A
0N/A // ---------- Private methods ----------
0N/A
0N/A /*
0N/A * Returns the properties contained in the provider resource file
0N/A * of an object's package. Returns an empty hash table if the
0N/A * object is null or the resource file cannot be found. The
0N/A * results are cached.
0N/A *
0N/A * @throws NamingException if an error occurs while reading the file.
0N/A */
0N/A private static Hashtable getProviderResource(Object obj)
0N/A throws NamingException
0N/A {
0N/A if (obj == null) {
0N/A return (new Hashtable(1));
0N/A }
0N/A synchronized (propertiesCache) {
0N/A Class c = obj.getClass();
0N/A
0N/A Hashtable props = (Hashtable)propertiesCache.get(c);
0N/A if (props != null) {
0N/A return props;
0N/A }
0N/A props = new Properties();
0N/A
0N/A InputStream istream =
0N/A helper.getResourceAsStream(c, PROVIDER_RESOURCE_FILE_NAME);
0N/A
0N/A if (istream != null) {
0N/A try {
0N/A ((Properties)props).load(istream);
0N/A } catch (IOException e) {
0N/A NamingException ne = new ConfigurationException(
0N/A "Error reading provider resource file for " + c);
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A }
0N/A }
0N/A propertiesCache.put(c, props);
0N/A return props;
0N/A }
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Returns the Hashtable (never null) that results from merging
0N/A * all application resource files available to this thread's
0N/A * context class loader. The properties file in <java.home>/lib
0N/A * is also merged in. The results are cached.
0N/A *
0N/A * SECURITY NOTES:
0N/A * 1. JNDI needs permission to read the application resource files.
0N/A * 2. Any class will be able to use JNDI to view the contents of
0N/A * the application resource files in its own classpath. Give
0N/A * careful consideration to this before storing sensitive
0N/A * information there.
0N/A *
0N/A * @throws NamingException if an error occurs while reading a resource
0N/A * file.
0N/A */
0N/A private static Hashtable getApplicationResources() throws NamingException {
0N/A
0N/A ClassLoader cl = helper.getContextClassLoader();
0N/A
0N/A synchronized (propertiesCache) {
0N/A Hashtable result = (Hashtable)propertiesCache.get(cl);
0N/A if (result != null) {
0N/A return result;
0N/A }
0N/A
0N/A try {
0N/A NamingEnumeration resources =
0N/A helper.getResources(cl, APP_RESOURCE_FILE_NAME);
0N/A while (resources.hasMore()) {
0N/A Properties props = new Properties();
0N/A props.load((InputStream)resources.next());
0N/A
0N/A if (result == null) {
0N/A result = props;
0N/A } else {
0N/A mergeTables(result, props);
0N/A }
0N/A }
0N/A
0N/A // Merge in properties from file in <java.home>/lib.
0N/A InputStream istream =
0N/A helper.getJavaHomeLibStream(JRELIB_PROPERTY_FILE_NAME);
0N/A if (istream != null) {
0N/A Properties props = new Properties();
0N/A props.load(istream);
0N/A
0N/A if (result == null) {
0N/A result = props;
0N/A } else {
0N/A mergeTables(result, props);
0N/A }
0N/A }
0N/A
0N/A } catch (IOException e) {
0N/A NamingException ne = new ConfigurationException(
0N/A "Error reading application resource file");
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A }
0N/A if (result == null) {
0N/A result = new Hashtable(11);
0N/A }
0N/A propertiesCache.put(cl, result);
0N/A return result;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Merge the properties from one hash table into another. Each
0N/A * property in props2 that is not in props1 is added to props1.
0N/A * For each property in both hash tables that is one of the
0N/A * standard JNDI properties that specify colon-separated lists,
0N/A * the values are concatenated and stored in props1.
0N/A */
0N/A private static void mergeTables(Hashtable props1, Hashtable props2) {
0N/A Enumeration keys = props2.keys();
0N/A
0N/A while (keys.hasMoreElements()) {
0N/A String prop = (String)keys.nextElement();
0N/A Object val1 = props1.get(prop);
0N/A if (val1 == null) {
0N/A props1.put(prop, props2.get(prop));
0N/A } else if (isListProperty(prop)) {
0N/A String val2 = (String)props2.get(prop);
0N/A props1.put(prop, ((String)val1) + ":" + val2);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Is a property one of the standard JNDI properties that specify
0N/A * colon-separated lists?
0N/A */
0N/A private static boolean isListProperty(String prop) {
0N/A prop = prop.intern();
0N/A for (int i = 0; i < listProperties.length; i++) {
0N/A if (prop == listProperties[i]) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A}