Example usage for org.springframework.util ResourceUtils useCachesIfNecessary

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

Introduction

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

Prototype

public static void useCachesIfNecessary(URLConnection con) 

Source Link

Document

Set the URLConnection#setUseCaches "useCaches" flag on the given connection, preferring false but leaving the flag at true for JNLP based resources.

Usage

From source file:com.sfxie.extension.spring4.properties.PropertiesLoaderUtils.java

/**
 * Load all properties from the specified class path resource
 * (in ISO-8859-1 encoding), using the given class loader.
 * <p>Merges properties if more than one resource of the same name
 * found in the class path./*w w w.  j  a  v a  2 s.c om*/
 * @param resourceName the name of the class path resource
 * @param classLoader the ClassLoader to use for loading
 * (or {@code null} to use the default class loader)
 * @return the populated Properties instance
 * @throws IOException if loading failed
 */
public static Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException {
    Assert.notNull(resourceName, "Resource name must not be null");
    ClassLoader classLoaderToUse = classLoader;
    if (classLoaderToUse == null) {
        classLoaderToUse = ClassUtils.getDefaultClassLoader();
    }
    Enumeration<URL> urls = (classLoaderToUse != null ? classLoaderToUse.getResources(resourceName)
            : ClassLoader.getSystemResources(resourceName));
    Properties props = new Properties();
    while (urls.hasMoreElements()) {
        URL url = urls.nextElement();
        URLConnection con = url.openConnection();
        ResourceUtils.useCachesIfNecessary(con);
        InputStream is = con.getInputStream();
        try {
            if (resourceName != null && resourceName.endsWith(XML_FILE_EXTENSION)) {
                props.loadFromXML(is);
            } else {
                props.load(is);
            }
        } finally {
            is.close();
        }
    }
    return props;
}

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

/**
 * Find all resources in jar files that match the given location pattern
 * via the Ant-style PathMatcher./*from   w  ww  .java2  s  .  c o m*/
 * @param rootDirResource the root directory as Resource
 * @param rootDirURL the pre-resolved root directory URL
 * @param subPattern the sub pattern to match (below the root directory)
 * @return a mutable Set of matching Resource instances
 * @throws IOException in case of I/O errors
 * @since 4.3
 * @see java.net.JarURLConnection
 * @see org.springframework.util.PathMatcher
 */
protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, URL rootDirURL,
        String subPattern) throws IOException {

    URLConnection con = rootDirURL.openConnection();
    JarFile jarFile;
    String jarFileUrl;
    String rootEntryPath;
    boolean closeJarFile;

    if (con instanceof JarURLConnection) {
        // Should usually be the case for traditional JAR files.
        JarURLConnection jarCon = (JarURLConnection) con;
        ResourceUtils.useCachesIfNecessary(jarCon);
        jarFile = jarCon.getJarFile();
        jarFileUrl = jarCon.getJarFileURL().toExternalForm();
        JarEntry jarEntry = jarCon.getJarEntry();
        rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
        closeJarFile = !jarCon.getUseCaches();
    } else {
        // No JarURLConnection -> need to resort to URL file parsing.
        // We'll assume URLs of the format "jar:path!/entry", with the protocol
        // being arbitrary as long as following the entry format.
        // We'll also handle paths with and without leading "file:" prefix.
        String urlFile = rootDirURL.getFile();
        try {
            int separatorIndex = urlFile.indexOf(ResourceUtils.WAR_URL_SEPARATOR);
            if (separatorIndex == -1) {
                separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
            }
            if (separatorIndex != -1) {
                jarFileUrl = urlFile.substring(0, separatorIndex);
                rootEntryPath = urlFile.substring(separatorIndex + 2); // both separators are 2 chars
                jarFile = getJarFile(jarFileUrl);
            } else {
                jarFile = new JarFile(urlFile);
                jarFileUrl = urlFile;
                rootEntryPath = "";
            }
            closeJarFile = true;
        } catch (ZipException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Skipping invalid jar classpath entry [" + urlFile + "]");
            }
            return Collections.emptySet();
        }
    }

    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
        }
        if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
            // Root entry path must end with slash to allow for proper matching.
            // The Sun JRE does not return a slash here, but BEA JRockit does.
            rootEntryPath = rootEntryPath + "/";
        }
        Set<Resource> result = new LinkedHashSet<>(8);
        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 (getPathMatcher().match(subPattern, relativePath)) {
                    result.add(rootDirResource.createRelative(relativePath));
                }
            }
        }
        return result;
    } finally {
        if (closeJarFile) {
            jarFile.close();
        }
    }
}