Search in sources :

Example 1 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.

the class DefaultEntityEventManager method executeEvent.

@Override
@Transactional
public void executeEvent(IdmEntityEventDto event) {
    Assert.notNull(event);
    Assert.notNull(event.getOwnerId());
    if (!eventConfiguration.isAsynchronous()) {
        // synchronous processing
        // we don't persist events and their states
        process(new CoreEvent<>(EntityEventType.EXECUTE, event));
        return;
    }
    if (event.getPriority() == PriorityType.IMMEDIATE) {
        // synchronous processing
        // we don't persist events and their states
        // TODO: what about running event with the same owner? And events in queue for the same owner
        process(new CoreEvent<>(EntityEventType.EXECUTE, event));
        return;
    }
    // 
    if (runningOwnerEvents.putIfAbsent(event.getOwnerId(), event.getId()) != null) {
        LOG.debug("Previous event [{}] for owner with id [{}] is currently processed.", runningOwnerEvents.get(event.getOwnerId()), event.getOwnerId());
        // event will be processed in another scheduling
        return;
    }
    // check super owner is not processed
    UUID superOwnerId = event.getProperties().getUuid(EntityEventManager.EVENT_PROPERTY_SUPER_OWNER_ID);
    if (superOwnerId != null && !superOwnerId.equals(event.getOwnerId())) {
        if (runningOwnerEvents.putIfAbsent(superOwnerId, event.getId()) != null) {
            LOG.debug("Previous event [{}] for super owner with id [{}] is currently processed.", runningOwnerEvents.get(superOwnerId), superOwnerId);
            runningOwnerEvents.remove(event.getOwnerId());
            // event will be processed in another scheduling
            return;
        }
    }
    // execute event in new thread asynchronously
    try {
        eventConfiguration.getExecutor().execute(new Runnable() {

            @Override
            public void run() {
                try {
                    process(new CoreEvent<>(EntityEventType.EXECUTE, event));
                } catch (Exception ex) {
                    // exception handling only ... all processor should persist their own entity state (see AbstractEntityEventProcessor)
                    ResultModel resultModel;
                    if (ex instanceof ResultCodeException) {
                        resultModel = ((ResultCodeException) ex).getError().getError();
                    } else {
                        resultModel = new DefaultResultModel(CoreResultCode.EVENT_EXECUTE_FAILED, ImmutableMap.of("eventId", event.getId(), "eventType", String.valueOf(event.getEventType()), "ownerId", String.valueOf(event.getOwnerId()), "instanceId", String.valueOf(event.getInstanceId())));
                    }
                    context.getBean(DefaultEntityEventManager.this.getClass()).saveResult(event.getId(), new OperationResultDto.Builder(OperationState.EXCEPTION).setCause(ex).setModel(resultModel).build());
                    LOG.error(resultModel.toString(), ex);
                } finally {
                    LOG.trace("Event [{}] ends for owner with id [{}].", event.getId(), event.getOwnerId());
                    removeRunningEvent(event);
                }
            }
        });
        // 
        LOG.trace("Running event [{}] for owner with id [{}].", event.getId(), event.getOwnerId());
    } catch (RejectedExecutionException ex) {
        // thread pool is full - wait for another try
        // TODO: Thread.wait(300) ?
        removeRunningEvent(event);
    }
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) UUID(java.util.UUID) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) EventContentDeletedException(eu.bcvsolutions.idm.core.api.exception.EventContentDeletedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.

the class DefaultLongRunningTaskManager method createTaskExecutor.

/**
 * Create new LongRunningTaskExecutor from given LRT.
 * Handles exceptions, when task already processed, task type is removed or task initialization failed
 *
 * @param task
 * @return
 */
private LongRunningTaskExecutor<?> createTaskExecutor(IdmLongRunningTaskDto task) {
    Assert.notNull(task, "Long running task instance is required!");
    if (!OperationState.isRunnable(task.getResultState())) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_IS_PROCESSED, ImmutableMap.of("taskId", task.getId()));
    }
    // 
    LongRunningTaskExecutor<?> taskExecutor = null;
    ResultModel resultModel = null;
    Exception ex = null;
    try {
        taskExecutor = (LongRunningTaskExecutor<?>) AutowireHelper.createBean(Class.forName(task.getTaskType()));
        taskExecutor.setLongRunningTaskId(task.getId());
        taskExecutor.init((Map<String, Object>) task.getTaskProperties());
    } catch (ClassNotFoundException e) {
        ex = e;
        resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_NOT_FOUND, ImmutableMap.of("taskId", task.getId(), "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
    } catch (Exception e) {
        ex = e;
        resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_INIT_FAILED, ImmutableMap.of("taskId", task.getId(), "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
    }
    if (ex != null) {
        LOG.error(resultModel.toString(), ex);
        task.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setModel(resultModel).setCause(ex).build());
        service.save(task);
        return null;
    } else {
        return taskExecutor;
    }
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException)

Example 3 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.

the class DisabledSystemProcessor method process.

@Override
public EventResult<SysProvisioningOperationDto> process(EntityEvent<SysProvisioningOperationDto> event) {
    SysProvisioningOperationDto provisioningOperation = event.getContent();
    SysSystemDto system = systemService.get(provisioningOperation.getSystem());
    String uid = provisioningOperationService.getByProvisioningOperation(provisioningOperation).getUid();
    boolean closed = false;
    if (system.isDisabled()) {
        ResultModel resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_SYSTEM_DISABLED, ImmutableMap.of("name", uid, "system", system.getName()));
        provisioningOperation.setResult(new OperationResult.Builder(OperationState.NOT_EXECUTED).setModel(resultModel).build());
        // 
        provisioningOperation = provisioningOperationService.save(provisioningOperation);
        // 
        LOG.info(resultModel.toString());
        notificationManager.send(AccModuleDescriptor.TOPIC_PROVISIONING, new IdmMessageDto.Builder().setModel(resultModel).build());
        // 
        closed = true;
    }
    // set back to event content
    event.setContent(provisioningOperation);
    return new DefaultEventResult<>(event, this, closed);
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) SysProvisioningOperationDto(eu.bcvsolutions.idm.acc.dto.SysProvisioningOperationDto) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto)

Example 4 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.

the class ReadonlySystemProcessor method process.

@Override
public EventResult<SysProvisioningOperationDto> process(EntityEvent<SysProvisioningOperationDto> event) {
    SysProvisioningOperationDto provisioningOperation = event.getContent();
    SysSystemDto system = systemService.get(provisioningOperation.getSystem());
    boolean closed = false;
    if (system.isReadonly()) {
        String uid = provisioningOperationService.getByProvisioningOperation(provisioningOperation).getUid();
        ResultModel resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_SYSTEM_READONLY, ImmutableMap.of("name", uid, "system", system.getName()));
        provisioningOperation.setResult(new OperationResult.Builder(OperationState.NOT_EXECUTED).setModel(resultModel).build());
        // 
        provisioningOperation = provisioningOperationService.save(provisioningOperation);
        // 
        LOG.info(resultModel.toString());
        notificationManager.send(AccModuleDescriptor.TOPIC_PROVISIONING, new IdmMessageDto.Builder().setModel(resultModel).build());
        // 
        closed = true;
    }
    // set back to event content
    event.setContent(provisioningOperation);
    return new DefaultEventResult<>(event, this, closed);
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) SysProvisioningOperationDto(eu.bcvsolutions.idm.acc.dto.SysProvisioningOperationDto) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto)

Example 5 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.

the class DefaultSysProvisioningOperationService method handleFailed.

@Override
@Transactional
public SysProvisioningOperationDto handleFailed(SysProvisioningOperationDto operation, Exception ex) {
    SysSystemDto system = systemService.get(operation.getSystem());
    String uid = this.getByProvisioningOperation(operation).getUid();
    ResultModel resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_FAILED, ImmutableMap.of("name", uid, "system", system.getName(), "operationType", operation.getOperationType(), "objectClass", operation.getProvisioningContext().getConnectorObject().getObjectClass().getType()));
    LOG.error(resultModel.toString(), ex);
    // 
    operation.increaseAttempt();
    // TODO: from configuration
    operation.setMaxAttempts(6);
    operation.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setCode(resultModel.getStatusEnum()).setModel(resultModel).setCause(ex).build());
    // 
    operation = save(operation);
    // 
    // calculate next attempt
    SysProvisioningOperationDto firstOperation = getFirstOperationByBatchId(operation.getBatch());
    if (firstOperation.equals(operation)) {
        SysProvisioningBatchDto batch = batchService.get(operation.getBatch());
        batch.setNextAttempt(batchService.calculateNextAttempt(operation));
        batch = batchService.save(batch);
    }
    // 
    if (securityService.getCurrentId() != null) {
        // TODO: check account owner
        notificationManager.send(AccModuleDescriptor.TOPIC_PROVISIONING, new IdmMessageDto.Builder().setModel(resultModel).build());
    }
    return operation;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) SysProvisioningBatchDto(eu.bcvsolutions.idm.acc.dto.SysProvisioningBatchDto) ConfidentialString(eu.bcvsolutions.idm.core.security.api.domain.ConfidentialString) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) SysProvisioningOperationDto(eu.bcvsolutions.idm.acc.dto.SysProvisioningOperationDto) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)20 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)20 IdmMessageDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto)13 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)10 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)7 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)6 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)6 Test (org.junit.Test)6 Transactional (org.springframework.transaction.annotation.Transactional)5 SysProvisioningOperationDto (eu.bcvsolutions.idm.acc.dto.SysProvisioningOperationDto)4 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)3 SysProvisioningBatchDto (eu.bcvsolutions.idm.acc.dto.SysProvisioningBatchDto)2 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)2 PageRequest (org.springframework.data.domain.PageRequest)2 Objects (com.google.common.base.Objects)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 SysSystemEntityDto (eu.bcvsolutions.idm.acc.dto.SysSystemEntityDto)1 ProvisioningException (eu.bcvsolutions.idm.acc.exception.ProvisioningException)1 CoreModuleDescriptor (eu.bcvsolutions.idm.core.CoreModuleDescriptor)1