001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2015 ForgeRock AS.
016 */
017package org.opends.quicksetup.util;
018
019
020
021/**
022 * This class provides a mechanism for running a task in the background using a
023 * separate thread and providing the caller with notification when it has
024 * completed.
025 * @param <T> type of object returned by this process
026 */
027public abstract class BackgroundTask<T>
028{
029  /**
030   * Creates a new thread and begins running the task in the background.  When
031   * the task has completed, the {@code backgroundTaskCompleted} method will be
032   * invoked.
033   */
034  public final void startBackgroundTask()
035  {
036    BackgroundTaskThread<T> taskThread = new BackgroundTaskThread<>(this);
037    taskThread.start();
038  }
039
040
041
042  /**
043   * Performs all processing associated with the task.
044   *
045   * @return  An {@code Object} with information about the processing performed
046   *          for this task, or {@code null} if no return value is needed.
047   *
048   * @throws Throwable throwable that will be passed through the method
049   *          backgroundTaskCompleted.
050   */
051  public abstract T processBackgroundTask() throws Throwable;
052
053
054
055  /**
056   * This method will be invoked to indicate that the background task has
057   * completed.  If processing completed successfully, then the
058   * {@code Throwable} argument will be {@code null} and the {@code returnValue}
059   * argument will contain the value returned by the
060   * {@code processBackgroundTask} method.  If an exception or error was thrown,
061   * then the {@code throwable} argument will not be {@code null}.
062   *
063   * @param  returnValue  The value returned by the
064   *                      {@code processBackgroundTask} method when processing
065   *                      completed, or {@code null} if no value was returned or
066   *                      an exception was encountered during processing.
067   * @param  throwable    A {@code Throwable} instance (e.g., an exception) that
068   *                      was raised during processing, or {@code null} if all
069   *                      processing completed successfully.
070   */
071  public abstract void backgroundTaskCompleted(T returnValue,
072                                               Throwable throwable);
073}