001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027
028package org.opends.guitools.controlpanel.util;
029
030/**
031 * This class provides a mechanism for running a task in the background using a
032 * separate thread and providing the caller with notification when it has
033 * completed.
034 * @param <T> type of object returned by this process
035 */
036public abstract class BackgroundTask<T>
037{
038  private BackgroundTaskThread<T> taskThread;
039  private boolean interrupted;
040  /**
041   * Creates a new thread and begins running the task in the background.  When
042   * the task has completed, the {@code backgroundTaskCompleted} method will be
043   * invoked.
044   */
045  public final void startBackgroundTask()
046  {
047    interrupted = false;
048    taskThread = new BackgroundTaskThread<>(this);
049    taskThread.start();
050  }
051
052  /** Interrupts the thread that is running background. */
053  public final void interrupt()
054  {
055    interrupted = true;
056    if (taskThread != null)
057    {
058      taskThread.interrupt();
059    }
060  }
061
062  /**
063   * Returns <CODE>true</CODE> if the thread running in the background is
064   * interrupted and <CODE>false</CODE> otherwise.
065   * @return <CODE>true</CODE> if the thread running in the background is
066   * interrupted and <CODE>false</CODE> otherwise.
067   */
068  public boolean isInterrupted()
069  {
070    return interrupted;
071  }
072
073  /**
074   * Performs all processing associated with the task.
075   *
076   * @return  An {@code Object} with information about the processing performed
077   *          for this task, or {@code null} if no return value is needed.
078   *
079   * @throws Throwable throwable that will be passed through the method
080   *          backgroundTaskCompleted.
081   */
082  public abstract T processBackgroundTask() throws Throwable;
083
084
085
086  /**
087   * This method will be invoked to indicate that the background task has
088   * completed.  If processing completed successfully, then the
089   * {@code Throwable} argument will be {@code null} and the {@code returnValue}
090   * argument will contain the value returned by the
091   * {@code processBackgroundTask} method.  If an exception or error was thrown,
092   * then the {@code throwable} argument will not be {@code null}.
093   *
094   * @param  returnValue  The value returned by the
095   *                      {@code processBackgroundTask} method when processing
096   *                      completed, or {@code null} if no value was returned or
097   *                      an exception was encountered during processing.
098   * @param  throwable    A {@code Throwable} instance (e.g., an exception) that
099   *                      was raised during processing, or {@code null} if all
100   *                      processing completed successfully.
101   */
102  public abstract void backgroundTaskCompleted(T returnValue,
103                                               Throwable throwable);
104}
105