|
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. 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.IOException; |
|
28 import java.io.ObjectInput; |
|
29 import java.io.ObjectOutput; |
|
30 import jdk.jshell.spi.ExecutionControl; |
|
31 import jdk.jshell.spi.ExecutionControl.ClassBytecodes; |
|
32 import jdk.jshell.spi.ExecutionControl.ClassInstallException; |
|
33 import jdk.jshell.spi.ExecutionControl.EngineTerminationException; |
|
34 import jdk.jshell.spi.ExecutionControl.InternalException; |
|
35 import jdk.jshell.spi.ExecutionControl.NotImplementedException; |
|
36 import jdk.jshell.spi.ExecutionControl.ResolutionException; |
|
37 import jdk.jshell.spi.ExecutionControl.StoppedException; |
|
38 import jdk.jshell.spi.ExecutionControl.UserException; |
|
39 import static jdk.jshell.execution.RemoteCodes.*; |
|
40 |
|
41 /** |
|
42 * Forwards commands from the input to the specified {@link ExecutionControl} |
|
43 * instance, then responses back on the output. |
|
44 */ |
|
45 class ExecutionControlForwarder { |
|
46 |
|
47 /** |
|
48 * Represent null in a streamed UTF string. Vanishingly improbable string to |
|
49 * occur in a user string. |
|
50 */ |
|
51 static final String NULL_MARKER = "\u0002*\u03C0*NULL*\u03C0*\u0003"; |
|
52 |
|
53 /** |
|
54 * Maximum number of characters for writeUTF(). Byte maximum is 65535, at |
|
55 * maximum three bytes per character that is 65535 / 3 == 21845. Minus one |
|
56 * for safety. |
|
57 */ |
|
58 private static final int MAX_UTF_CHARS = 21844; |
|
59 |
|
60 private final ExecutionControl ec; |
|
61 private final ObjectInput in; |
|
62 private final ObjectOutput out; |
|
63 |
|
64 ExecutionControlForwarder(ExecutionControl ec, ObjectInput in, ObjectOutput out) { |
|
65 this.ec = ec; |
|
66 this.in = in; |
|
67 this.out = out; |
|
68 } |
|
69 |
|
70 private boolean writeSuccess() throws IOException { |
|
71 writeStatus(RESULT_SUCCESS); |
|
72 flush(); |
|
73 return true; |
|
74 } |
|
75 |
|
76 private boolean writeSuccessAndResult(String result) throws IOException { |
|
77 writeStatus(RESULT_SUCCESS); |
|
78 writeUTF(result); |
|
79 flush(); |
|
80 return true; |
|
81 } |
|
82 |
|
83 private boolean writeSuccessAndResult(Object result) throws IOException { |
|
84 writeStatus(RESULT_SUCCESS); |
|
85 writeObject(result); |
|
86 flush(); |
|
87 return true; |
|
88 } |
|
89 |
|
90 private void writeStatus(int status) throws IOException { |
|
91 out.writeInt(status); |
|
92 } |
|
93 |
|
94 private void writeObject(Object o) throws IOException { |
|
95 out.writeObject(o); |
|
96 } |
|
97 |
|
98 private void writeInt(int i) throws IOException { |
|
99 out.writeInt(i); |
|
100 } |
|
101 |
|
102 private void writeNullOrUTF(String s) throws IOException { |
|
103 writeUTF(s == null ? NULL_MARKER : s); |
|
104 } |
|
105 |
|
106 private void writeUTF(String s) throws IOException { |
|
107 if (s == null) { |
|
108 s = ""; |
|
109 } else if (s.length() > MAX_UTF_CHARS) { |
|
110 // Truncate extremely long strings to prevent writeUTF from crashing the VM |
|
111 s = s.substring(0, MAX_UTF_CHARS); |
|
112 } |
|
113 out.writeUTF(s); |
|
114 } |
|
115 |
|
116 private void flush() throws IOException { |
|
117 out.flush(); |
|
118 } |
|
119 |
|
120 private boolean processCommand() throws IOException { |
|
121 try { |
|
122 int prefix = in.readInt(); |
|
123 if (prefix != COMMAND_PREFIX) { |
|
124 throw new EngineTerminationException("Invalid command prefix: " + prefix); |
|
125 } |
|
126 String cmd = in.readUTF(); |
|
127 switch (cmd) { |
|
128 case CMD_LOAD: { |
|
129 // Load a generated class file over the wire |
|
130 ClassBytecodes[] cbcs = (ClassBytecodes[]) in.readObject(); |
|
131 ec.load(cbcs); |
|
132 return writeSuccess(); |
|
133 } |
|
134 case CMD_REDEFINE: { |
|
135 // Load a generated class file over the wire |
|
136 ClassBytecodes[] cbcs = (ClassBytecodes[]) in.readObject(); |
|
137 ec.redefine(cbcs); |
|
138 return writeSuccess(); |
|
139 } |
|
140 case CMD_INVOKE: { |
|
141 // Invoke executable entry point in loaded code |
|
142 String className = in.readUTF(); |
|
143 String methodName = in.readUTF(); |
|
144 String res = ec.invoke(className, methodName); |
|
145 return writeSuccessAndResult(res); |
|
146 } |
|
147 case CMD_VAR_VALUE: { |
|
148 // Retrieve a variable value |
|
149 String className = in.readUTF(); |
|
150 String varName = in.readUTF(); |
|
151 String res = ec.varValue(className, varName); |
|
152 return writeSuccessAndResult(res); |
|
153 } |
|
154 case CMD_ADD_CLASSPATH: { |
|
155 // Append to the claspath |
|
156 String cp = in.readUTF(); |
|
157 ec.addToClasspath(cp); |
|
158 return writeSuccess(); |
|
159 } |
|
160 case CMD_STOP: { |
|
161 // Stop the current execution |
|
162 try { |
|
163 ec.stop(); |
|
164 } catch (Throwable ex) { |
|
165 // JShell-core not waiting for a result, ignore |
|
166 } |
|
167 return true; |
|
168 } |
|
169 case CMD_CLOSE: { |
|
170 // Terminate this process |
|
171 try { |
|
172 ec.close(); |
|
173 } catch (Throwable ex) { |
|
174 // JShell-core not waiting for a result, ignore |
|
175 } |
|
176 return true; |
|
177 } |
|
178 default: { |
|
179 Object arg = in.readObject(); |
|
180 Object res = ec.extensionCommand(cmd, arg); |
|
181 return writeSuccessAndResult(res); |
|
182 } |
|
183 } |
|
184 } catch (IOException ex) { |
|
185 // handled by the outer level |
|
186 throw ex; |
|
187 } catch (EngineTerminationException ex) { |
|
188 writeStatus(RESULT_TERMINATED); |
|
189 writeUTF(ex.getMessage()); |
|
190 flush(); |
|
191 return false; |
|
192 } catch (NotImplementedException ex) { |
|
193 writeStatus(RESULT_NOT_IMPLEMENTED); |
|
194 writeUTF(ex.getMessage()); |
|
195 flush(); |
|
196 return true; |
|
197 } catch (InternalException ex) { |
|
198 writeStatus(RESULT_INTERNAL_PROBLEM); |
|
199 writeUTF(ex.getMessage()); |
|
200 flush(); |
|
201 return true; |
|
202 } catch (ClassInstallException ex) { |
|
203 writeStatus(RESULT_CLASS_INSTALL_EXCEPTION); |
|
204 writeUTF(ex.getMessage()); |
|
205 writeObject(ex.installed()); |
|
206 flush(); |
|
207 return true; |
|
208 } catch (UserException ex) { |
|
209 writeStatus(RESULT_USER_EXCEPTION); |
|
210 writeNullOrUTF(ex.getMessage()); |
|
211 writeUTF(ex.causeExceptionClass()); |
|
212 writeObject(ex.getStackTrace()); |
|
213 flush(); |
|
214 return true; |
|
215 } catch (ResolutionException ex) { |
|
216 writeStatus(RESULT_CORRALLED); |
|
217 writeInt(ex.id()); |
|
218 writeObject(ex.getStackTrace()); |
|
219 flush(); |
|
220 return true; |
|
221 } catch (StoppedException ex) { |
|
222 writeStatus(RESULT_STOPPED); |
|
223 flush(); |
|
224 return true; |
|
225 } catch (Throwable ex) { |
|
226 // Unexpected exception, have something in the message |
|
227 writeStatus(RESULT_TERMINATED); |
|
228 String msg = ex.getMessage(); |
|
229 writeUTF(msg == null? ex.toString() : msg); |
|
230 flush(); |
|
231 return false; |
|
232 } |
|
233 } |
|
234 |
|
235 void commandLoop() { |
|
236 try { |
|
237 while (processCommand()) { |
|
238 // condition is loop action |
|
239 } |
|
240 } catch (IOException ex) { |
|
241 // drop out of loop |
|
242 } |
|
243 } |
|
244 |
|
245 } |