simple.http.load
Class PrefixResolver

java.lang.Object
  extended by simple.util.xml.Traverser
      extended by simple.http.load.PrefixResolver

public class PrefixResolver
extends Traverser

The PrefixResolver is used to extract service names and types from an XML configuration file. Each service name can be associated with match and prepare XML elements, which can be used to determine the URI targets used to locate the services and the properties that are be used to initialize the service.

This is used to implement a scheme similar to the Java Servlet context path mapping scheme. In this scheme a prefix path is used to resolve a Servlet, and the remaining path part is then used to acquire a resource relative the the Servlet context.

Author:
Niall Gallagher

Constructor Summary
PrefixResolver(Locator lookup)
          Constructor for the PrefixResolver.
PrefixResolver(Locator lookup, int max)
          Constructor for the PrefixResolver.
 
Method Summary
protected  void commit(Node node)
          This is used to commit any data that has been collected during the processing of an element node.
protected  void finish()
          Used to prepare the prefix paths so that they can be matched with relative URI paths quickly.
 java.lang.String getClass(java.lang.String name)
          Used to resolve the class name using a service name.
 Configuration getConfiguration(java.lang.String name)
          This method is used retrieve properties for a service by using the service name.
 java.lang.String getName(java.lang.String prefix)
          Used to resolve the service name using a path prefix.
 java.lang.String getPath(java.lang.String normal)
          Used to acquire the path relative to the prefix.
 java.lang.String getPrefix(java.lang.String normal)
          Used to get the prefix path for the given relative URI path, which must be normalized.
protected  void process(Node node)
          This is used to process a element node extracted from the XML document.
protected  void start()
          This method is used to initialize this resolver.
 
Methods inherited from class simple.util.xml.Traverser
parse, parse, parse, parse, parse, parse, parse, parse, traverse, traverse
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrefixResolver

public PrefixResolver(Locator lookup)
Constructor for the PrefixResolver. This uses a configuration file located with the Locator object supplied. Once the configuration file is located the service names can be resolved for arbitrary URI paths.

Parameters:
lookup - the locator used to find the configuration

PrefixResolver

public PrefixResolver(Locator lookup,
                      int max)
Constructor for the PrefixResolver. This uses a configuration file located with the Locator object supplied. Once the configuration file is located the service names can be resolved for arbitrary URI paths.

This includes a parameter that enables a maximum expected path length to be entered. This helps to optimize the resolution of a path prefix. This should typically be at least big enough to include the maximum possible path.

Parameters:
lookup - the object used to perform configuration
max - this is the maximum path length expected
Method Detail

getConfiguration

public Configuration getConfiguration(java.lang.String name)
This method is used retrieve properties for a service by using the service name. This will acquire the properties if any for the named service instance. The properties will contain zero or more name value pairs. If no properties are associated with the service the instance returned will be an empty map rather than a null object.

Parameters:
name - this is the name of the service instance
Returns:
returns a properties object for configuration

getClass

public java.lang.String getClass(java.lang.String name)
Used to resolve the class name using a service name. This is required to resolve the class name once the service name has been acquired from the getName method. If there is no match for the service name then null is returned.

Parameters:
name - this is the service name to get a class name for
Returns:
the class name that matches the service name given

getName

public java.lang.String getName(java.lang.String prefix)
Used to resolve the service name using a path prefix. This is required to resolve the service name once the prefix of the path is acquired from the getPrefix method. If there is no match for the prefix then null is returned.

Parameters:
prefix - the path prefix to acquire a service name for
Returns:
the service name that matches the prefix path given

getPrefix

public java.lang.String getPrefix(java.lang.String normal)
Used to get the prefix path for the given relative URI path, which must be normalized. This will attempt to match the start of the given path to the highest directory path. For example, given the URI path /pub/bin/README, the start of the path will be compared for a prefix. So it should match /pub/bin/, /bin/, and finally / in that order.

Parameters:
normal - the normalized URI path to get a prefix for
Returns:
the highest matched directory for the given path

getPath

public java.lang.String getPath(java.lang.String normal)
Used to acquire the path relative to the prefix. This will return the path as it is relative to the prefix resolved for the given normalized path. This will remove the start of the given normalized path if it matches a prefix path.

Parameters:
normal - the normalized URI path to get a path for
Returns:
the full path once its prefix has been removed

start

protected void start()
This method is used to initialize this resolver. This will clear out all data structures used in the parsing process. It is invoked before the process method is used. to evaluate the element nodes extracted from the XML document.

Specified by:
start in class Traverser

process

protected void process(Node node)
This is used to process a element node extracted from the XML document. It will be given each element that exists within the document. The elements of interest to this implementation are the "property", "match", "section", and "service" elements.

Specified by:
process in class Traverser
Parameters:
node - this is the node to be evaluated by this method

commit

protected void commit(Node node)
This is used to commit any data that has been collected during the processing of an element node. The elements of interest to this method are the "service" and "property" elements. This will save the properties collected for each "service" element.

Overrides:
commit in class Traverser
Parameters:
node - this is the node to be committed by this method

finish

protected void finish()
Used to prepare the prefix paths so that they can be matched with relative URI paths quickly. The XML configuration file used to specify the prefix paths with the service and class names will be loaded unordered into a HashMap. This ensures the acquired keys are sorted for searching.

Overrides:
finish in class Traverser