1/*
2 * Copyright (c) 1998, 2016, 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 4134233
26 * @bug 4213186
27 * @summary synopsis: ActivationSystem.unregisterGroup should unregister objects in group
28 * @author Ann Wollrath
29 *
30 * @library ../../../testlibrary
31 * @modules java.rmi/sun.rmi.registry
32 *          java.rmi/sun.rmi.server
33 *          java.rmi/sun.rmi.transport
34 *          java.rmi/sun.rmi.transport.tcp
35 *          java.base/sun.nio.ch
36 * @build TestLibrary RMID ActivationLibrary ActivateMe RMIDSelectorProvider
37 * @run main/othervm/policy=security.policy UnregisterGroup
38 */
39
40import java.io.*;
41import java.rmi.*;
42import java.rmi.activation.*;
43import java.rmi.server.*;
44import java.util.Properties;
45
46public class UnregisterGroup extends Activatable implements ActivateMe
47{
48    private static volatile Exception exception = null;
49    private static volatile String error = null;
50    private static volatile boolean done = false;
51    private static final int NUM_OBJECTS = 10;
52
53    public UnregisterGroup(ActivationID id, MarshalledObject mobj)
54        throws Exception
55    {
56        super(id, 0);
57    }
58
59    /**
60     * Does nothing, but serves to activate this object.
61     */
62    public void ping() { }
63
64    /**
65     * Deactivates the object. We need to unexport forcibly because
66     * this call is in-progress on this object, which is the same object
67     * that we are trying to deactivate.
68     */
69    public void shutdown() throws Exception {
70        Activatable.unexportObject(this, true);
71        ActivationLibrary.deactivate(this, getID());
72    }
73
74    public static void main(String[] args) throws RemoteException {
75        TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
76        RMID rmid = null;
77
78        try {
79            RMID.removeLog();
80            rmid = RMID.createRMIDOnEphemeralPort();
81            rmid.start();
82
83            /* Cause activation groups to have a security policy that will
84             * allow security managers to be downloaded and installed
85             */
86            final Properties p = new Properties();
87            // this test must always set policies/managers in its
88            // activation groups
89            p.put("java.security.policy", TestParams.defaultGroupPolicy);
90            p.put("java.security.manager", TestParams.defaultSecurityManager);
91
92            Thread t = new Thread() {
93                public void run () {
94                    try {
95                        System.err.println("Creating group descriptor");
96                        ActivationGroupDesc groupDesc =
97                            new ActivationGroupDesc(p, null);
98                        ActivationSystem system = ActivationGroup.getSystem();
99                        ActivationGroupID groupID =
100                            system.registerGroup(groupDesc);
101
102                        ActivateMe[] obj = new ActivateMe[NUM_OBJECTS];
103
104                        for (int i = 0; i < NUM_OBJECTS; i++) {
105                            System.err.println("Creating descriptor: " + i);
106                            ActivationDesc desc =
107                                new ActivationDesc(groupID, "UnregisterGroup",
108                                                   null, null);
109                            System.err.println("Registering descriptor: " + i);
110                            obj[i] = (ActivateMe) Activatable.register(desc);
111                            System.err.println("Activating object: " + i);
112                            obj[i].ping();
113                        }
114
115                        System.err.println("Unregistering group");
116                        system.unregisterGroup(groupID);
117
118                        try {
119                            System.err.println("Get the group descriptor");
120                            system.getActivationGroupDesc(groupID);
121                            error = "test failed: group still registered";
122                        } catch (UnknownGroupException e) {
123                            System.err.println("Test passed: " +
124                                               "group unregistered");
125                        }
126
127                        /*
128                         * Deactivate objects so group VM will exit.
129                         */
130                        for (int i = 0; i < NUM_OBJECTS; i++) {
131                            System.err.println("Deactivating object: " + i);
132                            obj[i].shutdown();
133                            obj[i] = null;
134                        }
135                        System.err.println("Successfully deactivated all objects.");
136
137                    } catch (Exception e) {
138                        exception = e;
139                    }
140
141                    done = true;
142                }
143            };
144
145            t.start();
146
147            // Default jtreg timeout is two minutes.
148            // Timeout ourselves after one minute so that
149            // we can clean up.
150            t.join(60000);
151
152            if (exception != null) {
153                TestLibrary.bomb("test failed", exception);
154            } else if (error != null) {
155                TestLibrary.bomb(error, null);
156            } else if (!done) {
157                TestLibrary.bomb("test failed: not completed before timeout", null);
158            } else {
159                System.err.println("Test passed");
160            }
161        } catch (Exception e) {
162            TestLibrary.bomb("test failed", e);
163        } finally {
164            rmid.cleanup();
165        }
166    }
167}
168