--- /dev/null 2016-07-12 14:35:29.888302783 -0700 +++ new/src/jdk.jshell/share/classes/jdk/jshell/execution/ExecutionControlForwarder.java 2016-07-12 22:46:55.623957570 -0700 @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package jdk.jshell.execution; + +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import jdk.jshell.spi.ExecutionControl; +import jdk.jshell.spi.ExecutionControl.ClassBytecodes; +import jdk.jshell.spi.ExecutionControl.ClassInstallException; +import jdk.jshell.spi.ExecutionControl.EngineTerminationException; +import jdk.jshell.spi.ExecutionControl.InternalException; +import jdk.jshell.spi.ExecutionControl.ResolutionException; +import jdk.jshell.spi.ExecutionControl.StoppedException; +import jdk.jshell.spi.ExecutionControl.UserException; + +/** + * Forwards commands from the input to the specified {@link ExecutionControl} + * instance, then responses back on the output. + */ +class ExecutionControlForwarder { + + private final ExecutionControl ec; + private final ObjectInput in; + private final ObjectOutput out; + + ExecutionControlForwarder(ExecutionControl ec, ObjectInput in, ObjectOutput out) { + this.ec = ec; + this.in = in; + this.out = out; + } + + private boolean writeSuccess() throws IOException { + writeStatus(RemoteStatus.SUCCESS); + flush(); + return true; + } + + private boolean writeSuccessAndResult(String result) throws IOException { + writeStatus(RemoteStatus.SUCCESS); + writeUTF(result); + flush(); + return true; + } + + private void writeStatus(RemoteStatus status) throws IOException { + out.writeObject(status); + } + + private void writeObject(Object o) throws IOException { + out.writeObject(o); + } + + private void writeInt(int i) throws IOException { + out.writeInt(i); + } + + private void writeUTF(String s) throws IOException { + if (s == null) { + s = ""; + } + out.writeUTF(s); + } + + private void flush() throws IOException { + out.flush(); + } + + private boolean processCommand() throws IOException { + RemoteCommand cmd; + try { + cmd = (RemoteCommand) in.readObject(); + if (cmd == null) { + throw new IOException("Missing command: " + cmd); + } + switch (cmd) { + case LOAD: { + // Load a generated class file over the wire + ClassBytecodes[] cbcs = (ClassBytecodes[]) in.readObject(); + ec.load(cbcs); + return writeSuccess(); + } + case REDEFINE: { + // Load a generated class file over the wire + ClassBytecodes[] cbcs = (ClassBytecodes[]) in.readObject(); + ec.redefine(cbcs); + return writeSuccess(); + } + case INVOKE: { + // Invoke executable entry point in loaded code + String className = in.readUTF(); + String methodName = in.readUTF(); + String res = ec.invoke(className, methodName); + return writeSuccessAndResult(res); + } + case VAR_VALUE: { + // Retrieve a variable value + String className = in.readUTF(); + String varName = in.readUTF(); + String res = ec.varValue(className, varName); + return writeSuccessAndResult(res); + } + case ADD_CLASSPATH: { + // Append to the claspath + String cp = in.readUTF(); + ec.addToClasspath(cp); + return writeSuccess(); + } + case SET_CLASSPATH: { + // Set the claspath + String cp = in.readUTF(); + ec.setClasspath(cp); + return writeSuccess(); + } + case STOP: { + // Stop the current execution + try { + ec.stop(); + } catch (Throwable ex) { + // JShell-core not waiting for a result, ignore + } + return true; + } + case CLOSE: { + // Terminate this process + try { + ec.close(); + } catch (Throwable ex) { + // JShell-core not waiting for a result, ignore + } + return true; + } + } + return false; // cannot get here, make the compiler happy + } catch (IOException ex) { + // handled by the outer level + throw ex; + } catch (EngineTerminationException ex) { + writeStatus(RemoteStatus.TERMINATED); + writeUTF(ex.getMessage()); + flush(); + return false; + } catch (InternalException ex) { + writeStatus(RemoteStatus.INTERNAL_PROBLEM); + writeUTF(ex.getMessage()); + flush(); + return true; + } catch (ClassInstallException ex) { + writeStatus(RemoteStatus.CLASS_INSTALL_EXCEPTION); + writeUTF(ex.getMessage()); + writeObject(ex.installed()); + flush(); + return true; + } catch (UserException ex) { + writeStatus(RemoteStatus.USER_EXCEPTION); + writeUTF(ex.getMessage()); + writeUTF(ex.causeExceptionClass()); + writeObject(ex.getStackTrace()); + flush(); + return true; + } catch (ResolutionException ex) { + writeStatus(RemoteStatus.CORRALLED); + writeInt(ex.id()); + writeObject(ex.getStackTrace()); + flush(); + return true; + } catch (StoppedException ex) { + writeStatus(RemoteStatus.STOPPED); + flush(); + return true; + } catch (Throwable ex) { + writeStatus(RemoteStatus.TERMINATED); + writeUTF(ex.getMessage()); + flush(); + return false; + } + } + + void commandLoop() { + try { + while (processCommand()) { + // condition is loop action + } + } catch (IOException ex) { + // ignore + } + } + +}