src/jdk.jshell/share/classes/jdk/jshell/execution/StreamingExecutionControl.java
author rfield
Tue, 03 Apr 2018 13:27:58 -0700
changeset 49515 083318155ad1
parent 47216 71c04702a3d5
permissions -rw-r--r--
8198801: JShell: user exception chained cause not retained Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     1
/*
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     4
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    10
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    15
 * accompanied this code).
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    16
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    20
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    23
 * questions.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    24
 */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    25
package jdk.jshell.execution;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    26
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    27
import java.io.IOException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    28
import java.io.ObjectInput;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    29
import java.io.ObjectOutput;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    30
import jdk.jshell.JShellException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    31
import jdk.jshell.spi.ExecutionControl;
46926
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
    32
import static jdk.jshell.execution.ExecutionControlForwarder.NULL_MARKER;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    33
import static jdk.jshell.execution.RemoteCodes.*;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    34
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    35
/**
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    36
 * An implementation of the {@link jdk.jshell.spi.ExecutionControl}
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    37
 * execution engine SPI which streams requests to a remote agent where
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    38
 * execution takes place.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    39
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    40
 * @author Robert Field
43770
a321bed02000 8174762: JShell: @since tags missing
rfield
parents: 43132
diff changeset
    41
 * @since 9
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    42
 */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    43
public class StreamingExecutionControl implements ExecutionControl {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    44
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    45
    private final ObjectOutput out;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    46
    private final ObjectInput in;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    47
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    48
    /**
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    49
     * Creates an instance.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    50
     *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    51
     * @param out the output for commands
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    52
     * @param in the input for command responses
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    53
     */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    54
    public StreamingExecutionControl(ObjectOutput out, ObjectInput in) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    55
        this.out = out;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    56
        this.in = in;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    57
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    58
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    59
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    60
    public void load(ClassBytecodes[] cbcs)
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    61
            throws ClassInstallException, NotImplementedException, EngineTerminationException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    62
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    63
            // Send a load command to the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    64
            writeCommand(CMD_LOAD);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    65
            out.writeObject(cbcs);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    66
            out.flush();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    67
            // Retrieve and report results from the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    68
            readAndReportClassInstallResult();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    69
        } catch (IOException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    70
            throw new EngineTerminationException("Exception writing remote load: " + ex);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    71
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    72
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    73
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    74
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    75
    public void redefine(ClassBytecodes[] cbcs)
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    76
            throws ClassInstallException, NotImplementedException, EngineTerminationException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    77
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    78
            // Send a load command to the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    79
            writeCommand(CMD_REDEFINE);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    80
            out.writeObject(cbcs);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    81
            out.flush();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    82
            // Retrieve and report results from the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    83
            readAndReportClassInstallResult();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    84
        } catch (IOException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    85
            throw new EngineTerminationException("Exception writing remote redefine: " + ex);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    86
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    87
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    88
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    89
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    90
    public String invoke(String classname, String methodname)
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    91
            throws RunException, EngineTerminationException, InternalException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    92
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    93
            // Send the invoke command to the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    94
            writeCommand(CMD_INVOKE);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    95
            out.writeUTF(classname);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    96
            out.writeUTF(methodname);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    97
            out.flush();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    98
            // Retrieve and report results from the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    99
            readAndReportExecutionResult();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   100
            String result = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   101
            return result;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   102
        } catch (IOException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   103
            throw new EngineTerminationException("Exception writing remote invoke: " + ex);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   104
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   105
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   106
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   107
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   108
    public String varValue(String classname, String varname)
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   109
            throws RunException, EngineTerminationException, InternalException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   110
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   111
            // Send the variable-value command to the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   112
            writeCommand(CMD_VAR_VALUE);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   113
            out.writeUTF(classname);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   114
            out.writeUTF(varname);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   115
            out.flush();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   116
            // Retrieve and report results from the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   117
            readAndReportExecutionResult();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   118
            String result = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   119
            return result;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   120
        } catch (IOException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   121
            throw new EngineTerminationException("Exception writing remote varValue: " + ex);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   122
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   123
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   124
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   125
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   126
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   127
    public void addToClasspath(String path)
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   128
            throws EngineTerminationException, InternalException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   129
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   130
            // Send the classpath addition command to the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   131
            writeCommand(CMD_ADD_CLASSPATH);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   132
            out.writeUTF(path);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   133
            out.flush();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   134
            // Retrieve and report results from the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   135
            readAndReportClassSimpleResult();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   136
        } catch (IOException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   137
            throw new EngineTerminationException("Exception writing remote add to classpath: " + ex);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   138
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   139
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   140
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   141
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   142
    public void stop()
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   143
            throws EngineTerminationException, InternalException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   144
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   145
            // Send the variable-value command to the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   146
            writeCommand(CMD_STOP);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   147
            out.flush();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   148
        } catch (IOException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   149
            throw new EngineTerminationException("Exception writing remote stop: " + ex);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   150
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   151
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   152
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   153
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   154
    public Object extensionCommand(String command, Object arg)
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   155
            throws RunException, EngineTerminationException, InternalException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   156
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   157
            writeCommand(command);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   158
            out.writeObject(arg);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   159
            out.flush();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   160
            // Retrieve and report results from the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   161
            readAndReportExecutionResult();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   162
            Object result = in.readObject();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   163
            return result;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   164
        } catch (IOException | ClassNotFoundException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   165
            throw new EngineTerminationException("Exception transmitting remote extensionCommand: "
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   166
                    + command + " -- " + ex);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   167
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   168
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   169
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   170
    /**
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   171
     * Closes the execution engine. Send an exit command to the remote agent.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   172
     */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   173
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   174
    public void close() {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   175
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   176
            writeCommand(CMD_CLOSE);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   177
            out.flush();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   178
        } catch (IOException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   179
            // ignore;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   180
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   181
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   182
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   183
    private void writeCommand(String cmd) throws IOException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   184
        out.writeInt(COMMAND_PREFIX);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   185
        out.writeUTF(cmd);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   186
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   187
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   188
    /**
46926
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   189
     * Read a UTF or a null encoded as a null marker.
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   190
     * @return a string or null
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   191
     * @throws IOException passed through from readUTF()
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   192
     */
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   193
    private String readNullOrUTF() throws IOException {
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   194
        String s = in.readUTF();
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   195
        return s.equals(NULL_MARKER) ? null : s;
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   196
    }
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   197
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 43770
diff changeset
   198
    /**
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   199
     * Reports results from a remote agent command that does not expect
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   200
     * exceptions.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   201
     */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   202
    private void readAndReportClassSimpleResult() throws EngineTerminationException, InternalException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   203
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   204
            int status = in.readInt();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   205
            switch (status) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   206
                case RESULT_SUCCESS:
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   207
                    return;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   208
                case RESULT_NOT_IMPLEMENTED: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   209
                    String message = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   210
                    throw new NotImplementedException(message);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   211
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   212
                case RESULT_INTERNAL_PROBLEM: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   213
                    String message = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   214
                    throw new InternalException(message);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   215
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   216
                case RESULT_TERMINATED: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   217
                    String message = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   218
                    throw new EngineTerminationException(message);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   219
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   220
                default: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   221
                    throw new EngineTerminationException("Bad remote result code: " + status);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   222
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   223
            }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   224
        } catch (IOException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   225
            throw new EngineTerminationException(ex.toString());
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   226
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   227
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   228
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   229
    /**
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   230
     * Reports results from a remote agent command that does not expect
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   231
     * exceptions.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   232
     */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   233
    private void readAndReportClassInstallResult() throws ClassInstallException,
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   234
            NotImplementedException, EngineTerminationException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   235
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   236
            int status = in.readInt();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   237
            switch (status) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   238
                case RESULT_SUCCESS:
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   239
                    return;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   240
                case RESULT_NOT_IMPLEMENTED: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   241
                    String message = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   242
                    throw new NotImplementedException(message);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   243
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   244
                case RESULT_CLASS_INSTALL_EXCEPTION: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   245
                    String message = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   246
                    boolean[] loaded = (boolean[]) in.readObject();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   247
                    throw new ClassInstallException(message, loaded);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   248
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   249
                case RESULT_TERMINATED: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   250
                    String message = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   251
                    throw new EngineTerminationException(message);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   252
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   253
                default: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   254
                    throw new EngineTerminationException("Bad remote result code: " + status);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   255
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   256
            }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   257
        } catch (IOException | ClassNotFoundException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   258
            throw new EngineTerminationException(ex.toString());
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   259
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   260
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   261
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   262
    /**
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   263
     * Reports results from a remote agent command that expects runtime
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   264
     * exceptions.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   265
     *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   266
     * @return true if successful
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   267
     * @throws IOException if the connection has dropped
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   268
     * @throws JShellException {@link jdk.jshell.EvalException}, if a user
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   269
     * exception was encountered on invoke;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   270
     * {@link jdk.jshell.UnresolvedReferenceException}, if an unresolved
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   271
     * reference was encountered
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   272
     * @throws java.lang.ClassNotFoundException
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   273
     */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   274
    private void readAndReportExecutionResult() throws RunException,
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   275
            EngineTerminationException, InternalException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   276
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   277
            int status = in.readInt();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   278
            switch (status) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   279
                case RESULT_SUCCESS:
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   280
                    return;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   281
                case RESULT_NOT_IMPLEMENTED: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   282
                    String message = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   283
                    throw new NotImplementedException(message);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   284
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   285
                case RESULT_USER_EXCEPTION: {
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   286
                    // A user exception was encountered.  Handle pre JDK 11 back-ends
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   287
                    throw readUserException();
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   288
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   289
                case RESULT_CORRALLED: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   290
                    // An unresolved reference was encountered.
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   291
                    throw readResolutionException();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   292
                }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   293
                case RESULT_USER_EXCEPTION_CHAINED: {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   294
                    // A user exception was encountered -- transmit chained.
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   295
                    in.readInt(); // always RESULT_USER_EXCEPTION
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   296
                    UserException result = readUserException();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   297
                    RunException caused = result;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   298
                    // Loop through the chained causes (if any) building a chained exception
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   299
                    loop: while (true) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   300
                        RunException ex;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   301
                        int cstatus = in.readInt();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   302
                        switch (cstatus) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   303
                            case RESULT_USER_EXCEPTION: {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   304
                                // A user exception was the proximal cause.
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   305
                                ex = readUserException();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   306
                                break;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   307
                            }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   308
                            case RESULT_CORRALLED: {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   309
                                // An unresolved reference was the underlying cause.
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   310
                                ex = readResolutionException();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   311
                                break;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   312
                            }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   313
                            case RESULT_SUCCESS: {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   314
                                // End of chained exceptions
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   315
                                break loop;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   316
                            }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   317
                            default: {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   318
                                throw new EngineTerminationException("Bad chained remote result code: " + cstatus);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   319
                            }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   320
                        }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   321
                        caused.initCause(ex);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   322
                        caused = ex;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   323
                    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   324
                    caused.initCause(null); // root cause has no cause
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   325
                    throw result;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   326
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   327
                case RESULT_STOPPED: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   328
                    // Execution was aborted by the stop()
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   329
                    throw new StoppedException();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   330
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   331
                case RESULT_INTERNAL_PROBLEM: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   332
                    // An internal error has occurred.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   333
                    String message = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   334
                    throw new InternalException(message);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   335
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   336
                case RESULT_TERMINATED: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   337
                    String message = in.readUTF();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   338
                    throw new EngineTerminationException(message);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   339
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   340
                default: {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   341
                    throw new EngineTerminationException("Bad remote result code: " + status);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   342
                }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   343
            }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   344
        } catch (IOException | ClassNotFoundException ex) {
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   345
            ex.printStackTrace();
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   346
            throw new EngineTerminationException(ex.toString());
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   347
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   348
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   349
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   350
    private UserException readUserException() throws IOException, ClassNotFoundException {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   351
        String message = readNullOrUTF();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   352
        String exceptionClassName = in.readUTF();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   353
        StackTraceElement[] elems = (StackTraceElement[]) in.readObject();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   354
        return new UserException(message, exceptionClassName, elems);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   355
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   356
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   357
    private ResolutionException readResolutionException() throws IOException, ClassNotFoundException {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   358
        int id = in.readInt();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   359
        StackTraceElement[] elems = (StackTraceElement[]) in.readObject();
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   360
        return new ResolutionException(id, elems);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   361
    }
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   362
}