private void addArchives(final Archive<?>[] archives) { for (final Archive<?> archive : archives) { addArchive(archive); } }
@Override public URL findResource(String name) { final String adjustedName = WAR_ROOT_LOCATION + name; final URL url = super.findResource(adjustedName); return url; }
@Override public Enumeration<URL> findResources(String name) throws IOException { final String adjustedName = WAR_ROOT_LOCATION + name; final Enumeration<URL> urls = super.findResources(adjustedName); return urls; }
private ShrinkWrapClassLoader getClassLoader(Archive<?> archive) { return (archive.getName().endsWith(SUFFIX_WEB_ARCHIVE)) ? new WebArchiveClassLoader(archive.getClass().getClassLoader(), archive) : new ShrinkWrapClassLoader(archive.getClass().getClassLoader(), archive); }
public void disable() { Thread.currentThread().setContextClassLoader(contextClassLoader); try { this.shrinkWrapClassLoader.close(); } catch (IOException e) { throw new RuntimeException("Could not close ShrinkWrapClassLoader", e); } } }
public Class<?> classForName(String name) { try { return classLoader.loadClass(name); } catch (Exception e) { throw new ResourceLoadingException(e); } } });
/** * Constructs a new ShrinkWrapClassLoader for the specified {@link Archive}s using the default delegation parent * <code>ClassLoader</code>. The {@link Archive}s will be searched in the order specified for classes and resources * after first searching in the parent class loader. * * @param archives * the {@link Archive}s from which to load classes and resources */ public ShrinkWrapClassLoader(final Archive<?>... archives) { super(new URL[] {}); if (archives == null) { throw new IllegalArgumentException("Archives must be specified"); } addArchives(archives); }
private void addArchive(final Archive<?> archive) { try { addURL(new URL(null, "archive:" + archive.getName() + "/", new URLStreamHandler() { @Override protected URLConnection openConnection(final URL u) throws IOException {
public URL getResource(String name) { return classLoader.getResource(name); }
public Collection<URL> getResources(String name) { try { return Collections.list(classLoader.getResources(name)); } catch (Exception e) { throw new ResourceLoadingException(e); } }
/** * Creates the {@link ShrinkWrapClassLoaderTestCase#shrinkWrapClassLoader} used to load classes from an * {@link Archive}. The {@link ClassLoader} will be isolated from the application classpath by specifying a null * parent explicitly. */ @Before public void createClassLoader() { shrinkWrapClassLoader = new ShrinkWrapClassLoader((ClassLoader)null , archive); }
public void disable() { Thread.currentThread().setContextClassLoader(contextClassLoader); try { this.shrinkWrapClassLoader.close(); } catch (IOException e) { throw new RuntimeException("Could not close ShrinkWrapClassLoader", e); } } }
/** * Constructs a new ShrinkWrapClassLoader for the given {@link Archive}s. The {@link Archive}s will be searched in * the order specified for classes and resources after first searching in the specified parent class loader. * * @param parent * the parent class loader for delegation * @param archives * the {@link Archive}s from which to load classes and resources */ public ShrinkWrapClassLoader(final ClassLoader parent, final Archive<?>... archives) { super(new URL[] {}, parent); if (archives == null) { throw new IllegalArgumentException("Archives must be specified"); } addArchives(archives); }
private void addArchives(final Archive<?>[] archives) { for (final Archive<?> archive : archives) { addArchive(archive); } }
private void addArchive(final Archive<?> archive) { try { addURL(new URL(null, "archive:" + archive.getName() + "/", new URLStreamHandler() { @Override protected URLConnection openConnection(final URL u) throws IOException {
public static <I, T extends I> I invoke(Class<T> clazz, JavaArchive... classPathArchives) { JavaArchive[] copy = new JavaArchive[classPathArchives.length + 1]; System.arraycopy(classPathArchives, 0, copy, 0, classPathArchives.length); copy[copy.length - 1] = ShrinkWrap.create(JavaArchive.class).addClass(SerializationUtils.class); ClassLoader separatedClassLoader = new ShrinkWrapClassLoader(ClassLoaderUtils.getBootstrapClassLoader(), copy); return invoke(clazz, separatedClassLoader); }
/** * Constructs a new ShrinkWrapClassLoader for the specified {@link Archive}s using the default delegation parent * <code>ClassLoader</code>. The {@link Archive}s will be searched in the order specified for classes and resources * after first searching in the parent class loader. Will assume classes are stored relative to the root of the archive. * * @param archives * the {@link Archive}s from which to load classes and resources */ public ShrinkWrapClassLoader(final Archive<?>... archives) { super(new URL[] {}); if (archives == null) { throw new IllegalArgumentException("Archives must be specified"); } addArchives(archives); this.classPrefix = ArchivePaths.root(); }
Validate.notNull(archive, "Archive must be specified"); this.classLoader = new ShrinkWrapClassLoader(archive.getClass().getClassLoader(), archive);
/** * Constructs a new ShrinkWrapClassLoader for the given {@link Archive}s. The {@link Archive}s will be searched in * the order specified for classes and resources after first searching in the specified parent class loader. * * @param parent * the parent class loader for delegation * @param classPrefix The location under which classes are located in the archive. For instance, * {@link org.jboss.shrinkwrap.api.spec.WebArchive} types store their classes under WEB-INF/classes. If * null, assumes the root * @param archives * the {@link Archive}s from which to load classes and resources */ public ShrinkWrapClassLoader(final ClassLoader parent, final String classPrefix, final Archive<?>... archives) { super(new URL[]{}, parent); if (archives == null) { throw new IllegalArgumentException("Archives must be specified"); } addArchives(archives); this.classPrefix = classPrefix == null ? ArchivePaths.root() : ArchivePaths.create(classPrefix); }
/** * Ensures that we can open up an asset that doesn't exist via a {@link URL} from the {@link ShrinkWrapClassLoader} * (ie. should throw {@link FileNotFoundException} * * SHRINKWRAP-308 */ @Test(expected = FileNotFoundException.class) public void shouldNotBeAbleToOpenStreamOnNonexistantAsset() throws IOException { // Make a new Archive with some content in a directory final String nestedResourceName = "nested/test"; final Asset testAsset = new StringAsset("testContent"); final GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add(testAsset, nestedResourceName); // Make a CL to load the content final ClassLoader swCl = new ShrinkWrapClassLoader(archive); // Get the URL to something that doesn't exist final URL nestedResourceUrl = swCl.getResource(nestedResourceName); final URL nestedResourceThatDoesntExistUrl = new URL(nestedResourceUrl, "../fake"); // openStream on the URL that doesn't exist should throw FNFE nestedResourceThatDoesntExistUrl.openStream(); }