Changes to return type of Executable.getType() method to make it compile with Oracle...
[jabaws.git] / engine / compbio / engine / client / SkeletalExecutable.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.client;\r
20 \r
21 import java.io.File;\r
22 import java.security.InvalidParameterException;\r
23 import java.util.Arrays;\r
24 import java.util.List;\r
25 \r
26 import org.apache.log4j.Logger;\r
27 \r
28 import compbio.engine.client.CommandBuilder.Parameter;\r
29 import compbio.engine.conf.PropertyHelperManager;\r
30 import compbio.metadata.Limit;\r
31 import compbio.metadata.LimitsManager;\r
32 import compbio.util.PropertyHelper;\r
33 import compbio.util.Util;\r
34 \r
35 public abstract class SkeletalExecutable<T> implements Executable<T> {\r
36 \r
37         protected static final PropertyHelper ph = PropertyHelperManager\r
38                         .getPropertyHelper();\r
39 \r
40         private static Logger log = Logger.getLogger(SkeletalExecutable.class);\r
41 \r
42         // Cache for Limits information\r
43         private LimitsManager<T> limits;\r
44 \r
45         protected String inputFile = "input.txt";\r
46         protected String outputFile = "output.txt";\r
47         protected String errorFile = "error.txt";\r
48 \r
49         private boolean isInputSet = false;\r
50         private boolean isOutputSet = false;\r
51         private boolean isErrorSet = false;\r
52 \r
53         /**\r
54          * This has to allow duplicate parameters as different options may have the\r
55          * same value e.g. Muscle -weight1 clustalw -weight2 clustalw\r
56          */\r
57         protected CommandBuilder<T> cbuilder;\r
58 \r
59         public SkeletalExecutable() {\r
60                 cbuilder = new CommandBuilder<T>(" ");\r
61         }\r
62 \r
63         public SkeletalExecutable(String parameterKeyValueDelimiter) {\r
64                 assert parameterKeyValueDelimiter != null;\r
65                 cbuilder = new CommandBuilder<T>(parameterKeyValueDelimiter);\r
66         }\r
67 \r
68         public SkeletalExecutable<T> setInput(String inFile) {\r
69                 if (compbio.util.Util.isEmpty(inFile)) {\r
70                         throw new IllegalArgumentException("Input file must not be NULL");\r
71                 }\r
72                 this.inputFile = inFile;\r
73                 this.isInputSet = true;\r
74                 return this;\r
75         }\r
76 \r
77         public SkeletalExecutable<T> setOutput(String outFile) {\r
78                 if (compbio.util.Util.isEmpty(outFile)\r
79                                 || PathValidator.isAbsolutePath(outFile)) {\r
80                         throw new IllegalArgumentException(\r
81                                         "Output file must not be NULL and Absolute path could not be used! Please provide the filename only. Value provided: "\r
82                                                         + outFile);\r
83                 }\r
84                 this.outputFile = outFile;\r
85                 this.isOutputSet = true;\r
86                 return this;\r
87         }\r
88 \r
89         public SkeletalExecutable<T> setError(String errFile) {\r
90                 if (compbio.util.Util.isEmpty(errFile)\r
91                                 || PathValidator.isAbsolutePath(errFile)) {\r
92                         throw new IllegalArgumentException(\r
93                                         "Error file must not be NULL and Absolute path could not be used! Please provide the filename only. Value provided: "\r
94                                                         + errFile);\r
95                 }\r
96                 this.errorFile = errFile;\r
97                 this.isErrorSet = true;\r
98                 return this;\r
99         }\r
100 \r
101         @Override\r
102         public CommandBuilder<T> getParameters(ExecProvider provider) {\r
103                 /*\r
104                  * Prevent modification of the parameters unintentionally. This is\r
105                  * important to preserve executable parameters intact as engine could\r
106                  * add things into the array as it see fit. For instance\r
107                  * ExecutableWrapper (part of local engines) add command line as the\r
108                  * first element of an array.\r
109                  */\r
110                 paramValueUpdater();\r
111                 return cbuilder;\r
112         }\r
113 \r
114         @Override\r
115         public Executable<T> addParameters(List<String> parameters) {\r
116                 cbuilder.addParams(parameters);\r
117                 return this;\r
118         }\r
119 \r
120         public Executable<T> setParameter(String parameter) {\r
121                 cbuilder.setParam(parameter);\r
122                 return this;\r
123         }\r
124 \r
125         /**\r
126          * This is a generic method of changing values of the parameters with\r
127          * properties\r
128          * \r
129          * This method iterates via commands for an executable finding matches from\r
130          * the Executable.properties file and replacing values in CommandBuilder\r
131          * with a combination of value from CommandBuilder to merge path from\r
132          * properties\r
133          */\r
134         void paramValueUpdater() {\r
135                 for (Parameter command : cbuilder.getCommandList()) {\r
136                         if (command.value == null) {\r
137                                 continue;\r
138                         }\r
139                         String propertyPath = compbio.engine.client.Util.getExecProperty(\r
140                                         command.name + ".path", getType());\r
141                         if (Util.isEmpty(propertyPath)) {\r
142                                 continue;\r
143                         }\r
144                         if (new File(command.value).isAbsolute()) {\r
145                                 // Matrix can be found so no actions necessary\r
146                                 // This method has been called already and the matrix name\r
147                                 // is modified to contain full path // no further actions is\r
148                                 // necessary\r
149                                 continue;\r
150                         }\r
151                         String absMatrixPath = compbio.engine.client.Util\r
152                                         .convertToAbsolute(propertyPath);\r
153                         command.value = absMatrixPath + File.separator + command.value;\r
154                         cbuilder.setParam(command);\r
155                 }\r
156         }\r
157 \r
158         /**\r
159          * This method cannot really tell whether the files has actually been\r
160          * created or not. It must be overridden as required.\r
161          * \r
162          * @see compbio.engine.client.Executable#getCreatedFiles()\r
163          */\r
164         @Override\r
165         public List<String> getCreatedFiles() {\r
166                 return Arrays.asList(getOutput(), getError());\r
167         }\r
168 \r
169         @Override\r
170         public String getInput() {\r
171                 return inputFile;\r
172         }\r
173 \r
174         protected boolean isInputSet() {\r
175                 return isInputSet;\r
176         }\r
177 \r
178         protected boolean isOutputSet() {\r
179                 return isOutputSet;\r
180         }\r
181 \r
182         protected boolean isErrorSet() {\r
183                 return isErrorSet;\r
184         }\r
185 \r
186         @Override\r
187         public String getOutput() {\r
188                 return outputFile;\r
189         }\r
190 \r
191         @Override\r
192         public String getError() {\r
193                 return errorFile;\r
194         }\r
195 \r
196         @Override\r
197         public String toString() {\r
198                 String value = "Input: " + this.getInput() + "\n";\r
199                 value += "Output: " + this.getOutput() + "\n";\r
200                 value += "Error: " + this.getError() + "\n";\r
201                 value += "Class: " + this.getClass() + "\n";\r
202                 value += "Params: " + cbuilder + "\n";\r
203                 return value;\r
204         }\r
205 \r
206         @Override\r
207         public Executable<?> loadRunConfiguration(RunConfiguration rconfig) {\r
208                 if (!compbio.util.Util.isEmpty(rconfig.getOutput())) {\r
209                         setOutput(rconfig.getOutput());\r
210                 }\r
211                 if (!compbio.util.Util.isEmpty(rconfig.getError())) {\r
212                         setError(rconfig.getError());\r
213                 }\r
214                 if (!compbio.util.Util.isEmpty(rconfig.getInput())) {\r
215                         setInput(rconfig.getInput());\r
216                 }\r
217                 this.cbuilder = (CommandBuilder<T>) rconfig.getParameters();\r
218                 return this;\r
219         }\r
220 \r
221         @Override\r
222         public boolean equals(Object obj) {\r
223                 if (obj == null) {\r
224                         return false;\r
225                 }\r
226                 if (!(obj instanceof SkeletalExecutable<?>)) {\r
227                         return false;\r
228                 }\r
229                 SkeletalExecutable<?> exec = (SkeletalExecutable<?>) obj;\r
230                 if (!Util.isEmpty(this.inputFile) && !Util.isEmpty(exec.inputFile)) {\r
231                         if (!this.inputFile.equals(exec.inputFile)) {\r
232                                 return false;\r
233                         }\r
234                 }\r
235                 if (!Util.isEmpty(this.outputFile) && !Util.isEmpty(exec.outputFile)) {\r
236                         if (!this.outputFile.equals(exec.outputFile)) {\r
237                                 return false;\r
238                         }\r
239                 }\r
240                 if (!Util.isEmpty(this.errorFile) && !Util.isEmpty(exec.errorFile)) {\r
241                         if (!this.errorFile.equals(exec.errorFile)) {\r
242                                 return false;\r
243                         }\r
244                 }\r
245                 if (!this.cbuilder.equals(exec.cbuilder)) {\r
246                         return false;\r
247                 }\r
248                 return true;\r
249         }\r
250 \r
251         @Override\r
252         public int hashCode() {\r
253                 int code = inputFile.hashCode();\r
254                 code += outputFile.hashCode();\r
255                 code += errorFile.hashCode();\r
256                 code *= this.cbuilder.hashCode();\r
257                 return code;\r
258         }\r
259 \r
260         public String getClusterSettings() {\r
261                 String settings = ph.getProperty(getType().getSimpleName()\r
262                                 .toLowerCase() + ".cluster.settings");\r
263                 return settings == null ? "" : settings;\r
264         }\r
265 \r
266         /**\r
267          * \r
268          * @return number of cpus to use on the cluster or 0 if the value is\r
269          *         undefined\r
270          */\r
271         public static int getClusterCpuNum(Class<? extends Executable<?>> type) {\r
272                 int cpus = 0;\r
273                 String cpuNum = ph.getProperty(type.getSimpleName().toLowerCase()\r
274                                 + ".cluster.cpunum");\r
275                 if (compbio.util.Util.isEmpty(cpuNum)) {\r
276                         return 0;\r
277                 }\r
278                 try {\r
279                         cpus = Integer.parseInt(cpuNum);\r
280                 } catch (NumberFormatException e) {\r
281                         // safe to ignore\r
282                         log.debug("Number of cpus to use for cluster execution is defined but could not be parsed as integer! Given value is: "\r
283                                         + cpuNum);\r
284                         return 0;\r
285                 }\r
286                 if (cpus < 1 || cpus > 100) {\r
287                         throw new InvalidParameterException(\r
288                                         "Number of cpu for cluster execution must be within 1 and 100! "\r
289                                                         + "Look at the value of 'tcoffee.cluster.cpunum' property. Given value is "\r
290                                                         + cpus);\r
291                 }\r
292                 return cpus;\r
293         }\r
294 \r
295         @Override\r
296         public synchronized Limit<T> getLimit(String presetName) {\r
297                 // Assume this function is called for the first time and thus need\r
298                 // initialization\r
299                 if (limits == null) {\r
300                         limits = getLimits();\r
301                 }\r
302                 // Either the initialization failed or limits were not configured.\r
303                 if (limits == null) {\r
304                         return null;\r
305                 }\r
306 \r
307                 Limit<T> limit = null;\r
308                 if (limits != null) {\r
309                         // this returns default limit if preset is undefined!\r
310                         limit = limits.getLimitByName(presetName);\r
311                 }\r
312                 // If limit is not defined for a particular preset, then return default\r
313                 // limit\r
314                 if (limit == null) {\r
315                         log.debug("Limit for the preset " + presetName\r
316                                         + " is not found. Using default");\r
317                         limit = limits.getDefaultLimit();\r
318                 }\r
319                 return limit;\r
320         }\r
321 \r
322         @Override\r
323         public LimitsManager<T> getLimits() {\r
324                 synchronized (SkeletalExecutable.class) {\r
325                         if (limits == null) {\r
326                                 limits = compbio.engine.client.Util.getLimits(this.getType());\r
327                         }\r
328                 }\r
329                 return limits;\r
330         }\r
331 \r
332         /**\r
333          * \r
334          * @return subclasses must return their type\r
335          */\r
336         public abstract Class<T> getType();\r
337 \r
338 }\r