1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.rundeck.api.parser;
17
18 import java.util.Date;
19 import org.apache.commons.lang.StringUtils;
20 import org.dom4j.Node;
21 import org.rundeck.api.domain.RundeckExecution;
22 import org.rundeck.api.domain.RundeckJob;
23 import org.rundeck.api.domain.RundeckExecution.ExecutionStatus;
24
25
26
27
28
29
30 public class ExecutionParser implements XmlNodeParser<RundeckExecution> {
31
32 private String xpath;
33
34 public ExecutionParser() {
35 super();
36 }
37
38
39
40
41 public ExecutionParser(String xpath) {
42 super();
43 this.xpath = xpath;
44 }
45
46 @Override
47 public RundeckExecution parseXmlNode(Node node) {
48 Node execNode = xpath != null ? node.selectSingleNode(xpath) : node;
49
50 RundeckExecution execution = new RundeckExecution();
51
52 execution.setId(Long.valueOf(execNode.valueOf("@id")));
53 execution.setUrl(StringUtils.trimToNull(execNode.valueOf("@href")));
54 try {
55 execution.setStatus(ExecutionStatus.valueOf(StringUtils.upperCase(execNode.valueOf("@status"))));
56 } catch (IllegalArgumentException e) {
57 execution.setStatus(null);
58 }
59 execution.setDescription(StringUtils.trimToNull(execNode.valueOf("description")));
60 execution.setStartedBy(StringUtils.trimToNull(execNode.valueOf("user")));
61 execution.setAbortedBy(StringUtils.trimToNull(execNode.valueOf("abortedby")));
62 String startedAt = StringUtils.trimToNull(execNode.valueOf("date-started/@unixtime"));
63 if (startedAt != null) {
64 execution.setStartedAt(new Date(Long.valueOf(startedAt)));
65 }
66 String endedAt = StringUtils.trimToNull(execNode.valueOf("date-ended/@unixtime"));
67 if (endedAt != null) {
68 execution.setEndedAt(new Date(Long.valueOf(endedAt)));
69 }
70
71 Node jobNode = execNode.selectSingleNode("job");
72 if (jobNode != null) {
73 RundeckJob job = new JobParser().parseXmlNode(jobNode);
74 execution.setJob(job);
75 }
76
77 return execution;
78 }
79
80 }