All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.arquillian.spacelift.task.Task Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package org.arquillian.spacelift.task;

import java.util.concurrent.Callable;
import org.arquillian.spacelift.Spacelift;
import org.arquillian.spacelift.execution.Execution;
import org.arquillian.spacelift.execution.ExecutionException;
import org.arquillian.spacelift.execution.ExecutionService;

/**
 * Representation of a task that can be executed by Arquillian Spacelift.
 *
 * @param 
 *     Input type of this task. Can be {@code Object} to mark that input in not relevant for this task.
 * @param 
 *     Output type of this task.
 *
 * @author Karel Piwko
 */
public abstract class Task {

    // task is protected so we can set it
    private Task previous;
    private ExecutionService executionService;

    /**
     * Allows to connect current task with next task, given the output of this task matches input of next task
     *
     * @param nextTask
     *     Task to be executed right after this task is finished
     */
    public > TASK_2 then(Class nextTask) {

        TASK_2 next = Spacelift.task(nextTask);
        next.setPreviousTask(this);

        return next;
    }

    /**
     * Allows to connect current task with next task, given the output of this task matches input of next task
     *
     * @param nextTask
     *     Task to be executed right after this task is finished
     */
    @SuppressWarnings("unchecked")
    public Task then(String nextTask) {

        @SuppressWarnings("rawtypes")
        Task next = Spacelift.task(nextTask);
        next.setPreviousTask(this);

        return next;
    }

    /**
     * Asynchronously executes current chain of tasks.
     *
     * @return Execution object that allows later retrieved result of the task
     *
     * @throws ExecutionException
     */
    public Execution execute() throws ExecutionException {

        if (getExecutionService() == null) {
            throw new ExecutionException("Unable to execute a task, execution service was not set.");
        }

        return getExecutionService().execute(new Callable() {
            @Override
            public OUT call() throws Exception {
                return Task.this.run();
            }
        });
    }

    /**
     * Represents a transformation of {@code input} into {@code output}.
     *
     * @param input
     *     Input of this task, can be ignored
     *
     * @throws Exception
     *     if processing fails for any reason
     */
    protected abstract OUT process(IN input) throws Exception;

    /**
     * Transforms a chain of tasks into action that will be executed asynchronously.
     *
     * @throws ExecutionException
     */
    protected OUT run() throws ExecutionException {
        IN in = null;

        if (previous != null) {
            in = previous.run();
        }

        try {
            return process(in);
        } catch (Exception e) {
            throw new ExecutionException(e, "Unable to execute task {0}", this.getClass().getSimpleName());
        }
    }

    /**
     * Returns {@see ExecutionService}. If using {@see Tasks} or {@see ToolRegistry}, this method is guaranteed to never
     * return {@code null}
     */
    protected ExecutionService getExecutionService() {
        return executionService;
    }

    /**
     * Sets {@see ExecutionService} to be used to execute this task asynchronously
     */
    protected Task setExecutionService(ExecutionService executionService) {
        this.executionService = executionService;
        return this;
    }

    /**
     * Sets previous task
     */
    protected void setPreviousTask(Task previous) {
        this.previous = previous;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy