View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans;
12  
13  import java.lang.management.ManagementFactory;
14  
15  import javax.management.MBeanServer;
16  import javax.management.MalformedObjectNameException;
17  import javax.management.ObjectName;
18  
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  import psiprobe.model.jmx.RuntimeInformation;
23  import psiprobe.tools.JmxTools;
24  
25  /**
26   * The Class RuntimeInfoAccessorBean.
27   */
28  public class RuntimeInfoAccessorBean {
29  
30    /** The Constant logger. */
31    private static final Logger logger = LoggerFactory.getLogger(RuntimeInfoAccessorBean.class);
32  
33    /**
34     * Gets the runtime information.
35     *
36     * @return the runtime information
37     */
38    public RuntimeInformation getRuntimeInformation() {
39      MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
40      RuntimeInformation ri = new RuntimeInformation();
41  
42      try {
43        ObjectName objectNameRuntime = new ObjectName("java.lang:type=Runtime");
44        ri.setStartTime(JmxTools.getLongAttr(mbeanServer, objectNameRuntime, "StartTime"));
45        ri.setUptime(JmxTools.getLongAttr(mbeanServer, objectNameRuntime, "Uptime"));
46        ri.setVmVendor(JmxTools.getStringAttr(mbeanServer, objectNameRuntime, "VmVendor"));
47  
48        ObjectName objectNameOperationSystem = new ObjectName("java.lang:type=OperatingSystem");
49        ri.setOsName(JmxTools.getStringAttr(mbeanServer, objectNameOperationSystem, "Name"));
50        ri.setOsVersion(JmxTools.getStringAttr(mbeanServer, objectNameOperationSystem, "Version"));
51  
52        if (!ri.getVmVendor().startsWith("IBM Corporation")) {
53          ri.setTotalPhysicalMemorySize(JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem,
54              "TotalPhysicalMemorySize"));
55          ri.setCommittedVirtualMemorySize(JmxTools.getLongAttr(mbeanServer,
56              objectNameOperationSystem, "CommittedVirtualMemorySize"));
57          ri.setFreePhysicalMemorySize(
58              JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "FreePhysicalMemorySize"));
59          ri.setFreeSwapSpaceSize(
60              JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "FreeSwapSpaceSize"));
61          ri.setTotalSwapSpaceSize(
62              JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "TotalSwapSpaceSize"));
63          ri.setProcessCpuTime(
64              JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "ProcessCpuTime"));
65          ri.setAvailableProcessors(Runtime.getRuntime().availableProcessors());
66        } else {
67          ri.setTotalPhysicalMemorySize(
68              JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "TotalPhysicalMemory"));
69        }
70  
71        if (JmxTools.hasAttribute(mbeanServer, objectNameOperationSystem, "OpenFileDescriptorCount")
72            && JmxTools.hasAttribute(mbeanServer, objectNameOperationSystem,
73                "MaxFileDescriptorCount")) {
74  
75          ri.setOpenFileDescriptorCount(JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem,
76              "OpenFileDescriptorCount"));
77          ri.setMaxFileDescriptorCount(
78              JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "MaxFileDescriptorCount"));
79        }
80  
81        return ri;
82      } catch (MalformedObjectNameException e) {
83        logger.debug("OS information is unavailable");
84        logger.trace("", e);
85        return null;
86      }
87    }
88  }