eap.filter
Class FilterBroker

java.lang.Object
  extended byeap.filter.FilterBroker

public class FilterBroker
extends Object

Maintains a registry of filters which it can apply to a data stream based on file name extensions. Filter classes are not loaded until they are needed, so there is little overhead for registering a large number of filters which will not be used. In fact a filter can be registered for which the implementation class bytecode is not available.


Constructor Summary
FilterBroker()
          Creates a new FilterBroker with no filters registered.
 
Method Summary
 void addDefaultFilters()
          Equivalent to calling addDefaultFilters(eap.filter.FilterBroker.class)
 void addDefaultFilters(Class c)
          Add the default filters listed in the "filter.properties" file which can be found in the same directory as the given class's .class file.
 void addFilter(String extension, String classname, String name)
          Add a filter with the given properties to the registry.
 void addFilters(Properties properties)
          Add the filters defined in a set of properties.
 InputStream filter(InputStream in, String filename)
          Get a filtered input stream appropriate for a filename.
 String getLastFileName()
          Accesses the file name in the last call to filter(InputStream, String) with all the filtered extensions removed.
 boolean hasFilter(String filename)
          Returns true if there is a filter registered for a given filename.
static void main(String[] args)
          Main method mostly useful for testing.
 void setPasswordProvider(PasswordProvider password)
          Sets the class which will be used to obtain a single string of metadata needed to create a filter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FilterBroker

public FilterBroker()
Creates a new FilterBroker with no filters registered.

Method Detail

setPasswordProvider

public void setPasswordProvider(PasswordProvider password)
Sets the class which will be used to obtain a single string of metadata needed to create a filter. Currently this is only used for decryption filters, but it could be used to input other sorts of parameters. Calling this method unregisters any previously registered password provider.

Parameters:
password - The password provider to be used. If this is null, then no password provider will be registered.

addDefaultFilters

public void addDefaultFilters()
                       throws IOException
Equivalent to calling addDefaultFilters(eap.filter.FilterBroker.class)

Throws:
IOException

addDefaultFilters

public void addDefaultFilters(Class c)
                       throws IOException
Add the default filters listed in the "filter.properties" file which can be found in the same directory as the given class's .class file.

Parameters:
c - The class whose class loader will be used to find the filter.properties file.
Throws:
IOException
See Also:
addFilters(Properties)

addFilters

public void addFilters(Properties properties)
Add the filters defined in a set of properties. Each filter should have a pair of keywords "extenn" and "classn". The first gives the filename extension corresponding for the filter and the second gives the name of the class which implements the filter. The value of n is 0, 1, 2, ... In other words it is an index counting from zero and not skipping any integers.

Parameters:
properties - the set of properties defining a set of filters

addFilter

public void addFilter(String extension,
                      String classname,
                      String name)
Add a filter with the given properties to the registry.

Parameters:
extension - The file name extension (e.g. ".gz") which indicates that this filter should be applied
classname - The name of the java class which implements the filter. A filter class must be a subclass of java.io.InputStream, and have a constructor: Class(java.io.InputStream), or Class(java.io.InputStream, String).
name - a human-readable name for this filter.

hasFilter

public boolean hasFilter(String filename)
Returns true if there is a filter registered for a given filename.

Parameters:
filename - the filename to examine.
Returns:
true if there is a filter registerd which would be appropriate for the given file name.

filter

public InputStream filter(InputStream in,
                          String filename)
                   throws FilterException
Get a filtered input stream appropriate for a filename. This method checks the filename ending against those of the registered filters. If it finds a filter, it wraps the filter around the input stream. This is done recursively, so e.g. file.gz.pgp would get two filters, first PGP and then gzip. If no filters match the filename, this method returns the original input stream.

Parameters:
in - the input stream to be filtered
filename - the file name or URL used to decide what filter(s) to apply.
Returns:
The filtered input stream.
Throws:
FilterException

getLastFileName

public String getLastFileName()
Accesses the file name in the last call to filter(InputStream, String) with all the filtered extensions removed. This can be useful if you want to write the filtered version of a file. Note that this is not thread safe. Another thread could apply a filter after the current thread but before this method could be called.

Returns:
The filtered file name or null if filter(InputStream, String) has never been called for this object.

main

public static void main(String[] args)
                 throws Exception
Main method mostly useful for testing. Loads the default filters, and filters the file given as the first argument on the command line. Prints the results to stdout.

Throws:
Exception