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