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.client;
\r
21 import java.io.File;
\r
22 import java.util.Arrays;
\r
23 import java.util.List;
\r
25 import org.apache.log4j.Logger;
\r
27 import compbio.engine.client.CommandBuilder.Parameter;
\r
28 import compbio.engine.conf.PropertyHelperManager;
\r
29 import compbio.util.PropertyHelper;
\r
30 import compbio.util.Util;
\r
32 public abstract class SkeletalExecutable<T> implements Executable<T> {
\r
34 protected static final PropertyHelper ph = PropertyHelperManager
\r
35 .getPropertyHelper();
\r
37 private static Logger log = Logger.getLogger(SkeletalExecutable.class);
\r
39 protected String inputFile = "input.txt";
\r
40 protected String outputFile = "output.txt";
\r
41 protected String errorFile = "error.txt";
\r
43 private boolean isInputSet = false;
\r
44 private boolean isOutputSet = false;
\r
45 private boolean isErrorSet = false;
\r
48 * This has to allow duplicate parameters as different options may have the
\r
49 * same value e.g. Muscle -weight1 clustalw -weight2 clustalw
\r
51 protected CommandBuilder<T> cbuilder;
\r
53 public SkeletalExecutable() {
\r
54 cbuilder = new CommandBuilder<T>(" ");
\r
57 public SkeletalExecutable(String parameterKeyValueDelimiter) {
\r
58 assert parameterKeyValueDelimiter != null;
\r
59 cbuilder = new CommandBuilder<T>(parameterKeyValueDelimiter);
\r
62 public SkeletalExecutable<T> setInput(String inFile) {
\r
63 if (compbio.util.Util.isEmpty(inFile)) {
\r
64 throw new IllegalArgumentException("Input file must not be NULL");
\r
66 this.inputFile = inFile;
\r
67 this.isInputSet = true;
\r
71 public SkeletalExecutable<T> setOutput(String outFile) {
\r
72 if (compbio.util.Util.isEmpty(outFile)
\r
73 || PathValidator.isAbsolutePath(outFile)) {
\r
74 throw new IllegalArgumentException(
\r
75 "Output file must not be NULL and Absolute path could not be used! Please provide the filename only. Value provided: "
\r
78 this.outputFile = outFile;
\r
79 this.isOutputSet = true;
\r
83 public SkeletalExecutable<T> setError(String errFile) {
\r
84 if (compbio.util.Util.isEmpty(errFile)
\r
85 || PathValidator.isAbsolutePath(errFile)) {
\r
86 throw new IllegalArgumentException(
\r
87 "Error file must not be NULL and Absolute path could not be used! Please provide the filename only. Value provided: "
\r
90 this.errorFile = errFile;
\r
91 this.isErrorSet = true;
\r
96 public CommandBuilder<T> getParameters(ExecProvider provider) {
\r
98 * Prevent modification of the parameters unintentionally. This is
\r
99 * important to preserve executable parameters intact as engine could
\r
100 * add things into the array as it see fit. For instance
\r
101 * ExecutableWrapper (part of local engines) add command line as the
\r
102 * first element of an array.
\r
104 paramValueUpdater();
\r
109 public Executable<T> addParameters(List<String> parameters) {
\r
110 cbuilder.addParams(parameters);
\r
114 public Executable<T> setParameter(String parameter) {
\r
115 cbuilder.setParam(parameter);
\r
120 * This is a generic method of changing values of the parameters with
\r
123 * This method iterates via commands for an executable finding matches from
\r
124 * the Executable.properties file and replacing values in CommandBuilder
\r
125 * with a combination of value from CommandBuilder to merge path from
\r
128 void paramValueUpdater() {
\r
129 for (Parameter command : cbuilder.getCommandList()) {
\r
130 if (command.value == null) {
\r
133 String propertyPath = compbio.engine.client.Util.getExecProperty(
\r
134 command.name + ".path", getType());
\r
135 if (Util.isEmpty(propertyPath)) {
\r
138 if (new File(command.value).isAbsolute()) {
\r
139 // Matrix can be found so no actions necessary
\r
140 // This method has been called already and the matrix name
\r
141 // is modified to contain full path // no further actions is
\r
145 String absMatrixPath = compbio.engine.client.Util
\r
146 .convertToAbsolute(propertyPath);
\r
147 command.value = absMatrixPath + File.separator + command.value;
\r
148 cbuilder.setParam(command);
\r
153 * This method cannot really tell whether the files has actually been
\r
154 * created or not. It must be overridden as required.
\r
156 * @see compbio.engine.client.Executable#getCreatedFiles()
\r
159 public List<String> getCreatedFiles() {
\r
160 return Arrays.asList(getOutput(), getError());
\r
164 public String getInput() {
\r
168 protected boolean isInputSet() {
\r
172 protected boolean isOutputSet() {
\r
173 return isOutputSet;
\r
176 protected boolean isErrorSet() {
\r
181 public String getOutput() {
\r
186 public String getError() {
\r
191 public String toString() {
\r
192 String value = "Input: " + this.getInput() + "\n";
\r
193 value += "Output: " + this.getOutput() + "\n";
\r
194 value += "Error: " + this.getError() + "\n";
\r
195 value += "Class: " + this.getClass() + "\n";
\r
196 value += "Params: " + cbuilder + "\n";
\r
201 public Executable<?> loadRunConfiguration(RunConfiguration rconfig) {
\r
202 if (!compbio.util.Util.isEmpty(rconfig.getOutput())) {
\r
203 setOutput(rconfig.getOutput());
\r
205 if (!compbio.util.Util.isEmpty(rconfig.getError())) {
\r
206 setError(rconfig.getError());
\r
208 if (!compbio.util.Util.isEmpty(rconfig.getInput())) {
\r
209 setInput(rconfig.getInput());
\r
211 this.cbuilder = (CommandBuilder<T>) rconfig.getParameters();
\r
216 public boolean equals(Object obj) {
\r
220 if (!(obj instanceof SkeletalExecutable<?>)) {
\r
223 SkeletalExecutable<?> exec = (SkeletalExecutable<?>) obj;
\r
224 if (!Util.isEmpty(this.inputFile) && !Util.isEmpty(exec.inputFile)) {
\r
225 if (!this.inputFile.equals(exec.inputFile)) {
\r
229 if (!Util.isEmpty(this.outputFile) && !Util.isEmpty(exec.outputFile)) {
\r
230 if (!this.outputFile.equals(exec.outputFile)) {
\r
234 if (!Util.isEmpty(this.errorFile) && !Util.isEmpty(exec.errorFile)) {
\r
235 if (!this.errorFile.equals(exec.errorFile)) {
\r
239 if (!this.cbuilder.equals(exec.cbuilder)) {
\r
246 public int hashCode() {
\r
247 int code = inputFile.hashCode();
\r
248 code += outputFile.hashCode();
\r
249 code += errorFile.hashCode();
\r
250 code *= this.cbuilder.hashCode();
\r
254 public String getClusterSettings() {
\r
255 String settings = ph.getProperty(getType().getSimpleName()
\r
257 + ".cluster.settings");
\r
258 return settings == null ? "" : settings;
\r
261 public abstract Class<? extends Executable<?>> getType();
\r