Fixed stop command jshell_support_jdk9
authorSvata Dedic <sdedic@netbeans.org>
Tue, 16 Aug 2016 11:19:38 +0200
branchjshell_support_jdk9
changeset 312388263903adbd69
parent 312387 a30d4c705c62
child 312434 744eb0ecf543
Fixed stop command
jshell.support/src/org/netbeans/modules/jshell/launch/RunExecutionEnvironment.java
jshell.support/src/org/netbeans/modules/jshell/launch/ShellAgent.java
lib.jshell.agent/agentsrc/org/netbeans/lib/jshell/agent/AgentWorker.java
lib.jshell.agent/agentsrc/org/netbeans/lib/jshell/agent/NbJShellAgent.java
     1.1 --- a/jshell.support/src/org/netbeans/modules/jshell/launch/RunExecutionEnvironment.java	Tue Aug 16 11:18:58 2016 +0200
     1.2 +++ b/jshell.support/src/org/netbeans/modules/jshell/launch/RunExecutionEnvironment.java	Tue Aug 16 11:19:38 2016 +0200
     1.3 @@ -48,14 +48,15 @@
     1.4  import java.io.ObjectOutput;
     1.5  import java.io.ObjectOutputStream;
     1.6  import java.io.OutputStream;
     1.7 -import java.util.Collection;
     1.8 -import java.util.Collections;
     1.9 +import java.util.HashMap;
    1.10  import java.util.Map;
    1.11 +import java.util.logging.Level;
    1.12 +import java.util.logging.Logger;
    1.13 +import jdk.jshell.execution.StreamingExecutionControl;
    1.14 +import jdk.jshell.execution.Util;
    1.15  import org.netbeans.lib.nbjshell.NbExecutionControl;
    1.16 -import jdk.jshell.spi.ExecutionEnv;
    1.17  import org.netbeans.lib.nbjshell.NbExecutionControlBase;
    1.18  import org.netbeans.lib.nbjshell.RemoteJShellService;
    1.19 -import org.openide.awt.StatusDisplayer;
    1.20  
    1.21  /**
    1.22   * Exec environment suitable for machines without active JDI connection.
    1.23 @@ -63,21 +64,23 @@
    1.24   * @author sdedic
    1.25   */
    1.26  public class RunExecutionEnvironment extends NbExecutionControlBase implements RemoteJShellService, ShellLaunchListener, NbExecutionControl {
    1.27 +    private static final Logger LOG = Logger.getLogger(RunExecutionEnvironment.class.getName());
    1.28 +    
    1.29      private final ShellAgent agent;
    1.30      
    1.31 -    private boolean added;
    1.32      private volatile boolean closed;
    1.33      private JShellConnection shellConnection;
    1.34      private ObjectInput dis;
    1.35      private ObjectOutput dos;
    1.36      private String targetSpec;
    1.37  
    1.38 -    public RunExecutionEnvironment(ShellAgent agent, ObjectOutput out, ObjectInput in, String targetSpec) {
    1.39 +    public RunExecutionEnvironment(ShellAgent agent, ObjectOutput out, ObjectInput in, String targetSpec, JShellConnection c) {
    1.40          super(out, in);
    1.41          this.dis = in;
    1.42          this.dos = out;
    1.43          this.agent = agent;
    1.44          this.targetSpec = targetSpec;
    1.45 +        this.shellConnection = c;
    1.46          ShellLaunchManager.getInstance().addLaunchListener(this);
    1.47      }
    1.48      
    1.49 @@ -109,17 +112,26 @@
    1.50       */
    1.51      @Override
    1.52      public void stop() {
    1.53 +        if (shellConnection == null) {
    1.54 +            return;
    1.55 +        }
    1.56          int id = this.shellConnection.getRemoteAgentId();
    1.57 +        Map<String, OutputStream> io = new HashMap<>();
    1.58 +        LOG.log(Level.FINE, "Creating agent connection for STOP command");
    1.59          try (JShellConnection stopConnection = agent.createConnection();
    1.60 -            ObjectInputStream in = new ObjectInputStream(stopConnection.getAgentOutput());
    1.61 -            ObjectOutputStream out = new ObjectOutputStream(stopConnection.getAgentInput())
    1.62 +            ObjectOutputStream out = new ObjectOutputStream(stopConnection.getAgentInput());
    1.63 +            ObjectInput cmdin = Util.remoteInput(stopConnection.getAgentOutput(), io);
    1.64          ) {
    1.65 -            
    1.66 -            out.writeInt(NbExecutionControl.CMD_STOP);
    1.67 -            out.writeInt(id);
    1.68 -            out.flush();
    1.69 -            int success = in.readInt();
    1.70 +            StreamingExecutionControl stopStream = new StreamingExecutionControl(out, cmdin);
    1.71 +            Object o = stopStream.extensionCommand("nb_stop", id);
    1.72 +            LOG.log(Level.FINE, "Sending STOP command for agent ID: " + id);
    1.73 +            int success = (o instanceof Integer) ? (Integer)o : -1;
    1.74 +        } catch (RunException | InternalException ex) {
    1.75 +            LOG.log(Level.INFO, "Error invoking JShell agent", ex.toString());
    1.76 +        } catch (EngineTerminationException ex) {
    1.77 +            shutdown();
    1.78          } catch (IOException ex) {
    1.79 +            LOG.log(Level.FINE, "STOP agent creation failed", ex);
    1.80          }
    1.81      }
    1.82  
     2.1 --- a/jshell.support/src/org/netbeans/modules/jshell/launch/ShellAgent.java	Tue Aug 16 11:18:58 2016 +0200
     2.2 +++ b/jshell.support/src/org/netbeans/modules/jshell/launch/ShellAgent.java	Tue Aug 16 11:19:38 2016 +0200
     2.3 @@ -341,7 +341,7 @@
     2.4          
     2.5          @Override
     2.6          protected ExecutionControl createExecControl(ShellAgent agent, ObjectOutput out, ObjectInput in, JShellConnection c) {
     2.7 -            return new RunExecutionEnvironment(agent, out, in, targetSpec);
     2.8 +            return new RunExecutionEnvironment(agent, out, in, targetSpec, c);
     2.9          }
    2.10      }
    2.11  
     3.1 --- a/lib.jshell.agent/agentsrc/org/netbeans/lib/jshell/agent/AgentWorker.java	Tue Aug 16 11:18:58 2016 +0200
     3.2 +++ b/lib.jshell.agent/agentsrc/org/netbeans/lib/jshell/agent/AgentWorker.java	Tue Aug 16 11:19:38 2016 +0200
     3.3 @@ -297,7 +297,7 @@
     3.4                          throw new InternalError("Unexpected agent ID: " + arg);
     3.5                      }
     3.6                      performStop((Integer)arg);
     3.7 -                    return null;
     3.8 +                    return 1;
     3.9                  default:
    3.10                      throw new NotImplementedException("Command " + command + " not implemented");
    3.11              }
    3.12 @@ -325,8 +325,9 @@
    3.13              forwardExecutionControlAndIO(this, ism, osm, chans);
    3.14          } catch (EOFException ex) {
    3.15              // expected.
    3.16 +            LOG.log(Level.FINE, "EOF", ex);
    3.17          } catch (IOException ex) {
    3.18 -            
    3.19 +            LOG.log(Level.FINE, "I/O", ex);
    3.20          } catch (Exception ex) {
    3.21              ex.printStackTrace();
    3.22          } finally {
    3.23 @@ -449,7 +450,7 @@
    3.24  
    3.25      @Override
    3.26      public String invoke(String className, String methodName) throws RunException, InternalException, EngineTerminationException {
    3.27 -        final Exception [] err = new IOException[1];
    3.28 +        final Exception [] err = new Exception[1];
    3.29          final CountDownLatch execLatch = new CountDownLatch(1);
    3.30          final String[] result = new String[1];
    3.31          // for Graalists :)
     4.1 --- a/lib.jshell.agent/agentsrc/org/netbeans/lib/jshell/agent/NbJShellAgent.java	Tue Aug 16 11:18:58 2016 +0200
     4.2 +++ b/lib.jshell.agent/agentsrc/org/netbeans/lib/jshell/agent/NbJShellAgent.java	Tue Aug 16 11:19:38 2016 +0200
     4.3 @@ -253,6 +253,7 @@
     4.4          try {
     4.5              ThreadGroup tg = new ThreadGroup("NetBeans JSHell agent support"); // NOI18N
     4.6              Thread t = new Thread(tg, agent, "JShell VM Agent Connector"); // NOI18N
     4.7 +            LOG.log(Level.INFO, "Starting JShell agent loop");
     4.8              t.setDaemon(true);
     4.9              t.setContextClassLoader(agent.createClassLoader());
    4.10              t.start();
    4.11 @@ -269,7 +270,6 @@
    4.12      }
    4.13      
    4.14      public void run() {
    4.15 -        LOG.setLevel(Level.FINE);
    4.16          try {
    4.17              workerClass = Class.forName("org.netbeans.lib.jshell.agent.AgentWorker", true, createClassLoader()); //createClassLoader().loadClass("jdk.internal.jshell.remote.AgentWorker"); // NOI18N
    4.18              workerCtor = workerClass.getConstructor(NbJShellAgent.class, Socket.class);