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.RundeckSystemInfo;
22  
23  /**
24   * Parser for a single {@link RundeckSystemInfo}
25   * 
26   * @author Vincent Behar
27   */
28  public class SystemInfoParser implements XmlNodeParser<RundeckSystemInfo> {
29  
30      private String xpath;
31  
32      public SystemInfoParser() {
33          super();
34      }
35  
36      /**
37       * @param xpath of the systemInfo element if it is not the root node
38       */
39      public SystemInfoParser(String xpath) {
40          super();
41          this.xpath = xpath;
42      }
43  
44      @Override
45      public RundeckSystemInfo parseXmlNode(Node node) {
46          Node infoNode = xpath != null ? node.selectSingleNode(xpath) : node;
47  
48          RundeckSystemInfo info = new RundeckSystemInfo();
49  
50          String timestamp = StringUtils.trimToNull(infoNode.valueOf("timestamp/@epoch"));
51          if (timestamp != null) {
52              info.setDate(new Date(Long.valueOf(timestamp)));
53          }
54          info.setVersion(StringUtils.trimToNull(infoNode.valueOf("rundeck/version")));
55          info.setBuild(StringUtils.trimToNull(infoNode.valueOf("rundeck/build")));
56          info.setNode(StringUtils.trimToNull(infoNode.valueOf("rundeck/node")));
57          info.setBaseDir(StringUtils.trimToNull(infoNode.valueOf("rundeck/base")));
58          info.setOsArch(StringUtils.trimToNull(infoNode.valueOf("os/arch")));
59          info.setOsName(StringUtils.trimToNull(infoNode.valueOf("os/name")));
60          info.setOsVersion(StringUtils.trimToNull(infoNode.valueOf("os/version")));
61          info.setJvmName(StringUtils.trimToNull(infoNode.valueOf("jvm/name")));
62          info.setJvmVendor(StringUtils.trimToNull(infoNode.valueOf("jvm/vendor")));
63          info.setJvmVersion(StringUtils.trimToNull(infoNode.valueOf("jvm/version")));
64          String startDate = StringUtils.trimToNull(infoNode.valueOf("stats/uptime/since/@epoch"));
65          if (startDate != null) {
66              info.setStartDate(new Date(Long.valueOf(startDate)));
67          }
68          info.setUptimeInMillis(Long.valueOf(infoNode.valueOf("stats/uptime/@duration")));
69          info.setCpuLoadAverage(StringUtils.trimToNull(infoNode.valueOf("stats/cpu/loadAverage")));
70          if (info.getCpuLoadAverage() != null) {
71              info.setCpuLoadAverage(info.getCpuLoadAverage() + " %");
72          }
73          info.setMaxMemoryInBytes(Long.valueOf(infoNode.valueOf("stats/memory/max")));
74          info.setFreeMemoryInBytes(Long.valueOf(infoNode.valueOf("stats/memory/free")));
75          info.setTotalMemoryInBytes(Long.valueOf(infoNode.valueOf("stats/memory/total")));
76          info.setRunningJobs(Integer.valueOf(infoNode.valueOf("stats/scheduler/running")));
77          info.setActiveThreads(Integer.valueOf(infoNode.valueOf("stats/threads/active")));
78  
79          return info;
80      }
81  }