Refactoring of all SequenceAnnotation web services
[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\r
40          * static\r
41          */\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
46 \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
51                 }\r
52                 this.executor = LocalExecutorService.getExecutor();\r
53                 this.executable = executable;\r
54                 this.workDirectory = executable.getWorkDirectory();\r
55                 // Save run configuration\r
56                 try {\r
57                         executable.saveRunConfiguration();\r
58                 } catch (IOException e) {\r
59                         log.error("Could not save run configuration! " + e.getMessage(), e\r
60                                         .getCause());\r
61                 }\r
62         }\r
63 \r
64         @Override\r
65         public String getWorkDirectory() {\r
66                 return this.workDirectory;\r
67         }\r
68 \r
69         @Override\r
70         public boolean cancelJob() {\r
71                 return LocalEngineUtil.cancelJob(future, getWorkDirectory());\r
72         }\r
73 \r
74         Future<ConfiguredExecutable<?>> getFuture() {\r
75                 return this.future;\r
76         }\r
77 \r
78         @Override\r
79         public JobStatus getJobStatus() {\r
80                 if (future == null) {\r
81                         return LocalEngineUtil.getRecordedJobStatus(executable.getTaskId());\r
82                 }\r
83                 return LocalEngineUtil.getJobStatus(future);\r
84         }\r
85 \r
86         @Override\r
87         public void executeJob() throws JobSubmissionException {\r
88                 ExecutableWrapper ewrapper = new ExecutableWrapper(executable,\r
89                                 workDirectory);\r
90                 this.future = executor.submit(ewrapper);\r
91         }\r
92 \r
93         /**\r
94          * @throws CancellationException\r
95          */\r
96         @Override\r
97         public ConfiguredExecutable<?> waitForResult() throws JobExecutionException {\r
98                 try {\r
99                         return LocalEngineUtil.getResults(future, executable.getTaskId());\r
100                 } catch (ResultNotAvailableException e) {\r
101                         throw new JobExecutionException(e);\r
102                 }\r
103         }\r
104 \r
105         @Override\r
106         public boolean cleanup() {\r
107                 return LocalEngineUtil.cleanup(executable);\r
108         }\r
109 \r
110 }\r