MyRemoteExecutionControl.java revision 3846:605b0823d19b
1/*
2 * Copyright (c) 2016, 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
24import java.io.InputStream;
25import java.io.OutputStream;
26import java.io.PrintStream;
27import java.net.Socket;
28import java.util.HashMap;
29import java.util.Map;
30import java.util.function.Consumer;
31import jdk.jshell.execution.DirectExecutionControl;
32import jdk.jshell.spi.ExecutionControl;
33import jdk.jshell.spi.ExecutionControl.EngineTerminationException;
34import jdk.jshell.spi.ExecutionControl.InternalException;
35import jdk.jshell.spi.ExecutionControl.RunException;
36import static jdk.jshell.execution.Util.forwardExecutionControlAndIO;
37
38/**
39 * A custom remote agent to verify aux channel and custom ExecutionControl.
40 */
41public class MyRemoteExecutionControl extends DirectExecutionControl implements ExecutionControl {
42
43    static PrintStream auxPrint;
44
45    /**
46     * Launch the agent, connecting to the JShell-core over the socket specified
47     * in the command-line argument.
48     *
49     * @param args standard command-line arguments, expectation is the socket
50     * number is the only argument
51     * @throws Exception any unexpected exception
52     */
53    public static void main(String[] args) throws Exception {
54        try {
55            String loopBack = null;
56            Socket socket = new Socket(loopBack, Integer.parseInt(args[0]));
57            InputStream inStream = socket.getInputStream();
58            OutputStream outStream = socket.getOutputStream();
59            Map<String, Consumer<OutputStream>> outputs = new HashMap<>();
60            outputs.put("out", st -> System.setOut(new PrintStream(st, true)));
61            outputs.put("err", st -> System.setErr(new PrintStream(st, true)));
62            outputs.put("aux", st -> { auxPrint = new PrintStream(st, true); });
63            Map<String, Consumer<InputStream>> input = new HashMap<>();
64            input.put("in", st -> System.setIn(st));
65            forwardExecutionControlAndIO(new MyRemoteExecutionControl(), inStream, outStream, outputs, input);
66        } catch (Throwable ex) {
67            throw ex;
68        }
69    }
70
71    @Override
72    public String varValue(String className, String varName)
73            throws RunException, EngineTerminationException, InternalException {
74        auxPrint.print(varName);
75        return super.varValue(className, varName);
76    }
77
78    @Override
79    public Object extensionCommand(String className, Object arg)
80            throws RunException, EngineTerminationException, InternalException {
81        if (!arg.equals("test")) {
82            throw new InternalException("expected extensionCommand arg to be 'test' got: " + arg);
83        }
84        return "ribbit";
85    }
86
87}
88