org.springframework.core.io.support
Class PathMatchingResourcePatternResolver

java.lang.Object
  extended byorg.springframework.core.io.support.PathMatchingResourcePatternResolver
All Implemented Interfaces:
ResourcePatternResolver

public class PathMatchingResourcePatternResolver
extends java.lang.Object
implements ResourcePatternResolver

ResourcePatternResolver that applies Ant-style path matching, using Spring's PathMatcher class.

Locations can either be suitable for ResourceLoader.getResource (URLs like "file:C:/context.xml", pseudo-URLs like "classpath:/context.xml", relative file paths like "/WEB-INF/context.xml"), or Ant-style patterns like "/WEB-INF/*-context.xml".

In the pattern case, the locations have to be resolvable to java.io.File, to allow for searching though the specified directory tree. In particular, this will neither work with WAR files that are not expanded nor with class path resources in a JAR file.

There is special support for retrieving multiple class path resources with the same name, via the "classpath*" prefix. For example, "classpath*:/beans.xml" will find all beans.xml files in the class path, be it in "classes" directories or in JAR files. This is particularly useful for auto-detecting config files.

If neither given a PathMatcher pattern nor a "classpath*:" location, this resolver will return a single resource via the underlying ResourceLoader.

Since:
01.05.2004
Author:
Juergen Hoeller
See Also:
ResourcePatternResolver.CLASSPATH_URL_PREFIX, PathMatcher, ResourceLoader.getResource(java.lang.String)

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_URL_PREFIX
 
Constructor Summary
PathMatchingResourcePatternResolver()
          Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.
PathMatchingResourcePatternResolver(java.lang.ClassLoader classLoader)
          Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.
PathMatchingResourcePatternResolver(ResourceLoader resourceLoader)
          Create a new PathMatchingResourcePatternResolver.
PathMatchingResourcePatternResolver(ResourceLoader resourceLoader, java.lang.ClassLoader classLoader)
          Create a new PathMatchingResourcePatternResolver.
 
Method Summary
protected  java.lang.String determineRootDir(java.lang.String location)
          Determine the root directory for the given location.
protected  void doRetrieveMatchingFiles(java.lang.String fullPattern, java.io.File dir, java.util.List result)
          Recursively retrieve files that match the given pattern, adding them to the given result list.
protected  Resource[] findAllClassPathResources(java.lang.String location)
          Find all class location resources with the given location via the ClassLoader.
protected  Resource[] findPathMatchingFileResources(java.lang.String locationPattern)
          Find all file resources that match the given location pattern via the Ant-style PathMatcher utility.
 java.lang.ClassLoader getClassLoader()
          Return the ClassLoader that this pattern resolver works with, or null if using the thread context class loader on actual access (applying to the thread that does the "getResources" call).
 ResourceLoader getResourceLoader()
          Return the ResourceLoader that this pattern resolver works with.
 Resource[] getResources(java.lang.String locationPattern)
          Resolve the given location pattern into Resource objects.
protected  java.util.List retrieveMatchingFiles(java.io.File rootDir, java.lang.String pattern)
          Retrieve files that match the given path pattern, checking the given directory and its subdirectories.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

PathMatchingResourcePatternResolver

public PathMatchingResourcePatternResolver()
Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.

ClassLoader access will happen via the thread context class loader on actual access (applying to the thread that does the "getResources" call)

See Also:
DefaultResourceLoader

PathMatchingResourcePatternResolver

public PathMatchingResourcePatternResolver(java.lang.ClassLoader classLoader)
Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader.

Parameters:
classLoader - the ClassLoader to load classpath resources with, or null for using the thread context class loader on actual access (applying to the thread that does the "getResources" call)
See Also:
DefaultResourceLoader

PathMatchingResourcePatternResolver

public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader)
Create a new PathMatchingResourcePatternResolver.

ClassLoader access will happen via the thread context class loader on actual access (applying to the thread that does the "getResources" call)

Parameters:
resourceLoader - ResourceLoader to load root directories and actual resources with

PathMatchingResourcePatternResolver

public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader,
                                           java.lang.ClassLoader classLoader)
Create a new PathMatchingResourcePatternResolver.

Parameters:
resourceLoader - ResourceLoader to load root directories and actual resources with
classLoader - the ClassLoader to load classpath resources with, or null for using the thread context class loader on actual access (applying to the thread that does the "getResources" call)
Method Detail

getResourceLoader

public ResourceLoader getResourceLoader()
Return the ResourceLoader that this pattern resolver works with.


getClassLoader

public java.lang.ClassLoader getClassLoader()
Return the ClassLoader that this pattern resolver works with, or null if using the thread context class loader on actual access (applying to the thread that does the "getResources" call).


getResources

public Resource[] getResources(java.lang.String locationPattern)
                        throws java.io.IOException
Description copied from interface: ResourcePatternResolver
Resolve the given location pattern into Resource objects.

Specified by:
getResources in interface ResourcePatternResolver
Parameters:
locationPattern - the location pattern to resolve
Returns:
the corresponding Resource objects
Throws:
java.io.IOException - in case of I/O errors

findAllClassPathResources

protected Resource[] findAllClassPathResources(java.lang.String location)
                                        throws java.io.IOException
Find all class location resources with the given location via the ClassLoader.

Parameters:
location - the absolute path within the classpath
Returns:
the result as Resource array
Throws:
java.io.IOException - in case of I/O errors
See Also:
ClassLoader.getResources(java.lang.String)

findPathMatchingFileResources

protected Resource[] findPathMatchingFileResources(java.lang.String locationPattern)
                                            throws java.io.IOException
Find all file resources that match the given location pattern via the Ant-style PathMatcher utility.

Parameters:
locationPattern - the location pattern to match
Returns:
the result as Resource array
Throws:
java.io.IOException - in case of I/O errors
See Also:
PathMatcher

determineRootDir

protected java.lang.String determineRootDir(java.lang.String location)
Determine the root directory for the given location.

Used for determining the starting point for file matching, resolving the root directory location to a java.io.File and passing it into retrieveMatchingFiles, with the remainder of the location as pattern.

Will return "/WEB-INF" for the pattern "/WEB-INF/*.xml", for example.

Parameters:
location - the location to check
Returns:
the part of the location that denotes the root directory
See Also:
retrieveMatchingFiles(java.io.File, java.lang.String)

retrieveMatchingFiles

protected java.util.List retrieveMatchingFiles(java.io.File rootDir,
                                               java.lang.String pattern)
                                        throws java.io.IOException
Retrieve files that match the given path pattern, checking the given directory and its subdirectories.

Parameters:
rootDir - the directory to start from
pattern - the pattern to match against, relative to the root directory
Returns:
the List of matching File instances
Throws:
java.io.IOException - if directory contents could not be retrieved

doRetrieveMatchingFiles

protected void doRetrieveMatchingFiles(java.lang.String fullPattern,
                                       java.io.File dir,
                                       java.util.List result)
                                throws java.io.IOException
Recursively retrieve files that match the given pattern, adding them to the given result list.

Parameters:
fullPattern - the pattern to match against, with preprended root directory path
dir - the current directory
result - the list of matching files to add to
Throws:
java.io.IOException - if directory contents could not be retrieved


Copyright (C) 2003-2004 The Spring Framework Project.