Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/naming/internal/ResourceManager.java
38923 views
/*1* Copyright (c) 1999, 2013, 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.lang.reflect.Method;31import java.lang.reflect.InvocationTargetException;32import java.util.HashMap;33import java.util.Hashtable;34import java.util.Map;35import java.util.Properties;36import java.util.StringTokenizer;37import java.util.List;38import java.util.ArrayList;39import java.util.WeakHashMap;4041import javax.naming.*;4243/**44* The ResourceManager class facilitates the reading of JNDI resource files.45*46* @author Rosanna Lee47* @author Scott Seligman48*/4950public final class ResourceManager {5152/*53* Name of provider resource files (without the package-name prefix.)54*/55private static final String PROVIDER_RESOURCE_FILE_NAME =56"jndiprovider.properties";5758/*59* Name of application resource files.60*/61private static final String APP_RESOURCE_FILE_NAME = "jndi.properties";6263/*64* Name of properties file in <java.home>/lib.65*/66private static final String JRELIB_PROPERTY_FILE_NAME = "jndi.properties";6768/*69* Internal environment property, that when set to "true", disables70* application resource files lookup to prevent recursion issues71* when validating signed JARs.72*/73private static final String DISABLE_APP_RESOURCE_FILES =74"com.sun.naming.disable.app.resource.files";7576/*77* The standard JNDI properties that specify colon-separated lists.78*/79private static final String[] listProperties = {80Context.OBJECT_FACTORIES,81Context.URL_PKG_PREFIXES,82Context.STATE_FACTORIES,83// The following shouldn't create a runtime dependence on ldap package.84javax.naming.ldap.LdapContext.CONTROL_FACTORIES85};8687private static final VersionHelper helper =88VersionHelper.getVersionHelper();8990/*91* A cache of the properties that have been constructed by92* the ResourceManager. A Hashtable from a provider resource93* file is keyed on a class in the resource file's package.94* One from application resource files is keyed on the thread's95* context class loader.96*/97// WeakHashMap<Class | ClassLoader, Hashtable>98private static final WeakHashMap<Object, Hashtable<? super String, Object>>99propertiesCache = new WeakHashMap<>(11);100101/*102* A cache of factory objects (ObjectFactory, StateFactory, ControlFactory).103*104* A two-level cache keyed first on context class loader and then105* on propValue. Value is a list of class or factory objects,106* weakly referenced so as not to prevent GC of the class loader.107* Used in getFactories().108*/109private static final110WeakHashMap<ClassLoader, Map<String, List<NamedWeakReference<Object>>>>111factoryCache = new WeakHashMap<>(11);112113/*114* A cache of URL factory objects (ObjectFactory).115*116* A two-level cache keyed first on context class loader and then117* on classSuffix+propValue. Value is the factory itself (weakly118* referenced so as not to prevent GC of the class loader) or119* NO_FACTORY if a previous search revealed no factory. Used in120* getFactory().121*/122private static final123WeakHashMap<ClassLoader, Map<String, WeakReference<Object>>>124urlFactoryCache = new WeakHashMap<>(11);125private static final WeakReference<Object> NO_FACTORY =126new WeakReference<>(null);127128/**129* A class to allow JNDI properties be specified as applet parameters130* without creating a static dependency on java.applet.131*/132private static class AppletParameter {133private static final Class<?> clazz = getClass("java.applet.Applet");134private static final Method getMethod =135getMethod(clazz, "getParameter", String.class);136private static Class<?> getClass(String name) {137try {138return Class.forName(name, true, null);139} catch (ClassNotFoundException e) {140return null;141}142}143private static Method getMethod(Class<?> clazz,144String name,145Class<?>... paramTypes)146{147if (clazz != null) {148try {149return clazz.getMethod(name, paramTypes);150} catch (NoSuchMethodException e) {151throw new AssertionError(e);152}153} else {154return null;155}156}157158/**159* Returns the value of the applet's named parameter.160*/161static Object get(Object applet, String name) {162// if clazz is null then applet cannot be an Applet.163if (clazz == null || !clazz.isInstance(applet))164throw new ClassCastException(applet.getClass().getName());165try {166return getMethod.invoke(applet, name);167} catch (InvocationTargetException |168IllegalAccessException e) {169throw new AssertionError(e);170}171}172}173174// There should be no instances of this class.175private ResourceManager() {176}177178179// ---------- Public methods ----------180181/*182* Given the environment parameter passed to the initial context183* constructor, returns the full environment for that initial184* context (never null). This is based on the environment185* parameter, the applet parameters (where appropriate), the186* system properties, and all application resource files.187*188* <p> This method will modify <tt>env</tt> and save189* a reference to it. The caller may no longer modify it.190*191* @param env environment passed to initial context constructor.192* Null indicates an empty environment.193*194* @throws NamingException if an error occurs while reading a195* resource file196*/197@SuppressWarnings("unchecked")198public static Hashtable<?, ?> getInitialEnvironment(199Hashtable<?, ?> env)200throws NamingException201{202String[] props = VersionHelper.PROPS; // system/applet properties203if (env == null) {204env = new Hashtable<>(11);205}206Object applet = env.get(Context.APPLET);207208// Merge property values from env param, applet params, and system209// properties. The first value wins: there's no concatenation of210// colon-separated lists.211// Read system properties by first trying System.getProperties(),212// and then trying System.getProperty() if that fails. The former213// is more efficient due to fewer permission checks.214//215String[] jndiSysProps = helper.getJndiProperties();216for (int i = 0; i < props.length; i++) {217Object val = env.get(props[i]);218if (val == null) {219if (applet != null) {220val = AppletParameter.get(applet, props[i]);221}222if (val == null) {223// Read system property.224val = (jndiSysProps != null)225? jndiSysProps[i]226: helper.getJndiProperty(i);227}228if (val != null) {229((Hashtable<String, Object>)env).put(props[i], val);230}231}232}233234// Return without merging if application resource files lookup235// is disabled.236String disableAppRes = (String)env.get(DISABLE_APP_RESOURCE_FILES);237if (disableAppRes != null && disableAppRes.equalsIgnoreCase("true")) {238return env;239}240241// Merge the above with the values read from all application242// resource files. Colon-separated lists are concatenated.243mergeTables((Hashtable<Object, Object>)env, getApplicationResources());244return env;245}246247/**248* Retrieves the property from the environment, or from the provider249* resource file associated with the given context. The environment250* may in turn contain values that come from applet parameters,251* system properties, or application resource files.252*253* If <tt>concat</tt> is true and both the environment and the provider254* resource file contain the property, the two values are concatenated255* (with a ':' separator).256*257* Returns null if no value is found.258*259* @param propName The non-null property name260* @param env The possibly null environment properties261* @param ctx The possibly null context262* @param concat True if multiple values should be concatenated263* @return the property value, or null is there is none.264* @throws NamingException if an error occurs while reading the provider265* resource file.266*/267public static String getProperty(String propName, Hashtable<?,?> env,268Context ctx, boolean concat)269throws NamingException {270271String val1 = (env != null) ? (String)env.get(propName) : null;272if ((ctx == null) ||273((val1 != null) && !concat)) {274return val1;275}276String val2 = (String)getProviderResource(ctx).get(propName);277if (val1 == null) {278return val2;279} else if ((val2 == null) || !concat) {280return val1;281} else {282return (val1 + ":" + val2);283}284}285286/**287* Retrieves an enumeration of factory classes/object specified by a288* property.289*290* The property is gotten from the environment and the provider291* resource file associated with the given context and concantenated.292* See getProperty(). The resulting property value is a list of class names.293*<p>294* This method then loads each class using the current thread's context295* class loader and keeps them in a list. Any class that cannot be loaded296* is ignored. The resulting list is then cached in a two-level297* hash table, keyed first by the context class loader and then by298* the property's value.299* The next time threads of the same context class loader call this300* method, they can use the cached list.301*<p>302* After obtaining the list either from the cache or by creating one from303* the property value, this method then creates and returns a304* FactoryEnumeration using the list. As the FactoryEnumeration is305* traversed, the cached Class object in the list is instantiated and306* replaced by an instance of the factory object itself. Both class307* objects and factories are wrapped in weak references so as not to308* prevent GC of the class loader.309*<p>310* Note that multiple threads can be accessing the same cached list311* via FactoryEnumeration, which locks the list during each next().312* The size of the list will not change,313* but a cached Class object might be replaced by an instantiated factory314* object.315*316* @param propName The non-null property name317* @param env The possibly null environment properties318* @param ctx The possibly null context319* @return An enumeration of factory classes/objects; null if none.320* @exception NamingException If encounter problem while reading the provider321* property file.322* @see javax.naming.spi.NamingManager#getObjectInstance323* @see javax.naming.spi.NamingManager#getStateToBind324* @see javax.naming.spi.DirectoryManager#getObjectInstance325* @see javax.naming.spi.DirectoryManager#getStateToBind326* @see javax.naming.ldap.ControlFactory#getControlInstance327*/328public static FactoryEnumeration getFactories(String propName,329Hashtable<?,?> env, Context ctx) throws NamingException {330331String facProp = getProperty(propName, env, ctx, true);332if (facProp == null)333return null; // no classes specified; return null334335// Cache is based on context class loader and property val336ClassLoader loader = helper.getContextClassLoader();337338Map<String, List<NamedWeakReference<Object>>> perLoaderCache = null;339synchronized (factoryCache) {340perLoaderCache = factoryCache.get(loader);341if (perLoaderCache == null) {342perLoaderCache = new HashMap<>(11);343factoryCache.put(loader, perLoaderCache);344}345}346347synchronized (perLoaderCache) {348List<NamedWeakReference<Object>> factories =349perLoaderCache.get(facProp);350if (factories != null) {351// Cached list352return factories.size() == 0 ? null353: new FactoryEnumeration(factories, loader);354} else {355// Populate list with classes named in facProp; skipping356// those that we cannot load357StringTokenizer parser = new StringTokenizer(facProp, ":");358factories = new ArrayList<>(5);359while (parser.hasMoreTokens()) {360try {361// System.out.println("loading");362String className = parser.nextToken();363Class<?> c = helper.loadClass(className, loader);364factories.add(new NamedWeakReference<Object>(c, className));365} catch (Exception e) {366// ignore ClassNotFoundException, IllegalArgumentException367}368}369// System.out.println("adding to cache: " + factories);370perLoaderCache.put(facProp, factories);371return new FactoryEnumeration(factories, loader);372}373}374}375376/**377* Retrieves a factory from a list of packages specified in a378* property.379*380* The property is gotten from the environment and the provider381* resource file associated with the given context and concatenated.382* classSuffix is added to the end of this list.383* See getProperty(). The resulting property value is a list of package384* prefixes.385*<p>386* This method then constructs a list of class names by concatenating387* each package prefix with classSuffix and attempts to load and388* instantiate the class until one succeeds.389* Any class that cannot be loaded is ignored.390* The resulting object is then cached in a two-level hash table,391* keyed first by the context class loader and then by the property's392* value and classSuffix.393* The next time threads of the same context class loader call this394* method, they use the cached factory.395* If no factory can be loaded, NO_FACTORY is recorded in the table396* so that next time it'll return quickly.397*398* @param propName The non-null property name399* @param env The possibly null environment properties400* @param ctx The possibly null context401* @param classSuffix The non-null class name402* (e.g. ".ldap.ldapURLContextFactory).403* @param defaultPkgPrefix The non-null default package prefix.404* (e.g., "com.sun.jndi.url").405* @return An factory object; null if none.406* @exception NamingException If encounter problem while reading the provider407* property file, or problem instantiating the factory.408*409* @see javax.naming.spi.NamingManager#getURLContext410* @see javax.naming.spi.NamingManager#getURLObject411*/412public static Object getFactory(String propName, Hashtable<?,?> env,413Context ctx, String classSuffix, String defaultPkgPrefix)414throws NamingException {415416// Merge property with provider property and supplied default417String facProp = getProperty(propName, env, ctx, true);418if (facProp != null)419facProp += (":" + defaultPkgPrefix);420else421facProp = defaultPkgPrefix;422423// Cache factory based on context class loader, class name, and424// property val425ClassLoader loader = helper.getContextClassLoader();426String key = classSuffix + " " + facProp;427428Map<String, WeakReference<Object>> perLoaderCache = null;429synchronized (urlFactoryCache) {430perLoaderCache = urlFactoryCache.get(loader);431if (perLoaderCache == null) {432perLoaderCache = new HashMap<>(11);433urlFactoryCache.put(loader, perLoaderCache);434}435}436437synchronized (perLoaderCache) {438Object factory = null;439440WeakReference<Object> factoryRef = perLoaderCache.get(key);441if (factoryRef == NO_FACTORY) {442return null;443} else if (factoryRef != null) {444factory = factoryRef.get();445if (factory != null) { // check if weak ref has been cleared446return factory;447}448}449450// Not cached; find first factory and cache451StringTokenizer parser = new StringTokenizer(facProp, ":");452String className;453while (factory == null && parser.hasMoreTokens()) {454className = parser.nextToken() + classSuffix;455try {456// System.out.println("loading " + className);457factory = helper.loadClass(className, loader).newInstance();458} catch (InstantiationException e) {459NamingException ne =460new NamingException("Cannot instantiate " + className);461ne.setRootCause(e);462throw ne;463} catch (IllegalAccessException e) {464NamingException ne =465new NamingException("Cannot access " + className);466ne.setRootCause(e);467throw ne;468} catch (Exception e) {469// ignore ClassNotFoundException, IllegalArgumentException,470// etc.471}472}473474// Cache it.475perLoaderCache.put(key, (factory != null)476? new WeakReference<>(factory)477: NO_FACTORY);478return factory;479}480}481482483// ---------- Private methods ----------484485/*486* Returns the properties contained in the provider resource file487* of an object's package. Returns an empty hash table if the488* object is null or the resource file cannot be found. The489* results are cached.490*491* @throws NamingException if an error occurs while reading the file.492*/493private static Hashtable<? super String, Object>494getProviderResource(Object obj)495throws NamingException496{497if (obj == null) {498return (new Hashtable<>(1));499}500synchronized (propertiesCache) {501Class<?> c = obj.getClass();502503Hashtable<? super String, Object> props =504propertiesCache.get(c);505if (props != null) {506return props;507}508props = new Properties();509510InputStream istream =511helper.getResourceAsStream(c, PROVIDER_RESOURCE_FILE_NAME);512513if (istream != null) {514try {515((Properties)props).load(istream);516} catch (IOException e) {517NamingException ne = new ConfigurationException(518"Error reading provider resource file for " + c);519ne.setRootCause(e);520throw ne;521}522}523propertiesCache.put(c, props);524return props;525}526}527528529/*530* Returns the Hashtable (never null) that results from merging531* all application resource files available to this thread's532* context class loader. The properties file in <java.home>/lib533* is also merged in. The results are cached.534*535* SECURITY NOTES:536* 1. JNDI needs permission to read the application resource files.537* 2. Any class will be able to use JNDI to view the contents of538* the application resource files in its own classpath. Give539* careful consideration to this before storing sensitive540* information there.541*542* @throws NamingException if an error occurs while reading a resource543* file.544*/545private static Hashtable<? super String, Object> getApplicationResources()546throws NamingException {547548ClassLoader cl = helper.getContextClassLoader();549550synchronized (propertiesCache) {551Hashtable<? super String, Object> result = propertiesCache.get(cl);552if (result != null) {553return result;554}555556try {557NamingEnumeration<InputStream> resources =558helper.getResources(cl, APP_RESOURCE_FILE_NAME);559try {560while (resources.hasMore()) {561Properties props = new Properties();562InputStream istream = resources.next();563try {564props.load(istream);565} finally {566istream.close();567}568569if (result == null) {570result = props;571} else {572mergeTables(result, props);573}574}575} finally {576while (resources.hasMore()) {577resources.next().close();578}579}580581// Merge in properties from file in <java.home>/lib.582InputStream istream =583helper.getJavaHomeLibStream(JRELIB_PROPERTY_FILE_NAME);584if (istream != null) {585try {586Properties props = new Properties();587props.load(istream);588589if (result == null) {590result = props;591} else {592mergeTables(result, props);593}594} finally {595istream.close();596}597}598599} catch (IOException e) {600NamingException ne = new ConfigurationException(601"Error reading application resource file");602ne.setRootCause(e);603throw ne;604}605if (result == null) {606result = new Hashtable<>(11);607}608propertiesCache.put(cl, result);609return result;610}611}612613/*614* Merge the properties from one hash table into another. Each615* property in props2 that is not in props1 is added to props1.616* For each property in both hash tables that is one of the617* standard JNDI properties that specify colon-separated lists,618* the values are concatenated and stored in props1.619*/620private static void mergeTables(Hashtable<? super String, Object> props1,621Hashtable<? super String, Object> props2) {622for (Object key : props2.keySet()) {623String prop = (String)key;624Object val1 = props1.get(prop);625if (val1 == null) {626props1.put(prop, props2.get(prop));627} else if (isListProperty(prop)) {628String val2 = (String)props2.get(prop);629props1.put(prop, ((String)val1) + ":" + val2);630}631}632}633634/*635* Is a property one of the standard JNDI properties that specify636* colon-separated lists?637*/638private static boolean isListProperty(String prop) {639prop = prop.intern();640for (int i = 0; i < listProperties.length; i++) {641if (prop == listProperties[i]) {642return true;643}644}645return false;646}647}648649650