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); } } });
@Test public void shouldBeAbleToLoadVetoedClasses() throws Exception { Archive<JavaArchive> jarWithVetoedServiceImpl = createJarWithVetoedServices(); ClassLoader emptyParent = null; ShrinkWrapClassLoader swClassloader = new ShrinkWrapClassLoader(emptyParent, jarWithVetoedServiceImpl); ClassLoader emptyClassLoader = new ClassLoader(null) { }; ClassLoader originalClassLoader = SecurityActions.getThreadContextClassLoader(); Map<Class<?>, Set<Class<?>>> vetoed = null; Class<?> service; try { Thread.currentThread().setContextClassLoader(emptyClassLoader); service = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.FakeService"); swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeIncluded"); swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeExcluded"); vetoed = new JavaSPIExtensionLoader().loadVetoed(swClassloader); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } Assert.assertEquals("Unexpected number of vetoed services", 1, vetoed.size()); Assert.assertEquals("Unexpected number of vetoed services impl", 2, vetoed.get(service).size()); }
final ShrinkWrapClassLoader isolatedArchiveCL = new ShrinkWrapClassLoader((ClassLoader) null, archive); testClass = isolatedArchiveCL.loadClass(testClassName); } catch (final ClassNotFoundException cnfe) { throw new IllegalStateException("Could not load class " + testClassName + " from deployed archive: " testRunnersClass = isolatedArchiveCL.loadClass(CLASS_NAME_ARQ_TEST_RUNNERS); } catch (final ClassNotFoundException cnfe) { throw new IllegalStateException("Could not load class " + CLASS_NAME_ARQ_TEST_RUNNERS SecurityActions.setTccl(oldCl); try { isolatedArchiveCL.close(); } catch (final IOException ignore) {
/** * 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 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 {
@Test public void shouldBeAbleToLoadVetoedClasses() throws Exception { Archive<JavaArchive> jarWithVetoedServiceImpl = createJarWithVetoedServices(); ClassLoader emptyParent = null; ShrinkWrapClassLoader swClassloader = new ShrinkWrapClassLoader(emptyParent, jarWithVetoedServiceImpl); ClassLoader emptyClassLoader = new ClassLoader(null) { }; ClassLoader originalClassLoader = SecurityActions.getThreadContextClassLoader(); Map<Class<?>, Set<Class<?>>> vetoed = null; Class<?> service; try { Thread.currentThread().setContextClassLoader(emptyClassLoader); service = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.FakeService"); swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeIncluded"); swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeExcluded"); vetoed = new JavaSPIExtensionLoader().loadVetoed(swClassloader); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } Assert.assertEquals("Unexpected number of vetoed services", 1, vetoed.size()); Assert.assertEquals("Unexpected number of vetoed services impl", 2, vetoed.get(service).size()); }
/** * 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 {
@Test public void shouldBeAbleToAddSelectedProviderFromClassLoader() throws Exception { Archive<JavaArchive> jarWithDefaultServiceImpl = createJarWithDefaultServiceImpl(); Archive<JavaArchive> jarThatReplaceServiceImpl = createJarThatReplaceServiceImpl(); ClassLoader emptyParent = null; ShrinkWrapClassLoader swClassloader = new ShrinkWrapClassLoader(emptyParent, jarThatReplaceServiceImpl, jarWithDefaultServiceImpl); ClassLoader emptyClassLoader = new ClassLoader(null) { }; ClassLoader originalClassLoader = SecurityActions.getThreadContextClassLoader(); Collection<?> providers = null; Class<?> expectedImplClass = null; try { Thread.currentThread().setContextClassLoader(emptyClassLoader); Class<?> serviceClass = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.FakeService"); expectedImplClass = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeIncluded"); providers = new JavaSPIExtensionLoader().all(swClassloader, serviceClass); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } Assert.assertEquals("Unexpected number of providers loaded", 1, providers.size()); Assert.assertEquals("Wrong provider loaded", expectedImplClass, providers.iterator().next().getClass()); }
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); }
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 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(); }
@Test public void shouldBeAbleToAddSelectedProviderFromClassLoader() throws Exception { Archive<JavaArchive> jarWithDefaultServiceImpl = createJarWithDefaultServiceImpl(); Archive<JavaArchive> jarThatReplaceServiceImpl = createJarThatReplaceServiceImpl(); ClassLoader emptyParent = null; ShrinkWrapClassLoader swClassloader = new ShrinkWrapClassLoader(emptyParent, jarThatReplaceServiceImpl, jarWithDefaultServiceImpl); ClassLoader emptyClassLoader = new ClassLoader(null) { }; ClassLoader originalClassLoader = SecurityActions.getThreadContextClassLoader(); Collection<?> providers = null; Class<?> expectedImplClass = null; try { Thread.currentThread().setContextClassLoader(emptyClassLoader); Class<?> serviceClass = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.FakeService"); expectedImplClass = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeIncluded"); providers = new JavaSPIExtensionLoader().all(swClassloader, serviceClass); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } Assert.assertEquals("Unexpected number of providers loaded", 1, providers.size()); Assert.assertEquals("Wrong provider loaded", expectedImplClass, providers.iterator().next().getClass()); }
/** * 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); }
/** * 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); }
Validate.notNull(archive, "Archive must be specified"); this.classLoader = new ShrinkWrapClassLoader(archive.getClass().getClassLoader(), archive);