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