package org.apache.commons.digester3.binder;
import static java.lang.System.getSecurityManager;
import static java.security.AccessController.doPrivileged;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
final class BinderClassLoader
extends ClassLoader
{
private static final Map<String, Class<?>> PRIMITIVE_TYPES;
static
{
HashMap<String, Class<?>> primitiveTypes = new HashMap<String, Class<?>>();
primitiveTypes.put( "boolean", boolean.class );
primitiveTypes.put( "byte", byte.class );
primitiveTypes.put( "short", short.class );
primitiveTypes.put( "int", int.class );
primitiveTypes.put( "char", char.class );
primitiveTypes.put( "long", long.class );
primitiveTypes.put( "float", float.class );
primitiveTypes.put( "double", double.class );
PRIMITIVE_TYPES = Collections.unmodifiableMap( primitiveTypes );
}
public static BinderClassLoader createBinderClassLoader( final ClassLoader adaptedClassLoader )
{
PrivilegedAction<BinderClassLoader> action = new PrivilegedAction<BinderClassLoader>()
{
public BinderClassLoader run()
{
return new BinderClassLoader( adaptedClassLoader );
}
};
if ( getSecurityManager() != null )
{
return doPrivileged( action );
}
return action.run();
}
private final ClassLoader adaptedClassLoader;
private BinderClassLoader( ClassLoader adaptedClassLoader )
{
this.adaptedClassLoader = adaptedClassLoader;
}
public ClassLoader getAdaptedClassLoader()
{
return adaptedClassLoader;
}
@Override
protected synchronized Class<?> loadClass( String name, boolean resolve )
throws ClassNotFoundException
{
if ( PRIMITIVE_TYPES.containsKey( name ) )
{
return PRIMITIVE_TYPES.get( name );
}
return adaptedClassLoader.loadClass( name );
}
}