Class FileHandler
A class that manages persistence of an associated FileBased
object.
Instances of this class can be used to load and save arbitrary objects implementing the FileBased
interface
in a convenient way from and to various locations. At construction time the FileBased
object to manage is
passed in. Basically, this object is assigned a location from which it is loaded and to which it can be saved. The
following possibilities exist to specify such a location:
- URLs: With the method
setURL()
a full URL to the configuration source can be specified. This is the most flexible way. Note that thesave()
methods support only file: URLs. - Files: The
setFile()
method allows to specify the configuration source as a file. This can be either a relative or an absolute file. In the former case the file is resolved based on the current directory. - As file paths in string form: With the
setPath()
method a full path to a configuration file can be provided as a string. - Separated as base path and file name: The base path is a string defining either a local directory or a URL. It
can be set using the
setBasePath()
method. The file name, non surprisingly, defines the name of the configuration file.
An instance stores a location. The load()
and save()
methods that do not take an argument make use of
this internal location. Alternatively, it is also possible to use overloaded variants of load()
and
save()
which expect a location. In these cases the location specified takes precedence over the internal one;
the internal location is not changed.
The actual position of the file to be loaded is determined by a FileLocationStrategy
based on the location
information that has been provided. By providing a custom location strategy the algorithm for searching files can be
adapted. Save operations require more explicit information. They cannot rely on a location strategy because the file
to be written may not yet exist. So there may be some differences in the way location information is interpreted by
load and save operations. In order to avoid this, the following approach is recommended:
- Use the desired
setXXX()
methods to define the location of the file to be loaded. - Call the
locate()
method. This method resolves the referenced file (if possible) and fills out all supported location information. - Later on,
save()
can be called. This method now has sufficient information to store the file at the correct location.
When loading or saving a FileBased
object some additional functionality is performed if the object implements
one of the following interfaces:
FileLocatorAware
: In this case an object with the current file location is injected before the load or save operation is executed. This is useful forFileBased
objects that depend on their current location, e.g. to resolve relative path names.SynchronizerSupport
: If this interface is implemented, load and save operations obtain a write lock on theFileBased
object before they access it. (In case of a save operation, a read lock would probably be sufficient, but because of the possible injection of aFileLocator
object it is not allowed to perform multiple save operations in parallel; therefore, by obtaining a write lock, we are on the safe side.)
This class is thread-safe.
- Since:
- 2.0
-
Constructor Summary
ConstructorDescriptionCreates a new instance ofFileHandler
which is not associated with aFileBased
object and thus does not have a content.FileHandler
(FileBased obj) Creates a new instance ofFileHandler
and sets the managedFileBased
object.FileHandler
(FileBased obj, FileHandler c) Creates a new instance ofFileHandler
which is associated with the givenFileBased
object and the location defined for the givenFileHandler
object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a listener to thisFileHandler
.void
Clears the location of thisFileHandler
.static FileHandler
Creates a newFileHandler
instance from properties stored in a map.Gets the base path.final FileBased
Gets theFileBased
object associated with thisFileHandler
.Gets the encoding of the associated file.getFile()
Gets the location of the associated file as aFile
object.Gets aFileLocator
object with the specification of the file stored by thisFileHandler
.Gets the name of the file.Gets theFileSystem
to be used by this object when locating files.Gets theFileLocationStrategy
to be applied when accessing the associated file.getPath()
Gets the full path to the associated file.getURL()
Gets the location of the associated file as a URL.boolean
Tests whether a location is defined for thisFileHandler
.void
load()
Loads the associated file from the underlying location.void
Loads the associated file from the specifiedFile
.void
load
(InputStream in) Loads the associated file from the specified stream, using the encoding returned bygetEncoding()
.void
load
(InputStream in, String encoding) Loads the associated file from the specified stream, using the specified encoding.void
Loads the associated file from the specified reader.void
Loads the associated file from the given file name.void
Loads the associated file from the specified URL.boolean
locate()
Locates the referenced file if necessary and ensures that the associatedFileLocator
is fully initialized.void
Removes the specified listener from this object.void
Resets theFileSystem
used by this object.void
save()
Saves the associated file to the current location set for this object.void
Saves the associated file to the specifiedFile
.void
save
(OutputStream out) Saves the associated file to the specified stream using the encoding returned bygetEncoding()
.void
save
(OutputStream out, String encoding) Saves the associated file to the specified stream using the specified encoding.void
Saves the associated file to the givenWriter
.void
Saves the associated file to the specified file name.void
Saves the associated file to the specified URL.void
setBasePath
(String basePath) Sets the base path.void
setEncoding
(String encoding) Sets the encoding of the associated file.void
Sets the location of the associated file as aFile
object.void
setFileLocator
(FileLocator locator) Sets the file to be accessed by thisFileHandler
as aFileLocator
object.void
setFileName
(String fileName) Sets the name of the file.void
setFileSystem
(FileSystem fileSystem) Sets theFileSystem
to be used by this object when locating files.void
setLocationStrategy
(FileLocationStrategy strategy) Sets theFileLocationStrategy
to be applied when accessing the associated file.void
Sets the location of the associated file as a full or relative path name.void
Sets the location of the associated file as a URL.void
setURL
(URL url, URLConnectionOptions urlConnectionOptions) Sets the location of the associated file as a URL.
-
Constructor Details
-
FileHandler
public FileHandler()Creates a new instance ofFileHandler
which is not associated with aFileBased
object and thus does not have a content. Objects of this kind can be used to define a file location, but it is not possible to actually load or save data. -
FileHandler
Creates a new instance ofFileHandler
and sets the managedFileBased
object.- Parameters:
obj
- the file-based object to manage
-
FileHandler
Creates a new instance ofFileHandler
which is associated with the givenFileBased
object and the location defined for the givenFileHandler
object. A copy of the location of the givenFileHandler
is created. This constructor is a possibility to associate a file location with aFileBased
object.- Parameters:
obj
- theFileBased
object to managec
- theFileHandler
from which to copy the location (must not be null)- Throws:
IllegalArgumentException
- if theFileHandler
is null
-
-
Method Details
-
fromMap
Creates a newFileHandler
instance from properties stored in a map. This method tries to extract aFileLocator
from the map. A newFileHandler
is created based on thisFileLocator
.- Parameters:
map
- the map (may be null)- Returns:
- the newly created
FileHandler
- See Also:
-
addFileHandlerListener
Adds a listener to thisFileHandler
. It is notified about property changes and IO operations.- Parameters:
l
- the listener to be added (must not be null)- Throws:
IllegalArgumentException
- if the listener is null
-
clearLocation
Clears the location of thisFileHandler
. Afterwards this handler does not point to any valid file. -
getBasePath
Gets the base path. If no base path is defined, but a URL, the base path is derived from there.- Returns:
- the base path
-
getContent
Gets theFileBased
object associated with thisFileHandler
.- Returns:
- the associated
FileBased
object
-
getEncoding
Gets the encoding of the associated file. Result can be null if no encoding has been set.- Returns:
- the encoding of the associated file
-
getFile
Gets the location of the associated file as aFile
object. If the base path is a URL with a protocol different than "file", or the file is within a compressed archive, the return value will not point to a valid file object.- Returns:
- the location as
File
object; this can be null
-
getFileLocator
Gets aFileLocator
object with the specification of the file stored by thisFileHandler
. Note that this method returns the internal data managed by thisFileHandler
as it was defined. This is not necessarily the same as the data returned by the single access methods likegetFileName()
orgetURL()
: These methods try to derive missing data from other values that have been set.- Returns:
- a
FileLocator
with the referenced file
-
getFileName
Gets the name of the file. If only a URL is defined, the file name is derived from there.- Returns:
- the file name
-
getFileSystem
Gets theFileSystem
to be used by this object when locating files. Result is never null; if no file system has been set, the default file system is returned.- Returns:
- the used
FileSystem
-
getLocationStrategy
Gets theFileLocationStrategy
to be applied when accessing the associated file. This method never returns null. If aFileLocationStrategy
has been set, it is returned. Otherwise, result is the defaultFileLocationStrategy
.- Returns:
- the
FileLocationStrategy
to be used
-
getPath
Gets the full path to the associated file. The return value is a validFile
path only if this location is based on a file on the local disk. If the file was loaded from a packed archive, the returned value is the string form of the URL from which the file was loaded.- Returns:
- the full path to the associated file
-
getURL
Gets the location of the associated file as a URL. If a URL is set, it is directly returned. Otherwise, an attempt to locate the referenced file is made.- Returns:
- a URL to the associated file; can be null if the location is unspecified
-
isLocationDefined
Tests whether a location is defined for thisFileHandler
.- Returns:
- true if a location is defined, false otherwise
-
load
Loads the associated file from the underlying location. If no location has been set, an exception is thrown.- Throws:
ConfigurationException
- if loading of the configuration fails
-
load
Loads the associated file from the specifiedFile
.- Parameters:
file
- the file to load- Throws:
ConfigurationException
- if an error occurs
-
load
Loads the associated file from the specified stream, using the encoding returned bygetEncoding()
.- Parameters:
in
- the input stream- Throws:
ConfigurationException
- if an error occurs during the load operation
-
load
Loads the associated file from the specified stream, using the specified encoding. If the encoding is null, the default encoding is used.- Parameters:
in
- the input streamencoding
- the encoding used,null
to use the default encoding- Throws:
ConfigurationException
- if an error occurs during the load operation
-
load
Loads the associated file from the specified reader.- Parameters:
in
- the reader- Throws:
ConfigurationException
- if an error occurs during the load operation
-
load
Loads the associated file from the given file name. The file name is interpreted in the context of the already set location (e.g. if it is a relative file name, a base path is applied if available). The underlying location is not changed.- Parameters:
fileName
- the name of the file to be loaded- Throws:
ConfigurationException
- if an error occurs
-
load
Loads the associated file from the specified URL. The location stored in this object is not changed.- Parameters:
url
- the URL of the file to be loaded- Throws:
ConfigurationException
- if an error occurs
-
locate
Locates the referenced file if necessary and ensures that the associatedFileLocator
is fully initialized. When accessing the referenced file the information stored in the associatedFileLocator
is used. If this information is incomplete (e.g. only the file name is set), an attempt to locate the file may have to be performed on each access. By calling this method such an attempt is performed once, and the results of a successful localization are stored. Hence, later access to the referenced file can be more efficient. Also, all properties pointing to the referenced file in this object'sFileLocator
are set (i.e. the URL, the base path, and the file name). If the referenced file cannot be located, result is false. This means that the information in the currentFileLocator
is insufficient or wrong. If theFileLocator
is already fully defined, it is not changed.- Returns:
- a flag whether the referenced file could be located successfully
- See Also:
-
removeFileHandlerListener
Removes the specified listener from this object.- Parameters:
l
- the listener to be removed
-
resetFileSystem
Resets theFileSystem
used by this object. It is set to the default file system. -
save
Saves the associated file to the current location set for this object. Before this method can be called a valid location must have been set.- Throws:
ConfigurationException
- if an error occurs or no location has been set yet
-
save
Saves the associated file to the specifiedFile
. The file is created automatically if it doesn't exist. This does not change the location of this object (usesetFile(java.io.File)
if you need it).- Parameters:
file
- the target file- Throws:
ConfigurationException
- if an error occurs during the save operation
-
save
Saves the associated file to the specified stream using the encoding returned bygetEncoding()
.- Parameters:
out
- the output stream- Throws:
ConfigurationException
- if an error occurs during the save operation
-
save
Saves the associated file to the specified stream using the specified encoding. If the encoding is null, the default encoding is used.- Parameters:
out
- the output streamencoding
- the encoding to be used,null
to use the default encoding- Throws:
ConfigurationException
- if an error occurs during the save operation
-
save
Saves the associated file to the specified file name. This does not change the location of this object (usesetFileName(String)
if you need it).- Parameters:
fileName
- the file name- Throws:
ConfigurationException
- if an error occurs during the save operation
-
save
Saves the associated file to the specified URL. This does not change the location of this object (usesetURL(URL)
if you need it).- Parameters:
url
- the URL- Throws:
ConfigurationException
- if an error occurs during the save operation
-
save
Saves the associated file to the givenWriter
.- Parameters:
out
- theWriter
- Throws:
ConfigurationException
- if an error occurs during the save operation
-
setBasePath
Sets the base path. The base path is typically either a path to a directory or a URL. Together with the value passed to thesetFileName()
method it defines the location of the configuration file to be loaded. The strategies for locating the file are quite tolerant. For instance if the file name is already an absolute path or a fully defined URL, the base path will be ignored. The base path can also be a URL, in which case the file name is interpreted in this URL's context. If other methods are used for determining the location of the associated file (e.g.setFile()
orsetURL()
), the base path is automatically set. Setting the base path using this method automatically sets the URL to null because it has to be determined anew based on the file name and the base path.- Parameters:
basePath
- the base path.
-
setEncoding
Sets the encoding of the associated file. The encoding applies if binary files are loaded. Note that in this case setting an encoding is recommended; otherwise the platform's default encoding is used.- Parameters:
encoding
- the encoding of the associated file
-
setFile
Sets the location of the associated file as aFile
object. The passed inFile
is made absolute if it is not yet. Then the file's path component becomes the base path and its name component becomes the file name.- Parameters:
file
- the location of the associated file
-
setFileLocator
Sets the file to be accessed by thisFileHandler
as aFileLocator
object.- Parameters:
locator
- theFileLocator
with the definition of the file to be accessed (must not be null- Throws:
IllegalArgumentException
- if theFileLocator
is null
-
setFileName
Sets the name of the file. The passed in file name can contain a relative path. It must be used when referring files with relative paths from classpath. UsesetPath()
to set a full qualified file name. The URL is set to null as it has to be determined anew based on the file name and the base path.- Parameters:
fileName
- the name of the file
-
setFileSystem
Sets theFileSystem
to be used by this object when locating files. If a null value is passed in, the file system is reset to the default file system.- Parameters:
fileSystem
- theFileSystem
-
setLocationStrategy
Sets theFileLocationStrategy
to be applied when accessing the associated file. The strategy is stored in the underlyingFileLocator
. The argument can be null; this causes the defaultFileLocationStrategy
to be used.- Parameters:
strategy
- theFileLocationStrategy
- See Also:
-
setPath
Sets the location of the associated file as a full or relative path name. The passed in path should represent a valid file name on the file system. It must not be used to specify relative paths for files that exist in classpath, either plain file system or compressed archive, because this method expands any relative path to an absolute one which may end in an invalid absolute path for classpath references.- Parameters:
path
- the full path name of the associated file
-
setURL
Sets the location of the associated file as a URL. For loading this can be an arbitrary URL with a supported protocol. If the file is to be saved, too, a URL with the "file" protocol should be provided. This method sets the file name and the base path to null. They have to be determined anew based on the new URL.- Parameters:
url
- the location of the file as URL
-
setURL
Sets the location of the associated file as a URL. For loading this can be an arbitrary URL with a supported protocol. If the file is to be saved, too, a URL with the "file" protocol should be provided. This method sets the file name and the base path to null. They have to be determined anew based on the new URL.- Parameters:
url
- the location of the file as URLurlConnectionOptions
- URL connection options- Since:
- 2.8.0
-