simple.http.serve
Class FileLocator

java.lang.Object
  extended by simple.http.serve.FileLocator
All Implemented Interfaces:
Locator

public class FileLocator
extends java.lang.Object
implements Locator

The FileLocator provides an implementation of the Locator interface for locating files from a list of directories. This will use the list of directories to search for named files. The list of files is searched by increasing index so the first occurrence of a named file found in the search path will be used. This accepts the name of the file to be used, so for example if the file required was example.xml then the name alone should be supplied. If however path information is supplied it can be in a URI or platform specific format.

Author:
Niall Gallagher

Constructor Summary
FileLocator()
          Constructor for the FileLocator object.
FileLocator(java.io.File path)
          Constructor for the FileLocator object.
FileLocator(java.io.File[] list)
          Constructor for the FileLocator object.
 
Method Summary
 java.io.File getFile(java.lang.String name)
          This is used to produce a File object pointing to the location of the named resource.
 java.lang.String getLocation(java.lang.String name)
          This is used to discover the location of a resource using the name of the resource.
 java.util.Properties getProperties(java.lang.String name)
          This is used to produce a Properties object that contains the contents of the named Java properties file.
 java.net.URL getResource(java.lang.String name)
          This is provided so that a ClassLoader can be used to load the named resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileLocator

public FileLocator()
Constructor for the FileLocator object. This no argument constructor will contain an empty search path and as a result will only search for resources within the current working directory.


FileLocator

public FileLocator(java.io.File path)
Constructor for the FileLocator object. This constructor requires a single path for searching. The path is searched for the named resources and if found it is used, however if the resource is not found in the given path the current working directory is searched.

Parameters:
path - this is the path to search for the resources

FileLocator

public FileLocator(java.io.File[] list)
Constructor for the FileLocator object. This constructor requires a list of directories with which to search for resources. The resources are searched for in increasing index, so index zero is the first directory searched followed by index one and so on. This uses the current working directory if the search fails.

Parameters:
list - this is the list of files to search within
Method Detail

getLocation

public java.lang.String getLocation(java.lang.String name)
                             throws LocateException
This is used to discover the location of a resource using the name of the resource. This will return the location of the resource in a system specific path format. This mirrors the FileContext.getRealPath method, however it should be supplied with only the name of the resource with no path information, which keeps any code interacting with this portable across systems using different path formats.

Specified by:
getLocation in interface Locator
Parameters:
name - the name of the resource that is to be located
Returns:
the location of the resource in the system format
Throws:
LocateException - thrown if the named resource could not be found after exhausting all lookup means

getFile

public java.io.File getFile(java.lang.String name)
                     throws LocateException
This is used to produce a File object pointing to the location of the named resource. This method mirrors the FileContext.getFile method, however it should be supplied with only the name of the resource with no path information, which keeps any code interacting with this portable across systems using different path formats.

Specified by:
getFile in interface Locator
Parameters:
name - the name of the resource that is to be located
Returns:
a File referencing the named resource
Throws:
LocateException - thrown if the named resource could not be found after exhausting all lookup means

getProperties

public java.util.Properties getProperties(java.lang.String name)
                                   throws LocateException
This is used to produce a Properties object that contains the contents of the named Java properties file. This mirrors the FileContext.getProperties method, however it should be supplied with only the name of the resource with no path information, which keeps any code interacting with this portable across different platforms.

Specified by:
getProperties in interface Locator
Parameters:
name - the name of the resource that is to be located
Returns:
a Properties object for the resource
Throws:
LocateException - thrown if the named resource could not be found after exhausting all lookup means

getResource

public java.net.URL getResource(java.lang.String name)
                         throws LocateException
This is provided so that a ClassLoader can be used to load the named resource. This method enables the configuration information to be loaded from the specified class path, which enables the files to be stored within a JAR resource or at any desired location. This only needs the name of the resource, no path information is required, this keeps the code portable and simple.

Specified by:
getResource in interface Locator
Parameters:
name - the name of the resource that is to be located
Returns:
a URL referencing the named resource
Throws:
LocateException - thrown if the named resource could not be found after exhausting all lookup means