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 4109103 26 * @summary rmid should annotate child process output 27 * 28 * @author Laird Dornin; code borrowed from 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 MyRMI CheckAnnotations_Stub 36 * @run main/othervm/policy=security.policy/timeout=480 CheckAnnotations 37 */ 38 39 import java.io.*; 40 import java.rmi.*; 41 import java.rmi.server.*; 42 import java.rmi.activation.*; 43 import java.security.CodeSource; 44 import java.util.Properties; 45 import java.util.StringTokenizer; 46 47 48 public class CheckAnnotations 49 extends Activatable implements MyRMI, Runnable 50 { 51 52 private static Object dummy = new Object(); 53 private static MyRMI myRMI = null; 54 55 // buffers to store rmid output. 56 private static ByteArrayOutputStream rmidOut = new ByteArrayOutputStream(); 57 private static ByteArrayOutputStream rmidErr = new ByteArrayOutputStream(); 58 59 public static void main(String args[]) { 60 /* 61 * The following line is required with the JDK 1.2 VM so that the 62 * VM can exit gracefully when this test completes. Otherwise, the 63 * conservative garbage collector will find a handle to the server 64 * object on the native stack and not clear the weak reference to 65 * it in the RMI runtime's object table. 66 */ 67 Object dummy1 = new Object(); 68 RMID rmid = null; 69 70 System.err.println("\nRegression test for bug/rfe 4109103\n"); 71 72 try { 73 74 // Set security manager according to the 75 // testlibrary. 76 TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager); 77 78 // start an rmid. 79 RMID.removeLog(); 80 rmid = RMID.createRMID(rmidOut, rmidErr, false); 81 rmid.start(); 82 83 /* Cause activation groups to have a security policy that will 84 * allow security managers to be downloaded and installed 85 */ 86 Properties p = new Properties(); 87 // this test must always set policies/managers in its 88 // activation groups 89 p.put("java.security.policy", 90 TestParams.defaultGroupPolicy); 91 p.put("java.security.manager", 92 TestParams.defaultSecurityManager); 93 94 /* new desc - we will reuse in order to get multiple vms.*/ 95 System.err.println("Create activation group in this VM"); 96 ActivationGroupDesc groupDesc = 97 new ActivationGroupDesc(p, null); 98 ActivationSystem system = ActivationGroup.getSystem(); 99 ActivationGroupID groupID = system.registerGroup(groupDesc); 100 ActivationGroup.createGroup(groupID, groupDesc, 0); 101 102 ActivationDesc desc = new ActivationDesc 103 ("CheckAnnotations", null, null); 104 myRMI = (MyRMI) Activatable.register(desc); 105 106 /* The test- 107 * Loop a bunch of times to force activator to 108 * spawn VMs (groups) 109 */ 110 for (int i = 0; i < 3; i++) { 111 112 // object activated in annotation check via method call 113 if(!checkAnnotations(i-1)) { 114 TestLibrary.bomb("Test failed: output improperly annotated."); 115 } 116 117 /* 118 * Clean up object too. 119 */ 120 System.err.println 121 ("Deactivate object via method call"); 122 myRMI.shutdown(); 123 } 124 System.err.println 125 ("\nsuccess: CheckAnnotations test passed "); 126 127 } catch (Exception e) { 128 TestLibrary.bomb("\nfailure: unexpected exception ", e); 129 } finally { 130 try { 131 Thread.sleep(4000); 132 } catch (InterruptedException e) { 133 } 134 135 myRMI = null; 136 System.err.println("rmid shut down"); 137 rmid.cleanup(); 138 } 139 } 140 141 /** 142 * check to make sure that the output from a spawned vm is 143 * formatted/annotated properly. 144 */ 145 public static boolean checkAnnotations(int iteration) 146 throws IOException 147 { 148 try { 149 Thread.sleep(5000); 150 } catch(Exception e) { 151 System.err.println(e.getMessage()); 152 } 153 154 /** 155 * cause the spawned vm to generate output that will 156 * be checked for proper annotation. printOut is 157 * actually being called on an activated implementation. 158 */ 159 myRMI.printOut("out" + iteration); 160 myRMI.printErr("err" + iteration); 161 myRMI.printOut("out" + iteration); 162 myRMI.printErr("err" + iteration); 163 164 /* we have to wait for output to filter down 165 * from children so we can read it before we 166 * kill rmid. 167 */ 168 169 String outString = null; 170 String errString = null; 171 172 for (int i = 0 ; i < 5 ; i ++ ) { 173 // have to give output from rmid time to trickle down to 174 // this process 175 try { 176 Thread.sleep(4000); 177 } catch(InterruptedException e) { 178 } 179 180 outString = rmidOut.toString(); 181 errString = rmidErr.toString(); 182 183 if ((!outString.equals("")) && 184 (!errString.equals(""))) 185 { 186 System.err.println("obtained annotations"); 187 break; 188 } 189 System.err.println("rmid output not yet received, retrying..."); 190 } 191 192 rmidOut.reset(); 193 rmidErr.reset(); 194 195 // only test when we are annotating..., first run does not annotate 196 if (iteration >= 0) { 197 System.err.println("Checking annotations..."); 198 System.err.println(outString); 199 System.err.println(errString); 200 201 StringTokenizer stOut = new StringTokenizer(outString, ":"); 202 StringTokenizer stErr = new StringTokenizer(errString, ":"); 203 204 String execErr = null; 205 String execOut = null; 206 String destOut = null; 207 String destErr = null; 208 String outTmp = null; 209 String errTmp = null; 210 211 while (stOut.hasMoreTokens()) { 212 execOut = outTmp; 213 outTmp = destOut; 214 destOut = stOut.nextToken(); 215 } 216 while (stErr.hasMoreTokens()) { 217 execErr = errTmp; 218 errTmp = destErr; 219 destErr = stErr.nextToken(); 220 } 221 222 if ((execErr == null)||(errTmp == null)|| 223 (destErr == null)) { 224 return false; 225 } 226 if ((execOut == null)||(outTmp == null)|| 227 (destOut == null)) { 228 return false; 229 } 230 231 // just make sure that last two strings are what we expect. 232 if (execOut.equals("ExecGroup-" + iteration) 233 && (new String(destOut.substring(0,4)).equals("out" + 234 iteration)) 235 && (execErr.equals("ExecGroup-"+iteration)) 236 && (new String(destErr.substring(0,4)).equals("err" + 237 iteration)) ) { 238 return true; 239 } else { 240 return false; 241 } 242 } 243 return true; 244 } 245 246 // implementation of MyRMI, make this object activatable. 247 public CheckAnnotations 248 (ActivationID id, MarshalledObject mo) 249 throws RemoteException { 250 251 // register/export anonymously 252 super(id,0); 253 } 254 255 public void printOut(String toPrint) { 256 System.out.println(toPrint); 257 } 258 259 public void printErr(String toPrint) { 260 System.err.println(toPrint); 261 } 262 263 /** 264 * Spawns a thread to deactivate the object. 265 */ 266 public void shutdown() throws Exception { 267 (new Thread(this,"CheckAnnotations")).start(); 268 } 269 270 /** 271 * Thread to deactivate object. First attempts to make object 272 * inactive (via the inactive method). If that fails (the 273 * object may still have pending/executing calls), then 274 * unexport the object forcibly. 275 */ 276 public void run() { 277 ActivationLibrary.deactivate(this, getID()); 278 } 279 }