1 /*
   2  * Copyright (c) 2002, 2015, 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 /*
  25  * @test
  26  * @bug 4510355
  27  * @summary ActivationGroup implementations cannot be downloaded by default;
  28  * Creates a custom activation group without setting a security manager
  29  * in activation group's descriptor.  The custom activation group
  30  * implementation should be downloaded when first object within that group
  31  * is activated.
  32  * @author Ann Wollrath
  33  *
  34  * @library ../../../testlibrary
  35  * @modules java.rmi/sun.rmi.registry
  36  *          java.rmi/sun.rmi.server
  37  *          java.rmi/sun.rmi.transport
  38  *          java.rmi/sun.rmi.transport.tcp
  39  * @build TestLibrary RMID ActivationLibrary
  40  *     DownloadActivationGroup MyActivationGroupImpl DownloadActivationGroup_Stub
  41  * @run main/othervm/policy=security.policy/timeout=240 DownloadActivationGroup
  42  */
  43 
  44 import java.net.URL;
  45 import java.rmi.MarshalledObject;
  46 import java.rmi.Remote;
  47 import java.rmi.RemoteException;
  48 import java.rmi.activation.Activatable;
  49 import java.rmi.activation.ActivationDesc;
  50 import java.rmi.activation.ActivationException;
  51 import java.rmi.activation.ActivationGroup;
  52 import java.rmi.activation.ActivationGroupDesc;
  53 import java.rmi.activation.ActivationGroupDesc.CommandEnvironment;
  54 import java.rmi.activation.ActivationGroupID;
  55 import java.rmi.activation.ActivationID;
  56 import java.rmi.server.UnicastRemoteObject;
  57 import java.util.Properties;
  58 
  59 public class DownloadActivationGroup
  60         implements Ping, Runnable
  61 {
  62 
  63     private ActivationID id;
  64 
  65     public DownloadActivationGroup(ActivationID id, MarshalledObject mobj)
  66         throws ActivationException, RemoteException
  67     {
  68         this.id = id;
  69         Activatable.exportObject(this, id, 0);
  70         System.err.println("object activated in group");
  71     }
  72 
  73     public DownloadActivationGroup() throws RemoteException {
  74         UnicastRemoteObject.exportObject(this, 0);
  75     }
  76 
  77     /**
  78      * Used to activate object.
  79      */
  80     public void ping() {
  81         System.err.println("received ping");
  82     }
  83 
  84     /**
  85      * Spawns a thread to deactivate the object (and thus, shuts down the
  86      * activation group).
  87      */
  88     public void shutdown() throws Exception
  89     {
  90         (new Thread(this,"DownloadActivationGroup")).start();
  91     }
  92 
  93     /**
  94      * Thread to deactivate object.
  95      */
  96     public void run() {
  97         ActivationLibrary.deactivate(this, getID());
  98     }
  99 
 100     public ActivationID getID() {
 101         return id;
 102     }
 103 
 104 
 105     public static void main(String[] args) {
 106 
 107         RMID rmid = null;
 108 
 109         System.out.println("\nRegression test for bug 4510355\n");
 110 
 111         try {
 112             TestLibrary.suggestSecurityManager("java.lang.SecurityManager");
 113 
 114             /*
 115              * Install group class file in codebase.
 116              */
 117             System.err.println("install class file in codebase");
 118             URL groupURL = TestLibrary.installClassInCodebase(
 119                                   "MyActivationGroupImpl", "group");
 120             System.err.println("class file installed");
 121 
 122             /*
 123              * Start rmid.
 124              */
 125             RMID.removeLog();
 126             rmid = RMID.createRMID();
 127             String execPolicyOption = "-Dsun.rmi.activation.execPolicy=none";
 128             rmid.addOptions(new String[] { execPolicyOption });
 129             rmid.start();
 130 
 131             /*
 132              * Create and register descriptors for custom group and an
 133              * activatable object in that group.
 134              */
 135             System.err.println("register group");
 136 
 137             Properties p = new Properties();
 138             p.put("java.security.policy", TestParams.defaultGroupPolicy);
 139             CommandEnvironment cmd = new ActivationGroupDesc.CommandEnvironment(
 140                     null,
 141                     new String[] {
 142                         "--add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED",
 143                         "--add-exports=java.rmi/sun.rmi.server=ALL-UNNAMED",
 144                         "--add-exports=java.rmi/sun.rmi.transport=ALL-UNNAMED",
 145                         "--add-exports=java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED" });
 146 
 147             ActivationGroupDesc groupDesc =
 148                 new ActivationGroupDesc("MyActivationGroupImpl",
 149                                         groupURL.toExternalForm(),
 150                                         null, p, cmd);
 151             ActivationGroupID groupID =
 152                 ActivationGroup.getSystem().registerGroup(groupDesc);
 153 
 154 
 155             System.err.println("register activatable object");
 156             ActivationDesc desc =
 157                 new ActivationDesc(groupID, "DownloadActivationGroup",
 158                                    null, null);
 159             Ping obj = (Ping) Activatable.register(desc);
 160 
 161             /*
 162              * Start group (by calling ping).
 163              */
 164             System.err.println(
 165                 "ping object (forces download of group's class)");
 166             obj.ping();
 167             System.err.println(
 168                 "TEST PASSED: group's class downloaded successfully");
 169             System.err.println("shutdown object");
 170             obj.shutdown();
 171             System.err.println("TEST PASSED");
 172 
 173         } catch (Exception e) {
 174             TestLibrary.bomb(e);
 175         } finally {
 176             rmid.cleanup();
 177         }
 178     }
 179 }
 180 
 181 interface Ping extends Remote {
 182     public void ping() throws RemoteException;
 183     public void shutdown() throws Exception;
 184 }