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