Next version of JABA
[jabaws.git] / engine / compbio / engine / client / RunConfiguration.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.io.FileNotFoundException;\r
23 import java.io.FileOutputStream;\r
24 import java.io.IOException;\r
25 import java.io.InputStream;\r
26 \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
33 \r
34 import org.apache.log4j.Logger;\r
35 \r
36 import compbio.engine.conf.RunnerConfigMarshaller;\r
37 import compbio.util.Util;\r
38 \r
39 /**\r
40  * Value class for persisting ConfExecutable instances\r
41  * \r
42  * @author pvtroshin\r
43  * \r
44  */\r
45 @XmlRootElement\r
46 @XmlAccessorType(XmlAccessType.FIELD)\r
47 public class RunConfiguration {\r
48 \r
49         @XmlTransient\r
50         private static Logger log = Logger.getLogger(RunConfiguration.class);\r
51 \r
52         @XmlTransient\r
53         public static final String rconfigFile = "RunnerConfig.xml";\r
54 \r
55         /**\r
56          * Task ID (is a part of workDirectory but extracting it from there could be\r
57          * tricky)\r
58          */\r
59         @XmlElement(required = true)\r
60         String taskId;\r
61 \r
62         /**\r
63          * Working directory\r
64          */\r
65         @XmlElement(required = true)\r
66         String workDirectory;\r
67 \r
68         /**\r
69          * Parameters\r
70          */\r
71         @XmlElement(required = true)\r
72         private CommandBuilder<?> parameters;\r
73 \r
74         /**\r
75          * Input option\r
76          */\r
77         @XmlElement(nillable = true)\r
78         private String error;\r
79 \r
80         /**\r
81          * Output\r
82          */\r
83         @XmlElement(nillable = true)\r
84         private String output;\r
85 \r
86         /**\r
87          * Input\r
88          */\r
89         @XmlElement(nillable = true)\r
90         private String input;\r
91 \r
92         /**\r
93          * Runner class name\r
94          */\r
95         @XmlElement(required = true)\r
96         String runnerClassName;\r
97 \r
98         public RunConfiguration() {\r
99                 // JAXB default constructor\r
100         }\r
101 \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
111         }\r
112 \r
113         public static boolean write(RunConfiguration rconf) throws IOException {\r
114                 File rconfFile = new File(rconf.workDirectory, rconfigFile);\r
115                 try {\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
126                 }\r
127                 return rconfFile.exists() && rconfFile.length() > 0;\r
128         }\r
129 \r
130         public static RunConfiguration load(InputStream input) throws IOException {\r
131                 RunConfiguration rconf = null;\r
132                 try {\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
139                 }\r
140                 return rconf;\r
141         }\r
142 \r
143         public void setOutput(String output) {\r
144                 this.output = output;\r
145         }\r
146 \r
147         public CommandBuilder<?> getParameters() {\r
148                 return parameters;\r
149         }\r
150 \r
151         public void setParameters(CommandBuilder<?> parameters) {\r
152                 this.parameters = parameters;\r
153         }\r
154 \r
155         public String getOutput() {\r
156                 return output;\r
157         }\r
158 \r
159         public void setError(String error) {\r
160                 this.error = error;\r
161         }\r
162 \r
163         public String getError() {\r
164                 return error;\r
165         }\r
166 \r
167         public String getInput() {\r
168                 return input;\r
169         }\r
170 \r
171         public void setInput(String input) {\r
172                 this.input = input;\r
173         }\r
174 \r
175         public String getRunnerClassName() {\r
176                 return this.runnerClassName;\r
177         }\r
178 \r
179         @Override\r
180         public boolean equals(Object obj) {\r
181                 if (obj == null) {\r
182                         return false;\r
183                 }\r
184                 if (!(obj instanceof RunConfiguration)) {\r
185                         return false;\r
186                 }\r
187                 RunConfiguration rconf = (RunConfiguration) obj;\r
188                 if (!strEquals(this.error, rconf.error)) {\r
189                         return false;\r
190                 }\r
191                 if (!strEquals(this.input, rconf.input)) {\r
192                         return false;\r
193                 }\r
194                 if (!strEquals(this.output, rconf.output)) {\r
195                         return false;\r
196                 }\r
197                 if (!strEquals(this.runnerClassName, rconf.runnerClassName)) {\r
198                         return false;\r
199                 }\r
200                 if (!strEquals(this.taskId, rconf.taskId)) {\r
201                         return false;\r
202                 }\r
203                 if (!strEquals(this.workDirectory, rconf.workDirectory)) {\r
204                         return false;\r
205                 }\r
206                 if (!parameters.equals(rconf.parameters)) {\r
207                         return false;\r
208                 }\r
209                 return true;\r
210         }\r
211 \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
215                                 return false;\r
216                         }\r
217                 }\r
218                 return true;\r
219         }\r
220 \r
221         @Override\r
222         public int hashCode() {\r
223                 int code = this.taskId.hashCode() * this.runnerClassName.hashCode();\r
224                 code *= this.parameters.hashCode();\r
225                 return code;\r
226         }\r
227 \r
228         @Override\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
237                 return value;\r
238         }\r
239 \r
240 }\r