2.17.2: 2011-06-17

net.sf.basedb.clients.web.extensions
Class ExtensionsFile

java.lang.Object
  extended by net.sf.basedb.clients.web.extensions.ExtensionsFile
All Implemented Interfaces:
Comparable<ExtensionsFile>

public class ExtensionsFile
extends Object
implements Comparable<ExtensionsFile>

Represents a single extensions XML file or a JAR file containing extensions. Each file has an individual XmlLoader to keep track of the extensions/extension points loaded from the file.

Version:
2.7
Author:
nicklas
Last modified
$Date:2008-03-20 12:15:25 +0100 (Thu, 20 Mar 2008) $

Field Summary
private  About about
           
private  ExtensionsDirectory directory
           
private  File file
           
private  boolean hasError
           
private  boolean hasValidated
           
private  boolean isJar
           
private  boolean isValid
           
private  JarClassLoader jarLoader
           
private  long lastModified
           
private  XmlLoader loader
           
private static Logger log
           
private  File resourcesDir
           
private  Map<String,ServletWrapper> servlets
           
private  long size
           
private  Throwable validationError
           
 
Constructor Summary
ExtensionsFile(ExtensionsDirectory directory, File file, File resourcesDir, XmlLoader loader)
          Create a new object.
 
Method Summary
 int compareTo(ExtensionsFile other)
           
 boolean equals(Object obj)
           
 boolean exists()
          Check if the file really exists on the disk.
(package private)  int extractResources(Pattern filter, String replacement, boolean forceOverwrite)
          Extract resources from a JAR file to a directory.
 About getAbout()
          Get information about this extensions file.
 Iterator<ExtensionPoint<?>> getExtensionPoints()
          Get an iterator returning all extension points that are defined by this file.
 Iterator<Extension<?>> getExtensions()
          Get an iterator returning all extensions that are defined by this file.
 String getFactoryParameters(Object factory)
          Get a XML-like string representation of the parameters that was used to initialise a factory.
 String getName()
          Get the name of the file.
 ServletWrapper getServlet(String servletName)
          Get a servlet that has been defined in this JAR file.
 Throwable getValidationError()
          Get more information about the error that caused the validation to fail.
 boolean hasError()
          If there was an error when registering the extensions in this file.
 int hashCode()
           
 boolean isJar()
          If the file name ends with '.jar' the file is considered a JAR file.
 boolean isModified()
          Check if the file has been modified since the last call to resetModified().
 boolean isNew()
          If the file is a new file not previously registered with the extension system.
 boolean isValid()
          Checks if the file is a valid extensions definition file.
(package private)  void loadExtensions()
          Load the extension definitions from the XML or JAR file.
private  About loadJar()
          Load extensions from a validated JAR file.
(package private)  int loadServlets(ServletContext context)
          Load servlet definitions from a JAR file.
private  About loadXml()
          Load extensions from a validated XML file.
(package private)  int registerExtensionPoints(Registry registry, boolean forceUpdate)
          Register all extension points with a registry.
(package private)  int registerExtensions(Registry registry, boolean forceUpdate)
          Register all extensions with a registry.
(package private)  int removeResources()
          Remove all extracted resources.
(package private)  void resetModified()
          Resets the remembered timestamp and size attributes of the file.
(package private)  void setError(boolean error)
          Sets the error status.
(package private)  int unregisterAll(Registry registry)
          Unregister all extensions and extension points.
(package private)  int unregisterMissing(Registry registry)
          Unregister extensions and extension points that have been removed from this package after an update.
private  void validate()
           
private  About validateJar()
          Try to validate a JAR file with extensions.
private  About validateXml()
          Try to validate an XML file with extensions.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final Logger log

directory

private final ExtensionsDirectory directory

file

private final File file

resourcesDir

private final File resourcesDir

loader

private final XmlLoader loader

isJar

private final boolean isJar

jarLoader

private JarClassLoader jarLoader

hasValidated

private boolean hasValidated

isValid

private boolean isValid

validationError

private Throwable validationError

lastModified

private long lastModified

size

private long size

about

private About about

hasError

private boolean hasError

servlets

private Map<String,ServletWrapper> servlets
Constructor Detail

ExtensionsFile

public ExtensionsFile(ExtensionsDirectory directory,
                      File file,
                      File resourcesDir,
                      XmlLoader loader)
Create a new object.

Parameters:
directory - The directory with extensions;
file - The XML or JAR file containting the extension definition
resourcesDir - The directory where resources are extracted
loader - The XML loader to use
Method Detail

compareTo

public int compareTo(ExtensionsFile other)
Specified by:
compareTo in interface Comparable<ExtensionsFile>

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

getName

public String getName()
Get the name of the file.


isJar

public boolean isJar()
If the file name ends with '.jar' the file is considered a JAR file. Otherwise it is considered an XML file. The file name check is case sensitive.

Returns:
TRUE if the file is a JAR file, false if it is an XML file

exists

public boolean exists()
Check if the file really exists on the disk.

Returns:
See File.exists()

isModified

public boolean isModified()
Check if the file has been modified since the last call to resetModified(). We consider the file to be modified if the timestamp (File.lastModified()) or size (File.length()) has changed, or if the underlying class loader has detected a change in any files it depends on (see JarClassLoader.hasChanged(boolean)).

Returns:
TRUE if the file has been modified, FALSE otherwise
See Also:
resetModified()

resetModified

void resetModified()
Resets the remembered timestamp and size attributes of the file.

See Also:
isModified()

isNew

public boolean isNew()
If the file is a new file not previously registered with the extension system. Thew 'new' status is changed when resetModified() is called which usually happens when all extensions has been registered.

Returns:
TRUE if the file is a new file, FALSE otherwise

isValid

public boolean isValid()
Checks if the file is a valid extensions definition file. This means that the file must be either:

Returns:
TRUE if the file is valid, FALSE otherwise
See Also:
getValidationError()

getValidationError

public Throwable getValidationError()
Get more information about the error that caused the validation to fail.

Returns:
An exception or null if the validation succeeded

hasError

public boolean hasError()
If there was an error when registering the extensions in this file. This property is only set if the file has been determined to be a valid extensions file by the isValid() method. Extensions that has an error are automatically disabled.

Returns:
TRUE if there was some error, FALSE if everything is ok

setError

void setError(boolean error)
Sets the error status.


getExtensionPoints

public Iterator<ExtensionPoint<?>> getExtensionPoints()
Get an iterator returning all extension points that are defined by this file. The returned iterator doesn't support removal and will not reflect changes made by the same or other threads after this call has returned.


getExtensions

public Iterator<Extension<?>> getExtensions()
Get an iterator returning all extensions that are defined by this file. The returned iterator doesn't support removal and will not reflect changes made by the same or other threads after this call has returned.


getAbout

public About getAbout()
Get information about this extensions file.

Returns:
An About object or null if no information is available

getFactoryParameters

public String getFactoryParameters(Object factory)
Get a XML-like string representation of the parameters that was used to initialise a factory. The factory must belong to an extension or extension point defined by this file.

Parameters:
factory - The factory object
Returns:
The parameters as found in the XML file

loadExtensions

void loadExtensions()
Load the extension definitions from the XML or JAR file.


validate

private void validate()

validateXml

private About validateXml()
Try to validate an XML file with extensions. The XML file is valid if it matches the extension definition XML file format.

Returns:
The global about information or null if not present

validateJar

private About validateJar()
Try to validate a JAR file with extensions. The JAR file is valid if it contains a file 'META-INF/extensions.xml' which is a valid extension definition XML file.

Returns:
The global about information or null if not present

loadXml

private About loadXml()
Load extensions from a validated XML file.

Returns:
The global about information or null if not present

loadJar

private About loadJar()
Load extensions from a validated JAR file.

Returns:
The global about information or null if not present

loadServlets

int loadServlets(ServletContext context)
Load servlet definitions from a JAR file.


extractResources

int extractResources(Pattern filter,
                     String replacement,
                     boolean forceOverwrite)
Extract resources from a JAR file to a directory. This method does nothing if the file is an XML file.

Parameters:
filter - A regular expression that must match all files that should be extracted, or null to every file
replacement - A replacement pattern used to convert the filename in the JAR file to the filename on disk, or null to not change the filename. See Matcher.replaceAll(String).
forceOverwrite - If TRUE, overwrite files even if they have the same size and timestamp as the file in the JAR file
Returns:
The number of extracted files

removeResources

int removeResources()
Remove all extracted resources. This method simply removes all files and directories it can find in the home directory of the extensions. Files from two different extension packages should thus never be mixed in the same directory.

Returns:
The number of removed files

registerExtensions

int registerExtensions(Registry registry,
                       boolean forceUpdate)
Register all extensions with a registry.

Parameters:
registry - The registry
forceUpdate - TRUE to force an update of already registered extensions, FALSE to only update if the extension package has been modified
Returns:
The number of registered or updated extensions

registerExtensionPoints

int registerExtensionPoints(Registry registry,
                            boolean forceUpdate)
Register all extension points with a registry.

Parameters:
registry - The registry
forceUpdate - TRUE to force an update of already registered extension points, FALSE to only update if the extension package has been modified
Returns:
The number of registered or updated extensions

unregisterMissing

int unregisterMissing(Registry registry)
Unregister extensions and extension points that have been removed from this package after an update.

Parameters:
registry - The registry
Returns:
The number of unregistered extensions and extension points

unregisterAll

int unregisterAll(Registry registry)
Unregister all extensions and extension points.

Parameters:
registry - The registry
Returns:
The number of unregistered extensions and extension points

getServlet

public ServletWrapper getServlet(String servletName)
Get a servlet that has been defined in this JAR file.

Parameters:
servletName - The name of the servlet, specified by the <servlet-name> tag in the XML file.
Returns:
A wrapper that can be used to invoke the servlet, or null if not servlet with the given name exists

2.17.2: 2011-06-17