feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,116 @@
/*
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.naming.internal;
import java.util.List;
import javax.naming.NamingException;
/**
* The FactoryEnumeration is used for returning factory instances.
*
* @author Rosanna Lee
* @author Scott Seligman
*/
// no need to implement Enumeration since this is only for internal use
public final class FactoryEnumeration {
// List<NamedWeakReference<Class | Object>>
private List<NamedWeakReference<Object>> factories;
private int posn = 0;
private ClassLoader loader;
/**
* Records the input list and uses it directly to satisfy
* hasMore()/next() requests. An alternative would have been to use
* an enumeration/iterator from the list, but we want to update the
* list so we keep the
* original list. The list initially contains Class objects.
* As each element is used, the Class object is replaced by an
* instance of the Class itself; eventually, the list contains
* only a list of factory instances and no more updates are required.
*
* <p> Both Class objects and factories are wrapped in weak
* references so as not to prevent GC of the class loader. Each
* weak reference is tagged with the factory's class name so the
* class can be reloaded if the reference is cleared.
*
* @param factories A non-null list
* @param loader The class loader of the list's contents
*
* This internal method is used with Thread Context Class Loader (TCCL),
* please don't expose this method as public.
*/
FactoryEnumeration(List<NamedWeakReference<Object>> factories,
ClassLoader loader) {
this.factories = factories;
this.loader = loader;
}
public Object next() throws NamingException {
synchronized (factories) {
NamedWeakReference<Object> ref = factories.get(posn++);
Object answer = ref.get();
if ((answer != null) && !(answer instanceof Class)) {
return answer;
}
String className = ref.getName();
try {
if (answer == null) { // reload class if weak ref cleared
Class<?> cls = Class.forName(className, true, loader);
answer = cls;
}
// Instantiate Class to get factory
answer = ((Class) answer).newInstance();
ref = new NamedWeakReference<>(answer, className);
factories.set(posn-1, ref); // replace Class object or null
return answer;
} catch (ClassNotFoundException e) {
NamingException ne =
new NamingException("No longer able to load " + className);
ne.setRootCause(e);
throw ne;
} catch (InstantiationException e) {
NamingException ne =
new NamingException("Cannot instantiate " + answer);
ne.setRootCause(e);
throw ne;
} catch (IllegalAccessException e) {
NamingException ne = new NamingException("Cannot access " + answer);
ne.setRootCause(e);
throw ne;
}
}
}
public boolean hasMore() {
synchronized (factories) {
return posn < factories.size();
}
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.naming.internal;
/**
* A NamedWeakReference is a WeakReference with an immutable string name.
*
* @author Scott Seligman
*/
class NamedWeakReference<T> extends java.lang.ref.WeakReference<T> {
private final String name;
NamedWeakReference(T referent, String name) {
super(referent);
this.name = name;
}
String getName() {
return name;
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.naming.internal;
import javax.naming.Reference;
import sun.misc.ObjectInputFilter;
import sun.misc.ObjectInputFilter.FilterInfo;
import sun.misc.ObjectInputFilter.Status;
import sun.security.util.SecurityProperties;
/**
* This class implements the filter that validates object factories classes instantiated
* during {@link Reference} lookups.
* There is one system-wide filter instance per VM that can be set via
* the {@code "jdk.jndi.object.factoriesFilter"} system property value, or via
* setting the property in the security properties file. The system property value supersedes
* the security property value. If none of the properties are specified the default
* "*" value is used.
* The filter is implemented as {@link ObjectInputFilter} with capabilities limited to the
* validation of a factory's class types only ({@linkplain FilterInfo#serialClass()}).
* Array length, number of object references, depth, and stream size filtering capabilities are
* not supported by the filter.
*/
public final class ObjectFactoriesFilter {
/**
* Checks if serial filter configured with {@code "jdk.jndi.object.factoriesFilter"}
* system property value allows instantiation of the specified objects factory class.
* If the filter result is not {@linkplain Status#REJECTED REJECTED}, the filter will
* allow the instantiation of objects factory class.
*
* @param factoryClass objects factory class
* @return true - if the factory is allowed to be instantiated; false - otherwise
*/
public static boolean canInstantiateObjectsFactory(Class<?> factoryClass) {
return checkInput(() -> factoryClass);
}
private static boolean checkInput(FactoryInfo factoryInfo) {
Status result = GLOBAL.checkInput(factoryInfo);
return result != Status.REJECTED;
}
// FilterInfo to check if objects factory class is allowed by the system-wide
// filter. Array length, number of object references, depth, and stream size
// capabilities are ignored.
@FunctionalInterface
private interface FactoryInfo extends FilterInfo {
@Override
default long arrayLength() {
return -1;
}
@Override
default long depth() {
return 1;
}
@Override
default long references() {
return 0;
}
@Override
default long streamBytes() {
return 0;
}
}
// Prevent instantiation of the factories filter class
private ObjectFactoriesFilter() {
throw new InternalError("Not instantiable");
}
// System property name that contains the patterns to filter object factory names
private static final String FACTORIES_FILTER_PROPNAME = "jdk.jndi.object.factoriesFilter";
// Default system property value that allows the load of any object factory classes
private static final String DEFAULT_SP_VALUE = "*";
// System wide object factories filter constructed from the system property
private static final ObjectInputFilter GLOBAL =
ObjectInputFilter.Config.createFilter(getFilterPropertyValue());
// Get security or system property value
private static String getFilterPropertyValue() {
String propVal = SecurityProperties.privilegedGetOverridable(FACTORIES_FILTER_PROPNAME);
return propVal != null ? propVal : DEFAULT_SP_VALUE;
}
}

View File

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

View File

@@ -0,0 +1,158 @@
/*
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.naming.internal;
import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.naming.NamingEnumeration;
/**
* VersionHelper was used by JNDI to accommodate differences between
* JDK 1.1.x and the Java 2 platform. As this is no longer necessary
* since JNDI's inclusion in the platform, this class currently
* serves as a set of utilities for performing system-level things,
* such as class-loading and reading system properties.
*
* @author Rosanna Lee
* @author Scott Seligman
*/
public abstract class VersionHelper {
private static VersionHelper helper = null;
final static String[] PROPS = new String[] {
javax.naming.Context.INITIAL_CONTEXT_FACTORY,
javax.naming.Context.OBJECT_FACTORIES,
javax.naming.Context.URL_PKG_PREFIXES,
javax.naming.Context.STATE_FACTORIES,
javax.naming.Context.PROVIDER_URL,
javax.naming.Context.DNS_URL,
// The following shouldn't create a runtime dependence on ldap package.
javax.naming.ldap.LdapContext.CONTROL_FACTORIES
};
public final static int INITIAL_CONTEXT_FACTORY = 0;
public final static int OBJECT_FACTORIES = 1;
public final static int URL_PKG_PREFIXES = 2;
public final static int STATE_FACTORIES = 3;
public final static int PROVIDER_URL = 4;
public final static int DNS_URL = 5;
public final static int CONTROL_FACTORIES = 6;
VersionHelper() {} // Disallow anyone from creating one of these.
static {
helper = new VersionHelper12();
}
public static VersionHelper getVersionHelper() {
return helper;
}
public abstract Class<?> loadClassWithoutInit(String className)
throws ClassNotFoundException;
public abstract Class<?> loadClass(String className)
throws ClassNotFoundException;
abstract Class<?> loadClass(String className, ClassLoader cl)
throws ClassNotFoundException;
public abstract Class<?> loadClass(String className, String codebase)
throws ClassNotFoundException, MalformedURLException;
/*
* Returns a JNDI property from the system properties. Returns
* null if the property is not set, or if there is no permission
* to read it.
*/
abstract String getJndiProperty(int i);
/*
* Reads each property in PROPS from the system properties, and
* returns their values -- in order -- in an array. For each
* unset property, the corresponding array element is set to null.
* Returns null if there is no permission to call System.getProperties().
*/
abstract String[] getJndiProperties();
/*
* Returns the resource of a given name associated with a particular
* class (never null), or null if none can be found.
*/
abstract InputStream getResourceAsStream(Class<?> c, String name);
/*
* Returns an input stream for a file in <java.home>/lib,
* or null if it cannot be located or opened.
*
* @param filename The file name, sans directory.
*/
abstract InputStream getJavaHomeLibStream(String filename);
/*
* Returns an enumeration (never null) of InputStreams of the
* resources of a given name associated with a particular class
* loader. Null represents the bootstrap class loader in some
* Java implementations.
*/
abstract NamingEnumeration<InputStream> getResources(
ClassLoader cl, String name)
throws IOException;
/*
* Returns the context class loader associated with the current thread.
* Null indicates the bootstrap class loader in some Java implementations.
*
* @throws SecurityException if the class loader is not accessible.
*/
abstract ClassLoader getContextClassLoader();
static protected URL[] getUrlArray(String codebase)
throws MalformedURLException {
// Parse codebase into separate URLs
StringTokenizer parser = new StringTokenizer(codebase);
Vector<String> vec = new Vector<>(10);
while (parser.hasMoreTokens()) {
vec.addElement(parser.nextToken());
}
String[] url = new String[vec.size()];
for (int i = 0; i < url.length; i++) {
url[i] = vec.elementAt(i);
}
URL[] urlArray = new URL[url.length];
for (int i = 0; i < urlArray.length; i++) {
urlArray[i] = new URL(url[i]);
}
return urlArray;
}
}

View File

@@ -0,0 +1,299 @@
/*
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.naming.internal;
import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Properties;
import javax.naming.*;
/**
* VersionHelper was used by JNDI to accommodate differences between
* JDK 1.1.x and the Java 2 platform. As this is no longer necessary
* since JNDI's inclusion in the platform, this class currently
* serves as a set of utilities for performing system-level things,
* such as class-loading and reading system properties.
*
* @author Rosanna Lee
* @author Scott Seligman
*/
final class VersionHelper12 extends VersionHelper {
// Disallow external from creating one of these.
VersionHelper12() {
}
public Class<?> loadClass(String className) throws ClassNotFoundException {
return loadClass(className, getContextClassLoader());
}
public Class<?> loadClassWithoutInit(String className) throws ClassNotFoundException {
return loadClass(className, false, getContextClassLoader());
}
/**
* Determines whether classes may be loaded from an arbitrary URL code base.
*/
private static final String TRUST_URL_CODEBASE_PROPERTY =
"com.sun.jndi.ldap.object.trustURLCodebase";
private static final String trustURLCodebase =
AccessController.doPrivileged(
new PrivilegedAction<String>() {
public String run() {
try {
return System.getProperty(TRUST_URL_CODEBASE_PROPERTY,
"false");
} catch (SecurityException e) {
return "false";
}
}
}
);
/**
* Package private.
*
* This internal method is used with Thread Context Class Loader (TCCL),
* please don't expose this method as public.
*/
Class<?> loadClass(String className, boolean initialize, ClassLoader cl)
throws ClassNotFoundException {
Class<?> cls = Class.forName(className, initialize, cl);
return cls;
}
Class<?> loadClass(String className, ClassLoader cl)
throws ClassNotFoundException {
return loadClass(className, true, cl);
}
/**
* @param className A non-null fully qualified class name.
* @param codebase A non-null, space-separated list of URL strings.
*/
public Class<?> loadClass(String className, String codebase)
throws ClassNotFoundException, MalformedURLException {
if ("true".equalsIgnoreCase(trustURLCodebase)) {
ClassLoader parent = getContextClassLoader();
ClassLoader cl =
URLClassLoader.newInstance(getUrlArray(codebase), parent);
return loadClass(className, cl);
} else {
return null;
}
}
String getJndiProperty(final int i) {
return AccessController.doPrivileged(
new PrivilegedAction<String>() {
public String run() {
try {
return System.getProperty(PROPS[i]);
} catch (SecurityException e) {
return null;
}
}
}
);
}
String[] getJndiProperties() {
Properties sysProps = AccessController.doPrivileged(
new PrivilegedAction<Properties>() {
public Properties run() {
try {
return System.getProperties();
} catch (SecurityException e) {
return null;
}
}
}
);
if (sysProps == null) {
return null;
}
String[] jProps = new String[PROPS.length];
for (int i = 0; i < PROPS.length; i++) {
jProps[i] = sysProps.getProperty(PROPS[i]);
}
return jProps;
}
InputStream getResourceAsStream(final Class<?> c, final String name) {
return AccessController.doPrivileged(
new PrivilegedAction<InputStream>() {
public InputStream run() {
return c.getResourceAsStream(name);
}
}
);
}
InputStream getJavaHomeLibStream(final String filename) {
return AccessController.doPrivileged(
new PrivilegedAction<InputStream>() {
public InputStream run() {
try {
String javahome = System.getProperty("java.home");
if (javahome == null) {
return null;
}
String pathname = javahome + java.io.File.separator +
"lib" + java.io.File.separator + filename;
return new java.io.FileInputStream(pathname);
} catch (Exception e) {
return null;
}
}
}
);
}
NamingEnumeration<InputStream> getResources(final ClassLoader cl,
final String name) throws IOException {
Enumeration<URL> urls;
try {
urls = AccessController.doPrivileged(
new PrivilegedExceptionAction<Enumeration<URL>>() {
public Enumeration<URL> run() throws IOException {
return (cl == null)
? ClassLoader.getSystemResources(name)
: cl.getResources(name);
}
}
);
} catch (PrivilegedActionException e) {
throw (IOException)e.getException();
}
return new InputStreamEnumeration(urls);
}
/**
* Package private.
*
* This internal method returns Thread Context Class Loader (TCCL),
* if null, returns the system Class Loader.
*
* Please don't expose this method as public.
*/
ClassLoader getContextClassLoader() {
return AccessController.doPrivileged(
new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
ClassLoader loader =
Thread.currentThread().getContextClassLoader();
if (loader == null) {
// Don't use bootstrap class loader directly!
loader = ClassLoader.getSystemClassLoader();
}
return loader;
}
}
);
}
/**
* Given an enumeration of URLs, an instance of this class represents
* an enumeration of their InputStreams. Each operation on the URL
* enumeration is performed within a doPrivileged block.
* This is used to enumerate the resources under a foreign codebase.
* This class is not MT-safe.
*/
class InputStreamEnumeration implements NamingEnumeration<InputStream> {
private final Enumeration<URL> urls;
private InputStream nextElement = null;
InputStreamEnumeration(Enumeration<URL> urls) {
this.urls = urls;
}
/*
* Returns the next InputStream, or null if there are no more.
* An InputStream that cannot be opened is skipped.
*/
private InputStream getNextElement() {
return AccessController.doPrivileged(
new PrivilegedAction<InputStream>() {
public InputStream run() {
while (urls.hasMoreElements()) {
try {
return urls.nextElement().openStream();
} catch (IOException e) {
// skip this URL
}
}
return null;
}
}
);
}
public boolean hasMore() {
if (nextElement != null) {
return true;
}
nextElement = getNextElement();
return (nextElement != null);
}
public boolean hasMoreElements() {
return hasMore();
}
public InputStream next() {
if (hasMore()) {
InputStream res = nextElement;
nextElement = null;
return res;
} else {
throw new NoSuchElementException();
}
}
public InputStream nextElement() {
return next();
}
public void close() {
}
}
}