1 /*
   2  * Copyright (c) 1999, 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 4164971
  26  * @summary allow non-public activatable class and/or constructor
  27  * @author Laird Dornin
  28  *
  29  * @library ../../../testlibrary
  30  * @modules java.rmi/sun.rmi.registry
  31  *          java.rmi/sun.rmi.server
  32  *          java.rmi/sun.rmi.transport
  33  *          java.rmi/sun.rmi.transport.tcp
  34  * @build TestLibrary RMID ActivateMe
  35  * @run main/othervm/policy=security.policy/timeout=240 CreatePrivateActivatable
  36  */
  37 
  38 import java.io.*;
  39 import java.rmi.*;
  40 import java.rmi.server.*;
  41 import java.rmi.activation.*;
  42 import sun.rmi.server.ActivatableRef;
  43 import java.lang.reflect.*;
  44 import java.util.Properties;
  45 
  46 /**
  47  * Test creates a private inner class Activatable object with a
  48  * private constructor and makes sure that the object can be
  49  * activated.
  50  */
  51 public class CreatePrivateActivatable
  52 {
  53     private static class PrivateActivatable extends Activatable
  54         implements ActivateMe, Runnable
  55     {
  56         private PrivateActivatable(ActivationID id, MarshalledObject obj)
  57             throws ActivationException, RemoteException
  58         {
  59             super(id, 0);
  60         }
  61 
  62         public void ping()
  63         {}
  64 
  65         /**
  66          * Spawns a thread to deactivate the object.
  67          */
  68         public void shutdown() throws Exception
  69         {
  70             (new Thread(this, "CreatePrivateActivatable$PrivateActivatable")).start();
  71         }
  72 
  73         /**
  74          * Thread to deactivate object. First attempts to make object
  75          * inactive (via the inactive method).  If that fails (the
  76          * object may still have pending/executing calls), then
  77          * unexport the object forcibly.
  78          */
  79         public void run() {
  80             ActivationLibrary.deactivate(this, getID());
  81         }
  82     }
  83 
  84     public static void main(String[] args)  {
  85         /*
  86          * The following line is required with the JDK 1.2 VM so that the
  87          * VM can exit gracefully when this test completes.  Otherwise, the
  88          * conservative garbage collector will find a handle to the server
  89          * object on the native stack and not clear the weak reference to
  90          * it in the RMI runtime's object table.
  91          */
  92         Object dummy = new Object();
  93         RMID rmid = null;
  94         ActivateMe obj;
  95 
  96         System.err.println("\nRegression test for bug 4164971\n");
  97         System.err.println("java.security.policy = " +
  98                            System.getProperty("java.security.policy", "no policy"));
  99 
 100         CreatePrivateActivatable server;
 101         try {
 102             TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager);
 103 
 104             // start an rmid.
 105             RMID.removeLog();
 106             rmid = RMID.createRMID();
 107             rmid.start();
 108 
 109             /* Cause activation groups to have a security policy that will
 110              * allow security managers to be downloaded and installed
 111              */
 112             Properties p = new Properties();
 113             // this test must always set policies/managers in its
 114             // activation groups
 115             p.put("java.security.policy",
 116                   TestParams.defaultGroupPolicy);
 117             p.put("java.security.manager",
 118                   TestParams.defaultSecurityManager);
 119 
 120             /*
 121              * Activate an object by registering its object
 122              * descriptor and invoking a method on the
 123              * stub returned from the register call.
 124              */
 125             ActivationGroupDesc groupDesc =
 126                 new ActivationGroupDesc(p, null);
 127             ActivationSystem system = ActivationGroup.getSystem();
 128             ActivationGroupID groupID = system.registerGroup(groupDesc);
 129 
 130             System.err.println("Creating descriptor");
 131             ActivationDesc desc =
 132                 new ActivationDesc(groupID,
 133                     "CreatePrivateActivatable$PrivateActivatable",
 134                      null, null);
 135 
 136             System.err.println("Registering descriptor");
 137             obj = (ActivateMe) Activatable.register(desc);
 138 
 139             /*
 140              * Loop a bunch of times to force activator to
 141              * spawn VMs (groups)
 142              */
 143             System.err.println("Activate object via method call");
 144             obj.ping();
 145 
 146             /*
 147              * Clean up object too.
 148              */
 149             System.err.println("Deactivate object via method call");
 150             obj.shutdown();
 151 
 152             System.err.println("\nsuccess: CreatePrivateActivatable test passed ");
 153 
 154         } catch (Exception e) {
 155             if (e instanceof java.security.PrivilegedActionException) {
 156                 e = ((java.security.PrivilegedActionException)e).getException();
 157             }
 158             TestLibrary.bomb("\nfailure: unexpected exception " +
 159                              e.getClass().getName(), e);
 160 
 161         } finally {
 162             rmid.cleanup();
 163             obj = null;
 164         }
 165     }
 166 }