0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4924683
0N/A * @summary Check RMI/JRMP stubs can be deserialized using user's loader
0N/A * @author Eamonn McManus
0N/A * @run clean DeserializeEncodedURLTest SingleClassLoader
0N/A * @run build DeserializeEncodedURLTest SingleClassLoader
0N/A * @run main DeserializeEncodedURLTest
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.rmi.*;
0N/Aimport java.util.*;
0N/Aimport javax.management.*;
0N/Aimport javax.management.remote.*;
0N/Aimport javax.management.remote.rmi.*;
0N/A
0N/A/*
0N/A Test that the RMI connector client can handle a URL of the form
0N/A where the serialized RMIServer stub is encoded directly in the URL,
0N/A when the class of that stub is known to the supplied
0N/A DEFAULT_CLASS_LOADER but not to the calling code's class loader.
0N/A This is an unusual usage, and is not explicitly specified in the JMX
0N/A Remote API, but it is potentially useful where client and server
0N/A agree to a code base for mutant stubs (that e.g. use a different
0N/A protocol or include debugging or optimization).
0N/A
0N/A We make an RMI connector server by giving it an instance of an
0N/A RMIJRMPServerImpl subclass that manufactures mutant stubs. These
0N/A stubs are known to a special loader (mutantLoader) but not to this
0N/A test's loader. We set up the client's default loader to
0N/A mutantLoader, and check that it can deserialize the stub containing
0N/A the mutant stub.
0N/A
0N/A This test incidentally creates the connector server as an MBean
0N/A rather than using the JMXConnectorServerFactory, just because I'm
0N/A not sure we have coverage of that elsewhere.
0N/A*/
0N/Apublic class DeserializeEncodedURLTest {
0N/A private static final ClassLoader mutantLoader =
0N/A new SingleClassLoader("SubMutantRMIServerStub",
0N/A MutantRMIServerStub.class,
0N/A MutantRMIServerStub.class.getClassLoader());
0N/A private static final Class subMutantRMIServerStubClass;
0N/A static {
0N/A try {
0N/A subMutantRMIServerStubClass =
0N/A mutantLoader.loadClass("SubMutantRMIServerStub");
0N/A } catch (ClassNotFoundException e) {
0N/A throw new Error(e);
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A System.out.println("Check that we can deserialize a mutant stub " +
0N/A "from an RMI connector URL even when the stub's " +
0N/A "class is known to the user's default loader " +
0N/A "but not the caller's loader");
0N/A
0N/A System.out.println("Create RMI connector server as an MBean");
0N/A
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A ObjectName csName = new ObjectName("test:type=RMIConnectorServer");
0N/A JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
0N/A RMIServerImpl impl = new MutantRMIServerImpl();
0N/A mbs.createMBean("javax.management.remote.rmi.RMIConnectorServer",
0N/A csName,
0N/A new Object[] {url, null, impl, null},
0N/A new String[] {JMXServiceURL.class.getName(),
0N/A Map.class.getName(),
0N/A RMIServerImpl.class.getName(),
0N/A MBeanServer.class.getName()});
0N/A mbs.invoke(csName, "start", new Object[0], new String[0]);
0N/A
0N/A JMXServiceURL address =
0N/A (JMXServiceURL) mbs.getAttribute(csName, "Address");
0N/A
0N/A System.out.println("Address with mutant stub: " + address);
0N/A
0N/A Map env = new HashMap();
0N/A env.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER, mutantLoader);
0N/A JMXConnector conn = JMXConnectorFactory.newJMXConnector(address, env);
0N/A
0N/A System.out.println("Client successfully created with this address");
0N/A System.out.println("Try to connect newly-created client");
0N/A
0N/A try {
0N/A conn.connect();
0N/A System.out.println("TEST FAILS: Connect worked but should not " +
0N/A "have");
0N/A System.exit(1);
0N/A } catch (MutantException e) {
0N/A System.out.println("Caught MutantException as expected");
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILS: Caught unexpected exception:");
0N/A e.printStackTrace(System.out);
0N/A System.exit(1);
0N/A }
0N/A
0N/A mbs.invoke(csName, "stop", new Object[0], new String[0]);
0N/A System.out.println("Test passed");
0N/A }
0N/A
0N/A private static class MutantException extends IOException {}
0N/A
0N/A public static class MutantRMIServerStub
0N/A implements RMIServer, Serializable {
0N/A public MutantRMIServerStub() {}
0N/A
0N/A public String getVersion() {
0N/A return "1.0 BOGUS";
0N/A }
0N/A
0N/A public RMIConnection newClient(Object credentials) throws IOException {
0N/A throw new MutantException();
0N/A }
0N/A }
0N/A
0N/A private static class MutantRMIServerImpl extends RMIJRMPServerImpl {
0N/A public MutantRMIServerImpl() throws IOException {
0N/A super(0, null, null, null);
0N/A }
0N/A
0N/A public Remote toStub() throws IOException {
0N/A try {
0N/A return (Remote) subMutantRMIServerStubClass.newInstance();
0N/A } catch (Exception e) {
0N/A IOException ioe =
0N/A new IOException("Couldn't make submutant stub");
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A }
0N/A }
0N/A}