1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.engine.local;
\r
21 import java.io.IOException;
\r
22 import java.util.concurrent.CancellationException;
\r
23 import java.util.concurrent.ExecutorService;
\r
24 import java.util.concurrent.Future;
\r
26 import org.apache.log4j.Logger;
\r
28 import compbio.engine.SyncExecutor;
\r
29 import compbio.engine.client.ConfiguredExecutable;
\r
30 import compbio.metadata.JobExecutionException;
\r
31 import compbio.metadata.JobStatus;
\r
32 import compbio.metadata.JobSubmissionException;
\r
33 import compbio.metadata.ResultNotAvailableException;
\r
35 public final class LocalRunner implements SyncExecutor {
\r
37 private static final Logger log = Logger.getLogger(LocalRunner.class);
\r
39 * If is not advisable to start threads statically, thus this field is not static
\r
41 private final ExecutorService executor;
\r
42 private final ConfiguredExecutable<?> executable;
\r
43 private Future<ConfiguredExecutable<?>> future;
\r
44 private final String workDirectory;
\r
46 public LocalRunner(ConfiguredExecutable<?> executable) {
\r
47 if (executable == null) {
\r
48 throw new IllegalArgumentException("Executable value is NULL. Executable must be provided!");
\r
50 this.executor = LocalExecutorService.getExecutor();
\r
51 this.executable = executable;
\r
52 this.workDirectory = executable.getWorkDirectory();
\r
53 // Save run configuration
\r
55 executable.saveRunConfiguration();
\r
56 } catch (IOException e) {
\r
57 log.error("Could not save run configuration! " + e.getMessage(), e.getCause());
\r
62 public String getWorkDirectory() {
\r
63 return this.workDirectory;
\r
67 public boolean cancelJob() {
\r
68 return LocalEngineUtil.cancelJob(future, getWorkDirectory());
\r
71 Future<ConfiguredExecutable<?>> getFuture() {
\r
76 public JobStatus getJobStatus() {
\r
77 if (future == null) {
\r
78 return LocalEngineUtil.getRecordedJobStatus(executable.getTaskId());
\r
80 return LocalEngineUtil.getJobStatus(future);
\r
84 public void executeJob()
\r
85 throws JobSubmissionException {
\r
86 ExecutableWrapper ewrapper = new ExecutableWrapper(executable, workDirectory);
\r
87 this.future = executor.submit(ewrapper);
\r
91 * @throws CancellationException
\r
94 public ConfiguredExecutable<?> waitForResult()
\r
95 throws JobExecutionException {
\r
97 return LocalEngineUtil.getResults(future, executable.getTaskId());
\r
98 } catch (ResultNotAvailableException e) {
\r
99 throw new JobExecutionException(e);
\r
104 public boolean cleanup() {
\r
105 return LocalEngineUtil.cleanup(executable);
\r