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