001package com.identityworksllc.iiq.common;
002
003import sailpoint.object.TaskResult;
004import sailpoint.task.TaskMonitor;
005import sailpoint.tools.GeneralException;
006
007/**
008 * Utilities for TaskDefinitions and TaskResults
009 */
010public class TaskUtil {
011
012    /**
013     * The interface used for lock-and-callback utilities
014     */
015    public interface TaskResultConsumer {
016        /**
017         * Does something with the task result
018         *
019         * @param taskResult The task result to do something to
020         * @throws GeneralException if anything fails
021         */
022        void accept(TaskResult taskResult) throws GeneralException;
023    }
024
025    /**
026     * Executes the given action with a locked master result
027     *
028     * @param monitor  The TaskMonitor used to retrieve the locked result
029     * @param consumer An action to run against the locked partition result
030     * @throws GeneralException if anything fails
031     */
032    public static void withLockedMasterResult(TaskMonitor monitor, TaskResultConsumer consumer) throws GeneralException {
033        TaskResult lockedMasterResult = monitor.lockMasterResult();
034        try {
035            consumer.accept(lockedMasterResult);
036        } catch (GeneralException | Error e) {
037            throw e;
038        } catch (Throwable t) {
039            throw new GeneralException(t);
040        } finally {
041            monitor.commitMasterResult();
042        }
043    }
044
045    /**
046     * Executes the given action with a locked partition result
047     *
048     * @param monitor  The TaskMonitor used to retrieve the locked result
049     * @param consumer An action to run against the locked partition result
050     * @throws GeneralException if anything fails
051     */
052    public static void withLockedPartitionResult(TaskMonitor monitor, TaskResultConsumer consumer) throws GeneralException {
053        TaskResult lockedPartitionResult = monitor.lockPartitionResult();
054        try {
055            consumer.accept(lockedPartitionResult);
056        } catch (GeneralException | Error e) {
057            throw e;
058        } catch (Throwable t) {
059            throw new GeneralException(t);
060        } finally {
061            monitor.commitPartitionResult();
062        }
063    }
064}