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 8085192
  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 = RMID.launch();
 106 
 107             /* Cause activation groups to have a security policy that will
 108              * allow security managers to be downloaded and installed
 109              */
 110             Properties p = new Properties();
 111             // this test must always set policies/managers in its
 112             // activation groups
 113             p.put("java.security.policy",
 114                   TestParams.defaultGroupPolicy);
 115             p.put("java.security.manager",
 116                   TestParams.defaultSecurityManager);
 117 
 118             /*
 119              * Activate an object by registering its object
 120              * descriptor and invoking a method on the
 121              * stub returned from the register call.
 122              */
 123             ActivationGroupDesc groupDesc =
 124                 new ActivationGroupDesc(p, null);
 125             ActivationSystem system = ActivationGroup.getSystem();
 126             ActivationGroupID groupID = system.registerGroup(groupDesc);
 127 
 128             System.err.println("Creating descriptor");
 129             ActivationDesc desc =
 130                 new ActivationDesc(groupID,
 131                     "CreatePrivateActivatable$PrivateActivatable",
 132                      null, null);
 133 
 134             System.err.println("Registering descriptor");
 135             obj = (ActivateMe) Activatable.register(desc);
 136 
 137             /*
 138              * Loop a bunch of times to force activator to
 139              * spawn VMs (groups)
 140              */
 141             System.err.println("Activate object via method call");
 142             obj.ping();
 143 
 144             /*
 145              * Clean up object too.
 146              */
 147             System.err.println("Deactivate object via method call");
 148             obj.shutdown();
 149 
 150             System.err.println("\nsuccess: CreatePrivateActivatable test passed ");
 151 
 152         } catch (Exception e) {
 153             if (e instanceof java.security.PrivilegedActionException) {
 154                 e = ((java.security.PrivilegedActionException)e).getException();
 155             }
 156             TestLibrary.bomb("\nfailure: unexpected exception " +
 157                              e.getClass().getName(), e);
 158 
 159         } finally {
 160             rmid.cleanup();
 161             obj = null;
 162         }
 163     }
 164 }