jdk/test/javax/management/remote/mandatory/loading/DeserializeEncodedURLTest.java
author dfuchs
Thu, 02 Feb 2017 16:50:46 +0000
changeset 43503 bc7f8619ab70
parent 30376 2ccf2cf7ea48
child 44423 306c020eb154
permissions -rw-r--r--
8173607: JMX RMI connector should be in its own module Summary: The JMX RMI connector is moved to a new java.management.rmi module. Reviewed-by: mchung, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2015, 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 4924683
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check RMI/JRMP stubs can be deserialized using user's loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 30376
diff changeset
    29
 * @modules java.management.rmi
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean DeserializeEncodedURLTest SingleClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build DeserializeEncodedURLTest SingleClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main DeserializeEncodedURLTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.remote.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  Test that the RMI connector client can handle a URL of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  where the serialized RMIServer stub is encoded directly in the URL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  when the class of that stub is known to the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  DEFAULT_CLASS_LOADER but not to the calling code's class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  This is an unusual usage, and is not explicitly specified in the JMX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  Remote API, but it is potentially useful where client and server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  agree to a code base for mutant stubs (that e.g. use a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  protocol or include debugging or optimization).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  We make an RMI connector server by giving it an instance of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  RMIJRMPServerImpl subclass that manufactures mutant stubs.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  stubs are known to a special loader (mutantLoader) but not to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  test's loader.  We set up the client's default loader to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  mutantLoader, and check that it can deserialize the stub containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  the mutant stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  This test incidentally creates the connector server as an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  rather than using the JMXConnectorServerFactory, just because I'm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  not sure we have coverage of that elsewhere.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class DeserializeEncodedURLTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final ClassLoader mutantLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        new SingleClassLoader("SubMutantRMIServerStub",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                              MutantRMIServerStub.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                              MutantRMIServerStub.class.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final Class subMutantRMIServerStubClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            subMutantRMIServerStubClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                mutantLoader.loadClass("SubMutantRMIServerStub");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new Error(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.out.println("Check that we can deserialize a mutant stub " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                           "from an RMI connector URL even when the stub's " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                           "class is known to the user's default loader " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                           "but not the caller's loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.out.println("Create RMI connector server as an MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ObjectName csName = new ObjectName("test:type=RMIConnectorServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        RMIServerImpl impl = new MutantRMIServerImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        mbs.createMBean("javax.management.remote.rmi.RMIConnectorServer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        csName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        new Object[] {url, null, impl, null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        new String[] {JMXServiceURL.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                      Map.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                      RMIServerImpl.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                      MBeanServer.class.getName()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        mbs.invoke(csName, "start", new Object[0], new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        JMXServiceURL address =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            (JMXServiceURL) mbs.getAttribute(csName, "Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println("Address with mutant stub: " + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Map env = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        env.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER, mutantLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        JMXConnector conn = JMXConnectorFactory.newJMXConnector(address, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.out.println("Client successfully created with this address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        System.out.println("Try to connect newly-created client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            conn.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            System.out.println("TEST FAILS: Connect worked but should not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                               "have");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (MutantException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.out.println("Caught MutantException as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.out.println("TEST FAILS: Caught unexpected exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        mbs.invoke(csName, "stop", new Object[0], new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static class MutantException extends IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static class MutantRMIServerStub
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            implements RMIServer, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        public MutantRMIServerStub() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public String getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return "1.0 BOGUS";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        public RMIConnection newClient(Object credentials) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new MutantException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static class MutantRMIServerImpl extends RMIJRMPServerImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public MutantRMIServerImpl() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            super(0, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public Remote toStub() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return (Remote) subMutantRMIServerStubClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                IOException ioe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    new IOException("Couldn't make submutant stub");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                ioe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}