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