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.io.FileNotFoundException;
\r
23 import java.io.FileOutputStream;
\r
24 import java.io.IOException;
\r
25 import java.io.InputStream;
\r
27 import javax.xml.bind.JAXBException;
\r
28 import javax.xml.bind.annotation.XmlAccessType;
\r
29 import javax.xml.bind.annotation.XmlAccessorType;
\r
30 import javax.xml.bind.annotation.XmlElement;
\r
31 import javax.xml.bind.annotation.XmlRootElement;
\r
32 import javax.xml.bind.annotation.XmlTransient;
\r
34 import org.apache.log4j.Logger;
\r
36 import compbio.engine.conf.RunnerConfigMarshaller;
\r
37 import compbio.util.Util;
\r
40 * Value class for persisting ConfExecutable instances
\r
46 @XmlAccessorType(XmlAccessType.FIELD)
\r
47 public class RunConfiguration {
\r
50 private static Logger log = Logger.getLogger(RunConfiguration.class);
\r
53 public static final String rconfigFile = "RunnerConfig.xml";
\r
56 * Task ID (is a part of workDirectory but extracting it from there could be
\r
59 @XmlElement(required = true)
\r
65 @XmlElement(required = true)
\r
66 String workDirectory;
\r
71 @XmlElement(required = true)
\r
72 private CommandBuilder<?> parameters;
\r
77 @XmlElement(nillable = true)
\r
78 private String error;
\r
83 @XmlElement(nillable = true)
\r
84 private String output;
\r
89 @XmlElement(nillable = true)
\r
90 private String input;
\r
95 @XmlElement(required = true)
\r
96 String runnerClassName;
\r
98 public RunConfiguration() {
\r
99 // JAXB default constructor
\r
102 public RunConfiguration(ConfExecutable<?> cexec) {
\r
103 this.setParameters(cexec.getParameters());
\r
104 this.workDirectory = cexec.getWorkDirectory();
\r
105 this.taskId = cexec.getTaskId();
\r
106 this.runnerClassName = cexec.getExecutable().getClass()
\r
107 .getCanonicalName();
\r
108 this.setOutput(cexec.getOutput());
\r
109 this.setInput(cexec.getInput());
\r
110 this.setError(cexec.getError());
\r
113 public static boolean write(RunConfiguration rconf) throws IOException {
\r
114 File rconfFile = new File(rconf.workDirectory, rconfigFile);
\r
116 RunnerConfigMarshaller<ConfiguredExecutable<?>> confMarch = new RunnerConfigMarshaller<ConfiguredExecutable<?>>(
\r
117 RunConfiguration.class);
\r
118 confMarch.write(rconf, new FileOutputStream(rconfFile));
\r
119 } catch (JAXBException e) {
\r
120 e.printStackTrace();
\r
121 log.error("Failed to save RunConfiguration " + rconf + " Error: "
\r
122 + e.getMessage(), e.getCause());
\r
123 } catch (FileNotFoundException e) {
\r
124 log.error("Failed to save RunConfiguration " + rconf + " Error: "
\r
125 + e.getMessage(), e.getCause());
\r
127 return rconfFile.exists() && rconfFile.length() > 0;
\r
130 public static RunConfiguration load(InputStream input) throws IOException {
\r
131 RunConfiguration rconf = null;
\r
133 RunnerConfigMarshaller<RunConfiguration> confMarch = new RunnerConfigMarshaller<RunConfiguration>(
\r
134 RunConfiguration.class);
\r
135 rconf = confMarch.read(input, RunConfiguration.class);
\r
136 } catch (JAXBException e) {
\r
137 log.error("Failed to load RunConfiguration " + rconf + " Error: "
\r
138 + e.getMessage(), e.getCause());
\r
143 public void setOutput(String output) {
\r
144 this.output = output;
\r
147 public CommandBuilder<?> getParameters() {
\r
151 public void setParameters(CommandBuilder<?> parameters) {
\r
152 this.parameters = parameters;
\r
155 public String getOutput() {
\r
159 public void setError(String error) {
\r
160 this.error = error;
\r
163 public String getError() {
\r
167 public String getInput() {
\r
171 public void setInput(String input) {
\r
172 this.input = input;
\r
175 public String getRunnerClassName() {
\r
176 return this.runnerClassName;
\r
180 public boolean equals(Object obj) {
\r
184 if (!(obj instanceof RunConfiguration)) {
\r
187 RunConfiguration rconf = (RunConfiguration) obj;
\r
188 if (!strEquals(this.error, rconf.error)) {
\r
191 if (!strEquals(this.input, rconf.input)) {
\r
194 if (!strEquals(this.output, rconf.output)) {
\r
197 if (!strEquals(this.runnerClassName, rconf.runnerClassName)) {
\r
200 if (!strEquals(this.taskId, rconf.taskId)) {
\r
203 if (!strEquals(this.workDirectory, rconf.workDirectory)) {
\r
206 if (!parameters.equals(rconf.parameters)) {
\r
212 private boolean strEquals(String value, String other) {
\r
213 if (!Util.isEmpty(value) && !Util.isEmpty(other)) {
\r
214 if (!value.equals(other)) {
\r
222 public int hashCode() {
\r
223 int code = this.taskId.hashCode() * this.runnerClassName.hashCode();
\r
224 code *= this.parameters.hashCode();
\r
229 public String toString() {
\r
230 String value = "Class : " + this.runnerClassName + "\n";
\r
231 value += "TaskId: " + this.taskId + "\n";
\r
232 value += "Input: " + this.input + "\n";
\r
233 value += "Output: " + this.output + "\n";
\r
234 value += "Error: " + this.error + "\n";
\r
235 value += "Workdir: " + this.workDirectory + "\n";
\r
236 value += "Param: " + this.parameters + "\n";
\r