1 /* 2 * Copyright (c) 2005, 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 8058865 27 * @summary Checks access to test MXBean 28 * @author Olivier Lagneau 29 * @modules java.management.rmi 30 * @library /lib/testlibrary 31 * @compile Basic.java 32 * @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanInteropTest2 33 */ 34 35 import java.util.Iterator; 36 import java.util.Map; 37 import java.util.Set; 38 39 import javax.management.Attribute; 40 import javax.management.JMX; 41 import javax.management.MBeanAttributeInfo; 42 import javax.management.MBeanConstructorInfo; 43 import javax.management.MBeanServer; 44 import java.lang.management.ManagementFactory; 45 import javax.management.MBeanInfo; 46 import javax.management.MBeanNotificationInfo; 47 import javax.management.MBeanOperationInfo; 48 import javax.management.MBeanServerConnection; 49 import javax.management.ObjectName; 50 import javax.management.remote.JMXConnector; 51 import javax.management.remote.JMXConnectorFactory; 52 import javax.management.remote.JMXConnectorServer; 53 import javax.management.remote.JMXConnectorServerFactory; 54 import javax.management.remote.JMXServiceURL; 55 56 public class MXBeanInteropTest2 { 57 58 private static String BASIC_MXBEAN_CLASS_NAME = "Basic"; 59 60 /* 61 * First Debug properties and arguments are collect in expected 62 * map (argName, value) format, then calls original test's run method. 63 */ 64 public static void main(String args[]) throws Exception { 65 66 System.out.println("================================================="); 67 68 // Parses parameters 69 Utils.parseDebugProperties(); 70 Map<String, Object> map = Utils.parseParameters(args) ; 71 72 // Run test 73 MXBeanInteropTest2 test = new MXBeanInteropTest2(); 74 test.run(map); 75 76 } 77 78 public void run(Map<String, Object> args) { 79 80 System.out.println("MXBeanInteropTest2::run: Start") ; 81 int errorCount = 0 ; 82 83 try { 84 // JMX MbeanServer used inside single VM as if remote. 85 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 86 87 JMXServiceURL url = new JMXServiceURL("rmi", null, 0); 88 JMXConnectorServer cs = 89 JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); 90 cs.start(); 91 92 JMXServiceURL addr = cs.getAddress(); 93 JMXConnector cc = JMXConnectorFactory.connect(addr); 94 MBeanServerConnection mbsc = cc.getMBeanServerConnection(); 95 96 // Prints all MBeans whatever the domain is. 97 printMBeans(mbsc) ; 98 99 // Call test body 100 errorCount += doBasicMXBeanTest(mbsc) ; 101 102 // Terminate the JMX Client 103 cc.close(); 104 105 } catch(Exception e) { 106 Utils.printThrowable(e, true) ; 107 throw new RuntimeException(e); 108 } 109 110 if ( errorCount == 0 ) { 111 System.out.println("MXBeanInteropTest2::run: Done without any error") ; 112 } else { 113 System.out.println("MXBeanInteropTest2::run: Done with " 114 + errorCount 115 + " error(s)") ; 116 throw new RuntimeException("errorCount = " + errorCount); 117 } 118 } 119 120 121 /** 122 * Prints all MBeans whatever the domain is. 123 */ 124 private static void printMBeans(MBeanServerConnection mbsc) throws Exception { 125 Set<ObjectName> set = mbsc.queryNames(null, null); 126 System.out.println("---- MBeans found :"); 127 128 for (Iterator<ObjectName> iter = set.iterator(); iter.hasNext(); ) { 129 System.out.println(iter.next().toString()); 130 } 131 132 System.out.println("\n") ; 133 } 134 135 136 private final int doBasicMXBeanTest(MBeanServerConnection mbsc) { 137 int errorCount = 0 ; 138 System.out.println("---- doBasicMXBeanTest") ; 139 140 try { 141 ObjectName objName = 142 new ObjectName("sqe:type=BasicMXBean") ; 143 mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName); 144 MBeanInfo mbInfo = mbsc.getMBeanInfo(objName); 145 printMBeanInfo(mbInfo); 146 System.out.println("---- OK\n") ; 147 System.out.println("getMBeanInfo\t\t" 148 + mbInfo); 149 System.out.println("---- OK\n") ; 150 151 System.out.println("Check mxbean field in the MBeanInfo"); 152 String mxbeanField = 153 (String)mbInfo.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD); 154 155 if ( mxbeanField == null || ! mxbeanField.equals("true")) { 156 System.out.println("---- ERROR : Improper mxbean field value " 157 + mxbeanField); 158 errorCount++; 159 } 160 System.out.println("---- OK\n") ; 161 162 System.out.println("Set attribute ObjectNameAtt"); 163 Attribute att = new Attribute("ObjectNameAtt", objName); 164 mbsc.setAttribute(objName, att); 165 ObjectName value = 166 (ObjectName)mbsc.getAttribute(objName, "ObjectNameAtt"); 167 168 if ( ! value.equals(objName) ) { 169 errorCount++; 170 System.out.println("---- ERROR : setAttribute failed, got " 171 + value 172 + " while expecting " 173 + objName); 174 } 175 System.out.println("---- OK\n") ; 176 177 System.out.println("Call operation doNothing"); 178 mbsc.invoke(objName, "doNothing", null, null); 179 System.out.println("---- OK\n") ; 180 181 System.out.println("Call operation getWeather"); 182 Object weather = mbsc.invoke(objName, 183 "getWeather", 184 new Object[]{Boolean.TRUE}, 185 new String[]{"boolean"}); 186 System.out.println("Weather is " + weather); 187 System.out.println("---- OK\n") ; 188 } catch (Exception e) { 189 Utils.printThrowable(e, true) ; 190 errorCount++ ; 191 System.out.println("---- ERROR\n") ; 192 } 193 194 return errorCount ; 195 } 196 197 private void printMBeanInfo(MBeanInfo mbInfo) { 198 System.out.println("Description " + mbInfo.getDescription()); 199 200 for (MBeanConstructorInfo ctor : mbInfo.getConstructors()) { 201 System.out.println("Constructor " + ctor.getName()); 202 } 203 204 for (MBeanAttributeInfo att : mbInfo.getAttributes()) { 205 System.out.println("Attribute " + att.getName() 206 + " [" + att.getType() + "]"); 207 } 208 209 for (MBeanOperationInfo oper : mbInfo.getOperations()) { 210 System.out.println("Operation " + oper.getName()); 211 } 212 213 for (MBeanNotificationInfo notif : mbInfo.getNotifications()) { 214 System.out.println("Notification " + notif.getName()); 215 } 216 } 217 }