uws.job.manager
Class DefaultDestructionManager

java.lang.Object
  extended by uws.job.manager.DefaultDestructionManager
All Implemented Interfaces:
java.io.Serializable, DestructionManager

public class DefaultDestructionManager
extends java.lang.Object
implements DestructionManager

The default implementation of the DestructionManager interface. Its goal is to manage the automatic destruction any given jobs.

Jobs can be added thanks to update(AbstractJob) and removed with remove(AbstractJob). All added jobs are stored in a TreeSet which sorts them by ascending destruction time. The job which must be destroyed in first is used to start a timer. This one will destroyed the job once its destruction time is reached.

The list of jobs to destroy is supposed to be updated each time the destruction time of a job is changed. This update works only if the job knows its jobs list (AbstractJob.getJobList() != null) and its jobs list has a destruction manager.

Version:
02/2011
Author:
Grégory Mantelet (CDS)
See Also:
Serialized Form

Nested Class Summary
protected static class DefaultDestructionManager.TimeComparator
          Lets a TreeSet or a TreeMap sorting AbstractJob instances in an ascending order and according to their destruction time.
 
Field Summary
protected  java.util.Date currentDate
          The date used by the timer to trigger the destruction of currentJob.
protected  AbstractJob currentJob
          The job currently planned for destruction.
protected  java.util.TreeSet<AbstractJob> jobsToDestroy
          The list of jobs to destroy.
protected  java.util.Timer timDestruction
          Timer for the job destruction time.
 
Constructor Summary
DefaultDestructionManager()
          Builds the default destruction manager.
 
Method Summary
protected  void destroyJob(AbstractJob job)
          Merely destroys the given job (if not null).
 int getNbJobsToDestroy()
          Gets the total number of jobs planned to be destroyed.
 java.util.Date getNextDestruction()
          Gets the destruction date/time of the job currently planned for destruction.
 java.lang.String getNextJobToDestroy()
          Gets the ID of the job currently planned for destruction.
 boolean isRunning()
          Returns true if currentDate is different from null.
 void refresh()
           First, check the currently planned job: if currentDate is past, the timer is stopped.
 void remove(AbstractJob job)
          Merely removes the given job from the list of jobs to destroyed.
protected  void stop()
          Stops the timer if running and set to null timDestruction, currentDate and currentJob.
 void update(AbstractJob job)
          This function does something only if the given job knows its jobs list and has a valid destruction time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

jobsToDestroy

protected final java.util.TreeSet<AbstractJob> jobsToDestroy
The list of jobs to destroy. Jobs are sorted by ascending destruction time thanks to DefaultDestructionManager.TimeComparator.


timDestruction

protected transient java.util.Timer timDestruction
Timer for the job destruction time. Once the date-time indicated by this timer is reached the job is destroyed and removed from the jobs list.


currentJob

protected AbstractJob currentJob
The job currently planned for destruction. This job will be destroyed by the timer timDestruction.


currentDate

protected java.util.Date currentDate
The date used by the timer to trigger the destruction of currentJob.

Constructor Detail

DefaultDestructionManager

public DefaultDestructionManager()
Builds the default destruction manager. The list of jobs to destroy is initialized so that sorting jobs by ascending destruction time.

See Also:
DefaultDestructionManager.TimeComparator
Method Detail

stop

protected final void stop()
Stops the timer if running and set to null timDestruction, currentDate and currentJob.


destroyJob

protected final void destroyJob(AbstractJob job)
Merely destroys the given job (if not null).

Parameters:
job - The job to destroy.
See Also:
AbstractJob.getJobList(), JobList.destroyJob(String)

isRunning

public final boolean isRunning()

Returns true if currentDate is different from null.

Note: If this object has just been deserialized, timDestruction is NULL. In this case this function resets it, obviously if it is supposed to run (currentDate != null).

Specified by:
isRunning in interface DestructionManager
Returns:
true if a destruction is currently planned, false otherwise.
See Also:
DestructionManager.isRunning()

getNextDestruction

public final java.util.Date getNextDestruction()
Description copied from interface: DestructionManager
Gets the destruction date/time of the job currently planned for destruction.

Specified by:
getNextDestruction in interface DestructionManager
Returns:
The time of the currently planned destruction.

getNextJobToDestroy

public final java.lang.String getNextJobToDestroy()
Description copied from interface: DestructionManager
Gets the ID of the job currently planned for destruction.

Specified by:
getNextJobToDestroy in interface DestructionManager
Returns:
The ID of the job to destroy.

getNbJobsToDestroy

public final int getNbJobsToDestroy()
Description copied from interface: DestructionManager
Gets the total number of jobs planned to be destroyed.

Specified by:
getNbJobsToDestroy in interface DestructionManager
Returns:
The jobs to destroy.

refresh

public void refresh()
  1. First, check the currently planned job:
    • if currentDate is past, the timer is stopped.
    • else if another job must be destroyed before the current one or if the destruction time of the current job has been changed, the timer is stopped and the job is re-added into the list of jobs to destroy.
  2. Then, only if there is no currently planned job:
    • Clean the list of jobs to destroy (jobs which are supposed to be destroyed are destroyed and the others are only removed from this manager).
    • Restart the timer with the first job to destroy.

Specified by:
refresh in interface DestructionManager
See Also:
DestructionManager.refresh(), stop(), destroyJob(AbstractJob)

update

public void update(AbstractJob job)
This function does something only if the given job knows its jobs list and has a valid destruction time. Then there are two cases:
  1. The destruction time of the given job is past: the job is destroyed (see destroyJob(AbstractJob)).
  2. Otherwise: the job is removed and then added to the list of jobs to destroy. Finally this manager is refreshed (see refresh()).

Specified by:
update in interface DestructionManager
Parameters:
job - The job whose the destruction time may have changed.
See Also:
DestructionManager.update(uws.job.AbstractJob), destroyJob(AbstractJob), refresh()

remove

public void remove(AbstractJob job)
Merely removes the given job from the list of jobs to destroyed. However if the given job is the currently planned job, the timer is stopped and the manager is refreshed.

Specified by:
remove in interface DestructionManager
Parameters:
job - The job to remove.
See Also:
DestructionManager.remove(uws.job.AbstractJob), stop(), refresh()