| | |
| | | * Portions Copyright 2006-2007 Sun Microsystems, Inc. |
| | | */ |
| | | package org.opends.server.backends.task; |
| | | import org.opends.messages.Message; |
| | | |
| | | |
| | | |
| | |
| | | import java.util.List; |
| | | import java.util.TimeZone; |
| | | import java.util.UUID; |
| | | import java.util.Collections; |
| | | import java.util.concurrent.locks.Lock; |
| | | import javax.mail.MessagingException; |
| | | |
| | |
| | | import org.opends.server.types.DirectoryException; |
| | | import org.opends.server.types.DN; |
| | | import org.opends.server.types.Entry; |
| | | import org.opends.server.types.ErrorLogCategory; |
| | | import org.opends.server.types.ErrorLogSeverity; |
| | | |
| | | |
| | | import org.opends.server.types.InitializationException; |
| | | import org.opends.server.types.Operation; |
| | | import org.opends.server.util.EMailMessage; |
| | |
| | | |
| | | import static org.opends.server.config.ConfigConstants.*; |
| | | import static org.opends.server.loggers.debug.DebugLogger.*; |
| | | import static org.opends.server.messages.BackendMessages.*; |
| | | import static org.opends.server.messages.MessageHandler.*; |
| | | import static org.opends.messages.BackendMessages.*; |
| | | |
| | | import static org.opends.server.util.ServerConstants.*; |
| | | import static org.opends.server.util.StaticUtils.*; |
| | | |
| | |
| | | private LinkedList<String> dependencyIDs; |
| | | |
| | | // A set of log messages generated by this task. |
| | | // TODO: convert from String to Message objects. |
| | | // Since these are stored in an entry we would need |
| | | // to adopt some way for writing message to string in such |
| | | // a way that the information could be reparsed from its |
| | | // string value. |
| | | private LinkedList<String> logMessages; |
| | | |
| | | // The set of e-mail addresses of the users to notify when the task is done |
| | |
| | | { |
| | | if (recurringTaskID == null) |
| | | { |
| | | int msgID = MSGID_TASK_MISSING_ATTR; |
| | | String message = getMessage(msgID, String.valueOf(taskEntry.getDN()), |
| | | ATTR_TASK_ID); |
| | | throw new InitializationException(msgID, message); |
| | | Message message = ERR_TASK_MISSING_ATTR.get( |
| | | String.valueOf(taskEntry.getDN()), ATTR_TASK_ID); |
| | | throw new InitializationException(message); |
| | | } |
| | | else |
| | | { |
| | |
| | | taskState = TaskState.fromString(stateString); |
| | | if (taskState == null) |
| | | { |
| | | int msgID = MSGID_TASK_INVALID_STATE; |
| | | String message = getMessage(msgID, taskDN, stateString); |
| | | throw new InitializationException(msgID, message); |
| | | Message message = ERR_TASK_INVALID_STATE.get(taskDN, stateString); |
| | | throw new InitializationException(message); |
| | | } |
| | | } |
| | | |
| | |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_TASK_CANNOT_PARSE_SCHEDULED_START_TIME; |
| | | String message = getMessage(msgID, timeString, taskDN); |
| | | throw new InitializationException(msgID, message, e); |
| | | Message message = |
| | | ERR_TASK_CANNOT_PARSE_SCHEDULED_START_TIME.get(timeString, taskDN); |
| | | throw new InitializationException(message, e); |
| | | } |
| | | } |
| | | |
| | |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_TASK_CANNOT_PARSE_ACTUAL_START_TIME; |
| | | String message = getMessage(msgID, timeString, taskDN); |
| | | throw new InitializationException(msgID, message, e); |
| | | Message message = |
| | | ERR_TASK_CANNOT_PARSE_ACTUAL_START_TIME.get(timeString, taskDN); |
| | | throw new InitializationException(message, e); |
| | | } |
| | | } |
| | | |
| | |
| | | TRACER.debugCaught(DebugLogLevel.ERROR, e); |
| | | } |
| | | |
| | | int msgID = MSGID_TASK_CANNOT_PARSE_COMPLETION_TIME; |
| | | String message = getMessage(msgID, timeString, taskDN); |
| | | throw new InitializationException(msgID, message, e); |
| | | Message message = |
| | | ERR_TASK_CANNOT_PARSE_COMPLETION_TIME.get(timeString, taskDN); |
| | | throw new InitializationException(message, e); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | if (isRequired) |
| | | { |
| | | int msgID = MSGID_TASK_MISSING_ATTR; |
| | | String message = getMessage(msgID, String.valueOf(taskEntry.getDN()), |
| | | attributeName); |
| | | throw new InitializationException(msgID, message); |
| | | Message message = ERR_TASK_MISSING_ATTR.get( |
| | | String.valueOf(taskEntry.getDN()), attributeName); |
| | | throw new InitializationException(message); |
| | | } |
| | | else |
| | | { |
| | |
| | | |
| | | if (attrList.size() > 1) |
| | | { |
| | | int msgID = MSGID_TASK_MULTIPLE_ATTRS_FOR_TYPE; |
| | | String message = getMessage(msgID, attributeName, |
| | | String.valueOf(taskEntry.getDN())); |
| | | throw new InitializationException(msgID, message); |
| | | Message message = ERR_TASK_MULTIPLE_ATTRS_FOR_TYPE.get( |
| | | attributeName, String.valueOf(taskEntry.getDN())); |
| | | throw new InitializationException(message); |
| | | } |
| | | |
| | | Iterator<AttributeValue> iterator = attrList.get(0).getValues().iterator(); |
| | |
| | | { |
| | | if (isRequired) |
| | | { |
| | | int msgID = MSGID_TASK_NO_VALUES_FOR_ATTR; |
| | | String message = getMessage(msgID, attributeName, |
| | | String.valueOf(taskEntry.getDN())); |
| | | throw new InitializationException(msgID, message); |
| | | Message message = ERR_TASK_NO_VALUES_FOR_ATTR.get( |
| | | attributeName, String.valueOf(taskEntry.getDN())); |
| | | throw new InitializationException(message); |
| | | } |
| | | else |
| | | { |
| | |
| | | AttributeValue value = iterator.next(); |
| | | if (iterator.hasNext()) |
| | | { |
| | | int msgID = MSGID_TASK_MULTIPLE_VALUES_FOR_ATTR; |
| | | String message = getMessage(msgID, attributeName, |
| | | String.valueOf(taskEntry.getDN())); |
| | | throw new InitializationException(msgID, message); |
| | | Message message = ERR_TASK_MULTIPLE_VALUES_FOR_ATTR.get( |
| | | attributeName, String.valueOf(taskEntry.getDN())); |
| | | throw new InitializationException(message); |
| | | } |
| | | |
| | | return value.getStringValue(); |
| | |
| | | |
| | | if (attrList.size() > 1) |
| | | { |
| | | int msgID = MSGID_TASK_MULTIPLE_ATTRS_FOR_TYPE; |
| | | String message = getMessage(msgID, attributeName); |
| | | throw new InitializationException(msgID, message); |
| | | Message message = ERR_TASK_MULTIPLE_ATTRS_FOR_TYPE.get( |
| | | attributeName, String.valueOf(taskEntry.getDN())); |
| | | throw new InitializationException(message); |
| | | } |
| | | |
| | | Iterator<AttributeValue> iterator = attrList.get(0).getValues().iterator(); |
| | |
| | | * |
| | | * @return The set of messages that were logged by this task. |
| | | */ |
| | | public final LinkedList<String> getLogMessages() |
| | | public final List<Message> getLogMessages() |
| | | { |
| | | return logMessages; |
| | | List<Message> msgList = new ArrayList<Message>(); |
| | | for(String logString : logMessages) { |
| | | // TODO: a better job or recreating the message |
| | | msgList.add(Message.raw(logString)); |
| | | } |
| | | return Collections.unmodifiableList(msgList); |
| | | } |
| | | |
| | | |
| | |
| | | * the one in <code>org.opends.server.loggers.Error</code> to ensure the |
| | | * messages are included in the ds-task-log-message attribute. |
| | | * |
| | | * @param category The category that may be used to determine whether to |
| | | * actually log this message. |
| | | * @param severity The severity that may be used to determine whether to |
| | | * actually log this message. |
| | | * @param errorID The error ID that uniquely identifies the provided format |
| | | * string. |
| | | */ |
| | | protected void logError(ErrorLogCategory category, ErrorLogSeverity severity, |
| | | int errorID) |
| | | { |
| | | // Simply pass this on to the server error logger, and it will call back |
| | | // to the addLogMessage method for this task. |
| | | ErrorLogger.logError(category, severity, errorID); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Writes a message to the error log using the provided information. |
| | | * Tasks should use this method to log messages to the error log instead of |
| | | * the one in <code>org.opends.server.loggers.Error</code> to ensure the |
| | | * messages are included in the ds-task-log-message attribute. |
| | | * |
| | | * @param category The category that may be used to determine whether to |
| | | * actually log this message. |
| | | * @param severity The severity that may be used to determine whether to |
| | | * actually log this message. |
| | | * @param errorID The error ID that uniquely identifies the provided format |
| | | * string. |
| | | * @param args The set of arguments to use for the provided format |
| | | * string. |
| | | */ |
| | | protected void logError(ErrorLogCategory category, ErrorLogSeverity severity, |
| | | int errorID, Object... args) |
| | | { |
| | | // Simply pass this on to the server error logger, and it will call back |
| | | // to the addLogMessage method for this task. |
| | | ErrorLogger.logError(category, severity, errorID, args); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Writes a message to the error log using the provided information. |
| | | * Tasks should use this method to log messages to the error log instead of |
| | | * the one in <code>org.opends.server.loggers.Error</code> to ensure the |
| | | * messages are included in the ds-task-log-message attribute. |
| | | * |
| | | * @param category The category that may be used to determine whether to |
| | | * actually log this message. |
| | | * @param severity The severity that may be used to determine whether to |
| | | * actually log this message. |
| | | * @param message The message to be logged. |
| | | * @param errorID The error ID that uniquely identifies the format string |
| | | * used to generate the provided message. |
| | | */ |
| | | protected void logError(ErrorLogCategory category, ErrorLogSeverity severity, |
| | | String message, int errorID) |
| | | protected void logError(Message message) |
| | | { |
| | | // Simply pass this on to the server error logger, and it will call back |
| | | // to the addLogMessage method for this task. |
| | | ErrorLogger.logError(category, severity, message, errorID); |
| | | ErrorLogger.logError(message); |
| | | } |
| | | |
| | | |
| | |
| | | * indirectly through the {@code ErrorLog.logError} methods. It does not |
| | | * automatically persist the updated task information to disk. |
| | | * |
| | | * @param severity The severity level for the log message. |
| | | * @param messageID The ID that uniquely identifies the log message. |
| | | * @param messageString The text of the log message |
| | | * @param message he log message |
| | | */ |
| | | public void addLogMessage(ErrorLogSeverity severity, int messageID, |
| | | String messageString) |
| | | public void addLogMessage(Message message) |
| | | { |
| | | // We only need to grab the entry-level lock if we don't already hold the |
| | | // broader scheduler lock. |
| | |
| | | buffer.append("["); |
| | | buffer.append(TimeThread.getLocalTime()); |
| | | buffer.append("] severity=\""); |
| | | buffer.append(severity.getSeverityName()); |
| | | buffer.append(message.getDescriptor().getSeverity().name()); |
| | | buffer.append("\" msgCount="); |
| | | buffer.append(logMessageCounter++); |
| | | buffer.append(" msgID="); |
| | | buffer.append(messageID); |
| | | buffer.append(message.getDescriptor().getOrdinal()); |
| | | buffer.append(" message=\""); |
| | | buffer.append(messageString); |
| | | buffer.append(message.toString()); |
| | | buffer.append("\""); |
| | | |
| | | String message = buffer.toString(); |
| | | logMessages.add(message); |
| | | String messageString = buffer.toString(); |
| | | logMessages.add(messageString); |
| | | |
| | | |
| | | AttributeType type = DirectoryServer.getAttributeType( |
| | |
| | | |
| | | setTaskState(TaskState.STOPPED_BY_ERROR); |
| | | |
| | | int msgID = MSGID_TASK_EXECUTE_FAILED; |
| | | String message = getMessage(msgID, String.valueOf(taskEntry.getDN()), |
| | | stackTraceToSingleLineString(e)); |
| | | ErrorLogger.logError(ErrorLogCategory.TASK, ErrorLogSeverity.SEVERE_ERROR, |
| | | message, msgID); |
| | | Message message = ERR_TASK_EXECUTE_FAILED.get( |
| | | String.valueOf(taskEntry.getDN()), stackTraceToSingleLineString(e)); |
| | | logError(message); |
| | | } |
| | | finally |
| | | { |
| | |
| | | String actualStartDate = new Date(actualStartTime).toString(); |
| | | String completionDate = new Date(completionTime).toString(); |
| | | |
| | | message.setBody(getMessage(MSGID_TASK_COMPLETION_BODY, taskID, |
| | | message.setBody(INFO_TASK_COMPLETION_BODY.get(taskID, |
| | | String.valueOf(taskState), |
| | | scheduledStartDate, actualStartDate, |
| | | completionDate)); |
| | |
| | | * successfully interrupted. |
| | | * @param interruptReason A human-readable explanation for the cancellation. |
| | | */ |
| | | public void interruptTask(TaskState interruptState, String interruptReason) |
| | | public void interruptTask(TaskState interruptState, Message interruptReason) |
| | | { |
| | | // No action is performed by default. |
| | | } |