com.opensymphony.xwork.util
Class ResourceScanner

java.lang.Object
  extended by com.opensymphony.xwork.util.ResourceScanner

public class ResourceScanner
extends Object

Scans for resource that resides within classpath and jar file.

Typical usage would be :-

To scan for all resources (apple.txt) that lies in the root of a classpath or in the root of the jar file.

 
      // to scan for a resource (apple.txt) that lies in the root of a classpath or in
      // the root of the jar file.
      ResourceScanner resourceScanner = new ResourceScanner(
          new String[] { "" },  // an empty string indicate that the scanning should start at the 'root'
          MyMain.class
      );
      List ourResources = resourceScanner.scanForResources(
           new ResourceScanner.Filter() {
                 public boolean accept(URL resource) {
                     if (resource.getFile().endsWith("apple.txt")) {
                         return true;
                     }
                     return false;
                 }
           });

 
 
To scan for all resources that lies under the '/com/opensymphony/xwork/util' directory in the classpath, we could do
 
      List ourResources = new ResourceScanner(
              new String[] { "com/opensymphony/xwork/util/" }, // Note that it DOES NOT start with a '/' and MUST ends with '/'
              MyMain.class).scanForResources();
 
 

Version:
$Date$ $Id$
Author:
tmjee

Nested Class Summary
static interface ResourceScanner.Filter
           
 
Constructor Summary
ResourceScanner(String[] roots, Class callingClass)
          Create an instance of ResourceScanner, taking in arguments root - An array of String, defining the root we should start scanning for resources. callingClass - The class invoking methods on ResourceScanner
 
Method Summary
protected  URL[] getResources(String resourceName, Class callingClass)
          Returns an array of URL corresponding to resourceName.
 List loadResourcesFromClassPath(File rootFile, ResourceScanner.Filter filter, List resources)
           
 List loadResourcesFromJarFile(String jarFilePath, String root, File jarFile, ResourceScanner.Filter filter, List resources)
           
protected  List loadRootResourcesFromJarFile(String jarFilePath, File jarFile, ResourceScanner.Filter filter, List resources)
          Find resources (a List of URL) from a jar file that lies in the root of the jar file, applying ResourceScanner.Filter on each resources found.
 List scanForResources()
          Start scanning for the resources, assuming that all the resources under roots specified in the constructor is what we need.
 List scanForResources(ResourceScanner.Filter filter)
          Start scanning for the resources under roots as specified in the constructor applying filter(ResourceScanner.Filter) to the resources that we've found.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceScanner

public ResourceScanner(String[] roots,
                       Class callingClass)
Create an instance of ResourceScanner, taking in arguments

Parameters:
roots - An array of String, defining the root we should start scanning for resources.
callingClass - The class invoking methods on ResourceScanner
Method Detail

scanForResources

public List scanForResources()
                      throws IOException,
                             URISyntaxException
Start scanning for the resources, assuming that all the resources under roots specified in the constructor is what we need.

Returns:
List of URL.
Throws:
IOException
URISyntaxException

scanForResources

public List scanForResources(ResourceScanner.Filter filter)
                      throws IOException,
                             URISyntaxException
Start scanning for the resources under roots as specified in the constructor applying filter(ResourceScanner.Filter) to the resources that we've found.

Parameters:
filter -
Returns:
List of URL
Throws:
IOException
URISyntaxException

loadRootResourcesFromJarFile

protected List loadRootResourcesFromJarFile(String jarFilePath,
                                            File jarFile,
                                            ResourceScanner.Filter filter,
                                            List resources)
                                     throws IOException
Find resources (a List of URL) from a jar file that lies in the root of the jar file, applying ResourceScanner.Filter on each resources found.

Parameters:
jarFilePath -
jarFile -
filter -
resources -
Returns:
List of URL.
Throws:
IOException

loadResourcesFromJarFile

public List loadResourcesFromJarFile(String jarFilePath,
                                     String root,
                                     File jarFile,
                                     ResourceScanner.Filter filter,
                                     List resources)
                              throws IOException
Throws:
IOException

loadResourcesFromClassPath

public List loadResourcesFromClassPath(File rootFile,
                                       ResourceScanner.Filter filter,
                                       List resources)
                                throws URISyntaxException,
                                       MalformedURLException
Throws:
URISyntaxException
MalformedURLException

getResources

protected URL[] getResources(String resourceName,
                             Class callingClass)
                      throws IOException
Returns an array of URL corresponding to resourceName. Search for URLs using ClassLoader in the following precedence :-

Parameters:
resourceName -
callingClass -
Returns:
URL[]
Throws:
IOException

WebWork Project Page