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 6332962 27 * @summary Test that a RequiredModelMBean operation can have a targetObject 28 * that is not serializable 29 * @author Eamonn McManus 30 * 31 * @modules java.management.rmi 32 * 33 * @run clean UnserializableTargetObjectTest 34 * @run build UnserializableTargetObjectTest 35 * @run main UnserializableTargetObjectTest 36 */ 37 38 /* This test and DescriptorSupportSerialTest basically cover the same thing. 39 I wrote them at different times and forgot that I had written the earlier 40 one. However the coverage is slightly different so I'm keeping both. */ 41 42 import java.lang.reflect.Method; 43 import javax.management.Attribute; 44 import javax.management.Descriptor; 45 import javax.management.MBeanServer; 46 import javax.management.MBeanServerConnection; 47 import javax.management.MBeanServerFactory; 48 import javax.management.ObjectName; 49 import javax.management.modelmbean.DescriptorSupport; 50 import javax.management.modelmbean.ModelMBean; 51 import javax.management.modelmbean.ModelMBeanAttributeInfo; 52 import javax.management.modelmbean.ModelMBeanInfo; 53 import javax.management.modelmbean.ModelMBeanInfoSupport; 54 import javax.management.modelmbean.ModelMBeanOperationInfo; 55 import javax.management.modelmbean.RequiredModelMBean; 56 import javax.management.remote.JMXConnector; 57 import javax.management.remote.JMXConnectorFactory; 58 import javax.management.remote.JMXConnectorServer; 59 import javax.management.remote.JMXConnectorServerFactory; 60 import javax.management.remote.JMXServiceURL; 61 62 public class UnserializableTargetObjectTest { 63 public static class Resource { // not serializable! 64 int count; 65 int operationCount; 66 67 public void operation() { 68 operationCount++; 69 } 70 71 public int getCount() { 72 return count; 73 } 74 75 public void setCount(int count) { 76 this.count = count; 77 } 78 } 79 80 public static void main(String[] args) throws Exception { 81 MBeanServer mbs = MBeanServerFactory.newMBeanServer(); 82 ObjectName name = new ObjectName("a:b=c"); 83 Resource resource1 = new Resource(); 84 Resource resource2 = new Resource(); 85 Resource resource3 = new Resource(); 86 Method operationMethod = Resource.class.getMethod("operation"); 87 Method getCountMethod = Resource.class.getMethod("getCount"); 88 Method setCountMethod = Resource.class.getMethod("setCount", int.class); 89 Descriptor operationDescriptor = 90 new DescriptorSupport(new String[] { 91 "descriptorType", "name", "targetObject" 92 }, new Object[] { 93 "operation", "operation", resource1 94 }); 95 Descriptor getCountDescriptor = 96 new DescriptorSupport(new String[] { 97 "descriptorType", "name", "targetObject" 98 }, new Object[] { 99 "operation", "getCount", resource2 100 }); 101 Descriptor setCountDescriptor = 102 new DescriptorSupport(new String[] { 103 "descriptorType", "name", "targetObject" 104 }, new Object[] { 105 "operation", "setCount", resource2 106 }); 107 Descriptor countDescriptor = 108 new DescriptorSupport(new String[] { 109 "descriptorType", "name", "getMethod", "setMethod" 110 }, new Object[] { 111 "attribute", "Count", "getCount", "setCount" 112 }); 113 ModelMBeanOperationInfo operationInfo = 114 new ModelMBeanOperationInfo("operation description", 115 operationMethod, operationDescriptor); 116 ModelMBeanOperationInfo getCountInfo = 117 new ModelMBeanOperationInfo("getCount description", 118 getCountMethod, getCountDescriptor); 119 ModelMBeanOperationInfo setCountInfo = 120 new ModelMBeanOperationInfo("setCount description", 121 setCountMethod, setCountDescriptor); 122 ModelMBeanAttributeInfo countInfo = 123 new ModelMBeanAttributeInfo("Count", "Count description", 124 getCountMethod, setCountMethod, 125 countDescriptor); 126 ModelMBeanInfo mmbi = 127 new ModelMBeanInfoSupport(Resource.class.getName(), 128 "ModelMBean to test targetObject", 129 new ModelMBeanAttributeInfo[] {countInfo}, 130 null, // no constructors 131 new ModelMBeanOperationInfo[] { 132 operationInfo, getCountInfo, setCountInfo 133 }, 134 null); // no notifications 135 ModelMBean mmb = new RequiredModelMBean(mmbi); 136 mmb.setManagedResource(resource3, "ObjectReference"); 137 mbs.registerMBean(mmb, name); 138 mbs.invoke(name, "operation", null, null); 139 mbs.setAttribute(name, new Attribute("Count", 53)); 140 if (resource1.operationCount != 1) 141 throw new Exception("operationCount: " + resource1.operationCount); 142 if (resource2.count != 53) 143 throw new Exception("count: " + resource2.count); 144 int got = (Integer) mbs.getAttribute(name, "Count"); 145 if (got != 53) 146 throw new Exception("got count: " + got); 147 148 JMXServiceURL url = new JMXServiceURL("rmi", null, 0); 149 JMXConnectorServer cs = 150 JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); 151 cs.start(); 152 JMXServiceURL addr = cs.getAddress(); 153 JMXConnector cc = JMXConnectorFactory.connect(addr); 154 MBeanServerConnection mbsc = cc.getMBeanServerConnection(); 155 ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name); 156 // Above gets NotSerializableException if resource included in 157 // serialized form 158 cc.close(); 159 cs.stop(); 160 System.out.println("TEST PASSED"); 161 } 162 }