3.0.2: 2012-01-25

net.sf.basedb.util.extensions.manager
Class ExtensionsManager

java.lang.Object
  extended by net.sf.basedb.util.extensions.manager.ExtensionsManager

public class ExtensionsManager
extends Object

An extensions manager is responsible for handling a group of extensions in the form of files stored in the file system. The manager will keep track of added, updated and removed files.

Since:
3.0
Author:
Nicklas
Last modified
$Date: 2011-08-11 12:45:58 +0200 (Thu, 11 Aug 2011) $

Nested Class Summary
static class ExtensionsManager.JarFirstURIComparator
          Compare URI:s so that 'jar:' scheme always are listed first.
 
Field Summary
private  Set<File> directories
           
private  Set<File> ignore
           
private  Map<ObjectKey<?>,ExtensionsFile> installedObjects
           
private static Logger log
           
private  int numDeleted
           
private  int numModified
           
private  int numNew
           
private  int numUnmodified
           
private  Registry registry
           
private  Settings settings
           
private  Map<URI,ExtensionsFile> xtFiles
           
 
Constructor Summary
ExtensionsManager(Registry registry, File settingsFile)
          Create a new extensions manager and use the given registry to store extensions.
 
Method Summary
 int addDirectory(File dir)
          Add a directory to this manager.
private  void addExtensionsFile(ExtensionsFile xtFile)
           
 ExtensionsFile addFile(File file)
          Add a file to the manager that is supposed to contain extensions.
 void addIgnore(File file)
          Add a file to the ignore list.
 ExtensionsFile addURI(URI uri)
          Add an URI that points to an XML file containing definitions.
 ExtensionsFile getFileByObjectKey(ObjectKey<?> key)
          Find out which file the object with the given key was defined.
 ExtensionsFile getFileByURI(String uri)
          Get the extension file that is located at the given URI.
 List<ExtensionsFile> getFiles()
          Get a list of all extension files managed by this manager.
 int getNumDeleted()
          Get the number of deleted files that was found in the last call to scanForChanges().
 int getNumModified()
          Get the number of modified files that was found in the last call to scanForChanges().
 int getNumNew()
          Get the number of new files that was found in the last call to scanForChanges().
 int getNumUnmodified()
          Get the number of unmodified files that was found in the last call to scanForChanges().
 Registry getRegistry()
          Get the registry in which all extensions that are managed by this manager are stored.
 Settings getSettings()
          Get the settings for the extension manager.
 void processFiles(ExtensionsFileProcessor processor)
          Process all know file with the given processer.
 void processFiles(ExtensionsFileProcessor processor, Filter<ExtensionsFile> filter)
          Process all known extension files with the given processor.
(package private)  void registerObject(ObjectKey<?> key, ExtensionsFile xtFile)
          Register an object as defined by the given extensions file.
(package private)  void removeFile(File file)
          Remove a file from the extensions manager.
 void removeIgnore(File file)
          Remove a file from the ignore list.
 int scanForChanges()
          Scan the managed directories for new, updated and deleted files.
private  int scanForInstalledFiles(File dir, Settings settings)
          Scan the given directory and add files that are installed according to the settings.
private  int scanForNewFiles(File dir)
          Scan the given directory and add files that are new.
(package private)  void unregisterAllObjects(ExtensionsFile xtFile)
          Unregister all objects know in the file.
(package private)  void unregisterObject(ObjectKey<?> key, ExtensionsFile xtFile)
          Unregister an object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final Logger log

registry

private final Registry registry

settings

private final Settings settings

ignore

private final Set<File> ignore

directories

private final Set<File> directories

xtFiles

private final Map<URI,ExtensionsFile> xtFiles

installedObjects

private final Map<ObjectKey<?>,ExtensionsFile> installedObjects

numNew

private int numNew

numModified

private int numModified

numUnmodified

private int numUnmodified

numDeleted

private int numDeleted
Constructor Detail

ExtensionsManager

public ExtensionsManager(Registry registry,
                         File settingsFile)
Create a new extensions manager and use the given registry to store extensions.

Parameters:
registry - The registry to use
settingsFile - The file were settings are stored
Method Detail

getRegistry

public Registry getRegistry()
Get the registry in which all extensions that are managed by this manager are stored.


getSettings

public Settings getSettings()
Get the settings for the extension manager.


addIgnore

public void addIgnore(File file)
Add a file to the ignore list. Files in this list will never be checked for extensions again. This is only neccessary for XML/JAR files that doesn't contain extensions.

Parameters:
file - The file to add
See Also:
removeIgnore(File)

removeIgnore

public void removeIgnore(File file)
Remove a file from the ignore list.

Parameters:
file - The file to remove
See Also:
addIgnore(File)

addDirectory

public int addDirectory(File dir)
Add a directory to this manager. The directory will be monitored for extension files in the future. Files in the directory that are installed as per the settings Settings.isInstalledFile(File) are loaded into manager.

This method call is ignored if the path is not pointing to an existing directory.

Parameters:
dir - An existing directory
Returns:
The number of files in the directory that was installed

addFile

public ExtensionsFile addFile(File file)
Add a file to the manager that is supposed to contain extensions. The file either be an XML file containing definitions, or a JAR file with the definitions in the META-INF/extensions.xml path.

Parameters:
file - An extensions file (null is not allowed)
Returns:
The extension file object representing the added file

removeFile

void removeFile(File file)
Remove a file from the extensions manager. This method will remove some information about the file from the manager itself, but it will not touch registered extensions, plug-ins, extracted resources, etc. The file itself is not deleted from the disk.

Note that if the file remains in a managed directory it will reappear in the manager if scanForChanges() is called.

Parameters:
file - The file to remove (if null this method is ignored)

addURI

public ExtensionsFile addURI(URI uri)
Add an URI that points to an XML file containing definitions. This method is mostly useful for system-defined extensions that are defined in a XML file inside a JAR file.

This method can't be used for JAR files and if used for XML files it doesn't detect changes that are made after installation.

Parameters:
uri - An URI pointing to an extensions file (null is not allowed)
Returns:
The extension file object representing the added uri

addExtensionsFile

private void addExtensionsFile(ExtensionsFile xtFile)

scanForChanges

public int scanForChanges()
Scan the managed directories for new, updated and deleted files. This method is used for gathering statistics only. To do something with the files the processFiles(ExtensionsFileProcessor) method must be called.

Note! The new or modified status of files are NOT reset as a result of calling this method. The status can only be changed as a result of some processing.

Returns:
The number of new + modified + deleted files that was found

getNumNew

public int getNumNew()
Get the number of new files that was found in the last call to scanForChanges().


getNumModified

public int getNumModified()
Get the number of modified files that was found in the last call to scanForChanges().


getNumUnmodified

public int getNumUnmodified()
Get the number of unmodified files that was found in the last call to scanForChanges().


getNumDeleted

public int getNumDeleted()
Get the number of deleted files that was found in the last call to scanForChanges().


scanForNewFiles

private int scanForNewFiles(File dir)
Scan the given directory and add files that are new.

Parameters:
dir - The directory to scan
Returns:
The number of new files in the given directory

scanForInstalledFiles

private int scanForInstalledFiles(File dir,
                                  Settings settings)
Scan the given directory and add files that are installed according to the settings.

Parameters:
dir - The directory to scan
Returns:
The number of installed files in the given directory

getFiles

public List<ExtensionsFile> getFiles()
Get a list of all extension files managed by this manager.

Returns:
A list

getFileByURI

public ExtensionsFile getFileByURI(String uri)
Get the extension file that is located at the given URI. This method can be used for all files even those that have been added with addFile(File) or addDirectory(File).

Parameters:
uri - An URI that is pointing to an extensions file
Returns:
An extensions file object, or null if no file is found

getFileByObjectKey

public ExtensionsFile getFileByObjectKey(ObjectKey<?> key)
Find out which file the object with the given key was defined. See implementations of ObjectKey for various types of objects to look for.

Parameters:
key - The object key
Returns:
Information about the file the object is defined in, or null if no object is found for the given key

registerObject

void registerObject(ObjectKey<?> key,
                    ExtensionsFile xtFile)
Register an object as defined by the given extensions file.

Parameters:
key - The object key
xtFile - The extensions file the object is defined in

unregisterObject

void unregisterObject(ObjectKey<?> key,
                      ExtensionsFile xtFile)
Unregister an object. The given extensions file must match be equal to the one that registered the object, otherwise the unregistration is ignored.

Parameters:
key - The object key
xtFile - The extensions file the object is defined in

unregisterAllObjects

void unregisterAllObjects(ExtensionsFile xtFile)
Unregister all objects know in the file.


processFiles

public void processFiles(ExtensionsFileProcessor processor)
Process all know file with the given processer.

Parameters:
processor - A processor implementation, null is not allowed

processFiles

public void processFiles(ExtensionsFileProcessor processor,
                         Filter<ExtensionsFile> filter)
Process all known extension files with the given processor.

Parameters:
processor - A processor implementation, null is not allowed
filter - An optional filter implementation that can be used to limit which files are sent to the processor

3.0.2: 2012-01-25