Class FileReloadContainer<T>
- java.lang.Object
-
- org.opennms.core.spring.FileReloadContainer<T>
-
- Type Parameters:
T
- the class of the inner object that is stored in this container
public class FileReloadContainer<T> extends java.lang.Object
Provides a container for returning an object and reloading the object if an underlying file has changed. Ideally suited for automatically reloading configuration files that might be edited outside of the application.
There are two constructors:FileReloadContainer(T, Resource, FileReloadCallback<T>)
is used for objects having an underlying resource and are reloadableFileReloadContainer(T)
is used for objects that either do not have an underlying file or are otherwise not reloadable
If the first constructor is used, the Resource will be stored for later reloading. If
Resource.getFile()
does not throw an exception, the returned File object will be stored andFile.lastModified()
will be called every time thegetObject()
method is called to see if the file has changed. If the file has changed, the last modified time is updated and the reload callback,FileReloadCallback.reload
, is called. If it returns a non-null object, the new object is stored and it gets returned to the caller. If a null object is returned, the stored object isn't modified and the old object is returned to the caller.If an unchecked exception is thrown by the reload callback, it will be caught, logged, and a
DataAccessResourceFailureException
with a cause of the unchecked exception. This will propogate up to the caller of the getObject method. If you do not want unchecked exceptions on reloads to propogate up to the caller of getObject, they need to be caught within the reload method. Returning a null in the case of errors is a good alternative in this case.- Author:
- dj@opennms.org
-
-
Constructor Summary
Constructors Constructor Description FileReloadContainer(java.io.File file, FileReloadCallback<T> callback)
FileReloadContainer(T object)
Creates a new container with an object which has no underlying file.FileReloadContainer(T object, org.springframework.core.io.Resource resource, FileReloadCallback<T> callback)
Creates a new container with an object and a file underlying that object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.io.File
getFile()
Get the file underlying the object in this container, if any.long
getLastUpdate()
Get the timestamp in milliseconds of the last time the file was reloaded.T
getObject()
Get the object in this container.long
getReloadCheckInterval()
Get the reload check interval.void
reload()
Force a reload of the configuration.void
setReloadCheckInterval(long reloadCheckInterval)
Set the reload check interval.
-
-
-
Constructor Detail
-
FileReloadContainer
public FileReloadContainer(T object, org.springframework.core.io.Resource resource, FileReloadCallback<T> callback)
Creates a new container with an object and a file underlying that object. If reloadCheckInterval is set to a non-negative value (default is 1000 milliseconds), the last modified timestamp on the file will be checked and thereload
on the callback will be called when the file is modified. The check will be performed whengetObject()
is called and at least reloadCheckInterval milliseconds have passed.- Parameters:
object
- object to be stored in this containercallback
-reload
will be called when the underlying file object is modifiedresource
- aResource
object.- Throws:
java.lang.IllegalArgumentException
- if object, file, or callback are null
-
FileReloadContainer
public FileReloadContainer(java.io.File file, FileReloadCallback<T> callback)
-
FileReloadContainer
public FileReloadContainer(T object)
Creates a new container with an object which has no underlying file. This will not auto-reload.- Parameters:
object
- object to be stored in this container- Throws:
java.lang.IllegalArgumentException
- if object is null
-
-
Method Detail
-
getObject
public T getObject() throws org.springframework.dao.DataAccessResourceFailureException
Get the object in this container. If the object is backed by a file, the last modified time on the file will be checked, and if it has changed the object will be reloaded.- Returns:
- object in this container
- Throws:
org.springframework.dao.DataAccessResourceFailureException
- if an unchecked exception is received while trying to reload the object from the underlying file
-
reload
public void reload()
Force a reload of the configuration.
-
getFile
public java.io.File getFile()
Get the file underlying the object in this container, if any.- Returns:
- if the container was created with an underlying file the file will be returned, otherwise null
-
getReloadCheckInterval
public long getReloadCheckInterval()
Get the reload check interval.- Returns:
- reload check interval in milliseconds. A negative value
indicates that automatic reload checks are not performed and the
file will only be reloaded if
reload()
is explicitly called.
-
setReloadCheckInterval
public void setReloadCheckInterval(long reloadCheckInterval)
Set the reload check interval.- Parameters:
reloadCheckInterval
- reload check interval in milliseconds. A negative value indicates that automatic reload checks are not performed and the file will only be reloaded ifreload()
is explicitly called.
-
getLastUpdate
public long getLastUpdate()
Get the timestamp in milliseconds of the last time the file was reloaded.- Returns:
- the timestamp
-
-