Swtich off GA by default
[jabaws.git] / engine / compbio / engine / local / LocalRunner.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \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
7  * \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
11  * \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
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.engine.local;\r
20 \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
25 \r
26 import org.apache.log4j.Logger;\r
27 \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
34 \r
35 public final class LocalRunner implements SyncExecutor {\r
36 \r
37         private static final Logger log = Logger.getLogger(LocalRunner.class);\r
38         /*\r
39          * If is not advisable to start threads statically, thus this field is not static\r
40          */\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
45 \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
49                 }\r
50                 this.executor = LocalExecutorService.getExecutor();\r
51                 this.executable = executable;\r
52                 this.workDirectory = executable.getWorkDirectory();\r
53                 // Save run configuration\r
54                 try {\r
55                         executable.saveRunConfiguration();\r
56                 } catch (IOException e) {\r
57                         log.error("Could not save run configuration! " + e.getMessage(), e.getCause());\r
58                 }\r
59         }\r
60 \r
61         @Override\r
62         public String getWorkDirectory() {\r
63                 return this.workDirectory;\r
64         }\r
65 \r
66         @Override\r
67         public boolean cancelJob() {\r
68                 return LocalEngineUtil.cancelJob(future, getWorkDirectory());\r
69         }\r
70 \r
71         Future<ConfiguredExecutable<?>> getFuture() {\r
72                 return this.future;\r
73         }\r
74 \r
75         @Override\r
76         public JobStatus getJobStatus() {\r
77                 if (future == null) {\r
78                         return LocalEngineUtil.getRecordedJobStatus(executable.getTaskId());\r
79                 }\r
80                 return LocalEngineUtil.getJobStatus(future);\r
81         }\r
82 \r
83         @Override\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
88         }\r
89 \r
90         /**\r
91          * @throws CancellationException\r
92          */\r
93         @Override\r
94         public ConfiguredExecutable<?> waitForResult()\r
95                         throws JobExecutionException {\r
96                 try {\r
97                         return LocalEngineUtil.getResults(future, executable.getTaskId());\r
98                 } catch (ResultNotAvailableException e) {\r
99                         throw new JobExecutionException(e);\r
100                 }\r
101         }\r
102 \r
103         @Override\r
104         public boolean cleanup() {\r
105                 return LocalEngineUtil.cleanup(executable);\r
106         }\r
107 \r
108 }\r