/org.springframework.core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

https://github.com/michael-45/spring-framework · Java · 730 lines · 372 code · 56 blank · 302 comment · 71 complexity · cc829b194a22644ec289eda9a36fe46d MD5 · raw file

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