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.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   * Parser for a single {@link RundeckExecution}
27   * 
28   * @author Vincent Behar
29   */
30  public class ExecutionParser implements XmlNodeParser<RundeckExecution> {
31  
32      private String xpath;
33  
34      public ExecutionParser() {
35          super();
36      }
37  
38      /**
39       * @param xpath of the execution element if it is not the root node
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  }