Search in sources :

Example 1 with AuditResultHandler

use of com.evolveum.midpoint.audit.api.AuditResultHandler in project midpoint by Evolveum.

the class SqlAuditServiceImpl method listRecords.

@Override
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params) {
    final String operation = "listRecords";
    int attempt = 1;
    while (true) {
        try {
            final List<AuditEventRecord> auditEventRecords = new ArrayList<>();
            AuditResultHandler handler = new AuditResultHandler() {

                @Override
                public boolean handle(AuditEventRecord auditRecord) {
                    auditEventRecords.add(auditRecord);
                    return true;
                }

                @Override
                public int getProgress() {
                    return 0;
                }
            };
            listRecordsIterativeAttempt(query, params, handler);
            return auditEventRecords;
        } catch (RuntimeException ex) {
            attempt = baseHelper.logOperationAttempt(null, operation, attempt, ex, null);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AuditResultHandler(com.evolveum.midpoint.audit.api.AuditResultHandler) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord)

Example 2 with AuditResultHandler

use of com.evolveum.midpoint.audit.api.AuditResultHandler in project midpoint by Evolveum.

the class AuditReindexTaskHandler method run.

@Override
public TaskRunResult run(Task coordinatorTask) {
    OperationResult opResult = new OperationResult(OperationConstants.AUDIT_REINDEX + ".run");
    opResult.setStatus(OperationResultStatus.IN_PROGRESS);
    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(opResult);
    final Long expectedTotal = auditService.countObjects("select count(*) from RAuditEventRecord as aer where 1=1", null);
    AuditResultHandler resultHandler = new AuditResultHandler() {

        private AtomicInteger processedObjects = new AtomicInteger();

        @Override
        public boolean handle(AuditEventRecord auditRecord) {
            auditService.reindexEntry(auditRecord);
            processedObjects.incrementAndGet();
            return true;
        }

        @Override
        public int getProgress() {
            return processedObjects.get();
        }
    };
    if (resultHandler == null) {
        // the error should already be in the runResult
        return runResult;
    }
    try {
        LOGGER.trace("{}: expecting {} objects to be processed", taskName, expectedTotal);
        runResult.setProgress(0);
        coordinatorTask.setProgress(0);
        if (expectedTotal != null) {
            coordinatorTask.setExpectedTotal(expectedTotal);
        }
        try {
            coordinatorTask.savePendingModifications(opResult);
        } catch (ObjectAlreadyExistsException e) {
            // try block
            throw new IllegalStateException("Unexpected ObjectAlreadyExistsException when updating task progress/expectedTotal", e);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        while (true) {
            params.put("setFirstResult", firstResult);
            params.put("setMaxResults", maxResults);
            List<AuditEventRecord> records = auditService.listRecords(null, params);
            if (CollectionUtils.isNotEmpty(records)) {
                for (AuditEventRecord record : records) {
                    resultHandler.handle(record);
                    runResult.setProgress(resultHandler.getProgress());
                }
                firstResult += maxResults;
                maxResults = ((expectedTotal.intValue() - firstResult) > maxResults ? maxResults : (expectedTotal.intValue() - firstResult));
            } else {
                break;
            }
        }
        opResult.recordSuccess();
    } catch (ObjectNotFoundException e) {
        // This is bad. The resource does not exist. Permanent problem.
        logErrorAndSetResult(runResult, resultHandler, "Object not found", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    } catch (SchemaException e) {
        // Not sure about this. But most likely it is a misconfigured
        // resource or connector
        // It may be worth to retry. Error is fatal, but may not be
        // permanent.
        logErrorAndSetResult(runResult, resultHandler, "Error dealing with schema", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.TEMPORARY_ERROR);
        return runResult;
    } catch (RuntimeException e) {
        // Can be anything ... but we can't recover from that.
        // It is most likely a programming error. Does not make much sense
        // to retry.
        logErrorAndSetResult(runResult, resultHandler, "Internal error", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    // TODO: check last handler status
    runResult.setProgress(resultHandler.getProgress());
    runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
    String finishMessage = "Finished " + taskName + " (" + coordinatorTask + "). ";
    String statistics = "Processed " + resultHandler.getProgress() + " objects";
    opResult.createSubresult(OperationConstants.AUDIT_REINDEX + ".statistics").recordStatus(OperationResultStatus.SUCCESS, statistics);
    LOGGER.info(finishMessage + statistics);
    LOGGER.trace("{} run finished (task {}, run result {})", taskName, coordinatorTask, runResult);
    return runResult;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) HashMap(java.util.HashMap) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) AuditResultHandler(com.evolveum.midpoint.audit.api.AuditResultHandler) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Aggregations

AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)2 AuditResultHandler (com.evolveum.midpoint.audit.api.AuditResultHandler)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 TaskRunResult (com.evolveum.midpoint.task.api.TaskRunResult)1 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)1 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1