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 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:

  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.

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
 java.io.File getFile()
          Get the file underlying the object in this container, if any.
 T getObject()
          Get the object in this container.
 
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. The reload on the callback will be called when the file is modified.

Parameters:
object - object to be stored in this container
file - file underlying the object
callback - reload will be called when the underlying file object is modified
Throws:
java.lang.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:
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

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


Copyright © 2007. All Rights Reserved.