|
1 /* |
|
2 * Copyright (c) 2014, 2017, 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package jdk.jshell.execution; |
|
26 |
|
27 import java.io.InputStream; |
|
28 import java.io.OutputStream; |
|
29 import java.io.PrintStream; |
|
30 import java.lang.reflect.Method; |
|
31 import java.net.Socket; |
|
32 |
|
33 import java.util.HashMap; |
|
34 import java.util.Map; |
|
35 import java.util.function.Consumer; |
|
36 import jdk.jshell.spi.ExecutionControl; |
|
37 import static jdk.jshell.execution.Util.forwardExecutionControlAndIO; |
|
38 |
|
39 /** |
|
40 * The remote agent runs in the execution process (separate from the main JShell |
|
41 * process). This agent loads code over a socket from the main JShell process, |
|
42 * executes the code, and other misc, Specialization of |
|
43 * {@link DirectExecutionControl} which adds stop support controlled by |
|
44 * an external process. Designed to work with {@link JdiDefaultExecutionControl}. |
|
45 * |
|
46 * @author Jan Lahoda |
|
47 * @author Robert Field |
|
48 * @since 9 |
|
49 */ |
|
50 public class RemoteExecutionControl extends DirectExecutionControl implements ExecutionControl { |
|
51 |
|
52 /** |
|
53 * Launch the agent, connecting to the JShell-core over the socket specified |
|
54 * in the command-line argument. |
|
55 * |
|
56 * @param args standard command-line arguments, expectation is the socket |
|
57 * number is the only argument |
|
58 * @throws Exception any unexpected exception |
|
59 */ |
|
60 public static void main(String[] args) throws Exception { |
|
61 String loopBack = null; |
|
62 Socket socket = new Socket(loopBack, Integer.parseInt(args[0])); |
|
63 InputStream inStream = socket.getInputStream(); |
|
64 OutputStream outStream = socket.getOutputStream(); |
|
65 Map<String, Consumer<OutputStream>> outputs = new HashMap<>(); |
|
66 outputs.put("out", st -> System.setOut(new PrintStream(st, true))); |
|
67 outputs.put("err", st -> System.setErr(new PrintStream(st, true))); |
|
68 Map<String, Consumer<InputStream>> input = new HashMap<>(); |
|
69 input.put("in", System::setIn); |
|
70 forwardExecutionControlAndIO(new RemoteExecutionControl(), inStream, outStream, outputs, input); |
|
71 } |
|
72 |
|
73 // These three variables are used by the main JShell process in interrupting |
|
74 // the running process. Access is via JDI, so the reference is not visible |
|
75 // to code inspection. |
|
76 private boolean inClientCode; // Queried by the main process (in superclass) |
|
77 private boolean expectingStop; // Set by the main process |
|
78 // Set by the main process |
|
79 |
|
80 // thrown by the main process via JDI: |
|
81 private final StopExecutionException stopException = new StopExecutionException(); |
|
82 |
|
83 /** |
|
84 * Creates an instance, delegating loader operations to the specified |
|
85 * delegate. |
|
86 * |
|
87 * @param loaderDelegate the delegate to handle loading classes |
|
88 */ |
|
89 public RemoteExecutionControl(LoaderDelegate loaderDelegate) { |
|
90 super(loaderDelegate); |
|
91 } |
|
92 |
|
93 /** |
|
94 * Create an instance using the default class loading. |
|
95 */ |
|
96 public RemoteExecutionControl() { |
|
97 } |
|
98 |
|
99 /** |
|
100 * Redefine processing on the remote end is only to register the redefined classes |
|
101 */ |
|
102 @Override |
|
103 public void redefine(ClassBytecodes[] cbcs) |
|
104 throws ClassInstallException, NotImplementedException, EngineTerminationException { |
|
105 classesRedefined(cbcs); |
|
106 } |
|
107 |
|
108 @Override |
|
109 public void stop() throws EngineTerminationException, InternalException { |
|
110 // handled by JDI |
|
111 } |
|
112 |
|
113 // Overridden only so this stack frame is seen |
|
114 @Override |
|
115 protected String invoke(Method doitMethod) throws Exception { |
|
116 return super.invoke(doitMethod); |
|
117 } |
|
118 |
|
119 // Overridden only so this stack frame is seen |
|
120 @Override |
|
121 public String varValue(String className, String varName) throws RunException, EngineTerminationException, InternalException { |
|
122 return super.varValue(className, varName); |
|
123 } |
|
124 |
|
125 @Override |
|
126 protected String throwConvertedInvocationException(Throwable cause) throws RunException, InternalException { |
|
127 if (cause instanceof StopExecutionException) { |
|
128 expectingStop = false; |
|
129 throw new StoppedException(); |
|
130 } else { |
|
131 return super.throwConvertedInvocationException(cause); |
|
132 } |
|
133 } |
|
134 |
|
135 @Override |
|
136 protected String throwConvertedOtherException(Throwable ex) throws RunException, InternalException { |
|
137 if (ex instanceof StopExecutionException || |
|
138 ex.getCause() instanceof StopExecutionException) { |
|
139 expectingStop = false; |
|
140 throw new StoppedException(); |
|
141 } |
|
142 return super.throwConvertedOtherException(ex); |
|
143 } |
|
144 |
|
145 @Override |
|
146 protected void clientCodeEnter() { |
|
147 expectingStop = false; |
|
148 inClientCode = true; |
|
149 } |
|
150 |
|
151 @Override |
|
152 protected void clientCodeLeave() throws InternalException { |
|
153 inClientCode = false; |
|
154 while (expectingStop) { |
|
155 try { |
|
156 Thread.sleep(0); |
|
157 } catch (InterruptedException ex) { |
|
158 throw new InternalException("*** Sleep interrupted while waiting for stop exception: " + ex); |
|
159 } |
|
160 } |
|
161 } |
|
162 |
|
163 @SuppressWarnings("serial") // serialVersionUID intentionally omitted |
|
164 private class StopExecutionException extends ThreadDeath { |
|
165 |
|
166 @Override |
|
167 public synchronized Throwable fillInStackTrace() { |
|
168 return this; |
|
169 } |
|
170 } |
|
171 |
|
172 } |