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 4115296
  26  * @summary synopsis: NoSuchObjectException not thrown for non-existent
  27  * activatable objects
  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
  37  *     ActivateMe NonExistentActivatable_Stub
  38  * @run main/othervm/policy=security.policy/timeout=240 NonExistentActivatable
  39  */
  40 
  41 import java.io.*;
  42 import java.rmi.*;
  43 import java.rmi.activation.*;
  44 import java.rmi.server.*;
  45 import java.rmi.registry.*;
  46 import java.util.Properties;
  47 
  48 public class NonExistentActivatable
  49         extends Activatable
  50         implements ActivateMe, Runnable
  51 {
  52 
  53     public NonExistentActivatable(ActivationID id, MarshalledObject obj)
  54         throws ActivationException, RemoteException
  55     {
  56         super(id, 0);
  57     }
  58 
  59     public void ping()
  60     {}
  61 
  62     public void unregister() throws Exception {
  63         super.unregister(super.getID());
  64     }
  65 
  66     /**
  67      * Spawns a thread to deactivate the object.
  68      */
  69     public void shutdown() throws Exception
  70     {
  71         (new Thread(this,"NonExistentActivatable")).start();
  72     }
  73 
  74     /**
  75      * Thread to deactivate object. First attempts to make object
  76      * inactive (via the inactive method).  If that fails (the
  77      * object may still have pending/executing calls), then
  78      * unexport the object forcibly.
  79      */
  80     public void run()
  81     {
  82         ActivationLibrary.deactivate(this, getID());
  83     }
  84 
  85     public static void main(String[] args) {
  86 
  87         System.out.println("\nRegression test for bug 4115331\n");
  88 
  89         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  90 
  91         RMID rmid = null;
  92 
  93         try {
  94             RMID.removeLog();
  95             rmid = RMID.createRMID();
  96             rmid.start();
  97 
  98             /* Cause activation groups to have a security policy that will
  99              * allow security managers to be downloaded and installed
 100              */
 101             Properties p = new Properties();
 102             // this test must always set policies/managers in its
 103             // activation groups
 104             p.put("java.security.policy",
 105                   TestParams.defaultGroupPolicy);
 106             p.put("java.security.manager",
 107                   TestParams.defaultSecurityManager);
 108 
 109             System.err.println("Create activation group in this VM");
 110             ActivationGroupDesc groupDesc =
 111                 new ActivationGroupDesc(p, null);
 112             ActivationSystem system = ActivationGroup.getSystem();
 113             ActivationGroupID groupID = system.registerGroup(groupDesc);
 114             ActivationGroup.createGroup(groupID, groupDesc, 0);
 115 
 116             System.err.println("Creating descriptor");
 117             ActivationDesc desc =
 118                 new ActivationDesc("NonExistentActivatable", null, null);
 119 
 120             System.err.println("Registering descriptor");
 121             ActivateMe obj = (ActivateMe) Activatable.register(desc);
 122 
 123             System.err.println("Activate object via method call");
 124             obj.ping();
 125 
 126             System.err.println("Unregister object");
 127             obj.unregister();
 128 
 129             System.err.println("Make object inactive");
 130             obj.shutdown();
 131 
 132             System.err.println("Reactivate object");
 133             try {
 134                 obj.ping();
 135             } catch (NoSuchObjectException e) {
 136                 System.err.println("Test succeeded: " +
 137                                    "NoSuchObjectException caught");
 138                 return;
 139             } catch (Exception e) {
 140                 TestLibrary.bomb("Test failed: exception other than NoSuchObjectException",
 141                      e);
 142             }
 143 
 144         } catch (Exception e) {
 145             TestLibrary.bomb("test failed", e);
 146         } finally {
 147             rmid.cleanup();
 148         }
 149     }
 150 }