View Javadoc

1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.core.io.support;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.lang.reflect.InvocationHandler;
22  import java.lang.reflect.Method;
23  import java.net.JarURLConnection;
24  import java.net.URISyntaxException;
25  import java.net.URL;
26  import java.net.URLConnection;
27  import java.util.Collections;
28  import java.util.Enumeration;
29  import java.util.LinkedHashSet;
30  import java.util.Set;
31  import java.util.jar.JarEntry;
32  import java.util.jar.JarFile;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import org.springframework.core.io.DefaultResourceLoader;
38  import org.springframework.core.io.FileSystemResource;
39  import org.springframework.core.io.Resource;
40  import org.springframework.core.io.ResourceLoader;
41  import org.springframework.core.io.UrlResource;
42  import org.springframework.core.io.VfsResource;
43  import org.springframework.util.AntPathMatcher;
44  import org.springframework.util.Assert;
45  import org.springframework.util.PathMatcher;
46  import org.springframework.util.ReflectionUtils;
47  import org.springframework.util.ResourceUtils;
48  import org.springframework.util.StringUtils;
49  
50  /**
51   * A {@link ResourcePatternResolver} implementation that is able to resolve a
52   * specified resource location path into one or more matching Resources.
53   * The source path may be a simple path which has a one-to-one mapping to a
54   * target {@link org.springframework.core.io.Resource}, or alternatively
55   * may contain the special "<code>classpath*:</code>" prefix and/or
56   * internal Ant-style regular expressions (matched using Spring's
57   * {@link org.springframework.util.AntPathMatcher} utility).
58   * Both of the latter are effectively wildcards.
59   *
60   * <p><b>No Wildcards:</b>
61   *
62   * <p>In the simple case, if the specified location path does not start with the
63   * <code>"classpath*:</code>" prefix, and does not contain a PathMatcher pattern,
64   * this resolver will simply return a single resource via a
65   * <code>getResource()</code> call on the underlying <code>ResourceLoader</code>.
66   * Examples are real URLs such as "<code>file:C:/context.xml</code>", pseudo-URLs
67   * such as "<code>classpath:/context.xml</code>", and simple unprefixed paths
68   * such as "<code>/WEB-INF/context.xml</code>". The latter will resolve in a
69   * fashion specific to the underlying <code>ResourceLoader</code> (e.g. 
70   * <code>ServletContextResource</code> for a <code>WebApplicationContext</code>).
71   *
72   * <p><b>Ant-style Patterns:</b>
73   *
74   * <p>When the path location contains an Ant-style pattern, e.g.:
75   * <pre>
76   * /WEB-INF/*-context.xml
77   * com/mycompany/**&#47;applicationContext.xml
78   * file:C:/some/path/*-context.xml
79   * classpath:com/mycompany/**&#47;applicationContext.xml</pre>
80   * the resolver follows a more complex but defined procedure to try to resolve
81   * the wildcard. It produces a <code>Resource</code> for the path up to the last
82   * non-wildcard segment and obtains a <code>URL</code> from it. If this URL is
83   * not a "<code>jar:</code>" URL or container-specific variant (e.g.
84   * "<code>zip:</code>" in WebLogic, "<code>wsjar</code>" in WebSphere", etc.),
85   * then a <code>java.io.File</code> is obtained from it, and used to resolve the
86   * wildcard by walking the filesystem. In the case of a jar URL, the resolver
87   * either gets a <code>java.net.JarURLConnection</code> from it, or manually parses
88   * the jar URL, and then traverses the contents of the jar file, to resolve the
89   * wildcards.
90   *
91   * <p><b>Implications on portability:</b>
92   *
93   * <p>If the specified path is already a file URL (either explicitly, or
94   * implicitly because the base <code>ResourceLoader</code> is a filesystem one,
95   * then wildcarding is guaranteed to work in a completely portable fashion.
96   *
97   * <p>If the specified path is a classpath location, then the resolver must
98   * obtain the last non-wildcard path segment URL via a
99   * <code>Classloader.getResource()</code> call. Since this is just a
100  * node of the path (not the file at the end) it is actually undefined
101  * (in the ClassLoader Javadocs) exactly what sort of a URL is returned in
102  * this case. In practice, it is usually a <code>java.io.File</code> representing
103  * the directory, where the classpath resource resolves to a filesystem
104  * location, or a jar URL of some sort, where the classpath resource resolves
105  * to a jar location. Still, there is a portability concern on this operation.
106  *
107  * <p>If a jar URL is obtained for the last non-wildcard segment, the resolver
108  * must be able to get a <code>java.net.JarURLConnection</code> from it, or
109  * manually parse the jar URL, to be able to walk the contents of the jar,
110  * and resolve the wildcard. This will work in most environments, but will
111  * fail in others, and it is strongly recommended that the wildcard
112  * resolution of resources coming from jars be thoroughly tested in your
113  * specific environment before you rely on it.
114  *
115  * <p><b><code>classpath*:</code> Prefix:</b>
116  *
117  * <p>There is special support for retrieving multiple class path resources with
118  * the same name, via the "<code>classpath*:</code>" prefix. For example,
119  * "<code>classpath*:META-INF/beans.xml</code>" will find all "beans.xml"
120  * files in the class path, be it in "classes" directories or in JAR files.
121  * This is particularly useful for autodetecting config files of the same name
122  * at the same location within each jar file. Internally, this happens via a
123  * <code>ClassLoader.getResources()</code> call, and is completely portable.
124  *
125  * <p>The "classpath*:" prefix can also be combined with a PathMatcher pattern in
126  * the rest of the location path, for example "classpath*:META-INF/*-beans.xml".
127  * In this case, the resolution strategy is fairly simple: a
128  * <code>ClassLoader.getResources()</code> call is used on the last non-wildcard
129  * path segment to get all the matching resources in the class loader hierarchy,
130  * and then off each resource the same PathMatcher resolution strategy described
131  * above is used for the wildcard subpath.
132  *
133  * <p><b>Other notes:</b>
134  *
135  * <p><b>WARNING:</b> Note that "<code>classpath*:</code>" when combined with
136  * Ant-style patterns will only work reliably with at least one root directory
137  * before the pattern starts, unless the actual target files reside in the file
138  * system. This means that a pattern like "<code>classpath*:*.xml</code>" will
139  * <i>not</i> retrieve files from the root of jar files but rather only from the
140  * root of expanded directories. This originates from a limitation in the JDK's
141  * <code>ClassLoader.getResources()</code> method which only returns file system
142  * locations for a passed-in empty String (indicating potential roots to search).
143  *
144  * <p><b>WARNING:</b> Ant-style patterns with "classpath:" resources are not
145  * guaranteed to find matching resources if the root package to search is available
146  * in multiple class path locations. This is because a resource such as<pre>
147  *     com/mycompany/package1/service-context.xml
148  * </pre>may be in only one location, but when a path such as<pre>
149  *     classpath:com/mycompany/**&#47;service-context.xml
150  * </pre>is used to try to resolve it, the resolver will work off the (first) URL 
151  * returned by <code>getResource("com/mycompany");</code>. If this base package
152  * node exists in multiple classloader locations, the actual end resource may
153  * not be underneath. Therefore, preferably, use "<code>classpath*:<code>" with the same
154  * Ant-style pattern in such a case, which will search <i>all</i> class path
155  * locations that contain the root package.
156  *
157  * @author Juergen Hoeller
158  * @author Colin Sampaleanu
159  * @author Marius Bogoevici
160  * @author Costin Leau
161  * @since 1.0.2
162  * @see #CLASSPATH_ALL_URL_PREFIX
163  * @see org.springframework.util.AntPathMatcher
164  * @see org.springframework.core.io.ResourceLoader#getResource(String)
165  * @see java.lang.ClassLoader#getResources(String)
166  */
167 public class PathMatchingResourcePatternResolver implements ResourcePatternResolver {
168 
169 	private static final Log logger = LogFactory.getLog(PathMatchingResourcePatternResolver.class);
170 
171 	private static Method equinoxResolveMethod;
172 
173 	static {
174 		// Detect Equinox OSGi (e.g. on WebSphere 6.1)
175 		try {
176 			Class<?> fileLocatorClass = PathMatchingResourcePatternResolver.class.getClassLoader().loadClass(
177 					"org.eclipse.core.runtime.FileLocator");
178 			equinoxResolveMethod = fileLocatorClass.getMethod("resolve", URL.class);
179 			logger.debug("Found Equinox FileLocator for OSGi bundle URL resolution");
180 		}
181 		catch (Throwable ex) {
182 			equinoxResolveMethod = null;
183 		}
184 	}
185 
186 
187 	private final ResourceLoader resourceLoader;
188 
189 	private PathMatcher pathMatcher = new AntPathMatcher();
190 
191 
192 	/**
193 	 * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.
194 	 * <p>ClassLoader access will happen via the thread context class loader.
195 	 * @see org.springframework.core.io.DefaultResourceLoader
196 	 */
197 	public PathMatchingResourcePatternResolver() {
198 		this.resourceLoader = new DefaultResourceLoader();
199 	}
200 
201 	/**
202 	 * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.
203 	 * @param classLoader the ClassLoader to load classpath resources with,
204 	 * or <code>null</code> for using the thread context class loader
205 	 * at the time of actual resource access
206 	 * @see org.springframework.core.io.DefaultResourceLoader
207 	 */
208 	public PathMatchingResourcePatternResolver(ClassLoader classLoader) {
209 		this.resourceLoader = new DefaultResourceLoader(classLoader);
210 	}
211 
212 	/**
213 	 * Create a new PathMatchingResourcePatternResolver.
214 	 * <p>ClassLoader access will happen via the thread context class loader.
215 	 * @param resourceLoader the ResourceLoader to load root directories and
216 	 * actual resources with
217 	 */
218 	public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
219 		Assert.notNull(resourceLoader, "ResourceLoader must not be null");
220 		this.resourceLoader = resourceLoader;
221 	}
222 
223 	/**
224 	 * Return the ResourceLoader that this pattern resolver works with.
225 	 */
226 	public ResourceLoader getResourceLoader() {
227 		return this.resourceLoader;
228 	}
229 
230 	/**
231 	 * Return the ClassLoader that this pattern resolver works with
232 	 * (never <code>null</code>).
233 	 */
234 	public ClassLoader getClassLoader() {
235 		return getResourceLoader().getClassLoader();
236 	}
237 
238 	/**
239 	 * Set the PathMatcher implementation to use for this
240 	 * resource pattern resolver. Default is AntPathMatcher.
241 	 * @see org.springframework.util.AntPathMatcher
242 	 */
243 	public void setPathMatcher(PathMatcher pathMatcher) {
244 		Assert.notNull(pathMatcher, "PathMatcher must not be null");
245 		this.pathMatcher = pathMatcher;
246 	}
247 
248 	/**
249 	 * Return the PathMatcher that this resource pattern resolver uses.
250 	 */
251 	public PathMatcher getPathMatcher() {
252 		return this.pathMatcher;
253 	}
254 
255 
256 	public Resource getResource(String location) {
257 		return getResourceLoader().getResource(location);
258 	}
259 
260 	public Resource[] getResources(String locationPattern) throws IOException {
261 		Assert.notNull(locationPattern, "Location pattern must not be null");
262 		if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
263 			// a class path resource (multiple resources for same name possible)
264 			if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
265 				// a class path resource pattern
266 				return findPathMatchingResources(locationPattern);
267 			}
268 			else {
269 				// all class path resources with the given name
270 				return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
271 			}
272 		}
273 		else {
274 			// Only look for a pattern after a prefix here
275 			// (to not get fooled by a pattern symbol in a strange prefix).
276 			int prefixEnd = locationPattern.indexOf(":") + 1;
277 			if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
278 				// a file pattern
279 				return findPathMatchingResources(locationPattern);
280 			}
281 			else {
282 				// a single resource with the given name
283 				return new Resource[] {getResourceLoader().getResource(locationPattern)};
284 			}
285 		}
286 	}
287 
288 	/**
289 	 * Find all class location resources with the given location via the ClassLoader.
290 	 * @param location the absolute path within the classpath
291 	 * @return the result as Resource array
292 	 * @throws IOException in case of I/O errors
293 	 * @see java.lang.ClassLoader#getResources
294 	 * @see #convertClassLoaderURL
295 	 */
296 	protected Resource[] findAllClassPathResources(String location) throws IOException {
297 		String path = location;
298 		if (path.startsWith("/")) {
299 			path = path.substring(1);
300 		}
301 		Enumeration<URL> resourceUrls = getClassLoader().getResources(path);
302 		Set<Resource> result = new LinkedHashSet<Resource>(16);
303 		while (resourceUrls.hasMoreElements()) {
304 			URL url = resourceUrls.nextElement();
305 			result.add(convertClassLoaderURL(url));
306 		}
307 		return result.toArray(new Resource[result.size()]);
308 	}
309 
310 	/**
311 	 * Convert the given URL as returned from the ClassLoader into a Resource object.
312 	 * <p>The default implementation simply creates a UrlResource instance.
313 	 * @param url a URL as returned from the ClassLoader
314 	 * @return the corresponding Resource object
315 	 * @see java.lang.ClassLoader#getResources
316 	 * @see org.springframework.core.io.Resource
317 	 */
318 	protected Resource convertClassLoaderURL(URL url) {
319 		return new UrlResource(url);
320 	}
321 
322 	/**
323 	 * Find all resources that match the given location pattern via the
324 	 * Ant-style PathMatcher. Supports resources in jar files and zip files
325 	 * and in the file system.
326 	 * @param locationPattern the location pattern to match
327 	 * @return the result as Resource array
328 	 * @throws IOException in case of I/O errors
329 	 * @see #doFindPathMatchingJarResources
330 	 * @see #doFindPathMatchingFileResources
331 	 * @see org.springframework.util.PathMatcher
332 	 */
333 	protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
334 		String rootDirPath = determineRootDir(locationPattern);
335 		String subPattern = locationPattern.substring(rootDirPath.length());
336 		Resource[] rootDirResources = getResources(rootDirPath);
337 		Set<Resource> result = new LinkedHashSet<Resource>(16);
338 		for (Resource rootDirResource : rootDirResources) {
339 			rootDirResource = resolveRootDirResource(rootDirResource);
340 			if (isJarResource(rootDirResource)) {
341 				result.addAll(doFindPathMatchingJarResources(rootDirResource, subPattern));
342 			}
343 			else if (rootDirResource.getURL().getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
344 				result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirResource, subPattern, getPathMatcher()));
345 			}
346 			else {
347 				result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));
348 			}
349 		}
350 		if (logger.isDebugEnabled()) {
351 			logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result);
352 		}
353 		return result.toArray(new Resource[result.size()]);
354 	}
355 
356 	/**
357 	 * Determine the root directory for the given location.
358 	 * <p>Used for determining the starting point for file matching,
359 	 * resolving the root directory location to a <code>java.io.File</code>
360 	 * and passing it into <code>retrieveMatchingFiles</code>, with the
361 	 * remainder of the location as pattern.
362 	 * <p>Will return "/WEB-INF/" for the pattern "/WEB-INF/*.xml",
363 	 * for example.
364 	 * @param location the location to check
365 	 * @return the part of the location that denotes the root directory
366 	 * @see #retrieveMatchingFiles
367 	 */
368 	protected String determineRootDir(String location) {
369 		int prefixEnd = location.indexOf(":") + 1;
370 		int rootDirEnd = location.length();
371 		while (rootDirEnd > prefixEnd && getPathMatcher().isPattern(location.substring(prefixEnd, rootDirEnd))) {
372 			rootDirEnd = location.lastIndexOf('/', rootDirEnd - 2) + 1;
373 		}
374 		if (rootDirEnd == 0) {
375 			rootDirEnd = prefixEnd;
376 		}
377 		return location.substring(0, rootDirEnd);
378 	}
379 
380 	/**
381 	 * Resolve the specified resource for path matching.
382 	 * <p>The default implementation detects an Equinox OSGi "bundleresource:"
383 	 * / "bundleentry:" URL and resolves it into a standard jar file URL that
384 	 * can be traversed using Spring's standard jar file traversal algorithm.
385 	 * @param original the resource to resolve
386 	 * @return the resolved resource (may be identical to the passed-in resource)
387 	 * @throws IOException in case of resolution failure
388 	 */
389 	protected Resource resolveRootDirResource(Resource original) throws IOException {
390 		if (equinoxResolveMethod != null) {
391 			URL url = original.getURL();
392 			if (url.getProtocol().startsWith("bundle")) {
393 				return new UrlResource((URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, url));
394 			}
395 		}
396 		return original;
397 	}
398 
399 	/**
400 	 * Return whether the given resource handle indicates a jar resource
401 	 * that the <code>doFindPathMatchingJarResources</code> method can handle.
402 	 * <p>The default implementation checks against the URL protocols
403 	 * "jar", "zip" and "wsjar" (the latter are used by BEA WebLogic Server
404 	 * and IBM WebSphere, respectively, but can be treated like jar files).
405 	 * @param resource the resource handle to check
406 	 * (usually the root directory to start path matching from)
407 	 * @see #doFindPathMatchingJarResources
408 	 * @see org.springframework.util.ResourceUtils#isJarURL
409 	 */
410 	protected boolean isJarResource(Resource resource) throws IOException {
411 		return ResourceUtils.isJarURL(resource.getURL());
412 	}
413 
414 	/**
415 	 * Find all resources in jar files that match the given location pattern
416 	 * via the Ant-style PathMatcher.
417 	 * @param rootDirResource the root directory as Resource
418 	 * @param subPattern the sub pattern to match (below the root directory)
419 	 * @return the Set of matching Resource instances
420 	 * @throws IOException in case of I/O errors
421 	 * @see java.net.JarURLConnection
422 	 * @see org.springframework.util.PathMatcher
423 	 */
424 	protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, String subPattern)
425 			throws IOException {
426 
427 		URLConnection con = rootDirResource.getURL().openConnection();
428 		JarFile jarFile;
429 		String jarFileUrl;
430 		String rootEntryPath;
431 		boolean newJarFile = false;
432 
433 		if (con instanceof JarURLConnection) {
434 			// Should usually be the case for traditional JAR files.
435 			JarURLConnection jarCon = (JarURLConnection) con;
436 			ResourceUtils.useCachesIfNecessary(jarCon);
437 			jarFile = jarCon.getJarFile();
438 			jarFileUrl = jarCon.getJarFileURL().toExternalForm();
439 			JarEntry jarEntry = jarCon.getJarEntry();
440 			rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
441 		}
442 		else {
443 			// No JarURLConnection -> need to resort to URL file parsing.
444 			// We'll assume URLs of the format "jar:path!/entry", with the protocol
445 			// being arbitrary as long as following the entry format.
446 			// We'll also handle paths with and without leading "file:" prefix.
447 			String urlFile = rootDirResource.getURL().getFile();
448 			int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
449 			if (separatorIndex != -1) {
450 				jarFileUrl = urlFile.substring(0, separatorIndex);
451 				rootEntryPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length());
452 				jarFile = getJarFile(jarFileUrl);
453 			}
454 			else {
455 				jarFile = new JarFile(urlFile);
456 				jarFileUrl = urlFile;
457 				rootEntryPath = "";
458 			}
459 			newJarFile = true;
460 		}
461 
462 		try {
463 			if (logger.isDebugEnabled()) {
464 				logger.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
465 			}
466 			if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
467 				// Root entry path must end with slash to allow for proper matching.
468 				// The Sun JRE does not return a slash here, but BEA JRockit does.
469 				rootEntryPath = rootEntryPath + "/";
470 			}
471 			Set<Resource> result = new LinkedHashSet<Resource>(8);
472 			for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
473 				JarEntry entry = entries.nextElement();
474 				String entryPath = entry.getName();
475 				if (entryPath.startsWith(rootEntryPath)) {
476 					String relativePath = entryPath.substring(rootEntryPath.length());
477 					if (getPathMatcher().match(subPattern, relativePath)) {
478 						result.add(rootDirResource.createRelative(relativePath));
479 					}
480 				}
481 			}
482 			return result;
483 		}
484 		finally {
485 			// Close jar file, but only if freshly obtained -
486 			// not from JarURLConnection, which might cache the file reference.
487 			if (newJarFile) {
488 				jarFile.close();
489 			}
490 		}
491 	}
492 
493 	/**
494 	 * Resolve the given jar file URL into a JarFile object.
495 	 */
496 	protected JarFile getJarFile(String jarFileUrl) throws IOException {
497 		if (jarFileUrl.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
498 			try {
499 				return new JarFile(ResourceUtils.toURI(jarFileUrl).getSchemeSpecificPart());
500 			}
501 			catch (URISyntaxException ex) {
502 				// Fallback for URLs that are not valid URIs (should hardly ever happen).
503 				return new JarFile(jarFileUrl.substring(ResourceUtils.FILE_URL_PREFIX.length()));
504 			}
505 		}
506 		else {
507 			return new JarFile(jarFileUrl);
508 		}
509 	}
510 
511 	/**
512 	 * Find all resources in the file system that match the given location pattern
513 	 * via the Ant-style PathMatcher.
514 	 * @param rootDirResource the root directory as Resource
515 	 * @param subPattern the sub pattern to match (below the root directory)
516 	 * @return the Set of matching Resource instances
517 	 * @throws IOException in case of I/O errors
518 	 * @see #retrieveMatchingFiles
519 	 * @see org.springframework.util.PathMatcher
520 	 */
521 	protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern)
522 			throws IOException {
523 
524 		File rootDir;
525 		try {
526 			rootDir = rootDirResource.getFile().getAbsoluteFile();
527 		}
528 		catch (IOException ex) {
529 			if (logger.isWarnEnabled()) {
530 				logger.warn("Cannot search for matching files underneath " + rootDirResource +
531 						" because it does not correspond to a directory in the file system", ex);
532 			}
533 			return Collections.emptySet();
534 		}
535 		return doFindMatchingFileSystemResources(rootDir, subPattern);
536 	}
537 
538 	/**
539 	 * Find all resources in the file system that match the given location pattern
540 	 * via the Ant-style PathMatcher.
541 	 * @param rootDir the root directory in the file system
542 	 * @param subPattern the sub pattern to match (below the root directory)
543 	 * @return the Set of matching Resource instances
544 	 * @throws IOException in case of I/O errors
545 	 * @see #retrieveMatchingFiles
546 	 * @see org.springframework.util.PathMatcher
547 	 */
548 	protected Set<Resource> doFindMatchingFileSystemResources(File rootDir, String subPattern) throws IOException {
549 		if (logger.isDebugEnabled()) {
550 			logger.debug("Looking for matching resources in directory tree [" + rootDir.getPath() + "]");
551 		}
552 		Set<File> matchingFiles = retrieveMatchingFiles(rootDir, subPattern);
553 		Set<Resource> result = new LinkedHashSet<Resource>(matchingFiles.size());
554 		for (File file : matchingFiles) {
555 			result.add(new FileSystemResource(file));
556 		}
557 		return result;
558 	}
559 
560 	/**
561 	 * Retrieve files that match the given path pattern,
562 	 * checking the given directory and its subdirectories.
563 	 * @param rootDir the directory to start from
564 	 * @param pattern the pattern to match against,
565 	 * relative to the root directory
566 	 * @return the Set of matching File instances
567 	 * @throws IOException if directory contents could not be retrieved
568 	 */
569 	protected Set<File> retrieveMatchingFiles(File rootDir, String pattern) throws IOException {
570 		if (!rootDir.exists()) {
571 			// Silently skip non-existing directories.
572 			if (logger.isDebugEnabled()) {
573 				logger.debug("Skipping [" + rootDir.getAbsolutePath() + "] because it does not exist");
574 			}
575 			return Collections.emptySet();
576 		}
577 		if (!rootDir.isDirectory()) {
578 			// Complain louder if it exists but is no directory.
579 			if (logger.isWarnEnabled()) {
580 				logger.warn("Skipping [" + rootDir.getAbsolutePath() + "] because it does not denote a directory");
581 			}
582 			return Collections.emptySet();
583 		}
584 		if (!rootDir.canRead()) {
585 			if (logger.isWarnEnabled()) {
586 				logger.warn("Cannot search for matching files underneath directory [" + rootDir.getAbsolutePath() +
587 						"] because the application is not allowed to read the directory");
588 			}
589 			return Collections.emptySet();
590 		}
591 		String fullPattern = StringUtils.replace(rootDir.getAbsolutePath(), File.separator, "/");
592 		if (!pattern.startsWith("/")) {
593 			fullPattern += "/";
594 		}
595 		fullPattern = fullPattern + StringUtils.replace(pattern, File.separator, "/");
596 		Set<File> result = new LinkedHashSet<File>(8);
597 		doRetrieveMatchingFiles(fullPattern, rootDir, result);
598 		return result;
599 	}
600 
601 	/**
602 	 * Recursively retrieve files that match the given pattern,
603 	 * adding them to the given result list.
604 	 * @param fullPattern the pattern to match against,
605 	 * with prepended root directory path
606 	 * @param dir the current directory
607 	 * @param result the Set of matching File instances to add to
608 	 * @throws IOException if directory contents could not be retrieved
609 	 */
610 	protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> result) throws IOException {
611 		if (logger.isDebugEnabled()) {
612 			logger.debug("Searching directory [" + dir.getAbsolutePath() +
613 					"] for files matching pattern [" + fullPattern + "]");
614 		}
615 		File[] dirContents = dir.listFiles();
616 		if (dirContents == null) {
617 			if (logger.isWarnEnabled()) {
618 				logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
619 			}
620 			return;
621 		}
622 		for (File content : dirContents) {
623 			String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
624 			if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
625 				if (!content.canRead()) {
626 					if (logger.isDebugEnabled()) {
627 						logger.debug("Skipping subdirectory [" + dir.getAbsolutePath() +
628 								"] because the application is not allowed to read the directory");
629 					}
630 				}
631 				else {
632 					doRetrieveMatchingFiles(fullPattern, content, result);
633 				}
634 			}
635 			if (getPathMatcher().match(fullPattern, currPath)) {
636 				result.add(content);
637 			}
638 		}
639 	}
640 
641 
642 	/**
643 	 * Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.
644 	 */
645 	private static class VfsResourceMatchingDelegate {
646 
647 		public static Set<Resource> findMatchingResources(
648 				Resource rootResource, String locationPattern, PathMatcher pathMatcher) throws IOException {
649 			Object root = VfsPatternUtils.findRoot(rootResource.getURL());
650 			PatternVirtualFileVisitor visitor =
651 					new PatternVirtualFileVisitor(VfsPatternUtils.getPath(root), locationPattern, pathMatcher);
652 			VfsPatternUtils.visit(root, visitor);
653 			return visitor.getResources();
654 		}
655 	}
656 
657 
658 	/**
659 	 * VFS visitor for path matching purposes.
660 	 */
661 	private static class PatternVirtualFileVisitor implements InvocationHandler {
662 
663 		private final String subPattern;
664 
665 		private final PathMatcher pathMatcher;
666 
667 		private final String rootPath;
668 
669 		private final Set<Resource> resources = new LinkedHashSet<Resource>();
670 
671 		public PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher pathMatcher) {
672 			this.subPattern = subPattern;
673 			this.pathMatcher = pathMatcher;
674 			this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/");
675 		}
676 
677 		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
678 			String methodName = method.getName();
679 			if (Object.class.equals(method.getDeclaringClass())) {
680 				if (methodName.equals("equals")) {
681 					// Only consider equal when proxies are identical.
682 					return (proxy == args[0]);
683 				}
684 				else if (methodName.equals("hashCode")) {
685 					return System.identityHashCode(proxy);
686 				}
687 			}
688 			else if ("getAttributes".equals(methodName)) {
689 				return getAttributes();
690 			}
691 			else if ("visit".equals(methodName)) {
692 				visit(args[0]);
693 				return null;
694 			}
695 			else if ("toString".equals(methodName)) {
696 				return toString();
697 			}
698 			
699 			throw new IllegalStateException("Unexpected method invocation: " + method);
700 		}
701 
702 		public void visit(Object vfsResource) {
703 			if (this.pathMatcher.match(this.subPattern,
704 					VfsPatternUtils.getPath(vfsResource).substring(this.rootPath.length()))) {
705 				this.resources.add(new VfsResource(vfsResource));
706 			}
707 		}
708 
709 		public Object getAttributes() {
710 			return VfsPatternUtils.getVisitorAttribute();
711 		}
712 
713 		public Set<Resource> getResources() {
714 			return this.resources;
715 		}
716 
717 		@SuppressWarnings("unused")
718 		public int size() {
719 			return this.resources.size();
720 		}
721 
722 		public String toString() {
723 			StringBuilder sb = new StringBuilder();
724 			sb.append("sub-pattern: ").append(this.subPattern);
725 			sb.append(", resources: ").append(this.resources);
726 			return sb.toString();
727 		}
728 	}
729 
730 }