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

org.sejda.model.task.TaskExecutionContext Maven / Gradle / Ivy

/*
 * Created on 10 giu 2016
 * Copyright 2015 by Andrea Vacondio ([email protected]).
 * This file is part of Sejda.
 *
 * Sejda is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sejda is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Sejda.  If not, see .
 */
package org.sejda.model.task;

import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.sejda.model.exception.TaskExecutionException;
import org.sejda.model.exception.TaskNonLenientExecutionException;
import org.sejda.model.parameter.base.TaskParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.Objects.isNull;

/**
 * Mutable context holding a task execution
 *
 * @author Andrea Vacondio
 */
public class TaskExecutionContext {
    private static final Logger LOG = LoggerFactory.getLogger(TaskExecutionContext.class);

    private StopWatch stopWatch = new StopWatch();
    private NotifiableTaskMetadata taskMetadata;
    private Task task;
    private boolean lenient;
    private int outputDocumentsCounter = 0;

    public TaskExecutionContext(Task task, boolean lenient) {
        if (isNull(task)) {
            throw new IllegalArgumentException("Task cannot be null");
        }
        this.taskMetadata = new NotifiableTaskMetadata(task);
        this.task = task;
        this.lenient = lenient;
    }

    public NotifiableTaskMetadata notifiableTaskMetadata() {
        return taskMetadata;
    }

    @SuppressWarnings("rawtypes")
    public Task task() {
        return task;
    }

    public void taskStart() {
        stopWatch.start();
    }

    public void taskEnded() {
        stopWatch.stop();
        LOG.info("Task ({}) executed in {}", task,
                DurationFormatUtils.formatDurationWords(stopWatch.getTime(), true, true));
    }

    public long executionTime() {
        return stopWatch.getTime();
    }

    public int incrementAndGetOutputDocumentsCounter() {
        return ++outputDocumentsCounter;
    }

    public int outputDocumentsCounter() {
        return outputDocumentsCounter;
    }

    /**
     *
     * @param e
     *            the exception the lenient task can recover from
     * @throws TaskNonLenientExecutionException
     */
    public void assertTaskIsLenient(Exception e) throws TaskNonLenientExecutionException {
        if (!lenient) {
            throw new TaskNonLenientExecutionException(e);
        }
    }

    /**
     *
     * @param message
     *            the error the lenient task can recover from
     * @throws TaskNonLenientExecutionException
     */
    public void assertTaskIsLenient(String message) throws TaskNonLenientExecutionException {
        if (!lenient) {
            throw new TaskNonLenientExecutionException(message);
        }
    }

    /**
     * @param message the error message if no output documents have been created
     * @throws TaskExecutionException
     */
    public void assertHasOutputDocuments(String message) throws TaskExecutionException {
        if (outputDocumentsCounter <= 0) {
            throw new TaskExecutionException(message);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy