wildfly 的解决方法类加载器

作为一种变通方法,你可以使用特定于 Wildfly 的 VFS API 并编写自己的 ResourceAcessor 实现,如下所示。

public class WildflyVFSClassLoaderResourceAccessor extends AbstractResourceAccessor {

    private ClassLoader classLoader;

    public WildflyVFSClassLoaderResourceAccessor(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    @Override
    public Set<String> list(String relativeTo, String path, boolean includeFiles, boolean includeDirectories, boolean recursive) throws IOException {
        URL parentUrl = this.classLoader.getResource(relativeTo);
        if (parentUrl == null) {
            throw new IllegalStateException("Cannot locate parent");
        }
        URI parentUri;
        try {
            parentUri = parentUrl.toURI();
        } catch (URISyntaxException e) {
            throw new IllegalStateException("Invalid parent uri: " + parentUrl.toString());
        }
        VirtualFile parentFile = VFS.getChild(parentUri);
        VirtualFile parentDir = parentFile.getParent();
        VirtualFile changelogFiles = parentDir.getChild(path);
        Set<String> children = changelogFiles.getChildren()
            .stream()
            .map(VirtualFile::getName)
            .map(name -> path + name)
            .filter(file -> file != null)
            .collect(Collectors.toSet());
        return children;
    }

    @Override
    public ClassLoader toClassLoader() {
        return classLoader;
    }

    @Override
    public Set<InputStream> getResourcesAsStream(String path) throws IOException {
        Enumeration<URL> resources = classLoader.getResources(path);
        if (resources == null || !resources.hasMoreElements()) {
            return null;
        }
        Set<InputStream> resourceStream = new HashSet<>();
        while (resources.hasMoreElements()) {
            URL resourceUrl = resources.nextElement();
            try {
                URI resourceUri = resourceUrl.toURI();
                if (resourceUri.getScheme() != null && resourceUri.getScheme().equalsIgnoreCase("vfs")) {
                    VirtualFile virtualFile = VFS.getChild(resourceUri);
                    if (virtualFile.exists()) {
                        InputStream inputStream = virtualFile.openStream();
                        return Sets.newHashSet(inputStream);
                    }
                }
            } catch (URISyntaxException e) {
                throw new IOException("Failed to read resource " + resourceUrl.toString());
            }
        }
        return resourceStream;
    }
}