J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @author Laird Dornin |
| 26 | * @bug 4164971 |
| 27 | * @summary allow non-public activatable class and/or constructor |
| 28 | * |
| 29 | * @library ../../../testlibrary |
| 30 | * @build TestLibrary RMID |
| 31 | * @build ActivateMe |
| 32 | * @build CreatePrivateActivatable |
| 33 | * @run main/othervm/policy=security.policy/timeout=240 CreatePrivateActivatable |
| 34 | */ |
| 35 | |
| 36 | import java.io.*; |
| 37 | import java.rmi.*; |
| 38 | import java.rmi.server.*; |
| 39 | import java.rmi.activation.*; |
| 40 | import sun.rmi.server.ActivatableRef; |
| 41 | import java.lang.reflect.*; |
| 42 | import java.util.Properties; |
| 43 | |
| 44 | /** |
| 45 | * Test creates a private inner class Activatable object with a |
| 46 | * private constructor and makes sure that the object can be |
| 47 | * activated. |
| 48 | */ |
| 49 | public class CreatePrivateActivatable |
| 50 | { |
| 51 | private static class PrivateActivatable extends Activatable |
| 52 | implements ActivateMe, Runnable |
| 53 | { |
| 54 | private PrivateActivatable(ActivationID id, MarshalledObject obj) |
| 55 | throws ActivationException, RemoteException |
| 56 | { |
| 57 | super(id, 0); |
| 58 | } |
| 59 | |
| 60 | public void ping() |
| 61 | {} |
| 62 | |
| 63 | /** |
| 64 | * Spawns a thread to deactivate the object. |
| 65 | */ |
| 66 | public void shutdown() throws Exception |
| 67 | { |
| 68 | (new Thread(this, "CreatePrivateActivatable$PrivateActivatable")).start(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Thread to deactivate object. First attempts to make object |
| 73 | * inactive (via the inactive method). If that fails (the |
| 74 | * object may still have pending/executing calls), then |
| 75 | * unexport the object forcibly. |
| 76 | */ |
| 77 | public void run() { |
| 78 | ActivationLibrary.deactivate(this, getID()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public static void main(String[] args) { |
| 83 | /* |
| 84 | * The following line is required with the JDK 1.2 VM so that the |
| 85 | * VM can exit gracefully when this test completes. Otherwise, the |
| 86 | * conservative garbage collector will find a handle to the server |
| 87 | * object on the native stack and not clear the weak reference to |
| 88 | * it in the RMI runtime's object table. |
| 89 | */ |
| 90 | Object dummy = new Object(); |
| 91 | RMID rmid = null; |
| 92 | ActivateMe obj; |
| 93 | |
| 94 | System.err.println("\nRegression test for bug 4164971\n"); |
| 95 | System.err.println("java.security.policy = " + |
| 96 | System.getProperty("java.security.policy", "no policy")); |
| 97 | |
| 98 | CreatePrivateActivatable server; |
| 99 | try { |
| 100 | TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager); |
| 101 | |
| 102 | // start an rmid. |
| 103 | RMID.removeLog(); |
| 104 | rmid = RMID.createRMID(); |
| 105 | rmid.start(); |
| 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 | ActivationLibrary.rmidCleanup(rmid); |
| 161 | obj = null; |
| 162 | } |
| 163 | } |
| 164 | } |