Path: blob/master/src/java.naming/share/classes/com/sun/naming/internal/ResourceManager.java
41161 views
/*1* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.naming.internal;2627import java.io.InputStream;28import java.io.IOException;29import java.lang.ref.WeakReference;30import java.util.HashMap;31import java.util.Hashtable;32import java.util.Map;33import java.util.Properties;34import java.util.StringTokenizer;35import java.util.List;36import java.util.ArrayList;37import java.util.WeakHashMap;3839import javax.naming.*;4041/**42* The ResourceManager class facilitates the reading of JNDI resource files.43*44* @author Rosanna Lee45* @author Scott Seligman46*/4748public final class ResourceManager {4950/*51* Name of provider resource files (without the package-name prefix.)52*/53private static final String PROVIDER_RESOURCE_FILE_NAME =54"jndiprovider.properties";5556/*57* Name of application resource files.58*/59private static final String APP_RESOURCE_FILE_NAME = "jndi.properties";6061/*62* Name of properties file in <java.home>/conf.63*/64private static final String JRE_CONF_PROPERTY_FILE_NAME = "jndi.properties";6566/*67* Internal environment property, that when set to "true", disables68* application resource files lookup to prevent recursion issues69* when validating signed JARs.70*/71private static final String DISABLE_APP_RESOURCE_FILES =72"com.sun.naming.disable.app.resource.files";7374/*75* The standard JNDI properties that specify colon-separated lists.76*/77private static final String[] listProperties = {78Context.OBJECT_FACTORIES,79Context.URL_PKG_PREFIXES,80Context.STATE_FACTORIES,81// The following shouldn't create a runtime dependence on ldap package.82javax.naming.ldap.LdapContext.CONTROL_FACTORIES83};8485private static final VersionHelper helper =86VersionHelper.getVersionHelper();8788/*89* A cache of the properties that have been constructed by90* the ResourceManager. A Hashtable from a provider resource91* file is keyed on a class in the resource file's package.92* One from application resource files is keyed on the thread's93* context class loader.94*/95// WeakHashMap<Class | ClassLoader, Hashtable>96private static final WeakHashMap<Object, Hashtable<? super String, Object>>97propertiesCache = new WeakHashMap<>(11);9899/*100* A cache of factory objects (ObjectFactory, StateFactory, ControlFactory).101*102* A two-level cache keyed first on context class loader and then103* on propValue. Value is a list of class or factory objects,104* weakly referenced so as not to prevent GC of the class loader.105* Used in getFactories().106*/107private static final108WeakHashMap<ClassLoader, Map<String, List<NamedWeakReference<Object>>>>109factoryCache = new WeakHashMap<>(11);110111/*112* A cache of URL factory objects (ObjectFactory).113*114* A two-level cache keyed first on context class loader and then115* on classSuffix+propValue. Value is the factory itself (weakly116* referenced so as not to prevent GC of the class loader) or117* NO_FACTORY if a previous search revealed no factory. Used in118* getFactory().119*/120private static final121WeakHashMap<ClassLoader, Map<String, WeakReference<Object>>>122urlFactoryCache = new WeakHashMap<>(11);123private static final WeakReference<Object> NO_FACTORY =124new WeakReference<>(null);125126// There should be no instances of this class.127private ResourceManager() {128}129130131// ---------- Public methods ----------132133/**134* Given the environment parameter passed to the initial context135* constructor, returns the full environment for that initial136* context (never null). This is based on the environment137* parameter, the system properties, and all application resource files.138*139* <p> This method will modify {@code env} and save140* a reference to it. The caller may no longer modify it.141*142* @param env environment passed to initial context constructor.143* Null indicates an empty environment.144*145* @throws NamingException if an error occurs while reading a146* resource file147*/148@SuppressWarnings("unchecked")149public static Hashtable<?, ?> getInitialEnvironment(Hashtable<?, ?> env)150throws NamingException151{152String[] props = VersionHelper.PROPS; // system properties153if (env == null) {154env = new Hashtable<>(11);155}156157// Merge property values from env param, and system properties.158// The first value wins: there's no concatenation of159// colon-separated lists.160// Read system properties by first trying System.getProperties(),161// and then trying System.getProperty() if that fails. The former162// is more efficient due to fewer permission checks.163//164String[] jndiSysProps = helper.getJndiProperties();165for (int i = 0; i < props.length; i++) {166Object val = env.get(props[i]);167if (val == null) {168// Read system property.169val = (jndiSysProps != null)170? jndiSysProps[i]171: helper.getJndiProperty(i);172}173if (val != null) {174((Hashtable<String, Object>)env).put(props[i], val);175}176}177178// Return without merging if application resource files lookup179// is disabled.180String disableAppRes = (String)env.get(DISABLE_APP_RESOURCE_FILES);181if (disableAppRes != null && disableAppRes.equalsIgnoreCase("true")) {182return env;183}184185// Merge the above with the values read from all application186// resource files. Colon-separated lists are concatenated.187mergeTables((Hashtable<Object, Object>)env, getApplicationResources());188return env;189}190191/**192* Retrieves the property from the environment, or from the provider193* resource file associated with the given context. The environment194* may in turn contain values that come from system properties,195* or application resource files.196*197* If {@code concat} is true and both the environment and the provider198* resource file contain the property, the two values are concatenated199* (with a ':' separator).200*201* Returns null if no value is found.202*203* @param propName The non-null property name204* @param env The possibly null environment properties205* @param ctx The possibly null context206* @param concat True if multiple values should be concatenated207* @return the property value, or null is there is none.208* @throws NamingException if an error occurs while reading the provider209* resource file.210*/211public static String getProperty(String propName, Hashtable<?,?> env,212Context ctx, boolean concat)213throws NamingException {214215String val1 = (env != null) ? (String)env.get(propName) : null;216if ((ctx == null) ||217((val1 != null) && !concat)) {218return val1;219}220String val2 = (String)getProviderResource(ctx).get(propName);221if (val1 == null) {222return val2;223} else if ((val2 == null) || !concat) {224return val1;225} else {226return (val1 + ":" + val2);227}228}229230/**231* Retrieves an enumeration of factory classes/object specified by a232* property.233*234* The property is gotten from the environment and the provider235* resource file associated with the given context and concatenated.236* See getProperty(). The resulting property value is a list of class names.237*<p>238* This method then loads each class using the current thread's context239* class loader and keeps them in a list. Any class that cannot be loaded240* is ignored. The resulting list is then cached in a two-level241* hash table, keyed first by the context class loader and then by242* the property's value.243* The next time threads of the same context class loader call this244* method, they can use the cached list.245*<p>246* After obtaining the list either from the cache or by creating one from247* the property value, this method then creates and returns a248* FactoryEnumeration using the list. As the FactoryEnumeration is249* traversed, the cached Class object in the list is instantiated and250* replaced by an instance of the factory object itself. Both class251* objects and factories are wrapped in weak references so as not to252* prevent GC of the class loader.253*<p>254* Note that multiple threads can be accessing the same cached list255* via FactoryEnumeration, which locks the list during each next().256* The size of the list will not change,257* but a cached Class object might be replaced by an instantiated factory258* object.259*260* @param propName The non-null property name261* @param env The possibly null environment properties262* @param ctx The possibly null context263* @return An enumeration of factory classes/objects; null if none.264* @exception NamingException If encounter problem while reading the provider265* property file.266* @see javax.naming.spi.NamingManager#getObjectInstance267* @see javax.naming.spi.NamingManager#getStateToBind268* @see javax.naming.spi.DirectoryManager#getObjectInstance269* @see javax.naming.spi.DirectoryManager#getStateToBind270* @see javax.naming.ldap.ControlFactory#getControlInstance271*/272public static FactoryEnumeration getFactories(String propName,273Hashtable<?,?> env, Context ctx) throws NamingException {274275String facProp = getProperty(propName, env, ctx, true);276if (facProp == null)277return null; // no classes specified; return null278279// Cache is based on context class loader and property val280ClassLoader loader = helper.getContextClassLoader();281282Map<String, List<NamedWeakReference<Object>>> perLoaderCache = null;283synchronized (factoryCache) {284perLoaderCache = factoryCache.get(loader);285if (perLoaderCache == null) {286perLoaderCache = new HashMap<>(11);287factoryCache.put(loader, perLoaderCache);288}289}290291synchronized (perLoaderCache) {292List<NamedWeakReference<Object>> factories =293perLoaderCache.get(facProp);294if (factories != null) {295// Cached list296return factories.size() == 0 ? null297: new FactoryEnumeration(factories, loader);298} else {299// Populate list with classes named in facProp; skipping300// those that we cannot load301StringTokenizer parser = new StringTokenizer(facProp, ":");302factories = new ArrayList<>(5);303while (parser.hasMoreTokens()) {304try {305// System.out.println("loading");306String className = parser.nextToken();307Class<?> c = helper.loadClass(className, loader);308factories.add(new NamedWeakReference<Object>(c, className));309} catch (Exception e) {310// ignore ClassNotFoundException, IllegalArgumentException311}312}313// System.out.println("adding to cache: " + factories);314perLoaderCache.put(facProp, factories);315return new FactoryEnumeration(factories, loader);316}317}318}319320/**321* Retrieves a factory from a list of packages specified in a322* property.323*324* The property is gotten from the environment and the provider325* resource file associated with the given context and concatenated.326* classSuffix is added to the end of this list.327* See getProperty(). The resulting property value is a list of package328* prefixes.329*<p>330* This method then constructs a list of class names by concatenating331* each package prefix with classSuffix and attempts to load and332* instantiate the class until one succeeds.333* Any class that cannot be loaded is ignored.334* The resulting object is then cached in a two-level hash table,335* keyed first by the context class loader and then by the property's336* value and classSuffix.337* The next time threads of the same context class loader call this338* method, they use the cached factory.339* If no factory can be loaded, NO_FACTORY is recorded in the table340* so that next time it'll return quickly.341*342* @param propName The non-null property name343* @param env The possibly null environment properties344* @param ctx The possibly null context345* @param classSuffix The non-null class name346* (e.g. ".ldap.ldapURLContextFactory).347* @param defaultPkgPrefix The non-null default package prefix.348* (e.g., "com.sun.jndi.url").349* @return An factory object; null if none.350* @exception NamingException If encounter problem while reading the provider351* property file, or problem instantiating the factory.352*353* @see javax.naming.spi.NamingManager#getURLContext354* @see javax.naming.spi.NamingManager#getURLObject355*/356public static Object getFactory(String propName, Hashtable<?,?> env,357Context ctx, String classSuffix, String defaultPkgPrefix)358throws NamingException {359360// Merge property with provider property and supplied default361String facProp = getProperty(propName, env, ctx, true);362if (facProp != null)363facProp += (":" + defaultPkgPrefix);364else365facProp = defaultPkgPrefix;366367// Cache factory based on context class loader, class name, and368// property val369ClassLoader loader = helper.getContextClassLoader();370String key = classSuffix + " " + facProp;371372Map<String, WeakReference<Object>> perLoaderCache = null;373synchronized (urlFactoryCache) {374perLoaderCache = urlFactoryCache.get(loader);375if (perLoaderCache == null) {376perLoaderCache = new HashMap<>(11);377urlFactoryCache.put(loader, perLoaderCache);378}379}380381synchronized (perLoaderCache) {382Object factory = null;383384WeakReference<Object> factoryRef = perLoaderCache.get(key);385if (factoryRef == NO_FACTORY) {386return null;387} else if (factoryRef != null) {388factory = factoryRef.get();389if (factory != null) { // check if weak ref has been cleared390return factory;391}392}393394// Not cached; find first factory and cache395StringTokenizer parser = new StringTokenizer(facProp, ":");396String className;397while (factory == null && parser.hasMoreTokens()) {398className = parser.nextToken() + classSuffix;399try {400// System.out.println("loading " + className);401@SuppressWarnings("deprecation") // Class.newInstance402Object tmp = helper.loadClass(className, loader).newInstance();403factory = tmp;404} catch (InstantiationException e) {405NamingException ne =406new NamingException("Cannot instantiate " + className);407ne.setRootCause(e);408throw ne;409} catch (IllegalAccessException e) {410NamingException ne =411new NamingException("Cannot access " + className);412ne.setRootCause(e);413throw ne;414} catch (Exception e) {415// ignore ClassNotFoundException, IllegalArgumentException,416// etc.417}418}419420// Cache it.421perLoaderCache.put(key, (factory != null)422? new WeakReference<>(factory)423: NO_FACTORY);424return factory;425}426}427428429// ---------- Private methods ----------430431/*432* Returns the properties contained in the provider resource file433* of an object's package. Returns an empty hash table if the434* object is null or the resource file cannot be found. The435* results are cached.436*437* @throws NamingException if an error occurs while reading the file.438*/439private static Hashtable<? super String, Object>440getProviderResource(Object obj)441throws NamingException442{443if (obj == null) {444return (new Hashtable<>(1));445}446synchronized (propertiesCache) {447Class<?> c = obj.getClass();448449Hashtable<? super String, Object> props =450propertiesCache.get(c);451if (props != null) {452return props;453}454props = new Properties();455456InputStream istream =457helper.getResourceAsStream(c, PROVIDER_RESOURCE_FILE_NAME);458459if (istream != null) {460try {461((Properties)props).load(istream);462} catch (IOException e) {463NamingException ne = new ConfigurationException(464"Error reading provider resource file for " + c);465ne.setRootCause(e);466throw ne;467}468}469propertiesCache.put(c, props);470return props;471}472}473474475/*476* Returns the Hashtable (never null) that results from merging477* all application resource files available to this thread's478* context class loader. The properties file in <java.home>/conf479* is also merged in. The results are cached.480*481* SECURITY NOTES:482* 1. JNDI needs permission to read the application resource files.483* 2. Any class will be able to use JNDI to view the contents of484* the application resource files in its own classpath. Give485* careful consideration to this before storing sensitive486* information there.487*488* @throws NamingException if an error occurs while reading a resource489* file.490*/491private static Hashtable<? super String, Object> getApplicationResources()492throws NamingException {493494ClassLoader cl = helper.getContextClassLoader();495496synchronized (propertiesCache) {497Hashtable<? super String, Object> result = propertiesCache.get(cl);498if (result != null) {499return result;500}501502try {503NamingEnumeration<InputStream> resources =504helper.getResources(cl, APP_RESOURCE_FILE_NAME);505try {506while (resources.hasMore()) {507Properties props = new Properties();508InputStream istream = resources.next();509try {510props.load(istream);511} finally {512istream.close();513}514515if (result == null) {516result = props;517} else {518mergeTables(result, props);519}520}521} finally {522while (resources.hasMore()) {523resources.next().close();524}525}526527// Merge in properties from file in <java.home>/conf.528InputStream istream =529helper.getJavaHomeConfStream(JRE_CONF_PROPERTY_FILE_NAME);530if (istream != null) {531try {532Properties props = new Properties();533props.load(istream);534535if (result == null) {536result = props;537} else {538mergeTables(result, props);539}540} finally {541istream.close();542}543}544545} catch (IOException e) {546NamingException ne = new ConfigurationException(547"Error reading application resource file");548ne.setRootCause(e);549throw ne;550}551if (result == null) {552result = new Hashtable<>(11);553}554propertiesCache.put(cl, result);555return result;556}557}558559/*560* Merge the properties from one hash table into another. Each561* property in props2 that is not in props1 is added to props1.562* For each property in both hash tables that is one of the563* standard JNDI properties that specify colon-separated lists,564* the values are concatenated and stored in props1.565*/566private static void mergeTables(Hashtable<? super String, Object> props1,567Hashtable<? super String, Object> props2) {568for (Object key : props2.keySet()) {569String prop = (String)key;570Object val1 = props1.get(prop);571if (val1 == null) {572props1.put(prop, props2.get(prop));573} else if (isListProperty(prop)) {574String val2 = (String)props2.get(prop);575props1.put(prop, ((String)val1) + ":" + val2);576}577}578}579580/*581* Is a property one of the standard JNDI properties that specify582* colon-separated lists?583*/584private static boolean isListProperty(String prop) {585prop = prop.intern();586for (int i = 0; i < listProperties.length; i++) {587if (prop == listProperties[i]) {588return true;589}590}591return false;592}593}594595596