1 /*
   2  * Copyright (c) 1998, 2014, 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 /* @test
  25  * @bug 4149366
  26  * @summary The class loader used to load classes for parameter types sent in
  27  * an RMI call to an activatable object should delegate to the class loader
  28  * that loaded the class of the activatable object itself, to maximize the
  29  * likelihood of type compatibility between downloaded parameter types and
  30  * supertypes shared with the activatable object.
  31  * @author Peter Jones (much code taken from Ann Wollrath's activation tests)
  32  *
  33  * @library ../../../testlibrary
  34  * @modules java.rmi/sun.rmi.registry
  35  *          java.rmi/sun.rmi.server
  36  *          java.rmi/sun.rmi.transport
  37  *          java.rmi/sun.rmi.transport.tcp
  38  * @build TestLibrary RMID ActivationLibrary
  39  *     Foo FooReceiverImpl FooReceiverImpl_Stub Bar
  40  * @run main/othervm/policy=security.policy/timeout=240 DownloadParameterClass
  41  */
  42 
  43 import java.io.*;
  44 import java.net.*;
  45 import java.util.*;
  46 import java.rmi.*;
  47 import java.rmi.activation.*;
  48 import java.rmi.server.*;
  49 import java.rmi.registry.*;
  50 
  51 public class DownloadParameterClass {
  52 
  53     public interface FooReceiver extends Remote {
  54 
  55         /*
  56          * The interface can't actually declare that the method takes a
  57          * Foo, because then Foo would have to be in the test's CLASSPATH,
  58          * which might get propagated to the group VM's CLASSPATH, which
  59          * would nullify the test (the Foo supertype must be loaded in the
  60          * group VM only through the class loader that loaded the
  61          * activatable object).
  62          */
  63         public void receiveFoo(Object obj) throws RemoteException;
  64     }
  65 
  66     public static void main(String[] args) {
  67 
  68         System.err.println("\nRegression test for bug 4149366\n");
  69 
  70         /*
  71          * Install classes to be seen by the activatable object's class
  72          * loader in the "codebase1" subdirectory of working directory, and
  73          * install the subtype to be downloaded into the activatable object
  74          * into the "codebase2" subdirectory.
  75          */
  76         URL codebase1 = null;
  77         URL codebase2 = null;
  78         try {
  79             codebase1 = TestLibrary.installClassInCodebase("FooReceiverImpl", "codebase1");
  80             TestLibrary.installClassInCodebase("FooReceiverImpl_Stub", "codebase1");
  81             TestLibrary.installClassInCodebase("Foo", "codebase1");
  82             codebase2 = TestLibrary.installClassInCodebase("Bar", "codebase2");
  83         } catch (MalformedURLException e) {
  84             TestLibrary.bomb("failed to install test classes", e);
  85         }
  86 
  87         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  88 
  89         RMID rmid = null;
  90 
  91         try {
  92             RMID.removeLog();
  93             rmid = RMID.createRMID();
  94             rmid.start();
  95 
  96             /* Cause activation groups to have a security policy that will
  97              * allow security managers to be downloaded and installed
  98              */
  99             Properties p = new Properties();
 100             // this test must always set policies/managers in its
 101             // activation groups
 102             p.put("java.security.policy",
 103                   TestParams.defaultGroupPolicy);
 104             p.put("java.security.manager",
 105                   TestParams.defaultSecurityManager);
 106 
 107             /*
 108              * Create and register descriptors for activatable object in a
 109              * group other than this VM's group, so that another VM will be
 110              * spawned with the object is activated.
 111              */
 112             System.err.println("Creating descriptors");
 113             ActivationGroupDesc groupDesc =
 114                 new ActivationGroupDesc(p, null);
 115             ActivationGroupID groupID =
 116                 ActivationGroup.getSystem().registerGroup(groupDesc);
 117             ActivationDesc objDesc =
 118                 new ActivationDesc(groupID, "FooReceiverImpl",
 119                                    codebase1.toString(), null, false);
 120 
 121             System.err.println("Registering descriptors");
 122             FooReceiver obj = (FooReceiver) Activatable.register(objDesc);
 123 
 124             /*
 125              * Create an instance of the subtype to be downloaded by the
 126              * activatable object.  The codebase must be a path including
 127              * "codebase1" as well as "codebase2" because the supertype
 128              * must be visible here as well; the supertype cannot be
 129              * installed in both codebases (like it would be in a typical
 130              * setup) because of the trivial installation mechanism used
 131              * below, and see the comment above for why it can't be in
 132              * the test's CLASSPATH.
 133              */
 134             Class subtype = RMIClassLoader.loadClass(
 135                 codebase2 + " " + codebase1, "Bar");
 136             Object subtypeInstance = subtype.newInstance();
 137 
 138             obj.receiveFoo(subtypeInstance);
 139 
 140             System.err.println("\nTEST PASSED\n");
 141 
 142         } catch (Exception e) {
 143             TestLibrary.bomb("test failed", e);
 144         } finally {
 145             rmid.cleanup();
 146         }
 147     }
 148 }
--- EOF ---