Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/Activatable/checkAnnotations/CheckAnnotations.java
38829 views
/*1* Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 410910325* @summary rmid should annotate child process output26*27* @author Laird Dornin; code borrowed from Ann Wollrath28*29* @library ../../../testlibrary30* @build TestLibrary RMID MyRMI CheckAnnotations_Stub31* @run main/othervm/policy=security.policy/timeout=480 CheckAnnotations32*/3334import java.io.*;35import java.rmi.*;36import java.rmi.server.*;37import java.rmi.activation.*;38import java.security.CodeSource;39import java.util.Properties;40import java.util.StringTokenizer;414243public class CheckAnnotations44extends Activatable implements MyRMI, Runnable45{4647private static Object dummy = new Object();48private static MyRMI myRMI = null;4950// buffers to store rmid output.51private static ByteArrayOutputStream rmidOut = new ByteArrayOutputStream();52private static ByteArrayOutputStream rmidErr = new ByteArrayOutputStream();5354public static void main(String args[]) {55/*56* The following line is required with the JDK 1.2 VM so that the57* VM can exit gracefully when this test completes. Otherwise, the58* conservative garbage collector will find a handle to the server59* object on the native stack and not clear the weak reference to60* it in the RMI runtime's object table.61*/62Object dummy1 = new Object();63RMID rmid = null;6465System.err.println("\nRegression test for bug/rfe 4109103\n");6667try {6869// Set security manager according to the70// testlibrary.71TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager);7273// start an rmid.74RMID.removeLog();75rmid = RMID.createRMID(rmidOut, rmidErr, false);76rmid.start();7778/* Cause activation groups to have a security policy that will79* allow security managers to be downloaded and installed80*/81Properties p = new Properties();82// this test must always set policies/managers in its83// activation groups84p.put("java.security.policy",85TestParams.defaultGroupPolicy);86p.put("java.security.manager",87TestParams.defaultSecurityManager);8889/* new desc - we will reuse in order to get multiple vms.*/90System.err.println("Create activation group in this VM");91ActivationGroupDesc groupDesc =92new ActivationGroupDesc(p, null);93ActivationSystem system = ActivationGroup.getSystem();94ActivationGroupID groupID = system.registerGroup(groupDesc);95ActivationGroup.createGroup(groupID, groupDesc, 0);9697ActivationDesc desc = new ActivationDesc98("CheckAnnotations", null, null);99myRMI = (MyRMI) Activatable.register(desc);100101/* The test-102* Loop a bunch of times to force activator to103* spawn VMs (groups)104*/105for (int i = 0; i < 3; i++) {106107// object activated in annotation check via method call108if(!checkAnnotations(i-1)) {109TestLibrary.bomb("Test failed: output improperly annotated.");110}111112/*113* Clean up object too.114*/115System.err.println116("Deactivate object via method call");117myRMI.shutdown();118}119System.err.println120("\nsuccess: CheckAnnotations test passed ");121122} catch (Exception e) {123TestLibrary.bomb("\nfailure: unexpected exception ", e);124} finally {125try {126Thread.sleep(4000);127} catch (InterruptedException e) {128}129130myRMI = null;131System.err.println("rmid shut down");132ActivationLibrary.rmidCleanup(rmid);133}134}135136/**137* check to make sure that the output from a spawned vm is138* formatted/annotated properly.139*/140public static boolean checkAnnotations(int iteration)141throws IOException142{143try {144Thread.sleep(5000);145} catch(Exception e) {146System.err.println(e.getMessage());147}148149/**150* cause the spawned vm to generate output that will151* be checked for proper annotation. printOut is152* actually being called on an activated implementation.153*/154myRMI.printOut("out" + iteration);155myRMI.printErr("err" + iteration);156myRMI.printOut("out" + iteration);157myRMI.printErr("err" + iteration);158159/* we have to wait for output to filter down160* from children so we can read it before we161* kill rmid.162*/163164String outString = null;165String errString = null;166167for (int i = 0 ; i < 5 ; i ++ ) {168// have to give output from rmid time to trickle down to169// this process170try {171Thread.sleep(4000);172} catch(InterruptedException e) {173}174175outString = rmidOut.toString();176errString = rmidErr.toString();177178if ((!outString.equals("")) &&179(!errString.equals("")))180{181System.err.println("obtained annotations");182break;183}184System.err.println("rmid output not yet received, retrying...");185}186187rmidOut.reset();188rmidErr.reset();189190// only test when we are annotating..., first run does not annotate191if (iteration >= 0) {192System.err.println("Checking annotations...");193System.err.println(outString);194System.err.println(errString);195196StringTokenizer stOut = new StringTokenizer(outString, ":");197StringTokenizer stErr = new StringTokenizer(errString, ":");198199String execErr = null;200String execOut = null;201String destOut = null;202String destErr = null;203String outTmp = null;204String errTmp = null;205206while (stOut.hasMoreTokens()) {207execOut = outTmp;208outTmp = destOut;209destOut = stOut.nextToken();210}211while (stErr.hasMoreTokens()) {212execErr = errTmp;213errTmp = destErr;214destErr = stErr.nextToken();215}216217if ((execErr == null)||(errTmp == null)||218(destErr == null)) {219return false;220}221if ((execOut == null)||(outTmp == null)||222(destOut == null)) {223return false;224}225226// just make sure that last two strings are what we expect.227if (execOut.equals("ExecGroup-" + iteration)228&& (new String(destOut.substring(0,4)).equals("out" +229iteration))230&& (execErr.equals("ExecGroup-"+iteration))231&& (new String(destErr.substring(0,4)).equals("err" +232iteration)) ) {233return true;234} else {235return false;236}237}238return true;239}240241// implementation of MyRMI, make this object activatable.242public CheckAnnotations243(ActivationID id, MarshalledObject mo)244throws RemoteException {245246// register/export anonymously247super(id,0);248}249250public void printOut(String toPrint) {251System.out.println(toPrint);252}253254public void printErr(String toPrint) {255System.err.println(toPrint);256}257258/**259* Spawns a thread to deactivate the object.260*/261public void shutdown() throws Exception {262(new Thread(this,"CheckAnnotations")).start();263}264265/**266* Thread to deactivate object. First attempts to make object267* inactive (via the inactive method). If that fails (the268* object may still have pending/executing calls), then269* unexport the object forcibly.270*/271public void run() {272ActivationLibrary.deactivate(this, getID());273}274}275276277