org.umber.catalan.queues
Class DirectoryPollQueue

java.lang.Object
  extended byorg.umber.catalan.queues.DirectoryPollQueue
All Implemented Interfaces:
ICommandQueue

public abstract class DirectoryPollQueue
extends java.lang.Object
implements ICommandQueue

Queue which loads commands as files in the file system. As each single-command file is processed, the queue moves it into an old commands directory.

Author:
jsheets

Constructor Summary
DirectoryPollQueue(java.io.File inputDir, java.io.File outputDir)
          Creates a new instance of DirectoryPollQueue.
 
Method Summary
protected  void archiveFile(java.io.File commandFile)
          Archives a completed command file.
protected abstract  IForgeCommand buildCommand(java.io.File commandFile)
          Converts the file into an IForgeCommand object.
 boolean canQuit()
          Determines if the queue can safely disconnect without losing its current status.
 void connect()
          Establishes a connection to the queue.
 void disconnect()
          Closes down the queue.
protected  java.io.File findNextFile()
          Finds the next available command file in the input directory.
protected  java.io.File getInputDir()
          Retrieves the input directory.
 IForgeCommand getNextCommand()
          Retrieves the next command in the queue, or null if queue is empty.
protected  java.io.File getOutputDir()
          Retrieves the output directory.
 boolean isConnected()
          Determines if the queue is currently connected to a live command feed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.umber.catalan.queues.ICommandQueue
sendResult
 

Constructor Detail

DirectoryPollQueue

public DirectoryPollQueue(java.io.File inputDir,
                          java.io.File outputDir)
Creates a new instance of DirectoryPollQueue.

Parameters:
inputDir - directory to pull command files from
outputDir - directory to write completed command files to
Method Detail

buildCommand

protected abstract IForgeCommand buildCommand(java.io.File commandFile)
                                       throws CatalanCommandException
Converts the file into an IForgeCommand object.

Parameters:
commandFile - File object with command data
Returns:
a command object for the given file
Throws:
CatalanCommandException - if unable to load or convert the command data

getInputDir

protected java.io.File getInputDir()
Retrieves the input directory.

Returns:
input directory

getOutputDir

protected java.io.File getOutputDir()
Retrieves the output directory.

Returns:
output directory

isConnected

public boolean isConnected()
Determines if the queue is currently connected to a live command feed.

Specified by:
isConnected in interface ICommandQueue
Returns:
true if connected

connect

public void connect()
             throws CatalanQueueException
Establishes a connection to the queue.

This implementation checks that the input directory is writeable, and attempts to create the input and output directories if they don't already exist. The input and output must be directories, not files, and must not be null.

Specified by:
connect in interface ICommandQueue
Throws:
CatalanQueueException - if unable to fully access input and output directories

disconnect

public void disconnect()
Closes down the queue.

Specified by:
disconnect in interface ICommandQueue

canQuit

public boolean canQuit()
Determines if the queue can safely disconnect without losing its current status. This should only be called between commands.

This implementation always returns true, since each file should contain a single atomic command. Subclasses which allow multiple commands per file should override this method to return false in the middle of a multi-command file.

Specified by:
canQuit in interface ICommandQueue
Returns:
always true

getNextCommand

public IForgeCommand getNextCommand()
                             throws CatalanQueueException,
                                    CatalanCommandException
Retrieves the next command in the queue, or null if queue is empty.

This implementation grabs the next file, in alphabetic order, from the input directory. After processing, it moves the file to the output directory. Before processing, it checks for write permissions on the file, and will abort without processing if the file cannot be deleted. (Read-only files cannot be removed from the input directory, and will interfere with queue directory lookups.)

The file sorting order can be customized by overriding findNextFile().

Specified by:
getNextCommand in interface ICommandQueue
Returns:
processing command, or null if queue is empty
Throws:
CatalanQueueException - if the input file is not writeable (must be able to delete it when done)
CatalanCommandException - if unable to process the command data
See Also:
findNextFile()

findNextFile

protected java.io.File findNextFile()
                             throws CatalanQueueException
Finds the next available command file in the input directory.

Returns:
the next file to process, or null if no files left
Throws:
CatalanQueueException - if the file is not writable

archiveFile

protected void archiveFile(java.io.File commandFile)
                    throws CatalanQueueException
Archives a completed command file. Moves it from the input directory to the output directory.

This method should rarely throw an exception, since in theory the findNextFile() command should verify that the file can be moved, but in practice it may happen if that check is imperfect.

Also, this may throw an exception if the file is null or doesn't exist; however, this is unlikely unless a subclass improperly overrides getNextCommand().

Parameters:
commandFile - completed command
Throws:
CatalanQueueException - if unable to move the file, or if file is null or does not exist