All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.microsphere.classloading.AbstractArtifactResolver Maven / Gradle / Ivy

The newest version!
package io.microsphere.classloading;

import io.microsphere.logging.Logger;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

import static io.microsphere.logging.LoggerFactory.getLogger;
import static io.microsphere.net.URLUtils.resolveArchiveFile;

/**
 * Abstract {@link ArtifactResolver}
 *
 * @author Mercy
 * @since 1.0.0
 */
public abstract class AbstractArtifactResolver implements ArtifactResolver {

    protected final Logger logger = getLogger(getClass());

    private int priority;

    @Override
    public final Set resolve(URL... classPathURLs) {
        URLClassLoader urlClassLoader = newURLClassLoader(classPathURLs);
        Set artifactSet = new LinkedHashSet<>(classPathURLs.length);
        doResolve(artifactSet, urlClassLoader);
        return artifactSet;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public int getPriority() {
        return priority;
    }

    protected abstract void doResolve(Collection artifactSet, URLClassLoader urlClassLoader);

    protected URLClassLoader newURLClassLoader(URL[] urls) {
        return new URLClassLoader(urls, null);
    }

    protected URL resolveArtifactResourceURL(URL resourceURL) {
        URL url = null;
        try {
            File archiveFile = resolveArchiveFile(resourceURL);
            if (archiveFile != null) {
                url = archiveFile.toURI().toURL();
            }
        } catch (IOException e) {
            logger.error("The resource [url : {}] can't resolve the target artifact", resourceURL, e);
        }
        return url;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy