magoffin.matt.meta
Class MetadataResourceFactoryManager

java.lang.Object
  extended by magoffin.matt.meta.MetadataResourceFactoryManager

public class MetadataResourceFactoryManager
extends java.lang.Object

A simple manager of MetadataResourceFactory instances.

This class manages finding MetadataResourceFactory implementations for files based on a classpath Properties resource. When isDisableSearchPath() is false (the default setting) then this class will search in the following locations:

  1. The path configured via getManagerProperties()
  2. smeta.properties
  3. META-INF/smeta.properties

If the isDisableSearchPath() is true then only the getManagerProperties() path will be used. The properties are searched until the first suitable key is found. The properties resource is loaded via the same ClassLoader that loaded this class, i.e. getClass().getClassLoader().getResource() is used. The properties file must have the format

smeta.factory.KEY = CLASS

where KEY is either a file suffix or a MIME type, and CLASS is a fully-qualified class name for a class that implements MetadataResourceFactory. This class must provide a default no-argument constructor. The class will be loaded in the same ClassLoader that loaded this class, i.e. getClass().getClassLoader().loadClass() is used.

Version:
$Revision: 3 $ $Date: 2008-12-27 08:09:28 +1300 (Sat, 27 Dec 2008) $
Author:
Matt Magoffin (spamsqr@msqr.us)

Field Summary
static java.lang.String CUSTOM_MANAGER_PROPERTIES
          A custom class-path properties file.
static java.lang.String DEFAULT_MANAGER_PROPERTIES
          The default class-path properties file.
static java.lang.String FACTORY_PROPERTY_KEY_PREFIX
          The prefix used for MetadataResourceFactory configuration properties.
 
Constructor Summary
MetadataResourceFactoryManager()
           
 
Method Summary
protected  MetadataResourceFactory findFactory(java.io.File file, java.lang.String key)
          Find a MetadataResourceFactory for a given file and key.
static MetadataResourceFactoryManager getDefaultManagerInstance()
          Get the default MetadataResourceFactoryManager instance.
 java.lang.String getManagerProperties()
          Get the resource path of the manager Properties resource.
 MetadataResourceFactory getMetadataResourceFactory(java.io.File file)
          Get a MetadataResourceFactory instance for a particular file.
 MetadataResourceFactory getMetadataResourceFactory(java.lang.String mime)
          Get a MetadataResourceFactory instance for a particular MIME type.
 boolean isDisableSearchPath()
          Return the search path flag.
 void setDisableSearchPath(boolean disableSearchPath)
          Set the search path flag.
 void setManagerProperties(java.lang.String managerProperties)
          Set the resource path of the manager Properties resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CUSTOM_MANAGER_PROPERTIES

public static final java.lang.String CUSTOM_MANAGER_PROPERTIES
A custom class-path properties file.

See Also:
Constant Field Values

DEFAULT_MANAGER_PROPERTIES

public static final java.lang.String DEFAULT_MANAGER_PROPERTIES
The default class-path properties file.

See Also:
Constant Field Values

FACTORY_PROPERTY_KEY_PREFIX

public static final java.lang.String FACTORY_PROPERTY_KEY_PREFIX
The prefix used for MetadataResourceFactory configuration properties.

See Also:
Constant Field Values
Constructor Detail

MetadataResourceFactoryManager

public MetadataResourceFactoryManager()
Method Detail

getDefaultManagerInstance

public static MetadataResourceFactoryManager getDefaultManagerInstance()
Get the default MetadataResourceFactoryManager instance.

Returns:
the default instance

getMetadataResourceFactory

public MetadataResourceFactory getMetadataResourceFactory(java.io.File file)
                                                   throws MetadataConfigurationException
Get a MetadataResourceFactory instance for a particular file.

The file suffix (anything after the last period in the file name) is used as the key to lookup the factory implementation to use.

Parameters:
file - the file
Returns:
the MetadataResourceFactory instance, or null if unable to find a suitable implementation
Throws:
MetadataConfigurationException - if unable to load configuration

getMetadataResourceFactory

public MetadataResourceFactory getMetadataResourceFactory(java.lang.String mime)
                                                   throws MetadataConfigurationException
Get a MetadataResourceFactory instance for a particular MIME type.

The MIME type itself is used as the key to lookup the factory implementation to use.

Parameters:
mime - the MIME type of the resource
Returns:
the MetadataResourceFactory instance, or null if unable to find a suitable implementation
Throws:
MetadataConfigurationException - if unable to load configuration

findFactory

protected MetadataResourceFactory findFactory(java.io.File file,
                                              java.lang.String key)
                                       throws MetadataConfigurationException
Find a MetadataResourceFactory for a given file and key.

This method will use a search path of up to three locations in the class path to look for a Properties file with sMeta configuration settings. The first configuration setting found will be used. The three locations are:

  1. The path configured via getManagerProperties()
  2. smeta.properties
  3. META-INF/smeta.properties

If the isDisableSearchPath() returns true then only the getManagerProperties() path will be used.

The Properties resource will be queried for a key in the form of smeta.factory.KEY where KEY is the value of the key parameter passed to this method. The property value must be the fully qualified class name of something that implements MetadataResourceFactory, which must provide a no-argument constructor to be instantiated by this class.

Extending classes can override this method to provide more functionality.

Parameters:
file - the File being queried (if available)
key - the key to look up, eg. a file extension or MIME type
Returns:
the MetadataResourceFactory instance, or null if no configuration compatible for this file and key
Throws:
MetadataConfigurationException - if a configuration error occurs

getManagerProperties

public java.lang.String getManagerProperties()
Get the resource path of the manager Properties resource.

Returns:
the managerProperties

setManagerProperties

public void setManagerProperties(java.lang.String managerProperties)
Set the resource path of the manager Properties resource.

If configured, this will be the first properties resource to look for while searching for MetadataResourceFactory instances. If isDisableSearchPath() returns true then this will be the only properties resource looked in.

Parameters:
managerProperties - the managerProperties to set

isDisableSearchPath

public boolean isDisableSearchPath()
Return the search path flag.

If this returns true then this instance will not search for MetadataResourceFactory implementations as described in findFactory(File, String). Rather it will use only the configured properties resource set via setManagerProperties(String). This property defaults to false, meaning searching will be enabled.

Returns:
the disableSearchPath

setDisableSearchPath

public void setDisableSearchPath(boolean disableSearchPath)
Set the search path flag.

If set to true then this instance will not search for MetadataResourceFactory implementations as described in findFactory(File, String). Rather it will use only the configured properties resource set via setManagerProperties(String).

Parameters:
disableSearchPath - the disableSearchPath to set