From 8c76381d959f4dd1c7e6dae077c59b8b12e4b4e8 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 19 Aug 2014 20:34:45 +0200 Subject: [PATCH] PathMatchingResourcePatternResolver supports "classpath*" searches in jar file roots as well Issue: SPR-12095 --- .../PathMatchingResourcePatternResolver.java | 58 ++++++++++++++++++- .../springframework/util/ResourceUtils.java | 20 ++++++- ...hMatchingResourcePatternResolverTests.java | 43 +++++++++----- 3 files changed, 103 insertions(+), 18 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 1f54f63845..be2bcb9e1e 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -21,8 +21,10 @@ import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.net.JarURLConnection; +import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.net.URLClassLoader; import java.net.URLConnection; import java.util.Collections; import java.util.Enumeration; @@ -310,12 +312,17 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol URL url = resourceUrls.nextElement(); result.add(convertClassLoaderURL(url)); } + if ("".equals(path)) { + // The above result is likely to be incomplete, i.e. only containing file system references. + // We need to have pointers to each of the jar files on the classpath as well... + addAllClassLoaderJarRoots(cl, result); + } return result.toArray(new Resource[result.size()]); } /** - * Convert the given URL as returned from the ClassLoader into a Resource object. - *

The default implementation simply creates a UrlResource instance. + * Convert the given URL as returned from the ClassLoader into a {@link Resource}. + *

The default implementation simply creates a {@link UrlResource} instance. * @param url a URL as returned from the ClassLoader * @return the corresponding Resource object * @see java.lang.ClassLoader#getResources @@ -325,6 +332,53 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol return new UrlResource(url); } + /** + * 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 + */ + private void addAllClassLoaderJarRoots(ClassLoader classLoader, Set result) { + if (classLoader instanceof URLClassLoader) { + try { + for (URL url : ((URLClassLoader) classLoader).getURLs()) { + if (ResourceUtils.isJarFileURL(url)) { + try { + UrlResource jarResource = new UrlResource( + ResourceUtils.JAR_URL_PREFIX + url.toString() + 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 != null) { + try { + 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); + } + } + } + } + /** * Find all resources that match the given location pattern via the * Ant-style PathMatcher. Supports resources in jar files and zip files diff --git a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java index 855a8ca527..9cade42b6d 100644 --- a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java @@ -57,6 +57,9 @@ public abstract class ResourceUtils { /** URL prefix for loading from the file system: "file:" */ public static final String FILE_URL_PREFIX = "file:"; + /** URL prefix for loading from the file system: "jar:" */ + public static final String JAR_URL_PREFIX = "jar:"; + /** URL protocol for a file in the file system: "file" */ public static final String URL_PROTOCOL_FILE = "file"; @@ -78,7 +81,10 @@ public abstract class ResourceUtils { /** URL protocol for a general JBoss VFS resource: "vfs" */ public static final String URL_PROTOCOL_VFS = "vfs"; - /** Separator between JAR URL and file path within the JAR */ + /** File extension for a regular jar file: ".jar" */ + public static final String JAR_FILE_EXTENSION = ".jar"; + + /** Separator between JAR URL and file path within the JAR: "!/" */ public static final String JAR_URL_SEPARATOR = "!/"; @@ -273,6 +279,18 @@ public abstract class ResourceUtils { URL_PROTOCOL_VFSZIP.equals(protocol) || URL_PROTOCOL_WSJAR.equals(protocol)); } + /** + * Determine whether the given URL points to a jar file itself, + * that is, has protocol "file" and ends with the ".jar" extension. + * @param url the URL to check + * @return whether the URL has been identified as a JAR file URL + * @since 4.1 + */ + public static boolean isJarFileURL(URL url) { + return (URL_PROTOCOL_FILE.equals(url.getProtocol()) && + url.getPath().toLowerCase().endsWith(JAR_FILE_EXTENSION)); + } + /** * Extract the URL for the actual jar file from the given URL * (which may point to a resource in a jar file or to a jar file itself). diff --git a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java index b494c70810..6c1128ef21 100644 --- a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,6 @@ package org.springframework.core.io.support; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; @@ -27,8 +24,11 @@ import java.util.List; import org.junit.Ignore; import org.junit.Test; + import org.springframework.core.io.Resource; +import static org.junit.Assert.*; + /** * If this test case fails, uncomment diagnostics in * {@code assertProtocolAndFilenames} method. @@ -91,13 +91,13 @@ public class PathMatchingResourcePatternResolverTests { Resource[] resources = resolver.getResources("classpath*:org/springframework/core/io/sup*/*.class"); // Have to exclude Clover-generated class files here, // as we might be running as part of a Clover test run. - List noCloverResources = new ArrayList(); - for (int i = 0; i < resources.length; i++) { - if (resources[i].getFilename().indexOf("$__CLOVER_") == -1) { - noCloverResources.add(resources[i]); + List noCloverResources = new ArrayList(); + for (Resource resource : resources) { + if (!resource.getFilename().contains("$__CLOVER_")) { + noCloverResources.add(resource); } } - resources = (Resource[]) noCloverResources.toArray(new Resource[noCloverResources.size()]); + resources = noCloverResources.toArray(new Resource[noCloverResources.size()]); assertProtocolAndFilenames(resources, "file", CLASSES_IN_CORE_IO_SUPPORT, TEST_CLASSES_IN_CORE_IO_SUPPORT); } @@ -113,15 +113,29 @@ public class PathMatchingResourcePatternResolverTests { assertProtocolAndFilenames(resources, "jar", CLASSES_IN_COMMONSLOGGING); } + @Test + public void testRootPatternRetrievalInJarFiles() throws IOException { + Resource[] resources = resolver.getResources("classpath*:*.dtd"); + boolean found = false; + for (Resource resource : resources) { + if (resource.getFilename().equals("aspectj_1_5_0.dtd")) { + found = true; + } + } + assertTrue("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar", found); + } + + private void assertProtocolAndFilename(Resource resource, String urlProtocol, String fileName) throws IOException { assertProtocolAndFilenames(new Resource[] {resource}, urlProtocol, new String[] {fileName}); } private void assertProtocolAndFilenames( Resource[] resources, String urlProtocol, String[] fileNames1, String[] fileNames2) throws IOException { - List fileNames = new ArrayList(Arrays.asList(fileNames1)); + + List fileNames = new ArrayList(Arrays.asList(fileNames1)); fileNames.addAll(Arrays.asList(fileNames2)); - assertProtocolAndFilenames(resources, urlProtocol, (String[]) fileNames.toArray(new String[fileNames.size()])); + assertProtocolAndFilenames(resources, urlProtocol, fileNames.toArray(new String[fileNames.size()])); } private void assertProtocolAndFilenames(Resource[] resources, String urlProtocol, String[] fileNames) @@ -146,16 +160,15 @@ public class PathMatchingResourcePatternResolverTests { // } assertEquals("Correct number of files found", fileNames.length, resources.length); - for (int i = 0; i < resources.length; i++) { - Resource resource = resources[i]; + for (Resource resource : resources) { assertEquals(urlProtocol, resource.getURL().getProtocol()); assertFilenameIn(resource, fileNames); } } private void assertFilenameIn(Resource resource, String[] fileNames) { - for (int i = 0; i < fileNames.length; i++) { - if (resource.getFilename().endsWith(fileNames[i])) { + for (String fileName : fileNames) { + if (resource.getFilename().endsWith(fileName)) { return; } }