001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.io.support; 018 019import java.io.File; 020import java.io.FileNotFoundException; 021import java.io.IOException; 022import java.lang.reflect.InvocationHandler; 023import java.lang.reflect.Method; 024import java.net.JarURLConnection; 025import java.net.MalformedURLException; 026import java.net.URISyntaxException; 027import java.net.URL; 028import java.net.URLClassLoader; 029import java.net.URLConnection; 030import java.util.Arrays; 031import java.util.Collections; 032import java.util.Enumeration; 033import java.util.LinkedHashSet; 034import java.util.Set; 035import java.util.jar.JarEntry; 036import java.util.jar.JarFile; 037import java.util.zip.ZipException; 038 039import org.apache.commons.logging.Log; 040import org.apache.commons.logging.LogFactory; 041 042import org.springframework.core.io.DefaultResourceLoader; 043import org.springframework.core.io.FileSystemResource; 044import org.springframework.core.io.Resource; 045import org.springframework.core.io.ResourceLoader; 046import org.springframework.core.io.UrlResource; 047import org.springframework.core.io.VfsResource; 048import org.springframework.util.AntPathMatcher; 049import org.springframework.util.Assert; 050import org.springframework.util.ClassUtils; 051import org.springframework.util.PathMatcher; 052import org.springframework.util.ReflectionUtils; 053import org.springframework.util.ResourceUtils; 054import org.springframework.util.StringUtils; 055 056/** 057 * A {@link ResourcePatternResolver} implementation that is able to resolve a 058 * specified resource location path into one or more matching Resources. 059 * The source path may be a simple path which has a one-to-one mapping to a 060 * target {@link org.springframework.core.io.Resource}, or alternatively 061 * may contain the special "{@code classpath*:}" prefix and/or 062 * internal Ant-style regular expressions (matched using Spring's 063 * {@link org.springframework.util.AntPathMatcher} utility). 064 * Both of the latter are effectively wildcards. 065 * 066 * <p><b>No Wildcards:</b> 067 * 068 * <p>In the simple case, if the specified location path does not start with the 069 * {@code "classpath*:}" prefix, and does not contain a PathMatcher pattern, 070 * this resolver will simply return a single resource via a 071 * {@code getResource()} call on the underlying {@code ResourceLoader}. 072 * Examples are real URLs such as "{@code file:C:/context.xml}", pseudo-URLs 073 * such as "{@code classpath:/context.xml}", and simple unprefixed paths 074 * such as "{@code /WEB-INF/context.xml}". The latter will resolve in a 075 * fashion specific to the underlying {@code ResourceLoader} (e.g. 076 * {@code ServletContextResource} for a {@code WebApplicationContext}). 077 * 078 * <p><b>Ant-style Patterns:</b> 079 * 080 * <p>When the path location contains an Ant-style pattern, e.g.: 081 * <pre class="code"> 082 * /WEB-INF/*-context.xml 083 * com/mycompany/**/applicationContext.xml 084 * file:C:/some/path/*-context.xml 085 * classpath:com/mycompany/**/applicationContext.xml</pre> 086 * the resolver follows a more complex but defined procedure to try to resolve 087 * the wildcard. It produces a {@code Resource} for the path up to the last 088 * non-wildcard segment and obtains a {@code URL} from it. If this URL is 089 * not a "{@code jar:}" URL or container-specific variant (e.g. 090 * "{@code zip:}" in WebLogic, "{@code wsjar}" in WebSphere", etc.), 091 * then a {@code java.io.File} is obtained from it, and used to resolve the 092 * wildcard by walking the filesystem. In the case of a jar URL, the resolver 093 * either gets a {@code java.net.JarURLConnection} from it, or manually parses 094 * the jar URL, and then traverses the contents of the jar file, to resolve the 095 * wildcards. 096 * 097 * <p><b>Implications on portability:</b> 098 * 099 * <p>If the specified path is already a file URL (either explicitly, or 100 * implicitly because the base {@code ResourceLoader} is a filesystem one, 101 * then wildcarding is guaranteed to work in a completely portable fashion. 102 * 103 * <p>If the specified path is a classpath location, then the resolver must 104 * obtain the last non-wildcard path segment URL via a 105 * {@code Classloader.getResource()} call. Since this is just a 106 * node of the path (not the file at the end) it is actually undefined 107 * (in the ClassLoader Javadocs) exactly what sort of a URL is returned in 108 * this case. In practice, it is usually a {@code java.io.File} representing 109 * the directory, where the classpath resource resolves to a filesystem 110 * location, or a jar URL of some sort, where the classpath resource resolves 111 * to a jar location. Still, there is a portability concern on this operation. 112 * 113 * <p>If a jar URL is obtained for the last non-wildcard segment, the resolver 114 * must be able to get a {@code java.net.JarURLConnection} from it, or 115 * manually parse the jar URL, to be able to walk the contents of the jar, 116 * and resolve the wildcard. This will work in most environments, but will 117 * fail in others, and it is strongly recommended that the wildcard 118 * resolution of resources coming from jars be thoroughly tested in your 119 * specific environment before you rely on it. 120 * 121 * <p><b>{@code classpath*:} Prefix:</b> 122 * 123 * <p>There is special support for retrieving multiple class path resources with 124 * the same name, via the "{@code classpath*:}" prefix. For example, 125 * "{@code classpath*:META-INF/beans.xml}" will find all "beans.xml" 126 * files in the class path, be it in "classes" directories or in JAR files. 127 * This is particularly useful for autodetecting config files of the same name 128 * at the same location within each jar file. Internally, this happens via a 129 * {@code ClassLoader.getResources()} call, and is completely portable. 130 * 131 * <p>The "classpath*:" prefix can also be combined with a PathMatcher pattern in 132 * the rest of the location path, for example "classpath*:META-INF/*-beans.xml". 133 * In this case, the resolution strategy is fairly simple: a 134 * {@code ClassLoader.getResources()} call is used on the last non-wildcard 135 * path segment to get all the matching resources in the class loader hierarchy, 136 * and then off each resource the same PathMatcher resolution strategy described 137 * above is used for the wildcard subpath. 138 * 139 * <p><b>Other notes:</b> 140 * 141 * <p><b>WARNING:</b> Note that "{@code classpath*:}" when combined with 142 * Ant-style patterns will only work reliably with at least one root directory 143 * before the pattern starts, unless the actual target files reside in the file 144 * system. This means that a pattern like "{@code classpath*:*.xml}" will 145 * <i>not</i> retrieve files from the root of jar files but rather only from the 146 * root of expanded directories. This originates from a limitation in the JDK's 147 * {@code ClassLoader.getResources()} method which only returns file system 148 * locations for a passed-in empty String (indicating potential roots to search). 149 * This {@code ResourcePatternResolver} implementation is trying to mitigate the 150 * jar root lookup limitation through {@link URLClassLoader} introspection and 151 * "java.class.path" manifest evaluation; however, without portability guarantees. 152 * 153 * <p><b>WARNING:</b> Ant-style patterns with "classpath:" resources are not 154 * guaranteed to find matching resources if the root package to search is available 155 * in multiple class path locations. This is because a resource such as 156 * <pre class="code"> 157 * com/mycompany/package1/service-context.xml 158 * </pre> 159 * may be in only one location, but when a path such as 160 * <pre class="code"> 161 * classpath:com/mycompany/**/service-context.xml 162 * </pre> 163 * is used to try to resolve it, the resolver will work off the (first) URL 164 * returned by {@code getResource("com/mycompany");}. If this base package node 165 * exists in multiple classloader locations, the actual end resource may not be 166 * underneath. Therefore, preferably, use "{@code classpath*:}" with the same 167 * Ant-style pattern in such a case, which will search <i>all</i> class path 168 * locations that contain the root package. 169 * 170 * @author Juergen Hoeller 171 * @author Colin Sampaleanu 172 * @author Marius Bogoevici 173 * @author Costin Leau 174 * @author Phillip Webb 175 * @since 1.0.2 176 * @see #CLASSPATH_ALL_URL_PREFIX 177 * @see org.springframework.util.AntPathMatcher 178 * @see org.springframework.core.io.ResourceLoader#getResource(String) 179 * @see ClassLoader#getResources(String) 180 */ 181public class PathMatchingResourcePatternResolver implements ResourcePatternResolver { 182 183 private static final Log logger = LogFactory.getLog(PathMatchingResourcePatternResolver.class); 184 185 private static Method equinoxResolveMethod; 186 187 static { 188 try { 189 // Detect Equinox OSGi (e.g. on WebSphere 6.1) 190 Class<?> fileLocatorClass = ClassUtils.forName("org.eclipse.core.runtime.FileLocator", 191 PathMatchingResourcePatternResolver.class.getClassLoader()); 192 equinoxResolveMethod = fileLocatorClass.getMethod("resolve", URL.class); 193 logger.debug("Found Equinox FileLocator for OSGi bundle URL resolution"); 194 } 195 catch (Throwable ex) { 196 equinoxResolveMethod = null; 197 } 198 } 199 200 201 private final ResourceLoader resourceLoader; 202 203 private PathMatcher pathMatcher = new AntPathMatcher(); 204 205 206 /** 207 * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader. 208 * <p>ClassLoader access will happen via the thread context class loader. 209 * @see org.springframework.core.io.DefaultResourceLoader 210 */ 211 public PathMatchingResourcePatternResolver() { 212 this.resourceLoader = new DefaultResourceLoader(); 213 } 214 215 /** 216 * Create a new PathMatchingResourcePatternResolver. 217 * <p>ClassLoader access will happen via the thread context class loader. 218 * @param resourceLoader the ResourceLoader to load root directories and 219 * actual resources with 220 */ 221 public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) { 222 Assert.notNull(resourceLoader, "ResourceLoader must not be null"); 223 this.resourceLoader = resourceLoader; 224 } 225 226 /** 227 * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader. 228 * @param classLoader the ClassLoader to load classpath resources with, 229 * or {@code null} for using the thread context class loader 230 * at the time of actual resource access 231 * @see org.springframework.core.io.DefaultResourceLoader 232 */ 233 public PathMatchingResourcePatternResolver(ClassLoader classLoader) { 234 this.resourceLoader = new DefaultResourceLoader(classLoader); 235 } 236 237 238 /** 239 * Return the ResourceLoader that this pattern resolver works with. 240 */ 241 public ResourceLoader getResourceLoader() { 242 return this.resourceLoader; 243 } 244 245 @Override 246 public ClassLoader getClassLoader() { 247 return getResourceLoader().getClassLoader(); 248 } 249 250 /** 251 * Set the PathMatcher implementation to use for this 252 * resource pattern resolver. Default is AntPathMatcher. 253 * @see org.springframework.util.AntPathMatcher 254 */ 255 public void setPathMatcher(PathMatcher pathMatcher) { 256 Assert.notNull(pathMatcher, "PathMatcher must not be null"); 257 this.pathMatcher = pathMatcher; 258 } 259 260 /** 261 * Return the PathMatcher that this resource pattern resolver uses. 262 */ 263 public PathMatcher getPathMatcher() { 264 return this.pathMatcher; 265 } 266 267 268 @Override 269 public Resource getResource(String location) { 270 return getResourceLoader().getResource(location); 271 } 272 273 @Override 274 public Resource[] getResources(String locationPattern) throws IOException { 275 Assert.notNull(locationPattern, "Location pattern must not be null"); 276 if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) { 277 // a class path resource (multiple resources for same name possible) 278 if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) { 279 // a class path resource pattern 280 return findPathMatchingResources(locationPattern); 281 } 282 else { 283 // all class path resources with the given name 284 return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length())); 285 } 286 } 287 else { 288 // Generally only look for a pattern after a prefix here, 289 // and on Tomcat only after the "*/" separator for its "war:" protocol. 290 int prefixEnd = (locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 : 291 locationPattern.indexOf(':') + 1); 292 if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) { 293 // a file pattern 294 return findPathMatchingResources(locationPattern); 295 } 296 else { 297 // a single resource with the given name 298 return new Resource[] {getResourceLoader().getResource(locationPattern)}; 299 } 300 } 301 } 302 303 /** 304 * Find all class location resources with the given location via the ClassLoader. 305 * Delegates to {@link #doFindAllClassPathResources(String)}. 306 * @param location the absolute path within the classpath 307 * @return the result as Resource array 308 * @throws IOException in case of I/O errors 309 * @see java.lang.ClassLoader#getResources 310 * @see #convertClassLoaderURL 311 */ 312 protected Resource[] findAllClassPathResources(String location) throws IOException { 313 String path = location; 314 if (path.startsWith("/")) { 315 path = path.substring(1); 316 } 317 Set<Resource> result = doFindAllClassPathResources(path); 318 if (logger.isDebugEnabled()) { 319 logger.debug("Resolved classpath location [" + location + "] to resources " + result); 320 } 321 return result.toArray(new Resource[result.size()]); 322 } 323 324 /** 325 * Find all class location resources with the given path via the ClassLoader. 326 * Called by {@link #findAllClassPathResources(String)}. 327 * @param path the absolute path within the classpath (never a leading slash) 328 * @return a mutable Set of matching Resource instances 329 * @since 4.1.1 330 */ 331 protected Set<Resource> doFindAllClassPathResources(String path) throws IOException { 332 Set<Resource> result = new LinkedHashSet<Resource>(16); 333 ClassLoader cl = getClassLoader(); 334 Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path)); 335 while (resourceUrls.hasMoreElements()) { 336 URL url = resourceUrls.nextElement(); 337 result.add(convertClassLoaderURL(url)); 338 } 339 if ("".equals(path)) { 340 // The above result is likely to be incomplete, i.e. only containing file system references. 341 // We need to have pointers to each of the jar files on the classpath as well... 342 addAllClassLoaderJarRoots(cl, result); 343 } 344 return result; 345 } 346 347 /** 348 * Convert the given URL as returned from the ClassLoader into a {@link Resource}. 349 * <p>The default implementation simply creates a {@link UrlResource} instance. 350 * @param url a URL as returned from the ClassLoader 351 * @return the corresponding Resource object 352 * @see java.lang.ClassLoader#getResources 353 * @see org.springframework.core.io.Resource 354 */ 355 protected Resource convertClassLoaderURL(URL url) { 356 return new UrlResource(url); 357 } 358 359 /** 360 * Search all {@link URLClassLoader} URLs for jar file references and add them to the 361 * given set of resources in the form of pointers to the root of the jar file content. 362 * @param classLoader the ClassLoader to search (including its ancestors) 363 * @param result the set of resources to add jar roots to 364 * @since 4.1.1 365 */ 366 protected void addAllClassLoaderJarRoots(ClassLoader classLoader, Set<Resource> result) { 367 if (classLoader instanceof URLClassLoader) { 368 try { 369 for (URL url : ((URLClassLoader) classLoader).getURLs()) { 370 try { 371 UrlResource jarResource = new UrlResource( 372 ResourceUtils.JAR_URL_PREFIX + url + ResourceUtils.JAR_URL_SEPARATOR); 373 if (jarResource.exists()) { 374 result.add(jarResource); 375 } 376 } 377 catch (MalformedURLException ex) { 378 if (logger.isDebugEnabled()) { 379 logger.debug("Cannot search for matching files underneath [" + url + 380 "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage()); 381 } 382 } 383 } 384 } 385 catch (Exception ex) { 386 if (logger.isDebugEnabled()) { 387 logger.debug("Cannot introspect jar files since ClassLoader [" + classLoader + 388 "] does not support 'getURLs()': " + ex); 389 } 390 } 391 } 392 393 if (classLoader == ClassLoader.getSystemClassLoader()) { 394 // "java.class.path" manifest evaluation... 395 addClassPathManifestEntries(result); 396 } 397 398 if (classLoader != null) { 399 try { 400 // Hierarchy traversal... 401 addAllClassLoaderJarRoots(classLoader.getParent(), result); 402 } 403 catch (Exception ex) { 404 if (logger.isDebugEnabled()) { 405 logger.debug("Cannot introspect jar files in parent ClassLoader since [" + classLoader + 406 "] does not support 'getParent()': " + ex); 407 } 408 } 409 } 410 } 411 412 /** 413 * Determine jar file references from the "java.class.path." manifest property and add them 414 * to the given set of resources in the form of pointers to the root of the jar file content. 415 * @param result the set of resources to add jar roots to 416 * @since 4.3 417 */ 418 protected void addClassPathManifestEntries(Set<Resource> result) { 419 try { 420 String javaClassPathProperty = System.getProperty("java.class.path"); 421 for (String path : StringUtils.delimitedListToStringArray( 422 javaClassPathProperty, System.getProperty("path.separator"))) { 423 try { 424 String filePath = new File(path).getAbsolutePath(); 425 int prefixIndex = filePath.indexOf(':'); 426 if (prefixIndex == 1) { 427 // Possibly "c:" drive prefix on Windows, to be upper-cased for proper duplicate detection 428 filePath = StringUtils.capitalize(filePath); 429 } 430 UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX + 431 ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR); 432 // Potentially overlapping with URLClassLoader.getURLs() result above! 433 if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) { 434 result.add(jarResource); 435 } 436 } 437 catch (MalformedURLException ex) { 438 if (logger.isDebugEnabled()) { 439 logger.debug("Cannot search for matching files underneath [" + path + 440 "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage()); 441 } 442 } 443 } 444 } 445 catch (Exception ex) { 446 if (logger.isDebugEnabled()) { 447 logger.debug("Failed to evaluate 'java.class.path' manifest entries: " + ex); 448 } 449 } 450 } 451 452 /** 453 * Check whether the given file path has a duplicate but differently structured entry 454 * in the existing result, i.e. with or without a leading slash. 455 * @param filePath the file path (with or without a leading slash) 456 * @param result the current result 457 * @return {@code true} if there is a duplicate (i.e. to ignore the given file path), 458 * {@code false} to proceed with adding a corresponding resource to the current result 459 */ 460 private boolean hasDuplicate(String filePath, Set<Resource> result) { 461 if (result.isEmpty()) { 462 return false; 463 } 464 String duplicatePath = (filePath.startsWith("/") ? filePath.substring(1) : "/" + filePath); 465 try { 466 return result.contains(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + 467 duplicatePath + ResourceUtils.JAR_URL_SEPARATOR)); 468 } 469 catch (MalformedURLException ex) { 470 // Ignore: just for testing against duplicate. 471 return false; 472 } 473 } 474 475 /** 476 * Find all resources that match the given location pattern via the 477 * Ant-style PathMatcher. Supports resources in jar files and zip files 478 * and in the file system. 479 * @param locationPattern the location pattern to match 480 * @return the result as Resource array 481 * @throws IOException in case of I/O errors 482 * @see #doFindPathMatchingJarResources 483 * @see #doFindPathMatchingFileResources 484 * @see org.springframework.util.PathMatcher 485 */ 486 protected Resource[] findPathMatchingResources(String locationPattern) throws IOException { 487 String rootDirPath = determineRootDir(locationPattern); 488 String subPattern = locationPattern.substring(rootDirPath.length()); 489 Resource[] rootDirResources = getResources(rootDirPath); 490 Set<Resource> result = new LinkedHashSet<Resource>(16); 491 for (Resource rootDirResource : rootDirResources) { 492 rootDirResource = resolveRootDirResource(rootDirResource); 493 URL rootDirUrl = rootDirResource.getURL(); 494 if (equinoxResolveMethod != null) { 495 if (rootDirUrl.getProtocol().startsWith("bundle")) { 496 rootDirUrl = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirUrl); 497 rootDirResource = new UrlResource(rootDirUrl); 498 } 499 } 500 if (rootDirUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { 501 result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirUrl, subPattern, getPathMatcher())); 502 } 503 else if (ResourceUtils.isJarURL(rootDirUrl) || isJarResource(rootDirResource)) { 504 result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirUrl, subPattern)); 505 } 506 else { 507 result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern)); 508 } 509 } 510 if (logger.isDebugEnabled()) { 511 logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result); 512 } 513 return result.toArray(new Resource[result.size()]); 514 } 515 516 /** 517 * Determine the root directory for the given location. 518 * <p>Used for determining the starting point for file matching, 519 * resolving the root directory location to a {@code java.io.File} 520 * and passing it into {@code retrieveMatchingFiles}, with the 521 * remainder of the location as pattern. 522 * <p>Will return "/WEB-INF/" for the pattern "/WEB-INF/*.xml", 523 * for example. 524 * @param location the location to check 525 * @return the part of the location that denotes the root directory 526 * @see #retrieveMatchingFiles 527 */ 528 protected String determineRootDir(String location) { 529 int prefixEnd = location.indexOf(':') + 1; 530 int rootDirEnd = location.length(); 531 while (rootDirEnd > prefixEnd && getPathMatcher().isPattern(location.substring(prefixEnd, rootDirEnd))) { 532 rootDirEnd = location.lastIndexOf('/', rootDirEnd - 2) + 1; 533 } 534 if (rootDirEnd == 0) { 535 rootDirEnd = prefixEnd; 536 } 537 return location.substring(0, rootDirEnd); 538 } 539 540 /** 541 * Resolve the specified resource for path matching. 542 * <p>By default, Equinox OSGi "bundleresource:" / "bundleentry:" URL will be 543 * resolved into a standard jar file URL that be traversed using Spring's 544 * standard jar file traversal algorithm. For any preceding custom resolution, 545 * override this method and replace the resource handle accordingly. 546 * @param original the resource to resolve 547 * @return the resolved resource (may be identical to the passed-in resource) 548 * @throws IOException in case of resolution failure 549 */ 550 protected Resource resolveRootDirResource(Resource original) throws IOException { 551 return original; 552 } 553 554 /** 555 * Return whether the given resource handle indicates a jar resource 556 * that the {@code doFindPathMatchingJarResources} method can handle. 557 * <p>By default, the URL protocols "jar", "zip", "vfszip and "wsjar" 558 * will be treated as jar resources. This template method allows for 559 * detecting further kinds of jar-like resources, e.g. through 560 * {@code instanceof} checks on the resource handle type. 561 * @param resource the resource handle to check 562 * (usually the root directory to start path matching from) 563 * @see #doFindPathMatchingJarResources 564 * @see org.springframework.util.ResourceUtils#isJarURL 565 */ 566 protected boolean isJarResource(Resource resource) throws IOException { 567 return false; 568 } 569 570 /** 571 * Find all resources in jar files that match the given location pattern 572 * via the Ant-style PathMatcher. 573 * @param rootDirResource the root directory as Resource 574 * @param rootDirURL the pre-resolved root directory URL 575 * @param subPattern the sub pattern to match (below the root directory) 576 * @return a mutable Set of matching Resource instances 577 * @throws IOException in case of I/O errors 578 * @since 4.3 579 * @see java.net.JarURLConnection 580 * @see org.springframework.util.PathMatcher 581 */ 582 @SuppressWarnings("deprecation") 583 protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, URL rootDirURL, String subPattern) 584 throws IOException { 585 586 // Check deprecated variant for potential overriding first... 587 Set<Resource> result = doFindPathMatchingJarResources(rootDirResource, subPattern); 588 if (result != null) { 589 return result; 590 } 591 592 URLConnection con = rootDirURL.openConnection(); 593 JarFile jarFile; 594 String jarFileUrl; 595 String rootEntryPath; 596 boolean closeJarFile; 597 598 if (con instanceof JarURLConnection) { 599 // Should usually be the case for traditional JAR files. 600 JarURLConnection jarCon = (JarURLConnection) con; 601 ResourceUtils.useCachesIfNecessary(jarCon); 602 jarFile = jarCon.getJarFile(); 603 jarFileUrl = jarCon.getJarFileURL().toExternalForm(); 604 JarEntry jarEntry = jarCon.getJarEntry(); 605 rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); 606 closeJarFile = !jarCon.getUseCaches(); 607 } 608 else { 609 // No JarURLConnection -> need to resort to URL file parsing. 610 // We'll assume URLs of the format "jar:path!/entry", with the protocol 611 // being arbitrary as long as following the entry format. 612 // We'll also handle paths with and without leading "file:" prefix. 613 String urlFile = rootDirURL.getFile(); 614 try { 615 int separatorIndex = urlFile.indexOf(ResourceUtils.WAR_URL_SEPARATOR); 616 if (separatorIndex == -1) { 617 separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR); 618 } 619 if (separatorIndex != -1) { 620 jarFileUrl = urlFile.substring(0, separatorIndex); 621 rootEntryPath = urlFile.substring(separatorIndex + 2); // both separators are 2 chars 622 jarFile = getJarFile(jarFileUrl); 623 } 624 else { 625 jarFile = new JarFile(urlFile); 626 jarFileUrl = urlFile; 627 rootEntryPath = ""; 628 } 629 closeJarFile = true; 630 } 631 catch (ZipException ex) { 632 if (logger.isDebugEnabled()) { 633 logger.debug("Skipping invalid jar classpath entry [" + urlFile + "]"); 634 } 635 return Collections.emptySet(); 636 } 637 } 638 639 try { 640 if (logger.isDebugEnabled()) { 641 logger.debug("Looking for matching resources in jar file [" + jarFileUrl + "]"); 642 } 643 if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) { 644 // Root entry path must end with slash to allow for proper matching. 645 // The Sun JRE does not return a slash here, but BEA JRockit does. 646 rootEntryPath = rootEntryPath + "/"; 647 } 648 result = new LinkedHashSet<Resource>(8); 649 for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) { 650 JarEntry entry = entries.nextElement(); 651 String entryPath = entry.getName(); 652 if (entryPath.startsWith(rootEntryPath)) { 653 String relativePath = entryPath.substring(rootEntryPath.length()); 654 if (getPathMatcher().match(subPattern, relativePath)) { 655 result.add(rootDirResource.createRelative(relativePath)); 656 } 657 } 658 } 659 return result; 660 } 661 finally { 662 if (closeJarFile) { 663 jarFile.close(); 664 } 665 } 666 } 667 668 /** 669 * Find all resources in jar files that match the given location pattern 670 * via the Ant-style PathMatcher. 671 * @param rootDirResource the root directory as Resource 672 * @param subPattern the sub pattern to match (below the root directory) 673 * @return a mutable Set of matching Resource instances 674 * @throws IOException in case of I/O errors 675 * @deprecated as of Spring 4.3, in favor of 676 * {@link #doFindPathMatchingJarResources(Resource, URL, String)} 677 */ 678 @Deprecated 679 protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, String subPattern) 680 throws IOException { 681 682 return null; 683 } 684 685 /** 686 * Resolve the given jar file URL into a JarFile object. 687 */ 688 protected JarFile getJarFile(String jarFileUrl) throws IOException { 689 if (jarFileUrl.startsWith(ResourceUtils.FILE_URL_PREFIX)) { 690 try { 691 return new JarFile(ResourceUtils.toURI(jarFileUrl).getSchemeSpecificPart()); 692 } 693 catch (URISyntaxException ex) { 694 // Fallback for URLs that are not valid URIs (should hardly ever happen). 695 return new JarFile(jarFileUrl.substring(ResourceUtils.FILE_URL_PREFIX.length())); 696 } 697 } 698 else { 699 return new JarFile(jarFileUrl); 700 } 701 } 702 703 /** 704 * Find all resources in the file system that match the given location pattern 705 * via the Ant-style PathMatcher. 706 * @param rootDirResource the root directory as Resource 707 * @param subPattern the sub pattern to match (below the root directory) 708 * @return a mutable Set of matching Resource instances 709 * @throws IOException in case of I/O errors 710 * @see #retrieveMatchingFiles 711 * @see org.springframework.util.PathMatcher 712 */ 713 protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) 714 throws IOException { 715 716 File rootDir; 717 try { 718 rootDir = rootDirResource.getFile().getAbsoluteFile(); 719 } 720 catch (FileNotFoundException ex) { 721 if (logger.isInfoEnabled()) { 722 logger.info("Cannot search for matching files underneath " + rootDirResource + 723 " in the file system: " + ex.getMessage()); 724 } 725 return Collections.emptySet(); 726 } 727 catch (Exception ex) { 728 if (logger.isWarnEnabled()) { 729 logger.warn("Failed to resolve " + rootDirResource + " in the file system: " + ex); 730 } 731 return Collections.emptySet(); 732 } 733 return doFindMatchingFileSystemResources(rootDir, subPattern); 734 } 735 736 /** 737 * Find all resources in the file system that match the given location pattern 738 * via the Ant-style PathMatcher. 739 * @param rootDir the root directory in the file system 740 * @param subPattern the sub pattern to match (below the root directory) 741 * @return a mutable Set of matching Resource instances 742 * @throws IOException in case of I/O errors 743 * @see #retrieveMatchingFiles 744 * @see org.springframework.util.PathMatcher 745 */ 746 protected Set<Resource> doFindMatchingFileSystemResources(File rootDir, String subPattern) throws IOException { 747 if (logger.isDebugEnabled()) { 748 logger.debug("Looking for matching resources in directory tree [" + rootDir.getPath() + "]"); 749 } 750 Set<File> matchingFiles = retrieveMatchingFiles(rootDir, subPattern); 751 Set<Resource> result = new LinkedHashSet<Resource>(matchingFiles.size()); 752 for (File file : matchingFiles) { 753 result.add(new FileSystemResource(file)); 754 } 755 return result; 756 } 757 758 /** 759 * Retrieve files that match the given path pattern, 760 * checking the given directory and its subdirectories. 761 * @param rootDir the directory to start from 762 * @param pattern the pattern to match against, 763 * relative to the root directory 764 * @return a mutable Set of matching Resource instances 765 * @throws IOException if directory contents could not be retrieved 766 */ 767 protected Set<File> retrieveMatchingFiles(File rootDir, String pattern) throws IOException { 768 if (!rootDir.exists()) { 769 // Silently skip non-existing directories. 770 if (logger.isDebugEnabled()) { 771 logger.debug("Skipping [" + rootDir.getAbsolutePath() + "] because it does not exist"); 772 } 773 return Collections.emptySet(); 774 } 775 if (!rootDir.isDirectory()) { 776 // Complain louder if it exists but is no directory. 777 if (logger.isWarnEnabled()) { 778 logger.warn("Skipping [" + rootDir.getAbsolutePath() + "] because it does not denote a directory"); 779 } 780 return Collections.emptySet(); 781 } 782 if (!rootDir.canRead()) { 783 if (logger.isWarnEnabled()) { 784 logger.warn("Cannot search for matching files underneath directory [" + rootDir.getAbsolutePath() + 785 "] because the application is not allowed to read the directory"); 786 } 787 return Collections.emptySet(); 788 } 789 String fullPattern = StringUtils.replace(rootDir.getAbsolutePath(), File.separator, "/"); 790 if (!pattern.startsWith("/")) { 791 fullPattern += "/"; 792 } 793 fullPattern = fullPattern + StringUtils.replace(pattern, File.separator, "/"); 794 Set<File> result = new LinkedHashSet<File>(8); 795 doRetrieveMatchingFiles(fullPattern, rootDir, result); 796 return result; 797 } 798 799 /** 800 * Recursively retrieve files that match the given pattern, 801 * adding them to the given result list. 802 * @param fullPattern the pattern to match against, 803 * with prepended root directory path 804 * @param dir the current directory 805 * @param result the Set of matching File instances to add to 806 * @throws IOException if directory contents could not be retrieved 807 */ 808 protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> result) throws IOException { 809 if (logger.isDebugEnabled()) { 810 logger.debug("Searching directory [" + dir.getAbsolutePath() + 811 "] for files matching pattern [" + fullPattern + "]"); 812 } 813 File[] dirContents = dir.listFiles(); 814 if (dirContents == null) { 815 if (logger.isWarnEnabled()) { 816 logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]"); 817 } 818 return; 819 } 820 Arrays.sort(dirContents); 821 for (File content : dirContents) { 822 String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/"); 823 if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) { 824 if (!content.canRead()) { 825 if (logger.isDebugEnabled()) { 826 logger.debug("Skipping subdirectory [" + dir.getAbsolutePath() + 827 "] because the application is not allowed to read the directory"); 828 } 829 } 830 else { 831 doRetrieveMatchingFiles(fullPattern, content, result); 832 } 833 } 834 if (getPathMatcher().match(fullPattern, currPath)) { 835 result.add(content); 836 } 837 } 838 } 839 840 841 /** 842 * Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime. 843 */ 844 private static class VfsResourceMatchingDelegate { 845 846 public static Set<Resource> findMatchingResources( 847 URL rootDirURL, String locationPattern, PathMatcher pathMatcher) throws IOException { 848 849 Object root = VfsPatternUtils.findRoot(rootDirURL); 850 PatternVirtualFileVisitor visitor = 851 new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root), locationPattern, pathMatcher); 852 VfsPatternUtils.visit(root, visitor); 853 return visitor.getResources(); 854 } 855 } 856 857 858 /** 859 * VFS visitor for path matching purposes. 860 */ 861 @SuppressWarnings("unused") 862 private static class PatternVirtualFileVisitor implements InvocationHandler { 863 864 private final String subPattern; 865 866 private final PathMatcher pathMatcher; 867 868 private final String rootPath; 869 870 private final Set<Resource> resources = new LinkedHashSet<Resource>(); 871 872 public PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher pathMatcher) { 873 this.subPattern = subPattern; 874 this.pathMatcher = pathMatcher; 875 this.rootPath = (rootPath.isEmpty() || rootPath.endsWith("/") ? rootPath : rootPath + "/"); 876 } 877 878 @Override 879 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 880 String methodName = method.getName(); 881 if (Object.class == method.getDeclaringClass()) { 882 if (methodName.equals("equals")) { 883 // Only consider equal when proxies are identical. 884 return (proxy == args[0]); 885 } 886 else if (methodName.equals("hashCode")) { 887 return System.identityHashCode(proxy); 888 } 889 } 890 else if ("getAttributes".equals(methodName)) { 891 return getAttributes(); 892 } 893 else if ("visit".equals(methodName)) { 894 visit(args[0]); 895 return null; 896 } 897 else if ("toString".equals(methodName)) { 898 return toString(); 899 } 900 901 throw new IllegalStateException("Unexpected method invocation: " + method); 902 } 903 904 public void visit(Object vfsResource) { 905 if (this.pathMatcher.match(this.subPattern, 906 VfsPatternUtils.getPath(vfsResource).substring(this.rootPath.length()))) { 907 this.resources.add(new VfsResource(vfsResource)); 908 } 909 } 910 911 public Object getAttributes() { 912 return VfsPatternUtils.getVisitorAttribute(); 913 } 914 915 public Set<Resource> getResources() { 916 return this.resources; 917 } 918 919 public int size() { 920 return this.resources.size(); 921 } 922 923 @Override 924 public String toString() { 925 return "sub-pattern: " + this.subPattern + ", resources: " + this.resources; 926 } 927 } 928 929}