org.opennms.netmgt.dao.support
Class FileReloadContainer<T>

java.lang.Object
  extended by org.opennms.netmgt.dao.support.FileReloadContainer<T>
Type Parameters:
T - the class of the inner object that is stored in this container

public class FileReloadContainer<T>
extends 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:

  1. FileReloadContainer(T, Resource, FileReloadCallback<T>) is used for objects having an underlying resource and are reloadable
  2. FileReloadContainer(T) is used for objects that either do not have an underlying file or are otherwise not reloadable
The second constructor is provided for convenience so that reloadable and non-reloadable data of the same type can be handled similarly (the only difference in code is at initialization time when one constructor or the other is used).

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 and File.lastModified() will be called every time the getObject() 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.

Version:
$Id: $
Author:
dj@opennms.org

Constructor Summary
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
 File getFile()
          Get the file underlying the object in this container, if any.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 the reload on the callback will be called when the file is modified. The check will be performed when getObject() is called and at least reloadCheckInterval milliseconds have passed.

Type Parameters:
T - a T object.
Parameters:
object - object to be stored in this container
callback - reload will be called when the underlying file object is modified
resource - a Resource object.
Throws:
IllegalArgumentException - if object, file, or callback are null

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:
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 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 if reload() is explicitly called.


Copyright © 2011. All Rights Reserved.