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 2006-2010 Sun Microsystems, Inc. 025 * Portions Copyright 2015 ForgeRock AS. 026 */ 027package org.opends.quicksetup.util; 028 029 030 031/** 032 * This class provides a mechanism for running a task in the background using a 033 * separate thread and providing the caller with notification when it has 034 * completed. 035 * @param <T> type of object returned by this process 036 */ 037public abstract class BackgroundTask<T> 038{ 039 /** 040 * Creates a new thread and begins running the task in the background. When 041 * the task has completed, the {@code backgroundTaskCompleted} method will be 042 * invoked. 043 */ 044 public final void startBackgroundTask() 045 { 046 BackgroundTaskThread<T> taskThread = new BackgroundTaskThread<>(this); 047 taskThread.start(); 048 } 049 050 051 052 /** 053 * Performs all processing associated with the task. 054 * 055 * @return An {@code Object} with information about the processing performed 056 * for this task, or {@code null} if no return value is needed. 057 * 058 * @throws Throwable throwable that will be passed through the method 059 * backgroundTaskCompleted. 060 */ 061 public abstract T processBackgroundTask() throws Throwable; 062 063 064 065 /** 066 * This method will be invoked to indicate that the background task has 067 * completed. If processing completed successfully, then the 068 * {@code Throwable} argument will be {@code null} and the {@code returnValue} 069 * argument will contain the value returned by the 070 * {@code processBackgroundTask} method. If an exception or error was thrown, 071 * then the {@code throwable} argument will not be {@code null}. 072 * 073 * @param returnValue The value returned by the 074 * {@code processBackgroundTask} method when processing 075 * completed, or {@code null} if no value was returned or 076 * an exception was encountered during processing. 077 * @param throwable A {@code Throwable} instance (e.g., an exception) that 078 * was raised during processing, or {@code null} if all 079 * processing completed successfully. 080 */ 081 public abstract void backgroundTaskCompleted(T returnValue, 082 Throwable throwable); 083}