1 /* 2 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6610094 27 * @summary Test the OperatingSystemMXBean instance returned by 28 * ManagementFactory.getPlatformMXBeans() 29 * @author Mandy Chung 30 * 31 * @modules java.management 32 * jdk.management 33 * @run main PlatformMXBeanTest 34 */ 35 36 import java.lang.management.*; 37 import java.util.List; 38 39 public class PlatformMXBeanTest { 40 public static void main(String[] argv) throws Exception { 41 OperatingSystemMXBean osMBean = getOSPlatformMXBean(OperatingSystemMXBean.class); 42 43 // There should have only one single MXBean for the OS MXBean interfaces: 44 // java.lang.management.OperatingSystemMXBean 45 // com.sun.management.OperatingSystemMXBean 46 // com.sun.management.UnixOperatingSystemMXBean 47 if (osMBean != getOSPlatformMXBean(com.sun.management.OperatingSystemMXBean.class)) { 48 throw new RuntimeException( 49 "Invalid com.sun.management.OperatingSystemMXBean instance"); 50 } 51 52 if (!System.getProperty("os.name").startsWith("Windows") && 53 osMBean != getOSPlatformMXBean(com.sun.management.UnixOperatingSystemMXBean.class)) { 54 throw new RuntimeException( 55 "Invalid com.sun.management.UnixOperatingSystemMXBean instance"); 56 } 57 } 58 59 private static <T extends OperatingSystemMXBean> 60 T getOSPlatformMXBean(Class<T> c) { 61 List<T> result = ManagementFactory.getPlatformMXBeans(c); 62 if (result.isEmpty()) { 63 return null; 64 } else if (result.size() == 1) { 65 return result.get(0); 66 } else { 67 throw new RuntimeException(c.getName() + " has " + 68 result.size() + " number of instances"); 69 } 70 } 71 }