Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/CommandEnvironment/SetChildEnv.java
38828 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*25* @bug 410504326* @summary cannot set java.rmi.server.hostname on children of rmid in time27*28* @bug 409735729* @summary activation group should not overwrite system properties30*31* @bug 410718432* @summary activation groups should be able to control their JVM properties33*34* @author Adrian Colley35*36* @library ../../testlibrary37* @build TestLibrary RMID ActivationLibrary38* Eliza Retireable Doctor Doctor_Stub39* @run main/othervm/timeout=240/policy=security.policy40* -Djava.compiler=NONE SetChildEnv41*/42import java.rmi.*;43import java.util.Properties;44import java.io.*;45import java.util.StringTokenizer;46import java.util.Set;47import java.util.HashSet;48import java.util.Arrays;49import java.rmi.activation.*;5051public class SetChildEnv52{53public static void main(String argv[])54throws Exception55{56int runningPort = TestLibrary.getUnusedRandomPort();5758System.out.println("java.compiler=" + System.getProperty("java.compiler"));59// don't embed spaces in any of the test args/props, because60// they won't be parsed properly61runwith (new String[0], new String[0], runningPort);6263runwith (64new String[] { "-verbosegc" },65new String[] { "foo.bar=SetChildEnvTest",66"sun.rmi.server.doSomething=true" },67runningPort68);6970runwith (71new String[] { },72new String[] { "parameter.count=zero" },73runningPort74);7576runwith (77new String[] { "-Xmx32m" },78new String[] { },79runningPort80);81}8283private static void runwith(84String[] params, // extra args85String[] props, // extra system properties86int port // port on which to communicate87)88throws Exception89{90TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager);9192// make a "watcher" which listens on a pipe and searches for93// the debugExec line while teeing to System.err94DebugExecWatcher watcher = DebugExecWatcher.makeWithPipe();9596RMID.removeLog();97RMID rmid = RMID.createRMID(watcher.otherEnd(), watcher.otherEnd(),98true, // debugExec turned on99true, port);100101rmid.start();102103// compile props104Properties p = new Properties();105p.put("java.security.policy", TestParams.defaultGroupPolicy);106p.put("java.security.manager", TestParams.defaultSecurityManager);107//p.put("java.rmi.server.logCalls", "true");108int i;109for (i = 0; i < props.length; i++) {110p.put(props[i].substring(0, props[i].indexOf('=')),111props[i].substring(props[i].indexOf('=')+1));112}113114// create CommandEnvironment and ActivationGroupDesc115ActivationGroupDesc.CommandEnvironment cmdenv =116new ActivationGroupDesc.CommandEnvironment(117null,118params);119120ActivationGroupDesc gdesc = new ActivationGroupDesc(121p, cmdenv);122123// register group124ActivationSystem actsys = ActivationGroup.getSystem();125ActivationGroupID gid = actsys.registerGroup(gdesc);126127// create ActivationDesc128ActivationDesc odesc = new ActivationDesc(gid, // group129"Doctor", // class130null, // codesource131null); // closure data132133// register activatable object134Eliza doctor = (Eliza)Activatable.register(odesc);135136// invoke a call with oh-so-humorous sample text137System.out.println ("Invoking complain()...");138String complaint =139"HELP ME, DOCTOR. I FEEL VIOLENT TOWARDS PEOPLE " +140"WHO INQUIRE ABOUT MY PARENTS.";141142System.out.println(complaint);143//Runtime.getRuntime().traceMethodCalls(true);144String res = doctor.complain(complaint);145//Runtime.getRuntime().traceMethodCalls(false);146System.out.println (" => " + res);147148// Get debugExec line, allowing 15 seconds for it to flush149// through the buffers and pipes.150String found = watcher.found;151if (found == null) {152int fudge = 15;153while (found == null && --fudge > 0) {154Thread.sleep(1000);155found = watcher.found;156}157if (found == null) {158TestLibrary.bomb("rmid subprocess produced no " +159"recognizable debugExec line");160}161}162163System.err.println("debugExec found: <<" + found + ">>");164// q: first double-quote after debugExec165int q = found.indexOf('"', found.indexOf("rmid: debugExec"));166// qe: last double-quote on debugExec line167int qe = found.lastIndexOf('"');168if (q <= 1 || qe <= q) {169TestLibrary.bomb("rmid subprocess produced " +170"mangled debugExec line");171}172173// split args by whitespace174StringTokenizer tk = new StringTokenizer(found.substring(q+1, qe));175tk.nextToken(); // skip command path/name176177// Now check off the requested args. Order isn't important, and178// any extra args are ignored, even if they're inconsistent or179// bargage, or duplicates.180181Set argset = new HashSet(tk.countTokens());182while (tk.hasMoreTokens()) {183argset.add(tk.nextToken());184}185186int m;187for (m = 0; m < params.length; m++) {188if(!argset.contains(params[m]))189TestLibrary.bomb("Parameter \"" + params[m] + "\" not set");190}191192for (m = 0; m < props.length; m++) {193if (!argset.contains("-D" + props[m])) {194TestLibrary.bomb("Property binding \"" + props[m] +195"\" not set");196}197}198199// End doctor200if (doctor instanceof Retireable)201((Retireable)doctor).retire();202actsys.unregisterGroup(gid);203204Thread.sleep(5000);205ActivationLibrary.rmidCleanup(rmid);206}207208public static class DebugExecWatcher209extends Thread210{211public String found;212private BufferedReader str;213private OutputStream otherEnd;214215private DebugExecWatcher(InputStream readStream, OutputStream wrStream)216{217super("DebugExecWatcher");218found = null;219str = new BufferedReader(new InputStreamReader(readStream));220otherEnd = wrStream;221}222223static public DebugExecWatcher makeWithPipe()224throws IOException225{226PipedOutputStream wr = new PipedOutputStream();227PipedInputStream rd = new PipedInputStream(wr);228DebugExecWatcher embryo = new DebugExecWatcher(rd, wr);229embryo.start();230return embryo;231}232233public OutputStream otherEnd()234{235return otherEnd;236}237238public synchronized void notifyLine(String s)239{240if (s != null && s.indexOf("rmid: debugExec") != -1)241found = s;242}243244public void run()245{246try {247String line;248while ((line = str.readLine()) != null) {249this.notifyLine(line);250System.err.println(line);251}252} catch (IOException e) {253/* During termination of distant rmid, StreamPipes will be broken when254* distant vm terminates. A "Pipe broken" exception is expected because255* DebugExecWatcher points to the same streams as StreamPipes used by RMID.256* If we get this exception. We just terminate the thread.257*/258if (e.getMessage().equals("Pipe broken")) {259try {260str.close();261} catch (IOException ioe) {}262}263else {264e.printStackTrace();265}266}267}268}269}270271/*272code graveyard273274// activation should have proceeded by writing a wrapper.out275// when test.src/actgrpwrapper was run.276277// Read and check wrapper.out278BufferedReader r = new BufferedReader(new FileReader(wrapout));279String[] realArgs = null;280String line;281282while ( (line = r.readLine()) != null) {283StringTokenizer tkz = new StringTokenizer(line);284if (!tkz.nextToken().equals("actgrpwrapper")) {285// could throw an exception, but let's benignly286// assume that something unrelated is spewing.287continue;288}289String x; // writer's block290x = tkz.nextToken();291if (x.equals("argc")) {292if (realArgs != null) {293throw new RuntimeException(294"SetChildEnv: two argc lines in wrapper.out");295}296realArgs = new String[Integer.parseInt(tkz.nextToken())];297} else if (x.equals("argv")) {298if (realArgs == null)299throw new RuntimeException("SetChildEnv: missing argc");300int n = Integer.parseInt(tkz.nextToken());301if (n < 1 || n > realArgs.length) {302throw new RuntimeException("SetChildEnv: argc=" +303realArgs.length + "; argv[" + n + "]");304}305// Hack: manually skip the "actgrpwrapper argv 5 "306String remainder = line.substring(3071 + line.indexOf(' ',3081 + line.indexOf(' ',3091 + line.indexOf(' '))));310realArgs[n-1] = remainder;311} else {312throw new RuntimeException("SetChildEnv: bad token \"" + x + "\"");313}314}315r.close();316317private static void ensureLocalExecutable(String fname)318throws Exception319{320File target = new File(fname);321File source = new File(Dot, fname);322if (!target.exists()) {323// copy from source324System.err.println("Copying " + source.getPath() +325" to " + target.getPath());326java.io.InputStream in = new java.io.FileInputStream(source);327java.io.OutputStream out = new java.io.FileOutputStream(target);328byte[] buf = new byte[512];329int n;330while ((n = in.read(buf, 0, 512)) > 0) {331out.write(buf, 0, n);332}333out.close();334in.close();335}336// chmod337System.err.println("Doing: /bin/chmod 755 " + fname);338Runtime.getRuntime().exec("/bin/chmod 755 " + fname).waitFor();339}340341*/342343344