Example usage for org.springframework.util ResourceUtils JAR_URL_PREFIX

List of usage examples for org.springframework.util ResourceUtils JAR_URL_PREFIX

Introduction

In this page you can find the example usage for org.springframework.util ResourceUtils JAR_URL_PREFIX.

Prototype

String JAR_URL_PREFIX

To view the source code for org.springframework.util ResourceUtils JAR_URL_PREFIX.

Click Source Link

Document

URL prefix for loading from a jar file: "jar:".

Usage

From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java

/**
 * Search all {@link URLClassLoader} URLs for jar file references and add them to the
 * given set of resources in the form of pointers to the root of the jar file content.
 * @param classLoader the ClassLoader to search (including its ancestors)
 * @param result the set of resources to add jar roots to
 * @since 4.1.1/*from  w  w w .  java 2s . com*/
 */
protected void addAllClassLoaderJarRoots(@Nullable ClassLoader classLoader, Set<Resource> result) {
    if (classLoader instanceof URLClassLoader) {
        try {
            for (URL url : ((URLClassLoader) classLoader).getURLs()) {
                try {
                    UrlResource jarResource = new UrlResource(
                            ResourceUtils.JAR_URL_PREFIX + url + ResourceUtils.JAR_URL_SEPARATOR);
                    if (jarResource.exists()) {
                        result.add(jarResource);
                    }
                } catch (MalformedURLException ex) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Cannot search for matching files underneath [" + url
                                + "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage());
                    }
                }
            }
        } catch (Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Cannot introspect jar files since ClassLoader [" + classLoader
                        + "] does not support 'getURLs()': " + ex);
            }
        }
    }

    if (classLoader == ClassLoader.getSystemClassLoader()) {
        // "java.class.path" manifest evaluation...
        addClassPathManifestEntries(result);
    }

    if (classLoader != null) {
        try {
            // Hierarchy traversal...
            addAllClassLoaderJarRoots(classLoader.getParent(), result);
        } catch (Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Cannot introspect jar files in parent ClassLoader since [" + classLoader
                        + "] does not support 'getParent()': " + ex);
            }
        }
    }
}

From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java

/**
 * Determine jar file references from the "java.class.path." manifest property and add them
 * to the given set of resources in the form of pointers to the root of the jar file content.
 * @param result the set of resources to add jar roots to
 * @since 4.3//  ww w.  j a v a 2s  . com
 */
protected void addClassPathManifestEntries(Set<Resource> result) {
    try {
        String javaClassPathProperty = System.getProperty("java.class.path");
        for (String path : StringUtils.delimitedListToStringArray(javaClassPathProperty,
                System.getProperty("path.separator"))) {
            try {
                String filePath = new File(path).getAbsolutePath();
                int prefixIndex = filePath.indexOf(':');
                if (prefixIndex == 1) {
                    // Possibly "c:" drive prefix on Windows, to be upper-cased for proper duplicate detection
                    filePath = StringUtils.capitalize(filePath);
                }
                UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX
                        + ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR);
                // Potentially overlapping with URLClassLoader.getURLs() result above!
                if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) {
                    result.add(jarResource);
                }
            } catch (MalformedURLException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Cannot search for matching files underneath [" + path
                            + "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage());
                }
            }
        }
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to evaluate 'java.class.path' manifest entries: " + ex);
        }
    }
}

From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java

/**
 * Check whether the given file path has a duplicate but differently structured entry
 * in the existing result, i.e. with or without a leading slash.
 * @param filePath the file path (with or without a leading slash)
 * @param result the current result//from w  ww  .  j  a v  a 2 s .  com
 * @return {@code true} if there is a duplicate (i.e. to ignore the given file path),
 * {@code false} to proceed with adding a corresponding resource to the current result
 */
private boolean hasDuplicate(String filePath, Set<Resource> result) {
    if (result.isEmpty()) {
        return false;
    }
    String duplicatePath = (filePath.startsWith("/") ? filePath.substring(1) : "/" + filePath);
    try {
        return result.contains(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX
                + duplicatePath + ResourceUtils.JAR_URL_SEPARATOR));
    } catch (MalformedURLException ex) {
        // Ignore: just for testing against duplicate.
        return false;
    }
}