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
\r
42 private final ExecutorService executor;
\r
43 private final ConfiguredExecutable<?> executable;
\r
44 private Future<ConfiguredExecutable<?>> future;
\r
45 private final String workDirectory;
\r
47 public LocalRunner(ConfiguredExecutable<?> executable) {
\r
48 if (executable == null) {
\r
49 throw new IllegalArgumentException(
\r
50 "Executable value is NULL. Executable must be provided!");
\r
52 this.executor = LocalExecutorService.getExecutor();
\r
53 this.executable = executable;
\r
54 this.workDirectory = executable.getWorkDirectory();
\r
55 // Save run configuration
\r
57 executable.saveRunConfiguration();
\r
58 } catch (IOException e) {
\r
59 log.error("Could not save run configuration! " + e.getMessage(), e
\r
65 public String getWorkDirectory() {
\r
66 return this.workDirectory;
\r
70 public boolean cancelJob() {
\r
71 return LocalEngineUtil.cancelJob(future, getWorkDirectory());
\r
74 Future<ConfiguredExecutable<?>> getFuture() {
\r
79 public JobStatus getJobStatus() {
\r
80 if (future == null) {
\r
81 return LocalEngineUtil.getRecordedJobStatus(executable.getTaskId());
\r
83 return LocalEngineUtil.getJobStatus(future);
\r
87 public void executeJob() throws JobSubmissionException {
\r
88 ExecutableWrapper ewrapper = new ExecutableWrapper(executable,
\r
90 this.future = executor.submit(ewrapper);
\r
94 * @throws CancellationException
\r
97 public ConfiguredExecutable<?> waitForResult() throws JobExecutionException {
\r
99 return LocalEngineUtil.getResults(future, executable.getTaskId());
\r
100 } catch (ResultNotAvailableException e) {
\r
101 throw new JobExecutionException(e);
\r
106 public boolean cleanup() {
\r
107 return LocalEngineUtil.cleanup(executable);
\r