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