1 7 34 35 package com.sun.tools.example.debug.bdi; 36 37 import com.sun.jdi.*; 38 import com.sun.jdi.connect.LaunchingConnector; 39 import com.sun.jdi.connect.Connector; 40 import com.sun.jdi.connect.VMStartException; 41 import com.sun.jdi.connect.IllegalConnectorArgumentsException; 42 import java.io.*; 43 import java.util.Map ; 44 import javax.swing.SwingUtilities ; 45 46 47 class ChildSession extends Session { 48 49 private Process process; 50 51 private PrintWriter in; 52 private BufferedReader out; 53 private BufferedReader err; 54 55 private InputWriter inputWriter; 56 private OutputReader outputReader; 57 private OutputReader errorReader; 58 59 private InputListener input; 60 private OutputListener output; 61 private OutputListener error; 62 63 public ChildSession(ExecutionManager runtime, 64 String userVMArgs, String cmdLine, 65 InputListener input, 66 OutputListener output, 67 OutputListener error, 68 OutputListener diagnostics) { 69 this(runtime, getVM(diagnostics, userVMArgs, cmdLine), 70 input, output, error, diagnostics); 71 } 72 73 public ChildSession(ExecutionManager runtime, 74 LaunchingConnector connector, Map arguments, 75 InputListener input, 76 OutputListener output, 77 OutputListener error, 78 OutputListener diagnostics) { 79 this(runtime, generalGetVM(diagnostics, connector, arguments), 80 input, output, error, diagnostics); 81 } 82 83 private ChildSession(ExecutionManager runtime, 84 VirtualMachine vm, 85 InputListener input, 86 OutputListener output, 87 OutputListener error, 88 OutputListener diagnostics) { 89 super(vm, runtime, diagnostics); 90 this.input = input; 91 this.output = output; 92 this.error = error; 93 } 94 95 public boolean attach() { 96 97 if (!connectToVMProcess()) { 98 diagnostics.putString("Could not launch VM"); 99 return false; 100 } 101 102 107 108 111 OutputReader outputReader = 112 new OutputReader("output reader", "output", 113 out, output, diagnostics); 114 outputReader.setPriority(Thread.MAX_PRIORITY-1); 115 outputReader.start(); 116 117 OutputReader errorReader = 118 new OutputReader("error reader", "error", 119 err, error, diagnostics); 120 errorReader.setPriority(Thread.MAX_PRIORITY-1); 121 errorReader.start(); 122 123 InputWriter inputWriter = 124 new InputWriter("input writer", in, input); 125 inputWriter.setPriority(Thread.MAX_PRIORITY-1); 126 inputWriter.start(); 127 128 if (!super.attach()) { 129 if (process != null) { 130 process.destroy(); 131 process = null; 132 } 133 return false; 134 } 135 136 139 return true; 140 } 141 142 public void detach() { 143 144 147 super.detach(); 148 149 154 155 if (process != null) { 156 process.destroy(); 157 process = null; 158 } 159 160 } 161 162 165 166 static private void dumpStream(OutputListener diagnostics, 167 InputStream stream) throws IOException { 168 BufferedReader in = 169 new BufferedReader(new InputStreamReader(stream)); 170 String line; 171 while ((line = in.readLine()) != null) { 172 diagnostics.putString(line); 173 } 174 } 175 176 static private void dumpFailedLaunchInfo(OutputListener diagnostics, 177 Process process) { 178 try { 179 dumpStream(diagnostics, process.getErrorStream()); 180 dumpStream(diagnostics, process.getInputStream()); 181 } catch (IOException e) { 182 diagnostics.putString("Unable to display process output: " + 183 e.getMessage()); 184 } 185 } 186 187 static private VirtualMachine getVM(OutputListener diagnostics, 188 String userVMArgs, 189 String cmdLine) { 190 VirtualMachineManager manager = Bootstrap.virtualMachineManager(); 191 LaunchingConnector connector = manager.defaultConnector(); 192 Map arguments = connector.defaultArguments(); 193 ((Connector.Argument)arguments.get("options")).setValue(userVMArgs); 194 ((Connector.Argument)arguments.get("main")).setValue(cmdLine); 195 return generalGetVM(diagnostics, connector, arguments); 196 } 197 198 static private VirtualMachine generalGetVM(OutputListener diagnostics, 199 LaunchingConnector connector, 200 Map arguments) { 201 VirtualMachine vm = null; 202 try { 203 diagnostics.putString("Starting child."); 204 vm = connector.launch(arguments); 205 } catch (IOException ioe) { 206 diagnostics.putString("Unable to start child: " + ioe.getMessage()); 207 } catch (IllegalConnectorArgumentsException icae) { 208 diagnostics.putString("Unable to start child: " + icae.getMessage()); 209 } catch (VMStartException vmse) { 210 diagnostics.putString("Unable to start child: " + vmse.getMessage() + '\n'); 211 dumpFailedLaunchInfo(diagnostics, vmse.process()); 212 } 213 return vm; 214 } 215 216 private boolean connectToVMProcess() { 217 if (vm == null) { 218 return false; 219 } 220 process = vm.process(); 221 in = new PrintWriter(new OutputStreamWriter(process.getOutputStream())); 222 out = new BufferedReader(new InputStreamReader(process.getInputStream()), 1); 224 err = new BufferedReader(new InputStreamReader(process.getErrorStream()), 1); 225 return true; 226 } 227 228 231 232 private static class OutputReader extends Thread { 233 234 private String streamName; 235 private BufferedReader stream; 236 private OutputListener output; 237 private OutputListener diagnostics; 238 private boolean running = true; 239 private char[] buffer = new char[512]; 240 241 OutputReader(String threadName, 242 String streamName, 243 BufferedReader stream, 244 OutputListener output, 245 OutputListener diagnostics) { 246 super(threadName); 247 this.streamName = streamName; 248 this.stream = stream; 249 this.output = output; 250 this.diagnostics = diagnostics; 251 } 252 253 public void quit() { 254 running = false; 255 } 256 257 public void run() { 258 try { 259 int count; 260 while (running && (count = stream.read(buffer, 0, 512)) != -1) { 261 if (count > 0) { 262 final String chars = new String (buffer, 0, count); 264 SwingUtilities.invokeLater(new Runnable () { 265 public void run() { 266 output.putString(chars); 267 } 268 }); 269 } 270 } 272 } catch (IOException e) { 273 SwingUtilities.invokeLater(new Runnable () { 275 public void run() { 276 diagnostics.putString("IO error reading " + 277 streamName + 278 " stream of child java interpreter"); 279 } 280 }); 281 } 282 } 283 } 284 285 private static class InputWriter extends Thread { 286 287 private PrintWriter stream; 288 private InputListener input; 289 private boolean running = true; 290 291 InputWriter(String threadName, 292 PrintWriter stream, 293 InputListener input) { 294 super(threadName); 295 this.stream = stream; 296 this.input = input; 297 } 298 299 public void quit() { 300 running = false; 302 } 303 304 public void run() { 305 String line; 306 while (running) { 307 line = input.getLine(); 308 stream.println(line); 309 stream.flush(); 311 } 312 } 313 } 314 315 } 316 317 318 | Popular Tags |