uk.org.retep.util.classloader.CachingInputStreamClassLoader Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2008, 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.IOException;
import java.net.URL;
import java.util.Enumeration;
import uk.org.retep.util.collections.CollectionUtils;
import uk.org.retep.util.collections.EmptyIterator;
/**
* An abstract CachingClassLoader which will read a class from some sort of
* InputStream
* @author peter
*/
public abstract class CachingInputStreamClassLoader
extends CachingClassLoader
{
public CachingInputStreamClassLoader()
{
super();
}
public CachingInputStreamClassLoader( final ClassLoader parent )
{
super( parent );
}
@Override
public final Class> loadClass( final String className,
final boolean resolve )
throws ClassNotFoundException
{
try
{
Class> c = getClass( className );
if( c == null )
{
try
{
final byte[] data = readBytes( className );
c = defineClass( className, data, 0, data.length );
putClass( className, c );
}
catch( IOException ioe )
{
throw new ClassNotFoundException(
"Failed to read " + className, ioe );
}
}
if( resolve )
{
resolveClass( c );
}
return c;
}
catch( Exception e )
{
throw new ClassNotFoundException( e.toString(), e );
}
}
protected abstract byte[] readBytes( String className )
throws IOException;
/**
* Most instances of this class do not have child ClassLoader's, so this
* implementation simply makes a single Enumeration of the output of
* findResource...
*
* @param name
* @return
* @throws java.io.IOException
*/
@Override
protected Enumeration findResources( final String name )
throws IOException
{
final URL url = findResource( name );
if( url == null )
{
return new EmptyIterator();
}
return CollectionUtils.singletonEnumeration( url );
}
}