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