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.Collections;
19 import java.util.Date;
20 import java.util.HashSet;
21 import java.util.List;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.dom4j.Node;
25 import org.rundeck.api.domain.RundeckExecution;
26 import org.rundeck.api.domain.RundeckJob;
27 import org.rundeck.api.domain.RundeckExecution.ExecutionStatus;
28 import org.rundeck.api.domain.RundeckNode;
29 import org.rundeck.api.domain.RundeckNodeIdentity;
30
31
32
33
34
35
36 public class ExecutionParser extends BaseXpathParser<RundeckExecution> {
37
38
39 public ExecutionParser(final String xpath) {
40 super(xpath);
41 }
42
43 public ExecutionParser() {
44
45 }
46
47 @Override
48 public RundeckExecution parse(Node execNode) {
49 RundeckExecution execution = new RundeckExecution();
50
51 execution.setId(Long.valueOf(execNode.valueOf("@id")));
52 execution.setUrl(StringUtils.trimToNull(execNode.valueOf("@href")));
53 try {
54 execution.setStatus(ExecutionStatus.valueOf(StringUtils.replace(StringUtils.upperCase(execNode.valueOf("@status")),"-","_")));
55 } catch (IllegalArgumentException e) {
56 execution.setStatus(null);
57 }
58 execution.setDescription(StringUtils.trimToNull(execNode.valueOf("description")));
59 execution.setArgstring(StringUtils.trimToNull(execNode.valueOf("argstring")));
60 execution.setStartedBy(StringUtils.trimToNull(execNode.valueOf("user")));
61 execution.setAbortedBy(StringUtils.trimToNull(execNode.valueOf("abortedby")));
62 execution.setProject(StringUtils.trimToNull(execNode.valueOf("@project")));
63 String startedAt = StringUtils.trimToNull(execNode.valueOf("date-started/@unixtime"));
64 if (startedAt != null) {
65 execution.setStartedAt(new Date(Long.valueOf(startedAt)));
66 }
67 String endedAt = StringUtils.trimToNull(execNode.valueOf("date-ended/@unixtime"));
68 if (endedAt != null) {
69 execution.setEndedAt(new Date(Long.valueOf(endedAt)));
70 }
71
72 Node jobNode = execNode.selectSingleNode("job");
73 if (jobNode != null) {
74 RundeckJob job = new JobParser().parseXmlNode(jobNode);
75 execution.setJob(job);
76 }
77
78 final Node successfulNodes = execNode.selectSingleNode("successfulNodes");
79 if (successfulNodes != null) {
80 final List<RundeckNode> rundeckNodes =
81 new ListParser<RundeckNode>(new NodeParser(), "successfulNodes/node")
82 .parseXmlNode(execNode);
83 execution.setSuccessfulNodes(new HashSet<RundeckNodeIdentity>(rundeckNodes));
84 }else{
85 execution.setSuccessfulNodes(Collections.<RundeckNodeIdentity>emptySet());
86 }
87
88 final Node failedNodes = execNode.selectSingleNode("failedNodes");
89 if (failedNodes != null) {
90 final List<RundeckNode> rundeckNodes =
91 new ListParser<RundeckNode>(new NodeParser(), "failedNodes/node")
92 .parseXmlNode(execNode);
93 execution.setFailedNodes(new HashSet<RundeckNodeIdentity>(rundeckNodes));
94 } else {
95 execution.setFailedNodes(Collections.<RundeckNodeIdentity>emptySet());
96 }
97
98 return execution;
99 }
100
101 }