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 4127754
  26  *
  27  * @summary synopsis: need to modify registered ActivationDesc and
  28  * ActivationGroupDesc
  29  * @author Ann Wollrath
  30  *
  31  * @library ../../../testlibrary
  32  * @modules java.rmi/sun.rmi.registry
  33  *          java.rmi/sun.rmi.server
  34  *          java.rmi/sun.rmi.transport
  35  *          java.rmi/sun.rmi.transport.tcp
  36  * @build TestLibrary RMID ActivationLibrary
  37  *     ActivateMe ModifyDescriptor_Stub
  38  * @run main/othervm/policy=security.policy/timeout=240 ModifyDescriptor
  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.*;
  47 
  48 public class ModifyDescriptor
  49         implements ActivateMe, Runnable
  50 {
  51 
  52     private ActivationID id;
  53     private String message;
  54 
  55     private static final String MESSAGE1 = "hello";
  56     private static final String MESSAGE2 = "hello, again";
  57 
  58 
  59     public ModifyDescriptor(ActivationID id, MarshalledObject mobj)
  60         throws ActivationException, RemoteException
  61     {
  62         this.id = id;
  63         Activatable.exportObject(this, id, 0);
  64 
  65         try {
  66             message = (String) mobj.get();
  67         } catch (Exception e) {
  68             System.err.println("unable to get message from marshalled object");
  69         }
  70     }
  71 
  72     public String getMessage() {
  73         return message;
  74     }
  75 
  76     public String getProperty(String name) {
  77         return TestLibrary.getProperty(name, null);
  78     }
  79 
  80     public ActivationID getID() {
  81         return id;
  82     }
  83 
  84     /**
  85      * Spawns a thread to deactivate the object.
  86      */
  87     public void shutdown() throws Exception
  88     {
  89         (new Thread(this,"ModifyDescriptor")).start();
  90     }
  91 
  92     /**
  93      * Thread to deactivate object. First attempts to make object
  94      * inactive (via the inactive method).  If that fails (the
  95      * object may still have pending/executing calls), then
  96      * unexport the object forcibly.
  97      */
  98     public void run() {
  99         ActivationLibrary.deactivate(this, getID());
 100     }
 101 
 102     public static void main(String[] args) {
 103 
 104         System.out.println("\nRegression test for bug 4127754\n");
 105 
 106         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
 107 
 108         RMID rmid = null;
 109 
 110         try {
 111             RMID.removeLog();
 112             rmid = RMID.createRMID();
 113             rmid.start();
 114 
 115             /*
 116              * Create and register a group and activatable object
 117              */
 118 
 119             System.err.println("Creating group descriptor");
 120             Properties props = new Properties();
 121             props.put("java.security.policy",
 122                   TestParams.defaultGroupPolicy);
 123             props.put("java.security.manager",
 124                   TestParams.defaultSecurityManager);
 125             props.put("test.message", MESSAGE1);
 126             ActivationGroupDesc initialGroupDesc =
 127                 new ActivationGroupDesc(props, null);
 128             System.err.println("Registering group");
 129             ActivationSystem system = ActivationGroup.getSystem();
 130             ActivationGroupID groupID = system.registerGroup(initialGroupDesc);
 131 
 132             System.err.println("Creating descriptor");
 133             ActivationDesc initialDesc =
 134                 new ActivationDesc(groupID, "ModifyDescriptor", null,
 135                                    new MarshalledObject(MESSAGE1), false);
 136 
 137             System.err.println("Registering descriptor");
 138             ActivateMe obj = (ActivateMe) Activatable.register(initialDesc);
 139 
 140             /*
 141              * Ping object and verify that MarshalledObject is okay.
 142              */
 143             System.err.println("Ping object");
 144             String message1 = obj.getMessage();
 145             System.err.println("message = " + message1);
 146 
 147             if (message1.equals(MESSAGE1)) {
 148                 System.err.println("Test1a passed: initial MarshalledObject " +
 149                                    "correct");
 150             } else {
 151                 TestLibrary.bomb("Test1 failed: unexpected MarshalledObject passed to " +
 152                      "constructor", null);
 153             }
 154 
 155             /*
 156              * Get property from remote group and make sure it's okay
 157              */
 158             message1 = obj.getProperty("test.message");
 159             if (message1.equals(MESSAGE1)) {
 160                 System.err.println("Test1b passed: initial group property " +
 161                                    "correct");
 162             } else {
 163                 TestLibrary.bomb("Test1 failed: unexpected property passed to " +
 164                      "group", null);
 165             }
 166 
 167             /*
 168              * Update activation descriptor for object and group
 169              */
 170             System.err.println("Update activation descriptor");
 171             ActivationDesc newDesc =
 172                 new ActivationDesc(groupID, "ModifyDescriptor", null,
 173                                new MarshalledObject(MESSAGE2), false);
 174             ActivationID id = obj.getID();
 175             ActivationDesc oldDesc = system.setActivationDesc(id, newDesc);
 176 
 177             if (oldDesc.equals(initialDesc)) {
 178                 System.err.println("Test2a passed: desc returned from " +
 179                                    "setActivationDesc is okay");
 180             } else {
 181                 TestLibrary.bomb("Test2a failed: desc returned from setActivationDesc " +
 182                      "is not the initial descriptor!", null);
 183             }
 184 
 185 
 186             Properties props2 = new Properties();
 187             props2.put("test.message", MESSAGE2);
 188             props2.put("java.security.policy",
 189                   TestParams.defaultGroupPolicy);
 190             props2.put("java.security.manager",
 191                   TestParams.defaultSecurityManager);
 192             ActivationGroupDesc newGroupDesc =
 193                 new ActivationGroupDesc(props2, null);
 194 
 195             ActivationGroupDesc oldGroupDesc =
 196                 system.setActivationGroupDesc(groupID, newGroupDesc);
 197 
 198             if (oldGroupDesc.equals(initialGroupDesc)) {
 199                 System.err.println("Test2b passed: group desc returned from " +
 200                                    "setActivationGroupDesc is okay");
 201             } else {
 202                 TestLibrary.bomb("Test2b failed: group desc returned from " +
 203                      "setActivationGroupDesc is not the initial descriptor!",
 204                      null);
 205             }
 206 
 207             /*
 208              * Restart rmid; and ping object to make sure that it has
 209              * new message.
 210              */
 211             rmid.restart();
 212 
 213             System.err.println("Ping object after restart");
 214             String message2 = obj.getMessage();
 215 
 216             if (message2.equals(MESSAGE2)) {
 217                 System.err.println("Test3a passed: setActivationDesc takes " +
 218                                    "effect after a restart");
 219             } else {
 220                 TestLibrary.bomb("Test3a failed: setActivationDesc did not take effect " +
 221                      "after a restart", null);
 222             }
 223 
 224             message2 = obj.getProperty("test.message");
 225 
 226             if (message2.equals(MESSAGE2)) {
 227                 System.err.println("Test3b passed: setActivationGroupDesc " +
 228                                    "takes effect after a restart");
 229             } else {
 230                 TestLibrary.bomb("Test3b failed: setActivationGroupDesc did not take " +
 231                      "effect after a restart", null);
 232             }
 233 
 234             System.err.println("Get activation descriptor");
 235             ActivationDesc latestDesc = system.getActivationDesc(id);
 236 
 237             if (latestDesc.equals(newDesc)) {
 238                 System.err.println("Test4a passed: desc is same as latest");
 239             } else {
 240                 TestLibrary.bomb("Test4a failed: there is no way this would happen", null);
 241             }
 242 
 243             System.err.println("Get activation group descriptor");
 244             ActivationGroupDesc latestGroupDesc =
 245                 system.getActivationGroupDesc(groupID);
 246 
 247             if (latestGroupDesc.equals(newGroupDesc)) {
 248                 System.err.println("Test4b passed: group desc is same as " +
 249                                    "latest");
 250             } else {
 251                 TestLibrary.bomb("Test4b failed: there is no way this would happen", null);
 252             }
 253 
 254         } catch (Exception e) {
 255             TestLibrary.bomb("test failed", e);
 256         } finally {
 257             rmid.cleanup();
 258         }
 259     }
 260 }