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 4097135
26 * @summary Need a specific subtype of RemoteException for activation failure.
27 *          If activation fails to happen during a call to a remote object,
28 *          then the call should end in an ActivateFailedException. In this
29 *          test, the actual "activatable" remote object fails to activate
30 *          since its * "activation" constructor throws an exception.
31 * @author Ann Wollrath
32 *
33 * @library ../../../testlibrary
34 * @modules java.rmi/sun.rmi.registry
35 *          java.rmi/sun.rmi.server
36 *          java.rmi/sun.rmi.transport
37 *          java.rmi/sun.rmi.transport.tcp
38 *          java.base/sun.nio.ch
39 * @build TestLibrary RMID RMIDSelectorProvider ActivationLibrary
40 *     ActivateMe ActivateFails_Stub ShutdownThread
41 * @run main/othervm/java.security.policy=security.policy/timeout=240 ActivateFails
42 */
43
44import java.rmi.*;
45import java.rmi.server.*;
46import java.rmi.activation.*;
47import java.io.*;
48import java.util.Properties;
49
50public class ActivateFails
51        extends Activatable
52        implements ActivateMe
53{
54
55    public ActivateFails(ActivationID id, MarshalledObject obj)
56        throws ActivationException, RemoteException
57    {
58        super(id, 0);
59
60        boolean refuseToActivate = false;
61        try {
62            refuseToActivate = ((Boolean)obj.get()).booleanValue();
63        } catch (Exception impossible) {
64        }
65
66        if (refuseToActivate)
67            throw new RemoteException("object refuses to activate");
68    }
69
70    public void ping()
71    {}
72
73    /**
74     * Spawns a thread to deactivate the object.
75     */
76    public ShutdownThread shutdown() throws Exception
77    {
78        ShutdownThread shutdownThread = new ShutdownThread(this, getID());
79        shutdownThread.start();
80        return(shutdownThread);
81    }
82
83    public static void main(String[] args)
84    {
85        RMID rmid = null;
86        ActivateMe obj1, obj2;
87        ShutdownThread shutdownThread;
88
89        System.err.println("\nRegression test for bug 4097135\n");
90        try {
91            TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
92
93            /*
94             * First run "rmid" and wait for it to start up.
95             */
96            RMID.removeLog();
97            rmid = RMID.createRMIDOnEphemeralPort();
98            rmid.start();
99
100            /* Cause activation groups to have a security policy that will
101             * allow security managers to be downloaded and installed
102             */
103            Properties p = new Properties();
104            // this test must always set policies/managers in its
105            // activation groups
106            p.put("java.security.policy",
107                  TestParams.defaultGroupPolicy);
108            p.put("java.security.manager",
109                  TestParams.defaultSecurityManager);
110
111            /*
112             * Create activation descriptor...
113             */
114            System.err.println("creating activation descriptor...");
115            ActivationGroupDesc groupDesc =
116                new ActivationGroupDesc(p, null);
117            ActivationGroupID groupID =
118                ActivationGroup.getSystem().registerGroup(groupDesc);
119
120            ActivationDesc desc1 =
121                new ActivationDesc(groupID, "ActivateFails",
122                                   null,
123                                   new MarshalledObject(new Boolean(true)));
124
125            ActivationDesc desc2 =
126                new ActivationDesc(groupID, "ActivateFails",
127                                   null,
128                                   new MarshalledObject(new Boolean(false)));
129            /*
130             * Register activation descriptor and make a call on
131             * the stub. Activation should fail with an
132             * ActivateFailedException.  If not, report an
133             * error as a RuntimeException
134             */
135
136            System.err.println("registering activation descriptor...");
137            obj1 = (ActivateMe)Activatable.register(desc1);
138            obj2 = (ActivateMe)Activatable.register(desc2);
139
140            System.err.println("invoking method on activatable object...");
141            try {
142                obj1.ping();
143                throw new RuntimeException("ActivateFailedException is expected");
144            } catch (ActivateFailedException e) {
145
146                /*
147                 * This is what is expected so exit with status 0
148                 */
149                System.err.println("\nsuccess: ActivateFailedException " +
150                                   "generated");
151                e.getMessage();
152            }
153
154            obj2.ping();
155            shutdownThread = obj2.shutdown();
156
157            // wait for shutdown to work
158            Thread.sleep(2000);
159
160            shutdownThread = null;
161
162        } catch (Exception e) {
163            /*
164             * Test failed; unexpected exception generated.
165             */
166            TestLibrary.bomb("\nfailure: unexpected exception " +
167                               e.getClass().getName() + ": " + e.getMessage(), e);
168
169        } finally {
170            obj1 = obj2 = null;
171            rmid.cleanup();
172        }
173    }
174}
175