* @return TaskExecution.
*/
public TaskExec execute(final PropagationTask task, final PropagationHandler handler) {
final Date startDate = new Date();
TaskExec execution = new TaskExec();
execution.setStatus(PropagationTaskExecStatus.CREATED.name());
String taskExecutionMessage = null;
// Flag to state whether any propagation has been attempted
Set<String> propagationAttempted = new HashSet<String>();
ConnectorObject before = null;
ConnectorObject after = null;
try {
final ConnectorFacadeProxy connector = connLoader.getConnector(task.getResource());
if (connector == null) {
throw new NoSuchBeanDefinitionException(String.format(
"Connector instance bean for resource %s not found", task.getResource()));
}
// Try to read user BEFORE any actual operation
before = getRemoteObject(connector, task, false);
try {
switch (task.getPropagationOperation()) {
case CREATE:
case UPDATE:
// set of attributes to be propagated
final Set<Attribute> attributes = new HashSet<Attribute>(task.getAttributes());
if (before == null) {
// 1. get accountId
final String accountId = task.getAccountId();
// 2. get name
final Name name = (Name) AttributeUtil.find(Name.NAME, attributes);
// 3. check if:
// * accountId is not blank;
// * accountId is not equal to Name.
if (StringUtils.hasText(accountId)
&& (name == null || !accountId.equals(name.getNameValue()))) {
// 3.a retrieve uid
final Uid uid = (Uid) AttributeUtil.find(Uid.NAME, attributes);
// 3.b add Uid if not provided
if (uid == null) {
attributes.add(AttributeBuilder.build(Uid.NAME, Collections.singleton(accountId)));
}
}
// 4. provision entry
connector.create(task.getPropagationMode(), ObjectClass.ACCOUNT, attributes, null,
propagationAttempted);
} else {
// 1. check if rename is really required
final Name newName = (Name) AttributeUtil.find(Name.NAME, attributes);
LOG.debug("Rename required with value {}", newName);
if (newName != null && newName.equals(before.getName())
&& !before.getUid().getUidValue().equals(newName.getNameValue())) {
LOG.debug("Remote object name unchanged");
attributes.remove(newName);
}
LOG.debug("Attributes to be replaced {}", attributes);
// 2. update with a new "normalized" attribute set
connector.update(task.getPropagationMode(), ObjectClass.ACCOUNT, before.getUid(),
attributes, null, propagationAttempted);
}
break;
case DELETE:
if (before == null) {
LOG.debug("{} not found on external resource: ignoring delete", task.getAccountId());
} else {
/*
* We must choose here whether to a. actually delete the provided user from the external
* resource b. just update the provided user data onto the external resource
*
* (a) happens when either there is no user associated with the PropagationTask (this takes
* place when the task is generated via UserController.delete()) or the provided updated
* user hasn't the current resource assigned (when the task is generated via
* UserController.update()).
*
* (b) happens when the provided updated user does have the current resource assigned (when
* the task is generated via UserController.update()): this basically means that before such
* update, this user used to have the current resource assigned by more than one mean (for
* example, two different memberships with the same resource).
*/
SyncopeUser user = null;
if (task.getSyncopeUser() != null) {
try {
user = getSyncopeUser(task.getSyncopeUser().getId());
} catch (NotFoundException e) {
LOG.warn("Requesting to delete a non-existing user from {}",
task.getResource().getName(), e);
}
}
if (user == null || !user.getResourceNames().contains(task.getResource().getName())) {
LOG.debug("Perform deprovisioning on {}", task.getResource().getName());
connector.delete(
task.getPropagationMode(),
ObjectClass.ACCOUNT,
before.getUid(),
null,
propagationAttempted);
} else {
LOG.debug("Update remote object on {}", task.getResource().getName());
connector.update(
task.getPropagationMode(),
ObjectClass.ACCOUNT,
before.getUid(),
task.getAttributes(),
null,
propagationAttempted);
}
}
break;
default:
}
execution.setStatus(task.getPropagationMode() == PropagationMode.ONE_PHASE
? PropagationTaskExecStatus.SUCCESS.name()
: PropagationTaskExecStatus.SUBMITTED.name());
LOG.debug("Successfully propagated to {}", task.getResource());
// Try to read user AFTER any actual operation
after = getRemoteObject(connector, task, true);
} catch (Exception e) {
after = getRemoteObject(connector, task, false);
throw e;
}
} catch (Exception e) {
LOG.error("Exception during provision on resource " + task.getResource().getName(), e);
if (e instanceof ConnectorException && e.getCause() != null) {
taskExecutionMessage = e.getCause().getMessage();
} else {
StringWriter exceptionWriter = new StringWriter();
exceptionWriter.write(e.getMessage() + "\n\n");
e.printStackTrace(new PrintWriter(exceptionWriter));
taskExecutionMessage = exceptionWriter.toString();
}
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 {
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
// An alternative to this would be the following statement that might cause troubles with
// concurrent calls.
// taskExecDAO.findLatestStarted(task);
taskDAO.flush();
}
}
if (handler != null) {
handler.handle(
task.getResource().getName(),
PropagationTaskExecStatus.valueOf(execution.getStatus()),
before,
after);
}
return execution;