Java example source code file (ResourceManager.java)
This example Java source code file (ResourceManager.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* 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>>
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>
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 env 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 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> perLoaderCache = null;
synchronized (factoryCache) {
perLoaderCache = factoryCache.get(loader);
if (perLoaderCache == null) {
perLoaderCache = new HashMap<>(11);
factoryCache.put(loader, perLoaderCache);
}
}
synchronized (perLoaderCache) {
List<NamedWeakReference
Other Java examples (source code examples)
Here is a short list of links related to this Java ResourceManager.java source code file: