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 DescriptorSupport does not serialize targetObject
  28  * @author Eamonn McManus
  29  *
  30  * @run clean DescriptorSupportSerialTest
  31  * @run build DescriptorSupportSerialTest
  32  * @run main DescriptorSupportSerialTest
  33  */
  34 
  35 import java.lang.reflect.Method;
  36 import javax.management.*;
  37 import javax.management.remote.*;
  38 import javax.management.modelmbean.*;
  39 
  40 public class DescriptorSupportSerialTest {
  41     public static void main(String[] args) throws Exception {
  42         if (java.io.Serializable.class.isAssignableFrom(Thread.class))
  43             throw new Exception("TEST BAD: Thread is Serializable!");
  44         Method getName = Thread.class.getMethod("getName");
  45         Descriptor d = new DescriptorSupport();
  46         d.setField("name", "getName");
  47         d.setField("descriptorType", "operation");
  48         d.setField("TARGETObject", Thread.currentThread());
  49         d.setField("foo", "bar");
  50         ModelMBeanOperationInfo getNameInfo =
  51             new ModelMBeanOperationInfo("Get name", getName, d);
  52         ModelMBeanInfo mmbi =
  53             new ModelMBeanInfoSupport(Thread.class.getName(),
  54                                       "Thread!",
  55                                       null,  // no attributes
  56                                       null,  // no constructors
  57                                       new ModelMBeanOperationInfo[] {getNameInfo},
  58                                       null);  // no notifications
  59         ModelMBean mmb = new RequiredModelMBean(mmbi);
  60 
  61         ObjectName on = new ObjectName("d:type=Thread");
  62         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
  63         mbs.registerMBean(mmb, on);
  64 
  65         JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
  66         JMXConnectorServer cs =
  67             JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
  68         cs.start();
  69         JMXServiceURL addr = cs.getAddress();
  70         JMXConnector cc = JMXConnectorFactory.connect(addr);
  71         MBeanServerConnection mbsc = cc.getMBeanServerConnection();
  72 
  73         try {
  74             test(mbsc, on);
  75             System.out.println("TEST PASSED");
  76         } finally {
  77             cc.close();
  78             cs.stop();
  79         }
  80     }
  81 
  82     private static void test(MBeanServerConnection mbsc, ObjectName on)
  83             throws Exception {
  84         ModelMBeanInfo mmbi2 = (ModelMBeanInfo) mbsc.getMBeanInfo(on);
  85         // previous line will fail if serialization includes targetObject
  86 
  87         Descriptor d2 = mmbi2.getDescriptor("getName", "operation");
  88         if (!"bar".equals(d2.getFieldValue("foo")))
  89             throw new Exception("TEST FAILED: bad descriptor: " + d2);
  90 
  91         String name = (String) mbsc.invoke(on, "getName", null, null);
  92         String thisName = Thread.currentThread().getName();
  93         if (!thisName.equals(name)) {
  94             throw new Exception("TEST FAILED: wrong thread name: " +
  95                                 name + " should be " + thisName);
  96         }
  97     }
  98 }