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

io.microsphere.io.scanner.SimpleJarEntryScanner Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
/**
 *
 */
package io.microsphere.io.scanner;

import io.microsphere.constants.PathConstants;
import io.microsphere.filter.JarEntryFilter;
import io.microsphere.util.jar.JarUtils;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import static io.microsphere.util.StringUtils.EMPTY;
import static java.util.Collections.unmodifiableSet;

/**
 * Simple {@link JarEntry} Scanner
 *
 * @author Mercy
 * @see SimpleJarEntryScanner
 * @since 1.0.0
 */
public class SimpleJarEntryScanner {

    /**
     * Singleton
     */
    public static final SimpleJarEntryScanner INSTANCE = new SimpleJarEntryScanner();

    public SimpleJarEntryScanner() {
    }

    /**
     * @param jarURL    {@link URL} of {@link JarFile} or {@link JarEntry}
     * @param recursive recursive
     * @return Read-only {@link Set}
     * @throws NullPointerException     If argument null
     * @throws IllegalArgumentException 
  • {@link JarUtils#resolveRelativePath(URL)} * @throws IOException
    • {@link JarUtils#toJarFile(URL)} * @since 1.0.0 */ @Nonnull public Set scan(URL jarURL, final boolean recursive) throws NullPointerException, IllegalArgumentException, IOException { return scan(jarURL, recursive, null); } /** * @param jarURL {@link URL} of {@link JarFile} or {@link JarEntry} * @param recursive recursive * @param jarEntryFilter {@link JarEntryFilter} * @return Read-only {@link Set} * @throws NullPointerException If argument null * @throws IllegalArgumentException {@link JarUtils#resolveJarAbsolutePath(URL)} * @throws IOException {@link JarUtils#toJarFile(URL)} * @see JarEntryFilter * @since 1.0.0 */ @Nonnull public Set scan(URL jarURL, final boolean recursive, JarEntryFilter jarEntryFilter) throws NullPointerException, IllegalArgumentException, IOException { String relativePath = JarUtils.resolveRelativePath(jarURL); JarFile jarFile = JarUtils.toJarFile(jarURL); return scan(jarFile, relativePath, recursive, jarEntryFilter); } /** * @param jarFile * @param recursive * @return * @throws NullPointerException * @throws IllegalArgumentException * @throws IOException */ public Set scan(JarFile jarFile, final boolean recursive) throws NullPointerException, IllegalArgumentException, IOException { return scan(jarFile, recursive, null); } /** * @param jarFile * @param recursive * @param jarEntryFilter * @return * @throws NullPointerException * @throws IllegalArgumentException * @throws IOException */ public Set scan(JarFile jarFile, final boolean recursive, JarEntryFilter jarEntryFilter) throws NullPointerException, IllegalArgumentException, IOException { return scan(jarFile, EMPTY, recursive, jarEntryFilter); } protected Set scan(JarFile jarFile, String relativePath, final boolean recursive, JarEntryFilter jarEntryFilter) throws NullPointerException, IllegalArgumentException, IOException { Set jarEntriesSet = new LinkedHashSet<>(); List jarEntriesList = JarUtils.filter(jarFile, jarEntryFilter); for (JarEntry jarEntry : jarEntriesList) { String jarEntryName = jarEntry.getName(); boolean accept = false; if (recursive) { accept = jarEntryName.startsWith(relativePath); } else { if (jarEntry.isDirectory()) { accept = jarEntryName.equals(relativePath); } else { int beginIndex = jarEntryName.indexOf(relativePath); if (beginIndex == 0) { accept = jarEntryName.indexOf(PathConstants.SLASH, relativePath.length()) < 0; } } } if (accept) { jarEntriesSet.add(jarEntry); } } return unmodifiableSet(jarEntriesSet); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy