0N/A/*
5266N/A * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A *
0N/A * @bug 4105043
0N/A * @summary cannot set java.rmi.server.hostname on children of rmid in time
0N/A *
0N/A * @bug 4097357
0N/A * @summary activation group should not overwrite system properties
0N/A *
0N/A * @bug 4107184
0N/A * @summary activation groups should be able to control their JVM properties
0N/A *
0N/A * @author Adrian Colley
0N/A *
0N/A * @library ../../testlibrary
5551N/A * @build TestLibrary RMID ActivationLibrary
5551N/A * Eliza Retireable Doctor Doctor_Stub
5551N/A * @run main/othervm/timeout=240/policy=security.policy
5551N/A * -Djava.compiler=NONE SetChildEnv
0N/A */
0N/Aimport java.rmi.*;
0N/Aimport java.util.Properties;
0N/Aimport java.io.*;
0N/Aimport java.util.StringTokenizer;
0N/Aimport java.util.Set;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.Arrays;
0N/Aimport java.rmi.activation.*;
0N/A
0N/Apublic class SetChildEnv
0N/A{
0N/A public static void main(String argv[])
0N/A throws Exception
0N/A {
5266N/A int runningPort = TestLibrary.getUnusedRandomPort();
5266N/A
0N/A System.out.println("java.compiler=" + System.getProperty("java.compiler"));
0N/A // don't embed spaces in any of the test args/props, because
0N/A // they won't be parsed properly
5266N/A runwith (new String[0], new String[0], runningPort);
0N/A
0N/A runwith (
0N/A new String[] { "-verbosegc" },
0N/A new String[] { "foo.bar=SetChildEnvTest",
5266N/A "sun.rmi.server.doSomething=true" },
5266N/A runningPort
0N/A );
0N/A
0N/A runwith (
0N/A new String[] { },
5266N/A new String[] { "parameter.count=zero" },
5266N/A runningPort
0N/A );
0N/A
0N/A runwith (
0N/A new String[] { "-Xmx32m" },
5266N/A new String[] { },
5266N/A runningPort
0N/A );
0N/A }
0N/A
0N/A private static void runwith(
0N/A String[] params, // extra args
5266N/A String[] props, // extra system properties
5266N/A int port // port on which to communicate
0N/A )
0N/A throws Exception
0N/A {
0N/A TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager);
0N/A
0N/A // make a "watcher" which listens on a pipe and searches for
0N/A // the debugExec line while teeing to System.err
0N/A DebugExecWatcher watcher = DebugExecWatcher.makeWithPipe();
0N/A
0N/A RMID.removeLog();
0N/A RMID rmid = RMID.createRMID(watcher.otherEnd(), watcher.otherEnd(),
5266N/A true, // debugExec turned on
5266N/A true, port);
0N/A
0N/A rmid.start();
0N/A
0N/A // compile props
0N/A Properties p = new Properties();
0N/A p.put("java.security.policy", TestParams.defaultGroupPolicy);
0N/A p.put("java.security.manager", TestParams.defaultSecurityManager);
0N/A //p.put("java.rmi.server.logCalls", "true");
0N/A int i;
0N/A for (i = 0; i < props.length; i++) {
0N/A p.put(props[i].substring(0, props[i].indexOf('=')),
0N/A props[i].substring(props[i].indexOf('=')+1));
0N/A }
0N/A
0N/A // create CommandEnvironment and ActivationGroupDesc
0N/A ActivationGroupDesc.CommandEnvironment cmdenv =
0N/A new ActivationGroupDesc.CommandEnvironment(
0N/A null,
0N/A params);
0N/A
0N/A ActivationGroupDesc gdesc = new ActivationGroupDesc(
0N/A p, cmdenv);
0N/A
0N/A // register group
0N/A ActivationSystem actsys = ActivationGroup.getSystem();
0N/A ActivationGroupID gid = actsys.registerGroup(gdesc);
0N/A
0N/A // create ActivationDesc
0N/A ActivationDesc odesc = new ActivationDesc(gid, // group
0N/A "Doctor", // class
0N/A null, // codesource
0N/A null); // closure data
0N/A
0N/A // register activatable object
0N/A Eliza doctor = (Eliza)Activatable.register(odesc);
0N/A
0N/A // invoke a call with oh-so-humorous sample text
0N/A System.out.println ("Invoking complain()...");
0N/A String complaint =
0N/A "HELP ME, DOCTOR. I FEEL VIOLENT TOWARDS PEOPLE " +
0N/A "WHO INQUIRE ABOUT MY PARENTS.";
0N/A
0N/A System.out.println(complaint);
0N/A //Runtime.getRuntime().traceMethodCalls(true);
0N/A String res = doctor.complain(complaint);
0N/A //Runtime.getRuntime().traceMethodCalls(false);
0N/A System.out.println (" => " + res);
0N/A
0N/A // Get debugExec line, allowing 15 seconds for it to flush
0N/A // through the buffers and pipes.
0N/A String found = watcher.found;
0N/A if (found == null) {
0N/A int fudge = 15;
0N/A while (found == null && --fudge > 0) {
0N/A Thread.sleep(1000);
0N/A found = watcher.found;
0N/A }
0N/A if (found == null) {
0N/A TestLibrary.bomb("rmid subprocess produced no " +
0N/A "recognizable debugExec line");
0N/A }
0N/A }
0N/A
0N/A System.err.println("debugExec found: <<" + found + ">>");
0N/A // q: first double-quote after debugExec
0N/A int q = found.indexOf('"', found.indexOf("rmid: debugExec"));
0N/A // qe: last double-quote on debugExec line
0N/A int qe = found.lastIndexOf('"');
0N/A if (q <= 1 || qe <= q) {
0N/A TestLibrary.bomb("rmid subprocess produced " +
0N/A "mangled debugExec line");
0N/A }
0N/A
0N/A // split args by whitespace
0N/A StringTokenizer tk = new StringTokenizer(found.substring(q+1, qe));
0N/A tk.nextToken(); // skip command path/name
0N/A
0N/A // Now check off the requested args. Order isn't important, and
0N/A // any extra args are ignored, even if they're inconsistent or
0N/A // bargage, or duplicates.
0N/A
0N/A Set argset = new HashSet(tk.countTokens());
0N/A while (tk.hasMoreTokens()) {
0N/A argset.add(tk.nextToken());
0N/A }
0N/A
0N/A int m;
0N/A for (m = 0; m < params.length; m++) {
0N/A if(!argset.contains(params[m]))
0N/A TestLibrary.bomb("Parameter \"" + params[m] + "\" not set");
0N/A }
0N/A
0N/A for (m = 0; m < props.length; m++) {
0N/A if (!argset.contains("-D" + props[m])) {
0N/A TestLibrary.bomb("Property binding \"" + props[m] +
0N/A "\" not set");
0N/A }
0N/A }
0N/A
0N/A // End doctor
0N/A if (doctor instanceof Retireable)
0N/A ((Retireable)doctor).retire();
0N/A actsys.unregisterGroup(gid);
0N/A
0N/A Thread.sleep(5000);
5266N/A ActivationLibrary.rmidCleanup(rmid);
0N/A }
0N/A
0N/A public static class DebugExecWatcher
0N/A extends Thread
0N/A {
0N/A public String found;
0N/A private BufferedReader str;
0N/A private OutputStream otherEnd;
0N/A
0N/A private DebugExecWatcher(InputStream readStream, OutputStream wrStream)
0N/A {
0N/A super("DebugExecWatcher");
0N/A found = null;
0N/A str = new BufferedReader(new InputStreamReader(readStream));
0N/A otherEnd = wrStream;
0N/A }
0N/A
0N/A static public DebugExecWatcher makeWithPipe()
0N/A throws IOException
0N/A {
0N/A PipedOutputStream wr = new PipedOutputStream();
0N/A PipedInputStream rd = new PipedInputStream(wr);
0N/A DebugExecWatcher embryo = new DebugExecWatcher(rd, wr);
0N/A embryo.start();
0N/A return embryo;
0N/A }
0N/A
0N/A public OutputStream otherEnd()
0N/A {
0N/A return otherEnd;
0N/A }
0N/A
0N/A public synchronized void notifyLine(String s)
0N/A {
0N/A if (s != null && s.indexOf("rmid: debugExec") != -1)
0N/A found = s;
0N/A }
0N/A
0N/A public void run()
0N/A {
0N/A try {
0N/A String line;
0N/A while ((line = str.readLine()) != null) {
0N/A this.notifyLine(line);
0N/A System.err.println(line);
0N/A }
0N/A } catch (IOException e) {
5266N/A /* During termination of distant rmid, StreamPipes will be broken when
5266N/A * distant vm terminates. A "Pipe broken" exception is expected because
5266N/A * DebugExecWatcher points to the same streams as StreamPipes used by RMID.
5266N/A * If we get this exception. We just terminate the thread.
5266N/A */
5266N/A if (e.getMessage().equals("Pipe broken")) {
5266N/A try {
5266N/A str.close();
5266N/A } catch (IOException ioe) {}
5266N/A }
5266N/A else {
5266N/A e.printStackTrace();
5266N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A/*
0N/A code graveyard
0N/A
0N/A // activation should have proceeded by writing a wrapper.out
0N/A // when test.src/actgrpwrapper was run.
0N/A
0N/A // Read and check wrapper.out
0N/A BufferedReader r = new BufferedReader(new FileReader(wrapout));
0N/A String[] realArgs = null;
0N/A String line;
0N/A
0N/A while ( (line = r.readLine()) != null) {
0N/A StringTokenizer tkz = new StringTokenizer(line);
0N/A if (!tkz.nextToken().equals("actgrpwrapper")) {
0N/A // could throw an exception, but let's benignly
0N/A // assume that something unrelated is spewing.
0N/A continue;
0N/A }
0N/A String x; // writer's block
0N/A x = tkz.nextToken();
0N/A if (x.equals("argc")) {
0N/A if (realArgs != null) {
0N/A throw new RuntimeException(
0N/A "SetChildEnv: two argc lines in wrapper.out");
0N/A }
0N/A realArgs = new String[Integer.parseInt(tkz.nextToken())];
0N/A } else if (x.equals("argv")) {
0N/A if (realArgs == null)
0N/A throw new RuntimeException("SetChildEnv: missing argc");
0N/A int n = Integer.parseInt(tkz.nextToken());
0N/A if (n < 1 || n > realArgs.length) {
0N/A throw new RuntimeException("SetChildEnv: argc=" +
0N/A realArgs.length + "; argv[" + n + "]");
0N/A }
0N/A // Hack: manually skip the "actgrpwrapper argv 5 "
0N/A String remainder = line.substring(
0N/A 1 + line.indexOf(' ',
0N/A 1 + line.indexOf(' ',
0N/A 1 + line.indexOf(' '))));
0N/A realArgs[n-1] = remainder;
0N/A } else {
0N/A throw new RuntimeException("SetChildEnv: bad token \"" + x + "\"");
0N/A }
0N/A }
0N/A r.close();
0N/A
0N/A private static void ensureLocalExecutable(String fname)
0N/A throws Exception
0N/A {
0N/A File target = new File(fname);
0N/A File source = new File(Dot, fname);
0N/A if (!target.exists()) {
0N/A // copy from source
0N/A System.err.println("Copying " + source.getPath() +
0N/A " to " + target.getPath());
0N/A java.io.InputStream in = new java.io.FileInputStream(source);
0N/A java.io.OutputStream out = new java.io.FileOutputStream(target);
0N/A byte[] buf = new byte[512];
0N/A int n;
0N/A while ((n = in.read(buf, 0, 512)) > 0) {
0N/A out.write(buf, 0, n);
0N/A }
0N/A out.close();
0N/A in.close();
0N/A }
0N/A // chmod
0N/A System.err.println("Doing: /bin/chmod 755 " + fname);
0N/A Runtime.getRuntime().exec("/bin/chmod 755 " + fname).waitFor();
0N/A }
0N/A
0N/A*/