@Test void createClassLoader(@TempDir final Path temporaryFolder) throws Exception { final File root = temporaryFolder.toFile(); root.mkdirs(); final String dep = "org.apache.tomee:arquillian-tomee-codi-tests:jar:7.0.5"; final File nestedJar = new File(root, UUID.randomUUID().toString() + ".jar"); try (final JarOutputStream out = new JarOutputStream(new FileOutputStream(nestedJar))) { addDepToJar(dep, out); } final Thread thread = Thread.currentThread(); final ClassLoader contextClassLoader = thread.getContextClassLoader(); final URLClassLoader appLoader = new URLClassLoader(new URL[] { nestedJar.toURI().toURL() }, contextClassLoader); final ConfigurableClassLoader componentLoader = new ConfigurableClassLoader("test", new URL[0], appLoader, it -> true, it -> false, new String[0], new String[0]); thread.setContextClassLoader(componentLoader); try (final Resolver.ClassLoaderDescriptor desc = new ResolverImpl(null, coord -> PathFactory.get("maven2").resolve(coord)) .mapDescriptorToClassLoader(singletonList(dep))) { assertNotNull(desc); assertNotNull(desc.asClassLoader()); assertEquals(singletonList(dep), desc.resolvedDependencies()); final Properties props = new Properties(); try (final InputStream in = desc .asClassLoader() .getResourceAsStream( "META-INF/maven/org.apache.tomee/arquillian-tomee-codi-tests/pom.properties")) { assertNotNull(in); props.load(in); } assertEquals("arquillian-tomee-codi-tests", props.getProperty("artifactId")); } finally { thread.setContextClassLoader(contextClassLoader); appLoader.close(); } }
@Override public ClassLoaderDescriptor mapDescriptorToClassLoader(final InputStream descriptor) { final Collection<URL> urls = new ArrayList<>(); final Collection<String> nested = new ArrayList<>(); final Collection<String> resolved = new ArrayList<>(); final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final ClassLoader loader = ofNullable(classLoader).map(ClassLoader::getParent).orElseGet(ClassLoader::getSystemClassLoader); try { new MvnDependencyListLocalRepositoryResolver(null, fileResolver) .resolveFromDescriptor(descriptor) .forEach(artifact -> { final String path = artifact.toPath(); final Path file = fileResolver.apply(path); if (Files.exists(file)) { try { urls.add(file.toUri().toURL()); resolved.add(artifact.toCoordinate()); } catch (final MalformedURLException e) { throw new IllegalStateException(e); } } else if (loader.getResource("MAVEN-INF/repository/" + path) != null) { nested.add(path); resolved.add(artifact.toCoordinate()); } }); final ConfigurableClassLoader volatileLoader = new ConfigurableClassLoader(plugin + "#volatile-resolver", urls.toArray(new URL[0]), classLoader, it -> false, it -> true, nested.toArray(new String[0]), ConfigurableClassLoader.class.isInstance(classLoader) ? ConfigurableClassLoader.class.cast(classLoader).getJvmMarkers() : new String[] { "" }); return new ClassLoaderDescriptorImpl(volatileLoader, resolved); } catch (final IOException e) { throw new IllegalArgumentException(e); } }
ResolverImpl implements Resolver, Serializable { @Override public ClassLoaderDescriptor mapDescriptorToClassLoader(final InputStream descriptor) { final Collection<URL> urls = new ArrayList<>(); final Collection<String> nested = new ArrayList<>(); final Collection<String> resolved = new ArrayList<>(); final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final ClassLoader loader = ofNullable(classLoader).map(ClassLoader::getParent).orElseGet(ClassLoader::getSystemClassLoader); try { new MvnDependencyListLocalRepositoryResolver(null, fileResolver) .resolveFromDescriptor(descriptor) .forEach(artifact -> { final String path = artifact.toPath(); final Path file = fileResolver.apply(path); if (Files.exists(file)) { try { urls.add(file.toUri().toURL()); resolved.add(artifact.toCoordinate()); } catch (final MalformedURLException e) { throw new IllegalStateException(e); } } else if (loader.getResource("MAVEN-INF/repository/" + path) != null) { nested.add(path); resolved.add(artifact.toCoordinate()); } }); final ConfigurableClassLoader volatileLoader = new ConfigurableClassLoader(plugin + "#volatile-resolver", urls.toArray(new URL[0]), classLoader, it -> false, it -> true, nested.toArray(new String[0]), ConfigurableClassLoader.class.isInstance(classLoader) ? ConfigurableClassLoader.class.cast(classLoader).getJvmMarkers() : new String[] { "" }); return new ClassLoaderDescriptorImpl(volatileLoader, resolved); } catch (final IOException e) { throw new IllegalArgumentException(e); } } }
ResolverImpl implements Resolver, Serializable { @Override public ClassLoaderDescriptor mapDescriptorToClassLoader(final InputStream descriptor) { final Collection<URL> urls = new ArrayList<>(); final Collection<String> nested = new ArrayList<>(); final Collection<String> resolved = new ArrayList<>(); final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final ClassLoader loader = ofNullable(classLoader).map(ClassLoader::getParent).orElseGet(ClassLoader::getSystemClassLoader); try { new MvnDependencyListLocalRepositoryResolver(null, fileResolver) .resolveFromDescriptor(descriptor) .forEach(artifact -> { final String path = artifact.toPath(); final Path file = fileResolver.apply(path); if (Files.exists(file)) { try { urls.add(file.toUri().toURL()); resolved.add(artifact.toCoordinate()); } catch (final MalformedURLException e) { throw new IllegalStateException(e); } } else if (loader.getResource("MAVEN-INF/repository/" + path) != null) { nested.add(path); resolved.add(artifact.toCoordinate()); } }); final ConfigurableClassLoader volatileLoader = new ConfigurableClassLoader(plugin + "#volatile-resolver", urls.toArray(new URL[0]), classLoader, it -> false, it -> true, nested.toArray(new String[0]), ConfigurableClassLoader.class.isInstance(classLoader) ? ConfigurableClassLoader.class.cast(classLoader).getJvmMarkers() : new String[] { "" }); return new ClassLoaderDescriptorImpl(volatileLoader, resolved); } catch (final IOException e) { throw new IllegalArgumentException(e); } }  }
ResolverImpl implements Resolver, Serializable { @Override public ClassLoaderDescriptor mapDescriptorToClassLoader(final InputStream descriptor) { final Collection<URL> urls = new ArrayList<>(); final Collection<String> nested = new ArrayList<>(); final Collection<String> resolved = new ArrayList<>(); final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final ClassLoader loader = ofNullable(classLoader).map(ClassLoader::getParent).orElseGet(ClassLoader::getSystemClassLoader); try { new MvnDependencyListLocalRepositoryResolver(null, fileResolver) .resolveFromDescriptor(descriptor) .forEach(artifact -> { final String path = artifact.toPath(); final Path file = fileResolver.apply(path); if (Files.exists(file)) { try { urls.add(file.toUri().toURL()); resolved.add(artifact.toCoordinate()); } catch (final MalformedURLException e) { throw new IllegalStateException(e); } } else if (loader.getResource("MAVEN-INF/repository/" + path) != null) { nested.add(path); resolved.add(artifact.toCoordinate()); } }); final ConfigurableClassLoader volatileLoader = new ConfigurableClassLoader(plugin + "#volatile-resolver", urls.toArray(new URL[0]), classLoader, it -> false, it -> true, nested.toArray(new String[0]), ConfigurableClassLoader.class.isInstance(classLoader) ? ConfigurableClassLoader.class.cast(classLoader).getJvmMarkers() : new String[] { "" }); return new ClassLoaderDescriptorImpl(volatileLoader, resolved); } catch (final IOException e) { throw new IllegalArgumentException(e); } }  @Override ClassLoaderDescriptor mapDescriptorToClassLoader(final InputStream descriptor); @Override Collection<File> resolveFromDescriptor(final InputStream descriptor); }
ResolverImpl implements Resolver, Serializable { @Override public ClassLoaderDescriptor mapDescriptorToClassLoader(final InputStream descriptor) { final Collection<URL> urls = new ArrayList<>(); final Collection<String> nested = new ArrayList<>(); final Collection<String> resolved = new ArrayList<>(); final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final ClassLoader loader = ofNullable(classLoader).map(ClassLoader::getParent).orElseGet(ClassLoader::getSystemClassLoader); try { new MvnDependencyListLocalRepositoryResolver(null, fileResolver) .resolveFromDescriptor(descriptor) .forEach(artifact -> { final String path = artifact.toPath(); final Path file = fileResolver.apply(path); if (Files.exists(file)) { try { urls.add(file.toUri().toURL()); resolved.add(artifact.toCoordinate()); } catch (final MalformedURLException e) { throw new IllegalStateException(e); } } else if (loader.getResource("MAVEN-INF/repository/" + path) != null) { nested.add(path); resolved.add(artifact.toCoordinate()); } }); final ConfigurableClassLoader volatileLoader = new ConfigurableClassLoader(plugin + "#volatile-resolver", urls.toArray(new URL[0]), classLoader, it -> false, it -> true, nested.toArray(new String[0]), ConfigurableClassLoader.class.isInstance(classLoader) ? ConfigurableClassLoader.class.cast(classLoader).getJvmMarkers() : new String[] { "" }); return new ClassLoaderDescriptorImpl(volatileLoader, resolved); } catch (final IOException e) { throw new IllegalArgumentException(e); } }  @Override ClassLoaderDescriptor mapDescriptorToClassLoader(final InputStream descriptor); @Override Collection<File> resolveFromDescriptor(final InputStream descriptor);  }