Search in sources :

Example 1 with SourceResource

use of justlive.earth.breeze.snow.common.base.io.SourceResource in project earth-snow by justlive1.

the class AbstractResourceLoader method findMatchPath.

/**
 * 获取匹配的路径
 *
 * @param location
 * @return
 * @throws IOException
 */
protected List<SourceResource> findMatchPath(String location) throws IOException {
    List<SourceResource> all = new LinkedList<>();
    String rootPath = this.getRootDir(location);
    String subPattern = location.substring(rootPath.length());
    List<SourceResource> rootResources = this.parse(rootPath);
    for (SourceResource resource : rootResources) {
        URL rootUrl = resource.getURL();
        if (BaseConstants.URL_PROTOCOL_FILE.equals(rootUrl.getProtocol())) {
            all.addAll(this.findFileMatchPath(resource, subPattern));
        } else if (ResourceUtils.isJarURL(rootUrl)) {
            all.addAll(this.findJarMatchPath(resource, rootUrl, subPattern));
        } else {
        // TODO others
        }
    }
    return all;
}
Also used : SourceResource(justlive.earth.breeze.snow.common.base.io.SourceResource) LinkedList(java.util.LinkedList) URL(java.net.URL)

Example 2 with SourceResource

use of justlive.earth.breeze.snow.common.base.io.SourceResource in project earth-snow by justlive1.

the class AbstractResourceLoader method findJarMatchPath.

/**
 * 获取资源下匹配的jar中的文件
 *
 * @param resource
 * @param rootUrl
 * @param subPattern
 * @return
 * @throws IOException
 */
protected List<SourceResource> findJarMatchPath(SourceResource resource, URL rootUrl, String subPattern) throws IOException {
    List<SourceResource> all = new LinkedList<>();
    URLConnection con = rootUrl.openConnection();
    JarFile jarFile;
    String jarFileUrl;
    String rootEntryPath;
    if (con instanceof JarURLConnection) {
        JarURLConnection jarCon = (JarURLConnection) con;
        jarFile = jarCon.getJarFile();
        jarFileUrl = jarCon.getJarFileURL().toExternalForm();
        JarEntry jarEntry = jarCon.getJarEntry();
        rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
    } else {
        String urlFile = rootUrl.getFile();
        int separatorLength = BaseConstants.WAR_URL_SEPARATOR.length();
        int separatorIndex = urlFile.indexOf(BaseConstants.WAR_URL_SEPARATOR);
        if (separatorIndex == -1) {
            separatorIndex = urlFile.indexOf(BaseConstants.JAR_URL_SEPARATOR);
            separatorLength = BaseConstants.JAR_URL_SEPARATOR.length();
        }
        if (separatorIndex != -1) {
            jarFileUrl = urlFile.substring(0, separatorIndex);
            rootEntryPath = urlFile.substring(separatorIndex + separatorLength);
            jarFile = this.getJarFile(jarFileUrl);
        } else {
            jarFile = new JarFile(urlFile);
            jarFileUrl = urlFile;
            rootEntryPath = "";
        }
    }
    try {
        if (log.isDebugEnabled()) {
            log.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
        }
        if (rootEntryPath.length() > 0 && !rootEntryPath.endsWith(BaseConstants.PATH_SEPARATOR)) {
            rootEntryPath += BaseConstants.PATH_SEPARATOR;
        }
        for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
            JarEntry entry = entries.nextElement();
            String entryPath = entry.getName();
            if (entryPath.startsWith(rootEntryPath)) {
                String relativePath = entryPath.substring(rootEntryPath.length());
                if (matcher.match(subPattern, relativePath)) {
                    all.add(resource.createRelative(relativePath));
                }
            }
        }
    } finally {
        jarFile.close();
    }
    return all;
}
Also used : SourceResource(justlive.earth.breeze.snow.common.base.io.SourceResource) JarURLConnection(java.net.JarURLConnection) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) LinkedList(java.util.LinkedList) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 3 with SourceResource

use of justlive.earth.breeze.snow.common.base.io.SourceResource in project earth-snow by justlive1.

the class AbstractResourceLoader method findFileMatchPath.

/**
 * 获取资源下匹配的文件
 *
 * @param resource
 * @param subPattern
 * @return
 */
protected List<SourceResource> findFileMatchPath(SourceResource resource, String subPattern) throws IOException {
    List<SourceResource> list = new LinkedList<>();
    File rootDir = resource.getFile().getAbsoluteFile();
    Set<File> matchedFiles = findMatchedFiles(rootDir, subPattern);
    for (File file : matchedFiles) {
        list.add(new FileSystemResource(file));
    }
    return list;
}
Also used : SourceResource(justlive.earth.breeze.snow.common.base.io.SourceResource) JarFile(java.util.jar.JarFile) File(java.io.File) LinkedList(java.util.LinkedList)

Example 4 with SourceResource

use of justlive.earth.breeze.snow.common.base.io.SourceResource in project earth-snow by justlive1.

the class AbstractResourceLoader method resolveAllClassPathResource.

/**
 * 处理所有classpath下的资源
 *
 * @param location
 */
protected List<SourceResource> resolveAllClassPathResource(String location) throws IOException {
    List<SourceResource> list = new LinkedList<>();
    if (matcher.isPattern(location)) {
        list.addAll(this.findMatchPath(location));
    } else {
        if (location.startsWith(BaseConstants.ROOT_PATH)) {
            location = location.substring(BaseConstants.ROOT_PATH.length());
        }
        Enumeration<URL> resources = null;
        resources = loader.getResources(location);
        if (resources == null) {
            return list;
        }
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            list.add(new UrlResource(url));
        }
    }
    return list;
}
Also used : SourceResource(justlive.earth.breeze.snow.common.base.io.SourceResource) LinkedList(java.util.LinkedList) URL(java.net.URL)

Aggregations

LinkedList (java.util.LinkedList)4 SourceResource (justlive.earth.breeze.snow.common.base.io.SourceResource)4 URL (java.net.URL)2 JarFile (java.util.jar.JarFile)2 File (java.io.File)1 JarURLConnection (java.net.JarURLConnection)1 URLConnection (java.net.URLConnection)1 JarEntry (java.util.jar.JarEntry)1