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