jdk/test/javax/management/remote/mandatory/loading/TargetMBeanTest.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4910428
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests target MBean class loader used before JSR 160 loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean TargetMBeanTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build TargetMBeanTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main TargetMBeanTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  The JSR 160 spec says that, when invoking a method (or setting an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  attribute or creating) on a target MBean, that MBean's class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  is used to deserialize parameters.  The problem is that the RMI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  connector protocol wraps these parameters as MarshalledObjects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  When you call get() on a MarshalledObject, the context class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  is used to deserialize, so if we set this to the target MBean's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  class loader everything should work.  EXCEPT that MarshalledObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  first tries to load classes using the first class loader it finds in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  the caller's stack.  If our JSR 160 implementation is part of J2SE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  it will not find any such class loader (only the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  loader).  But if it's standalone, then it will find the class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  of the JSR 160 implementation.  If the class name of a parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  known to both the 160 loader and the target MBean loader, then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  will use the wrong loader for deserialization and the attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  invoke the target MBean with the deserialized object will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  We test this as follows.  We fabricate an MLet that has the same set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  of URLs as the 160 class loader, which we assume is the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  loader (or at least, it is a URLClassLoader).  This MLet is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  therefore a "shadow class loader" -- for every class name known to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  the 160 class loader, it can load the same name, but the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  not the same class, since it has not been loaded by the same loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  Then, we use the MLet to create an RMIConnectorServer MBean.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  MBean is an instance of "shadow RMIConnectorServer", and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  constructor has a parameter of type "shadow JMXServiceURL".  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  constructor is invoked with "real JMXServiceURL" it will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  While we are at it, we also test that the behaviour is correct for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  the JMXMP protocol, if that optional protocol is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.management.loading.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.management.remote.rmi.RMIConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public class TargetMBeanTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final ObjectName mletName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            mletName = new ObjectName("x:type=mlet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.out.println("Test that target MBean class loader is used " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                           "before JMX Remote API class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ClassLoader jmxRemoteClassLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            JMXServiceURL.class.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (jmxRemoteClassLoader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            System.out.println("JMX Remote API loaded by bootstrap " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                               "class loader, this test is irrelevant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (!(jmxRemoteClassLoader instanceof URLClassLoader)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            System.out.println("TEST INVALID: JMX Remote API not loaded by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                               "URLClassLoader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        URLClassLoader jrcl = (URLClassLoader) jmxRemoteClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        URL[] urls = jrcl.getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        PrivateMLet mlet = new PrivateMLet(urls, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Class shadowClass = mlet.loadClass(JMXServiceURL.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (shadowClass == JMXServiceURL.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.out.println("TEST INVALID: MLet got original " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                               "JMXServiceURL not shadow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        mbs.registerMBean(mlet, mletName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        final String[] protos = {"rmi", "iiop", "jmxmp"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        for (int i = 0; i < protos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                ok &= test(protos[i], mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                System.out.println("TEST FAILED WITH EXCEPTION:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println("TEST FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static boolean test(String proto, MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        System.out.println("Testing for proto " + proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        JMXConnectorServer cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        JMXServiceURL url = new JMXServiceURL(proto, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                                                 mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.out.println("System does not recognize URL: " + url +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                               "; ignoring");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        JMXServiceURL addr = cs.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        JMXServiceURL rmiurl = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        JMXConnector client = JMXConnectorFactory.connect(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        MBeanServerConnection mbsc = client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        ObjectName on = new ObjectName("x:proto=" + proto + ",ok=yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        mbsc.createMBean(RMIConnectorServer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                         on,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                         mletName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                         new Object[] {rmiurl, null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                         new String[] {JMXServiceURL.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                       Map.class.getName()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        System.out.println("Successfully deserialized with " + proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        mbsc.unregisterMBean(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}