org.eclipse.sisu.space.URLClassSpace Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2010-present Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
*******************************************************************************/
package org.eclipse.sisu.space;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import org.eclipse.sisu.inject.DeferredClass;
/**
* {@link ClassSpace} backed by a strongly-referenced {@link ClassLoader} and a {@link URL} class path.
*/
public class URLClassSpace
implements ClassSpace
{
// ----------------------------------------------------------------------
// Static initialization
// ----------------------------------------------------------------------
static
{
ClassLoader systemLoader;
String classPath;
try
{
systemLoader = ClassLoader.getSystemClassLoader();
classPath = System.getProperty( "java.class.path", "." );
}
catch ( final RuntimeException e )
{
systemLoader = null;
classPath = null;
}
catch ( final LinkageError e )
{
systemLoader = null;
classPath = null;
}
SYSTEM_LOADER = systemLoader;
SYSTEM_CLASSPATH = classPath;
}
// ----------------------------------------------------------------------
// Constants
// ----------------------------------------------------------------------
private static final String MANIFEST_ENTRY = "META-INF/MANIFEST.MF";
private static final URL[] NO_URLS = {};
private static final Enumeration NO_ENTRIES = Collections.enumeration( Collections. emptySet() );
private static final String[] EMPTY_CLASSPATH = {};
private static final ClassLoader SYSTEM_LOADER;
private static final String SYSTEM_CLASSPATH;
// ----------------------------------------------------------------------
// Implementation fields
// ----------------------------------------------------------------------
private final ClassLoader loader;
private final String pathDetails;
private URL[] classPath;
// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------
/**
* Creates a {@link ClassSpace} backed by a {@link ClassLoader} and its default class path.
*
* For {@link URLClassLoader}s this is their expanded Class-Path; otherwise it is empty.
*
* @param loader The class loader to use when getting/finding resources
*/
public URLClassSpace( final ClassLoader loader )
{
this.loader = loader;
pathDetails = null;
// compute class path on demand
}
/**
* Creates a {@link ClassSpace} backed by a {@link ClassLoader} with a restricted class path.
*
* @param loader The class loader to use when getting resources
* @param path The class path to use when finding resources
* @see #getResources(String)
* @see #findEntries(String, String, boolean)
*/
public URLClassSpace( final ClassLoader loader, final URL[] path )
{
this.loader = loader;
pathDetails = Arrays.toString( path );
if ( null != path && path.length > 0 )
{
classPath = expandClassPath( path );
}
else
{
classPath = NO_URLS;
}
}
// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
public final Class> loadClass( final String name )
{
try
{
return loader.loadClass( name );
}
catch ( final Exception e )
{
throw new TypeNotPresentException( name, e );
}
catch ( final LinkageError e )
{
throw new TypeNotPresentException( name, e );
}
}
public final DeferredClass> deferLoadClass( final String name )
{
return new NamedClass