blob: 8d7b880d56f3e198f7d02f531cf1209ef3897759 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pierre Allard,
* Regent L'Archeveque - initial API and implementation
*
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.common.processors.impl;
import java.util.Iterator;
import org.eclipse.apogy.common.ApogyCommonOSGiUtilities;
import org.eclipse.apogy.common.processors.Processor;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JobProcessorsChainCustomImpl<I, O> extends JobProcessorsChainImpl<I, O> {
private static final Logger Logger = LoggerFactory.getLogger(JobProcessorsChainImpl.class);
@Override
public O process(I input) throws Exception {
final Job chainJob = new Job(this.getClass().getSimpleName()) {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected IStatus run(IProgressMonitor monitor) {
setProgressMonitor(monitor);
getProgressMonitor().beginTask("", getProcessors().size());
Object currentInput = getInput();
// Initialize the current result to null.
setIntermediateResult(null);
setOutput(null);
Iterator<Processor<?, ?>> it = getProcessors().iterator();
int i = 0;
// Goes through the list of processors and call their process method.
while (it.hasNext() && !getProgressMonitor().isCanceled()) {
getProgressMonitor().subTask("Calling sub-processor <" + i + ">");
// Gets the next processor in the chain.
setRunningProcessor(it.next());
final Processor processor = getRunningProcessor();
final Object input = currentInput;
Job processorJob = new Job(processor.getClass().getSimpleName()) {
@Override
protected IStatus run(IProgressMonitor _monitor) {
try {
processor.setProgressMonitor(_monitor);
processor.setInput(input);
processor.process(input);
} catch (Exception e) {
Logger.error(e.getMessage(), e);
return new Status(IStatus.ERROR, ApogyCommonOSGiUtilities.INSTANCE.getBundleSymbolicName(JobProcessorsChainCustomImpl.this.getClass()), IStatus.ERROR, "Failed to run.", e);
}
return new Status(IStatus.OK, ApogyCommonOSGiUtilities.INSTANCE.getBundleSymbolicName(JobProcessorsChainCustomImpl.this.getClass()), IStatus.OK, "Done.", null);
}
};
processorJob.schedule();
try {
processorJob.join();
} catch (InterruptedException e) {
Logger.error(e.getMessage(), e);
}
if ((processorJob.getResult() != null) && (!processorJob.getResult().isOK())) {
return new Status(IStatus.ERROR, ApogyCommonOSGiUtilities.INSTANCE.getBundleSymbolicName(JobProcessorsChainCustomImpl.this.getClass()), IStatus.ERROR, "Error.", null);
}
// Updates the progress of the task.
getProgressMonitor().worked(1);
i++;
// If there is still yet another processor to call.
if (it.hasNext()) {
// Sets the current input to the last processor output.
currentInput = getRunningProcessor().getOutput();
setIntermediateResult(getRunningProcessor().getOutput());
} else {
// We are done.
setOutput((O) getRunningProcessor().getOutput());
getProgressMonitor().done();
}
}
if (getProgressMonitor().isCanceled()) {
return new Status(IStatus.CANCEL, ApogyCommonOSGiUtilities.INSTANCE.getBundleSymbolicName(JobProcessorsChainCustomImpl.this.getClass()), IStatus.CANCEL, "Cancelled.", null);
} else {
return new Status(IStatus.OK, ApogyCommonOSGiUtilities.INSTANCE.getBundleSymbolicName(JobProcessorsChainCustomImpl.this.getClass()), IStatus.OK, "Done.", null);
}
}
};
chainJob.schedule();
return getOutput();
}
} // JobProcessorsChainImpl