use of com.evolveum.midpoint.provisioning.api.ResourceOperationDescription in project midpoint by Evolveum.
the class ConfigurationExceptionHandler method handleError.
@Override
public <T extends ShadowType> T handleError(T shadow, FailedOperation op, Exception ex, boolean doDiscovery, boolean compensate, Task task, OperationResult parentResult) throws SchemaException, GenericFrameworkException, CommunicationException, ObjectNotFoundException, ObjectAlreadyExistsException, ConfigurationException {
if (!doDiscovery) {
parentResult.recordFatalError(ex);
if (ex instanceof ConfigurationException) {
throw (ConfigurationException) ex;
} else {
throw new ConfigurationException(ex.getMessage(), ex);
}
}
ObjectDelta delta = null;
switch(op) {
case ADD:
delta = ObjectDelta.createAddDelta(shadow.asPrismObject());
break;
case DELETE:
delta = ObjectDelta.createDeleteDelta(shadow.getClass(), shadow.getOid(), prismContext);
break;
case MODIFY:
Collection<? extends ItemDelta> modifications = null;
if (shadow.getObjectChange() != null) {
ObjectDeltaType deltaType = shadow.getObjectChange();
modifications = DeltaConvertor.toModifications(deltaType.getItemDelta(), shadow.asPrismObject().getDefinition());
}
delta = ObjectDelta.createModifyDelta(shadow.getOid(), modifications, shadow.getClass(), prismContext);
break;
case GET:
OperationResult operationResult = parentResult.createSubresult("com.evolveum.midpoint.provisioning.consistency.impl.ConfigurationExceptionHandler.handleError." + op.name());
operationResult.addParam("shadow", shadow);
operationResult.addParam("currentOperation", op);
operationResult.addParam("exception", ex.getMessage());
for (OperationResult subRes : parentResult.getSubresults()) {
subRes.muteError();
}
operationResult.recordPartialError("Could not get " + ObjectTypeUtil.toShortString(shadow) + " from the resource " + ObjectTypeUtil.toShortString(shadow.getResource()) + ", because of configuration error. Returning shadow from the repository");
shadow.setFetchResult(operationResult.createOperationResultType());
return shadow;
}
if (op != FailedOperation.GET) {
// Task task = taskManager.createTaskInstance();
ResourceOperationDescription operationDescription = createOperationDescription(shadow, ex, shadow.getResource(), delta, task, parentResult);
changeNotificationDispatcher.notifyFailure(operationDescription, task, parentResult);
}
if (shadow.getOid() == null) {
throw new ConfigurationException("Configuration error: " + ex.getMessage(), ex);
}
Collection<ItemDelta> modification = createAttemptModification(shadow, null);
try {
ConstraintsChecker.onShadowModifyOperation(modification);
cacheRepositoryService.modifyObject(shadow.asPrismObject().getCompileTimeClass(), shadow.getOid(), modification, parentResult);
} catch (Exception e) {
//this should not happen. But if it happens, we should return original exception
LOGGER.error("Unexpected error while modifying shadow {}: {}", shadow, e.getMessage(), e);
if (ex instanceof SchemaException) {
throw ((SchemaException) ex);
} else if (ex instanceof GenericFrameworkException) {
throw ((GenericFrameworkException) ex);
} else if (ex instanceof CommunicationException) {
throw ((CommunicationException) ex);
} else if (ex instanceof ObjectNotFoundException) {
throw ((ObjectNotFoundException) ex);
} else if (ex instanceof ObjectAlreadyExistsException) {
throw ((ObjectAlreadyExistsException) ex);
} else if (ex instanceof ConfigurationException) {
throw ((ConfigurationException) ex);
}
}
parentResult.recordFatalError("Configuration error: " + ex.getMessage(), ex);
throw new ConfigurationException("Configuration error: " + ex.getMessage(), ex);
}
use of com.evolveum.midpoint.provisioning.api.ResourceOperationDescription in project midpoint by Evolveum.
the class ErrorHandler method createOperationDescription.
protected ResourceOperationDescription createOperationDescription(ShadowType shadowType, Exception ex, ResourceType resource, ObjectDelta delta, Task task, OperationResult result) {
ResourceOperationDescription operationDescription = new ResourceOperationDescription();
operationDescription.setCurrentShadow(shadowType.asPrismObject());
if (resource != null) {
operationDescription.setResource(resource.asPrismObject());
}
if (task != null) {
operationDescription.setSourceChannel(task.getChannel());
}
operationDescription.setObjectDelta(delta);
// fill-in the message if necessary
// actually, the result shouldn't be null anyway
OperationResult storedResult = result != null ? result.clone() : new OperationResult("dummy");
storedResult.computeStatusIfUnknown();
if (storedResult.getMessage() == null && ex != null) {
storedResult.recordStatus(storedResult.getStatus(), ex.getMessage(), ex);
}
operationDescription.setResult(storedResult);
return operationDescription;
}
use of com.evolveum.midpoint.provisioning.api.ResourceOperationDescription in project midpoint by Evolveum.
the class ObjectNotFoundHandler method handleError.
@Override
public <T extends ShadowType> T handleError(T shadow, FailedOperation op, Exception ex, boolean doDiscovery, boolean compensate, Task task, OperationResult parentResult) throws SchemaException, GenericFrameworkException, CommunicationException, ObjectNotFoundException, ObjectAlreadyExistsException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
if (!doDiscovery) {
parentResult.recordFatalError(ex);
if (ex instanceof ObjectNotFoundException) {
throw (ObjectNotFoundException) ex;
} else {
throw new ObjectNotFoundException(ex.getMessage(), ex);
}
}
OperationResult result = parentResult.createSubresult("com.evolveum.midpoint.provisioning.consistency.impl.ObjectNotFoundHandler.handleError." + op.name());
result.addParam("shadow", shadow);
result.addParam("currentOperation", op);
if (ex.getMessage() != null) {
result.addParam("exception", ex.getMessage());
}
LOGGER.trace("Start compensating object not found situation while execution operation: {}", op.name(), ex);
// Task task = taskManager.createTaskInstance();
ObjectDelta delta = null;
switch(op) {
case DELETE:
LOGGER.debug("DISCOVERY: cannot find object {}. The operation in progress is DELETE, therefore just deleting the shadow", shadow);
LOGGER.trace("Deleting shadow from the repository.");
for (OperationResult subResult : parentResult.getSubresults()) {
subResult.muteError();
}
try {
cacheRepositoryService.deleteObject(ShadowType.class, shadow.getOid(), result);
} catch (ObjectNotFoundException e) {
LOGGER.debug("Cannot delete {} in consistency compensation (discovery): {} - this is probably harmless", shadow, e.getMessage());
}
String message = "Object was not found on the " + ObjectTypeUtil.toShortString(shadow.getResource()) + ". Shadow deleted from the repository to equalize the state on the resource and in the repository.";
parentResult.recordHandledError(message);
LOGGER.trace("Shadow deleted from the repository. Inconsistencies are now removed.");
result.computeStatus();
// parentResult status can be recomputed by the caller method
result.setStatus(OperationResultStatus.HANDLED_ERROR);
delta = ObjectDelta.createDeleteDelta(shadow.getClass(), shadow.getOid(), prismContext);
ResourceOperationDescription operationDescritpion = createOperationDescription(shadow, ex, shadow.getResource(), delta, task, result);
changeNotificationDispatcher.notifySuccess(operationDescritpion, task, result);
LOGGER.debug("DISCOVERY: cannot find object {}: DELETE operation handler done", shadow);
return shadow;
case MODIFY:
LOGGER.debug("DISCOVERY: cannot find object {}. The operation in progress is MODIFY, therefore initiating synchronization", shadow);
LOGGER.trace("Starting discovery to find out if the object should exist or not.");
OperationResult handleErrorResult = result.createSubresult("com.evolveum.midpoint.provisioning.consistency.impl.ObjectNotFoundHandler.handleError.discovery");
handleErrorResult.addContext(OperationResult.CONTEXT_RESOURCE, shadow.getResource());
ObjectDeltaType shadowModifications = shadow.getObjectChange();
Collection<? extends ItemDelta> modifications = DeltaConvertor.toModifications(shadowModifications.getItemDelta(), shadow.asPrismObject().getDefinition());
shadow.setDead(true);
Collection<? extends ItemDelta> deadDeltas = PropertyDelta.createModificationReplacePropertyCollection(ShadowType.F_DEAD, shadow.asPrismObject().getDefinition(), true);
ConstraintsChecker.onShadowModifyOperation(deadDeltas);
cacheRepositoryService.modifyObject(ShadowType.class, shadow.getOid(), deadDeltas, result);
ResourceObjectShadowChangeDescription change = createResourceObjectShadowChangeDescription(shadow, result);
try {
changeNotificationDispatcher.notifyChange(change, task, handleErrorResult);
} catch (RuntimeException e) {
handleErrorResult.recordFatalError(e);
result.computeStatus();
throw e;
}
handleErrorResult.computeStatus();
String oidVal = null;
String oid = findCreatedAccountOid(handleErrorResult, oidVal);
if (oid != null) {
LOGGER.trace("Found new oid {} as a return param from model. Probably the new shadow was created.", oid);
LOGGER.debug("DISCOVERY: object {} re-created, applying pending changes", shadow);
LOGGER.trace("Modifying re-created object by applying pending changes:\n{}", DebugUtil.debugDump(modifications));
try {
ProvisioningOperationOptions options = new ProvisioningOperationOptions();
options.setCompletePostponed(false);
options.setDoNotDiscovery(true);
provisioningService.modifyObject(ShadowType.class, oid, modifications, null, options, task, result);
parentResult.recordHandledError("Object was recreated and modifications were applied to newly created object.");
} catch (ObjectNotFoundException e) {
parentResult.recordHandledError("Modifications were not applied, because shadow was deleted by discovery. Repository state were refreshed and unused shadow was deleted.");
}
} else {
LOGGER.debug("DISCOVERY: object {} deleted, application of pending changes skipped", shadow);
parentResult.recordHandledError("Object was deleted by discovery. Modification were not applied.");
}
// We do not need the old shadow any more. Even if the object was re-created it has a new shadow now.
try {
cacheRepositoryService.deleteObject(ShadowType.class, shadow.getOid(), parentResult);
} catch (ObjectNotFoundException e) {
// delete the old shadow that was probably deleted from the
// user, or the new one was assigned
LOGGER.debug("Cannot delete {} in consistency compensation (discovery): {} - this is probably harmless", shadow, e.getMessage());
}
result.computeStatus();
if (parentResult.isHandledError()) {
// Ugly hack. We shouldn't set parentResult status in the first place, as it can be overriden by computeStatus/recomputeStatus called in the parent.
result.setStatus(OperationResultStatus.HANDLED_ERROR);
}
if (oid != null) {
shadowModifications.setOid(oid);
shadow.setOid(oid);
}
LOGGER.debug("DISCOVERY: cannot find object {}: MODIFY operation handler done", shadow);
return shadow;
case GET:
if (!compensate) {
LOGGER.trace("DISCOVERY: cannot find object {}, GET operation: handling skipped because discovery is disabled", shadow);
result.recordFatalError(ex.getMessage(), ex);
throw new ObjectNotFoundException(ex.getMessage(), ex);
}
LOGGER.debug("DISCOVERY: cannot find object {}. The operation in progress is GET, therefore initiating synchronization", shadow);
OperationResult handleGetErrorResult = result.createSubresult("com.evolveum.midpoint.provisioning.consistency.impl.ObjectNotFoundHandler.handleError.discovery");
handleGetErrorResult.addContext(OperationResult.CONTEXT_RESOURCE, shadow.getResource());
Collection<? extends ItemDelta> deadModification = PropertyDelta.createModificationReplacePropertyCollection(ShadowType.F_DEAD, shadow.asPrismObject().getDefinition(), true);
ConstraintsChecker.onShadowModifyOperation(deadModification);
try {
cacheRepositoryService.modifyObject(ShadowType.class, shadow.getOid(), deadModification, result);
} catch (ObjectNotFoundException e) {
// The shadow is not there. So we cannot mark it as dead.
LOGGER.debug("Cannot modify shadow {} in consistency compensation (discovery): {} - this is probably harmless", shadow, e.getMessage());
}
shadow.setDead(true);
ResourceObjectShadowChangeDescription getChange = createResourceObjectShadowChangeDescription(shadow, result);
if (task == null) {
task = taskManager.createTaskInstance();
}
try {
changeNotificationDispatcher.notifyChange(getChange, task, handleGetErrorResult);
} catch (RuntimeException e) {
LOGGER.trace("DISCOVERY: synchronization invoked for {} ended with an error {}", shadow, e);
handleGetErrorResult.recordFatalError(e);
result.computeStatus();
throw e;
}
// String oidVal = null;
handleGetErrorResult.computeStatus();
LOGGER.trace("DISCOVERY: synchronization invoked for {} finished with status {}", shadow, handleGetErrorResult.getStatus());
oid = findCreatedAccountOid(handleGetErrorResult, null);
try {
LOGGER.trace("DISCOVERY: deleting {}", shadow);
cacheRepositoryService.deleteObject(ShadowType.class, shadow.getOid(), result);
} catch (ObjectNotFoundException e) {
// delete the old shadow that was probably deleted from the
// user, or the new one was assigned
LOGGER.debug("Cannot delete {} in consistency compensation (discovery): {} - this is probably harmless", shadow, e.getMessage());
}
for (OperationResult subResult : parentResult.getSubresults()) {
subResult.muteError();
}
if (oid != null) {
PrismObject newShadow;
try {
LOGGER.trace("DISCOVERY: retrieving shadow {}", oid);
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(GetOperationOptions.createDoNotDiscovery());
newShadow = provisioningService.getObject(shadow.getClass(), oid, options, task, result);
LOGGER.trace("DISCOVERY: retrieved {}", newShadow);
} catch (ObjectNotFoundException e) {
String msg = "Strange thing did happen: new shadow (" + oid + ") was supposedly created for old shadow " + shadow + ", however the new shadow was not found: " + e.getMessage();
LOGGER.error(msg);
result.recordFatalError(msg, e);
parentResult.recordFatalError(msg);
throw new ObjectNotFoundException(msg, ex);
} finally {
result.computeStatus();
}
LOGGER.debug("DISCOVERY: object {} re-created as {}. GET operation handler done: returning new shadow", shadow, newShadow);
shadow = (T) newShadow.asObjectable();
parentResult.recordHandledError("Object was re-created by the discovery.");
// parentResult status can be recomputed by the caller method
result.setStatus(OperationResultStatus.HANDLED_ERROR);
return shadow;
} else {
parentResult.recordHandledError("Object was deleted by the discovery and the invalid link was removed from the user.");
result.computeStatus();
// parentResult status can be recomputed by the caller method
result.setStatus(OperationResultStatus.HANDLED_ERROR);
LOGGER.debug("DISCOVERY: object {} was deleted. GET operation handler done: throwing ObjectNotFoundException", shadow);
throw new ObjectNotFoundException(ex.getMessage(), ex);
}
default:
throw new ObjectNotFoundException(ex.getMessage(), ex);
}
}
use of com.evolveum.midpoint.provisioning.api.ResourceOperationDescription in project midpoint by Evolveum.
the class SimpleResourceObjectNotifier method getSubject.
@Override
protected String getSubject(Event event, GeneralNotifierType generalNotifierType, String transport, Task task, OperationResult result) {
ResourceObjectEvent resourceObjectEvent = (ResourceObjectEvent) event;
ResourceOperationDescription rod = resourceObjectEvent.getAccountOperationDescription();
ObjectDelta<ShadowType> delta = (ObjectDelta<ShadowType>) rod.getObjectDelta();
String objectTypeDescription = resourceObjectEvent.isShadowKind(ShadowKindType.ACCOUNT) ? "Account" : "Resource object";
if (delta.isAdd()) {
return objectTypeDescription + " creation notification";
} else if (delta.isModify()) {
return objectTypeDescription + " modification notification";
} else if (delta.isDelete()) {
return objectTypeDescription + " deletion notification";
} else {
return "(unknown resource object operation)";
}
}
use of com.evolveum.midpoint.provisioning.api.ResourceOperationDescription in project midpoint by Evolveum.
the class SimpleResourceObjectNotifier method getBody.
@Override
protected String getBody(ResourceObjectEvent event, SimpleResourceObjectNotifierType configuration, String transport, Task task, OperationResult result) {
boolean techInfo = Boolean.TRUE.equals(configuration.isShowTechnicalInformation());
StringBuilder body = new StringBuilder();
FocusType owner = (FocusType) event.getRequesteeObject();
ResourceOperationDescription rod = event.getOperationDescription();
// noinspection unchecked
ObjectDelta<ShadowType> delta = (ObjectDelta<ShadowType>) rod.getObjectDelta();
boolean isAccount = event.isShadowKind(ShadowKindType.ACCOUNT);
String objectTypeDescription = isAccount ? "account" : "resource object";
String userOrOwner = owner instanceof UserType ? "User" : "Owner";
body.append("Notification about ").append(objectTypeDescription).append("-related operation\n\n");
if (isAccount) {
if (owner != null) {
body.append(userOrOwner).append(": ").append(event.getRequesteeDisplayName());
body.append(" (").append(owner.getName()).append(", oid ").append(owner.getOid()).append(")\n");
} else {
body.append(userOrOwner).append(": unknown\n");
}
}
body.append("Notification created on: ").append(new Date()).append("\n\n");
body.append("Resource: ").append(event.getResourceName()).append(" (oid ").append(event.getResourceOid()).append(")\n");
boolean named;
if (rod.getCurrentShadow() != null && rod.getCurrentShadow().asObjectable().getName() != null) {
if (isAccount) {
body.append("Account: ").append(rod.getCurrentShadow().asObjectable().getName()).append("\n");
} else {
body.append("Resource object: ").append(rod.getCurrentShadow().asObjectable().getName()).append(" (kind: ").append(rod.getCurrentShadow().asObjectable().getKind()).append(")\n");
}
named = true;
} else {
named = false;
}
body.append("\n");
if (isAccount) {
body.append(named ? "The" : "An").append(" account ");
} else {
body.append(named ? "The" : "A").append(" resource object ");
}
switch(event.getOperationStatus()) {
case SUCCESS:
body.append("has been successfully ");
break;
case IN_PROGRESS:
body.append("has been ATTEMPTED to be ");
break;
case FAILURE:
body.append("FAILED to be ");
break;
}
final boolean watchSynchronizationAttributes = isWatchSynchronizationAttributes(configuration);
final boolean watchAuxiliaryAttributes = isWatchAuxiliaryAttributes(configuration);
if (delta.isAdd()) {
body.append("created on the resource with attributes:\n");
body.append(event.getContentAsFormattedList(watchSynchronizationAttributes, watchAuxiliaryAttributes));
body.append("\n");
} else if (delta.isModify()) {
body.append("modified on the resource. Modified attributes are:\n");
body.append(event.getContentAsFormattedList(watchSynchronizationAttributes, watchAuxiliaryAttributes));
body.append("\n");
} else if (delta.isDelete()) {
body.append("removed from the resource.\n\n");
}
if (event.getOperationStatus() == OperationStatus.IN_PROGRESS) {
body.append("The operation will be retried.\n\n");
} else if (event.getOperationStatus() == OperationStatus.FAILURE) {
body.append("Error: ").append(event.getOperationDescription().getResult().getMessage()).append("\n\n");
}
body.append("\n\n");
addRequesterAndChannelInformation(body, event, result);
if (techInfo) {
body.append("----------------------------------------\n");
body.append("Technical information:\n\n");
body.append(rod.debugDump(2));
}
return body.toString();
}
Aggregations