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 extends BaseXpathParser<RundeckSystemInfo> {
29  
30  
31      public SystemInfoParser(final String xpath) {
32          super(xpath);
33      }
34  
35      @Override
36      public RundeckSystemInfo parse(Node infoNode) {
37          RundeckSystemInfo info = new RundeckSystemInfo();
38  
39          String timestamp = StringUtils.trimToNull(infoNode.valueOf("timestamp/@epoch"));
40          if (timestamp != null) {
41              info.setDate(new Date(Long.valueOf(timestamp)));
42          }
43          info.setVersion(StringUtils.trimToNull(infoNode.valueOf("rundeck/version")));
44          info.setBuild(StringUtils.trimToNull(infoNode.valueOf("rundeck/build")));
45          info.setNode(StringUtils.trimToNull(infoNode.valueOf("rundeck/node")));
46          info.setBaseDir(StringUtils.trimToNull(infoNode.valueOf("rundeck/base")));
47          info.setOsArch(StringUtils.trimToNull(infoNode.valueOf("os/arch")));
48          info.setOsName(StringUtils.trimToNull(infoNode.valueOf("os/name")));
49          info.setOsVersion(StringUtils.trimToNull(infoNode.valueOf("os/version")));
50          info.setJvmName(StringUtils.trimToNull(infoNode.valueOf("jvm/name")));
51          info.setJvmVendor(StringUtils.trimToNull(infoNode.valueOf("jvm/vendor")));
52          info.setJvmVersion(StringUtils.trimToNull(infoNode.valueOf("jvm/version")));
53          String startDate = StringUtils.trimToNull(infoNode.valueOf("stats/uptime/since/@epoch"));
54          if (startDate != null) {
55              info.setStartDate(new Date(Long.valueOf(startDate)));
56          }
57          info.setUptimeInMillis(Long.valueOf(infoNode.valueOf("stats/uptime/@duration")));
58          info.setCpuLoadAverage(StringUtils.trimToNull(infoNode.valueOf("stats/cpu/loadAverage")));
59          if (info.getCpuLoadAverage() != null) {
60              info.setCpuLoadAverage(info.getCpuLoadAverage() + " %");
61          }
62          info.setMaxMemoryInBytes(Long.valueOf(infoNode.valueOf("stats/memory/max")));
63          info.setFreeMemoryInBytes(Long.valueOf(infoNode.valueOf("stats/memory/free")));
64          info.setTotalMemoryInBytes(Long.valueOf(infoNode.valueOf("stats/memory/total")));
65          info.setRunningJobs(Integer.valueOf(infoNode.valueOf("stats/scheduler/running")));
66          info.setActiveThreads(Integer.valueOf(infoNode.valueOf("stats/threads/active")));
67  
68          return info;
69      }
70  }