org.eclipse.sisu.space.BundleClassSpace 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.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import org.eclipse.sisu.inject.DeferredClass;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
/**
* {@link ClassSpace} backed by a strongly-referenced {@link Bundle}.
*/
public final class BundleClassSpace
implements ClassSpace
{
// ----------------------------------------------------------------------
// Constants
// ----------------------------------------------------------------------
private static final URL[] NO_URLS = {};
private static final Enumeration NO_ENTRIES = Collections.enumeration( Collections. emptySet() );
// ----------------------------------------------------------------------
// Implementation fields
// ----------------------------------------------------------------------
private final Bundle bundle;
private URL[] bundleClassPath;
// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------
public BundleClassSpace( final Bundle bundle )
{
this.bundle = bundle;
}
// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
public Class> loadClass( final String name )
{
try
{
return bundle.loadClass( name );
}
catch ( final Exception e )
{
throw new TypeNotPresentException( name, e );
}
catch ( final LinkageError e )
{
throw new TypeNotPresentException( name, e );
}
}
public DeferredClass> deferLoadClass( final String name )
{
return new NamedClass