ResourceManager.java revision 2362
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 * 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 * 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 * 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. 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 0N/A * The ResourceManager class facilitates the reading of JNDI resource files. 0N/A * @author Rosanna Lee 0N/A * @author Scott Seligman 0N/A * Name of provider resource files (without the package-name prefix.) 0N/A * Name of application resource files. 0N/A * Name of properties file in <java.home>/lib. 0N/A * The standard JNDI properties that specify colon-separated lists. 0N/A // The following shouldn't create a runtime dependence on ldap package. 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 * A cache of factory objects (ObjectFactory, StateFactory, ControlFactory). 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 * A cache of URL factory objects (ObjectFactory). 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 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 * Returns the value of the applet's named parameter. 1747N/A // if clazz is null then applet cannot be an Applet. 0N/A // There should be no instances of this class. 0N/A // ---------- Public methods ---------- 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 * <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 * @param env environment passed to initial context constructor. 0N/A * Null indicates an empty environment. 0N/A * @throws NamingException if an error occurs while reading 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 // Read system property. 0N/A // Merge the above with the values read from all application 0N/A // resource files. Colon-separated lists are concatenated. 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 * 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 * Returns null if no value is found. 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 * 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 * 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 * 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 * 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 * @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 * @exception NamingException If encounter problem while reading the provider 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 return null;
// no classes specified; return null 0N/A // Cache is based on context class loader and property val 0N/A // Populate list with classes named in facProp; skipping 0N/A // those that we cannot load 0N/A // System.out.println("loading"); 0N/A // ignore ClassNotFoundException, IllegalArgumentException 0N/A // System.out.println("adding to cache: " + factories); 0N/A * Retrieves a factory from a list of packages specified in 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 * 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 * @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 * @see javax.naming.spi.NamingManager#getURLContext 0N/A * @see javax.naming.spi.NamingManager#getURLObject 0N/A // Merge property with provider property and supplied default 0N/A // Cache factory based on context class loader, class name, and 0N/A // Not cached; find first factory and cache 0N/A // System.out.println("loading " + className); 0N/A // ignore ClassNotFoundException, IllegalArgumentException, 0N/A // ---------- Private methods ---------- 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 * @throws NamingException if an error occurs while reading the file. 0N/A "Error reading provider resource file for " + c);
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 * 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 * @throws NamingException if an error occurs while reading a resource 0N/A // Merge in properties from file in <java.home>/lib. 0N/A "Error reading application resource file");
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 * Is a property one of the standard JNDI properties that specify 0N/A * colon-separated lists?