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 24 import java.io.InputStream; 25 import java.io.OutputStream; 26 import java.io.PrintStream; 27 import java.net.Socket; 28 import java.util.HashMap; 29 import java.util.Map; 30 import java.util.function.Consumer; 31 import jdk.jshell.execution.DirectExecutionControl; 32 import jdk.jshell.spi.ExecutionControl; 33 import jdk.jshell.spi.ExecutionControl.EngineTerminationException; 34 import jdk.jshell.spi.ExecutionControl.InternalException; 35 import jdk.jshell.spi.ExecutionControl.RunException; 36 import static jdk.jshell.execution.Util.forwardExecutionControlAndIO; 37 38 /** 39 * A custom remote agent to verify aux channel and custom ExecutionControl. 40 */ 41 public 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 */ main(String[] args)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 varValue(String className, String varName)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 extensionCommand(String className, Object arg)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