View Javadoc

1   /*
2    * Copyright 2011 Vincent Behar
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Parser for a single {@link RundeckExecution}
33   * 
34   * @author Vincent Behar
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 }