Class ExtensionsFile
java.lang.Object
net.sf.basedb.util.extensions.manager.ExtensionsFile
- All Implemented Interfaces:
Comparable<ExtensionsFile>
Represents a file with extensions in it. The file is either an XML file
or a JAR file. A JAR file is always a real file on the file system and
can be accessed with
getFile()
. An XML file can be both a real file
or an abstract stream of bytes and can be accessed by getXmlStream()
.
The latter method can also be used in the case of a JAR file and will then
open the META-INF/extensions.xml
file.- Since:
- 3.0
- Author:
- Nicklas
- Last modified
- $Date: 2017-01-20 10:35:05 +0100 (fr, 20 jan 2017) $
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
An extensions file with additional methods that allows adding or modifying information in the underlying extensions file. -
Field Summary
Modifier and TypeFieldDescriptionprivate About
private final File
private boolean
private final boolean
private boolean
private boolean
private JarClassLoader
private long
private long
private static final Logger
private final ExtensionsManager
private final String
private final ReentrantReadWriteLock
private final URI
private Throwable
private boolean
-
Constructor Summary
ModifierConstructorDescription(package private)
ExtensionsFile
(ExtensionsManager manager, File file) Create a new extensions file backed by a real file on the file system.(package private)
ExtensionsFile
(ExtensionsManager manager, File file, boolean isJar) Create a new extensions file backed by a real file on the file system.private
ExtensionsFile
(ExtensionsManager manager, File file, URI uri, String name, boolean isJar) (package private)
ExtensionsFile
(ExtensionsManager manager, URI uri) Create a new extension file backed by an URI. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Check if the underlying file has been modified since it was last processed.int
compareTo
(ExtensionsFile other) boolean
boolean
exists()
Check if the file exists.getAbout()
Get information about the extensions in this file.Get a list of all objects defined in this extensions file.Get the class loader used to load classes for the extension.getFile()
Get the underlying file on the file system.<M> M
getMetadata
(ObjectKey<M> key) Get metadata registered for a given given key.<T> List<T>
getMetadataKeysOfClass
(Class<T> ofClass) Get a list of all metadata defined in this extensions file that are keyed with the specified class or interface.getName()
Get the name of the file.<O> O
getObjectForKey
(ObjectKey<O> key) Get the object that was registered for the given key.<T> List<T>
getObjectsOfClass
(Class<T> ofClass) Get a list of all objects defined in this extensions file that are of the specified class or interface.Get an input stream for reading the resource specified by the given pathgetURI()
Get an URI that points to the extensions file.Get more information about the error that caused the validation to fail.(package private) ExtensionsFile.WriteableExtensionsFile
Get a writeable view for the current file.Get an input stream for reading the XML file containing the extension definitions.boolean
hasError()
If there was an error when registering the extensions in this file.int
hashCode()
boolean
Is this an extension file that should be ignored?boolean
Is this file installed into the system or not?boolean
isJar()
If the file name ends with '.jar' the file is considered a JAR file.boolean
isNew()
If the file is a new file not previously processed by the extension system.boolean
isValid()
Is the file a valid extensions file according to the last performed validation.(package private) boolean
readLock()
Try to aquire a read-lock.toString()
(package private) boolean
validate()
Validate the XML file with the extension definitions.boolean
Check if the file was modified when the last call tocheckModified()
was made.(package private) boolean
Try to aquire a write-lock.
-
Field Details
-
log
-
manager
-
file
-
uri
-
isJar
private final boolean isJar -
name
-
rwLock
-
allObjects
-
objectMetadata
-
isNew
private boolean isNew -
wasModified
private boolean wasModified -
lastModified
private long lastModified -
lastLength
private long lastLength -
jarLoader
-
about
-
hasError
private boolean hasError -
isValid
private volatile boolean isValid -
validationError
-
-
Constructor Details
-
ExtensionsFile
ExtensionsFile(ExtensionsManager manager, File file) Create a new extensions file backed by a real file on the file system. If the file name ends with '.jar' it is assumed to be a JAR file, otherwise it must be an XML file containing extension definitions.- Parameters:
file
- The file
-
ExtensionsFile
ExtensionsFile(ExtensionsManager manager, File file, boolean isJar) Create a new extensions file backed by a real file on the file system. The file may be a JAR file or an XML file.- Parameters:
file
- The fileisJar
- TRUE if it is a JAR file, FALSE if it is an XML file
-
ExtensionsFile
ExtensionsFile(ExtensionsManager manager, URI uri) Create a new extension file backed by an URI. The URI must point to an XML file containing extension definitions.- Parameters:
uri
- The URI
-
ExtensionsFile
-
-
Method Details
-
compareTo
- Specified by:
compareTo
in interfaceComparable<ExtensionsFile>
-
hashCode
public int hashCode() -
equals
-
toString
-
getName
Get the name of the file. The name may or may not correspond to a real file. -
getFile
Get the underlying file on the file system. Return null if the extension is defined by an URI. -
getURI
Get an URI that points to the extensions 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 exists. For files that are only represented by an URI this method always return true. For other files the call is forwarded toFile.exists()
andFile.isFile()
.- Returns:
- See
File.exists()
-
isNew
public boolean isNew()If the file is a new file not previously processed by the extension system. Thew 'new' status is changed whenExtensionsFile.WriteableExtensionsFile.markAsProcessed()
is called which usually happens when all extensions has been registered.- Returns:
- TRUE if the file is a new file, FALSE otherwise
-
isInstalled
public boolean isInstalled()Is this file installed into the system or not? -
isIgnored
public boolean isIgnored()Is this an extension file that should be ignored?- Since:
- 3.10
-
wasModified
public boolean wasModified()Check if the file was modified when the last call tocheckModified()
was made. It is recommended that processor implementation use this method instead to avoid inconsistent behaviour if the file happens to be modified while a processor is running.- Returns:
- TRUE if the file was modified, FALSE otherwise
-
checkModified
public boolean checkModified()Check if the underlying file has been modified since it was last processed. Files that are new are always considered to be modified. Files that are only represented as an URI are considered to be unmodifiable. For other files, we check theFile.lastModified()
andFile.length()
of the underlying file and compare that to the last known timestamp and size. Files that are JAR files we also check with the class loader if any of the libraries it uses have changed.The result of this call is remembered until this method is called again or until the file is marked as processed.
-
isValid
public boolean isValid()Is the file a valid extensions file according to the last performed validation. This means that the file must be either:- An extensions definition XML file
- A JAR file with an extensions definition XML file at
META-INF/extensions.xml
.
checkModified()
returns true, the return value of this method may be out of sync. Re-validation of modified files are usually performed by the extensions manager by callingExtensionsManager.scanForChanges()
NOTE! The validation will only verify that the XML file follows the rules defined by the schema definition. It will not check that actual classes, etc. exists and can be created. This usually happens later in the registration process, and can be checked by
hasError()
.- Returns:
- TRUE if the file is valid, FALSE otherwise
- See Also:
-
getValidationError
Get more information about the error that caused the validation to fail.- Returns:
- An exception or null if the validation succeeded
- See Also:
-
getAbout
Get information about the extensions in this file.- Returns:
- An About object or null if the file is not a valid extensions file
-
getXmlStream
Get an input stream for reading the XML file containing the extension definitions.- Returns:
- An input stream
- Throws:
IOException
-
getStream
Get an input stream for reading the resource specified by the given path- Parameters:
path
- The path of the resource- Returns:
- An input stream (or null if this extensions file is not a JAR file or if the given path is not found)
- Throws:
IOException
-
validate
boolean validate()Validate the XML file with the extension definitions. -
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 theisValid()
method. Extensions that has an error are automatically disabled.- Returns:
- TRUE if there was some error, FALSE if everything is ok
-
getAllObjects
Get a list of all objects defined in this extensions file. This list can contain any type of objects that have been registered by a processer, but typically this list containsExtensionPoint
:s andExtension
:s. UsegetObjectsOfClass(Class)
to get a list with only one type of objects.- Returns:
- A list with the objects
-
getObjectsOfClass
Get a list of all objects defined in this extensions file that are of the specified class or interface. Note! The returned list may be empty if another thread is currently processing this file.- Parameters:
ofClass
- The class/interface the objects must be an instance of- Returns:
- A list with the objects, if no objects are found the list is empty
-
getObjectForKey
Get the object that was registered for the given key.- Parameters:
key
- An object key- Returns:
- The object or null if no object was found
-
getMetadata
Get metadata registered for a given given key.- Parameters:
key
- An object key- Returns:
- A metadata object, or null if no metadata was found
-
getMetadataKeysOfClass
Get a list of all metadata defined in this extensions file that are keyed with the specified class or interface. Note! The returned list may be empty if another thread is currently processing this file.- Parameters:
ofClass
- The class/interface the metadata keys must be an instance of- Returns:
- A list with the objects, if no objects are found the list is empty
-
getClassLoader
Get the class loader used to load classes for the extension. Only JAR files have class loaders so this method may return null. Once a class loader has been created it remains the same until a change has been detected withcheckModified()
which forces the creation of a new class loader when this method is called.- Throws:
IOException
-
getWriteableFile
ExtensionsFile.WriteableExtensionsFile getWriteableFile()Get a writeable view for the current file. Note that the view is returned in read-only mode and to really be able to write information the methodExtensionsFile.WriteableExtensionsFile.open()
must be called. -
readLock
boolean readLock()Try to aquire a read-lock. A read-lock is needed when reading information that may be corrupted if another thread is currently writing information to this file.- Returns:
- TRUE if the read-lock could be aquired immediately, FALSE if not
-
writeLock
boolean writeLock()Try to aquire a write-lock. A write-lock is needed by any thread that wants to update information about this file. The write-lock can only be held by a single thread at a time.- Returns:
- TRUE if the write-lock could be aquired within 1 second, FALSE if not
-