}
}
@Override
public TaskExec execute(final PropagationTask task, final PropagationReporter reporter) {
final PropagationActions actions = getPropagationActions(task.getResource());
final Date startDate = new Date();
final TaskExec execution = new TaskExec();
execution.setStatus(PropagationTaskExecStatus.CREATED.name());
String taskExecutionMessage = null;
String failureReason = null;
// Flag to state whether any propagation has been attempted
Set<String> propagationAttempted = new HashSet<String>();
ConnectorObject beforeObj = null;
ConnectorObject afterObj = null;
Connector connector = null;
Result result;
try {
connector = connLoader.getConnector(task.getResource());
// Try to read remote object (user / group) BEFORE any actual operation
beforeObj = getRemoteObject(task, connector, false);
actions.before(task, beforeObj);
switch (task.getPropagationOperation()) {
case CREATE:
case UPDATE:
createOrUpdate(task, beforeObj, connector, propagationAttempted);
break;
case DELETE:
delete(task, beforeObj, connector, propagationAttempted);
break;
default:
}
execution.setStatus(task.getPropagationMode() == PropagationMode.ONE_PHASE
? PropagationTaskExecStatus.SUCCESS.name()
: PropagationTaskExecStatus.SUBMITTED.name());
LOG.debug("Successfully propagated to {}", task.getResource());
result = Result.SUCCESS;
} catch (Exception e) {
result = Result.FAILURE;
LOG.error("Exception during provision on resource " + task.getResource().getName(), e);
if (e instanceof ConnectorException && e.getCause() != null) {
taskExecutionMessage = e.getCause().getMessage();
failureReason = e.getMessage() + "\n\n Cause: " + e.getCause().getMessage().split("\n")[0];
} else {
StringWriter exceptionWriter = new StringWriter();
exceptionWriter.write(e.getMessage() + "\n\n");
e.printStackTrace(new PrintWriter(exceptionWriter));
taskExecutionMessage = exceptionWriter.toString();
if (e.getCause() == null) {
failureReason = e.getMessage();
} else {
failureReason = e.getMessage() + "\n\n Cause: " + e.getCause().getMessage().split("\n")[0];
}
}
try {
execution.setStatus(task.getPropagationMode() == PropagationMode.ONE_PHASE
? PropagationTaskExecStatus.FAILURE.name()
: PropagationTaskExecStatus.UNSUBMITTED.name());
} catch (Exception wft) {
LOG.error("While executing KO action on {}", execution, wft);
}
propagationAttempted.add(task.getPropagationOperation().name().toLowerCase());
} finally {
// Try to read remote object (user / group) AFTER any actual operation
if (connector != null) {
try {
afterObj = getRemoteObject(task, connector, true);
} catch (Exception ignore) {
// ignore exception
LOG.error("Error retrieving after object", ignore);
}
}
LOG.debug("Update execution for {}", task);
execution.setStartDate(startDate);
execution.setMessage(taskExecutionMessage);
execution.setEndDate(new Date());
if (hasToBeregistered(task, execution)) {
if (propagationAttempted.isEmpty()) {
LOG.debug("No propagation attempted for {}", execution);
} else {
execution.setTask(task);
task.addExec(execution);
LOG.debug("Execution finished: {}", execution);
}
taskDAO.save(task);
// this flush call is needed to generate a value for the execution id
taskDAO.flush();
}
if (reporter != null) {
reporter.onSuccessOrSecondaryResourceFailures(
task.getResource().getName(),
PropagationTaskExecStatus.valueOf(execution.getStatus()),
failureReason,
beforeObj,
afterObj);
}
}
actions.after(task, execution, afterObj);
notificationManager.createTasks(
AuditElements.EventCategoryType.PROPAGATION,
task.getSubjectType().name().toLowerCase(),
task.getResource().getName(),