RecurringTask.java revision a23343e9e4e0b555b1bcfa99a7455e0e28117a3d
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2010 Sun Microsystems, Inc.
* Portions Copyright 2014-2015 ForgeRock AS
*/
/**
* This class defines a information about a recurring task, which will be used
* to repeatedly schedule tasks for processing.
* <br>
* It also provides some static methods that allow to validate strings in
* crontab (5) format.
*/
public class RecurringTask
{
/** The DN of the entry that actually defines this task. */
private final DN recurringTaskEntryDN;
/** The entry that actually defines this task. */
private final Entry recurringTaskEntry;
/** The unique ID for this recurring task. */
private final String recurringTaskID;
/**
* The fully-qualified name of the class that will be used to implement the
* class.
*/
private final String taskClassName;
/** Task instance. */
/** Task scheduler for this task. */
private final TaskScheduler taskScheduler;
/** Number of tokens in the task schedule tab. */
private static final int TASKTAB_NUM_TOKENS = 5;
/** Maximum year month days. */
static final int MONTH_LENGTH[]
= {31,28,31,30,31,30,31,31,30,31,30,31};
/** Maximum leap year month days. */
static final int LEAP_MONTH_LENGTH[]
= {31,29,31,30,31,30,31,31,30,31,30,31};
/** Task tab fields. */
private static final int MINUTE_INDEX = 0;
private static final int HOUR_INDEX = 1;
private static final int DAY_INDEX = 2;
private static final int MONTH_INDEX = 3;
private static final int WEEKDAY_INDEX = 4;
/** Wildcard match pattern. */
/** Exact match pattern. */
/** Range match pattern. */
/** Boolean arrays holding task tab slots. */
private final boolean[] minutesArray;
private final boolean[] hoursArray;
private final boolean[] daysArray;
private final boolean[] monthArray;
private final boolean[] weekdayArray;
private final ServerContext serverContext;
/**
* Creates a new recurring task based on the information in the provided
* entry.
*
* @param serverContext
* The server context.
*
* @param taskScheduler A reference to the task scheduler that may be
* used to schedule new tasks.
* @param recurringTaskEntry The entry containing the information to use to
* define the task to process.
*
* @throws DirectoryException If the provided entry does not contain a valid
* recurring task definition.
*/
public RecurringTask(ServerContext serverContext, TaskScheduler taskScheduler, Entry recurringTaskEntry)
throws DirectoryException
{
this.serverContext = serverContext;
this.taskScheduler = taskScheduler;
this.recurringTaskEntry = recurringTaskEntry;
// Get the recurring task ID from the entry. If there isn't one, then fail.
{
}
{
}
{
}
{
}
{
}
// Get the schedule for this task.
{
}
{
}
{
}
{
}
{
LocalizableMessage message = ERR_RECURRINGTASK_MULTIPLE_SCHEDULE_VALUES.get(ATTR_RECURRING_TASK_SCHEDULE);
}
// Get the class name from the entry. If there isn't one, then fail.
{
}
{
}
{
}
{
}
{
}
// Make sure that the specified class can be loaded.
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
// Make sure that the specified class can be instantiated as a task.
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
// Make sure that we can initialize the task with the information in the
// provided entry.
try
{
}
catch (InitializationException ie)
{
LocalizableMessage message = ERR_RECURRINGTASK_CANNOT_INITIALIZE_INTERNAL.get( taskClassName, ie.getMessage());
}
}
/**
* Retrieves the unique ID assigned to this recurring task.
*
* @return The unique ID assigned to this recurring task.
*/
public String getRecurringTaskID()
{
return recurringTaskID;
}
/**
* Retrieves the DN of the entry containing the data for this recurring task.
*
* @return The DN of the entry containing the data for this recurring task.
*/
public DN getRecurringTaskEntryDN()
{
return recurringTaskEntryDN;
}
/**
* Retrieves the entry containing the data for this recurring task.
*
* @return The entry containing the data for this recurring task.
*/
public Entry getRecurringTaskEntry()
{
return recurringTaskEntry;
}
/**
* Retrieves the fully-qualified name of the Java class that provides the
* implementation logic for this recurring task.
*
* @return The fully-qualified name of the Java class that provides the
* implementation logic for this recurring task.
*/
public String getTaskClassName()
{
return taskClassName;
}
/**
* Schedules the next iteration of this recurring task for processing.
* @param calendar date and time to schedule next iteration from.
* @return The task that has been scheduled for processing.
* @throws DirectoryException to indicate an error.
*/
throws DirectoryException
{
try {
} catch (IllegalArgumentException e) {
logger.traceException(e);
}
try {
// Make a regular task iteration from this recurring task.
"scheduled-start-time";
} catch (Exception e) {
// Should not happen, debug log it otherwise.
logger.traceException(e);
}
return nextTask;
}
/**
* Parse and validate recurring task schedule.
* @param taskSchedule recurring task schedule tab in crontab(5) format.
* @throws DirectoryException to indicate an error.
*/
{
false);
}
/**
* Parse and validate recurring task schedule.
* @param taskSchedule recurring task schedule tab in crontab(5) format.
* @param arrays an array of 5 boolean arrays. The array has the following
* structure: {minutesArray, hoursArray, daysArray, monthArray, weekdayArray}.
* @param referToTaskEntryAttribute whether the error messages must refer
* to the task entry attribute or not. This is used to have meaningful
* messages when the {@link #parseTaskTab(String)} is called to validate
* a crontab formatted string.
* @throws DirectoryException to indicate an error.
*/
boolean referToTaskEntryAttribute) throws DirectoryException
{
{
}
else
{
}
}
switch (taskTabToken) {
case MINUTE:
try {
} catch (IllegalArgumentException e) {
{
}
else
{
}
}
break;
case HOUR:
try {
} catch (IllegalArgumentException e) {
{
}
else
{
}
}
break;
case DAY:
try {
} catch (IllegalArgumentException e) {
{
}
else
{
}
}
break;
case MONTH:
try {
} catch (IllegalArgumentException e) {
{
}
else
{
}
}
break;
case WEEKDAY:
try {
} catch (IllegalArgumentException e) {
{
}
else
{
}
}
break;
}
}
}
/**
* Parse and validate recurring task schedule field.
*
* @param tabField recurring task schedule field in crontab(5) format.
* @param minValue minimum value allowed for this field.
* @param maxValue maximum value allowed for this field.
* @return boolean schedule slots range set according to the schedule field.
* @throws IllegalArgumentException if tab field is invalid.
*/
{
// Wildcard with optional increment.
{
{
valueList[i] = true;
}
return valueList;
}
// List.
{
// Single number.
{
if (isValueAbsent(exactValue))
{
throw new IllegalArgumentException();
}
{
throw new IllegalArgumentException();
}
continue;
}
// Range of numbers with optional increment.
{
throw new IllegalArgumentException();
}
{
throw new IllegalArgumentException();
}
{
valueList[i] = true;
}
continue;
}
// Can only have a list of numbers and ranges.
throw new IllegalArgumentException();
}
return valueList;
}
/**
* Check if a String from a Matcher group is absent. Matcher returns empty strings
* for optional groups that are absent.
*
* @param s A string returned from Matcher.group()
* @return true if the string is unusable, false if it is usable.
*/
private static boolean isValueAbsent(String s)
{
}
/**
* Get next recurring slot from the range.
* @param timesList the range.
* @param fromNow the current slot.
* @return next recurring slot in the range.
*/
{
if (timesList[i]) {
return i;
}
}
return -1;
}
/**
* Get next task iteration date according to recurring schedule.
* @param calendar date and time to schedule from.
* @return next task iteration date.
* @throws IllegalArgumentException if recurring schedule is invalid.
*/
throws IllegalArgumentException
{
calendar.setLenient(false);
// Weekday
for (;;) {
// Month
for (;;) {
// Day
for (;;) {
// Hour
for (;;) {
// Minute
for (;;) {
if (minute == -1) {
} else {
break;
}
}
if (hour == -1) {
} else {
break;
}
}
{
} else {
break;
}
}
if (month == -1) {
}
{
} else {
break;
}
}
if (weekday == -1
{
} else {
break;
}
}
}
}