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