uk.org.retep.util.classloader.DeploymentClassLoader Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2007, Peter T Mount
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the retep.org.uk nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package uk.org.retep.util.classloader;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import uk.org.retep.util.io.FileUtils;
/**
* A class loader that will load a class from a set of child class loaders,
* usually JarClassLoader instances.
* @author peter
*/
public class DeploymentClassLoader
extends CachingClassLoader
{
private final Set loaders = new LinkedHashSet();
public DeploymentClassLoader()
{
super();
}
public DeploymentClassLoader( final ClassLoader parent )
{
super( parent );
}
public DeploymentClassLoader( final File directory )
throws IOException
{
init( directory );
}
public DeploymentClassLoader( final File directory, final ClassLoader parent )
throws IOException
{
super( parent );
init( directory );
}
private void init( final File directory )
throws IOException
{
final List files = FileUtils.findFiles( directory,
FileUtils.JAR_FILTER,
false );
for( File file : files )
{
add( new URLClassLoader( new URL[]
{
file.toURI().toURL()
} ) );
}
}
public boolean add( final ClassLoader loader )
{
return loaders.add( loader );
}
public boolean remove( final ClassLoader loader )
{
return loaders.remove( loader );
}
@Override
protected synchronized Class> loadClass( final String className,
final boolean resolve )
throws ClassNotFoundException
{
Class c = getClass( className );
if( c == null )
{
final Iterator it = loaders.iterator();
while( c == null && it.hasNext() )
{
try
{
c = it.next().loadClass( className );
}
catch( ClassNotFoundException cnfe )
{
// ignore, pass to the next class loader
}
}
if( c == null )
{
throw new ClassNotFoundException(
"Unable to find class " + className );
}
putClass( className, c );
}
if( resolve )
{
resolveClass( c );
}
return c;
}
@Override
protected URL findResource( final String name )
{
final Iterator it = loaders.iterator();
while( it.hasNext() )
{
final URL url = it.next().getResource( name );
if( url != null )
{
return null;
}
}
return null;
}
@Override
protected Enumeration findResources( final String name )
throws IOException
{
throw new UnsupportedOperationException( "Not supported yet." );
}
}