KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > bdi > ChildSession


1 /*
2  * @(#)ChildSession.java 1.15 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

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 JavaDoc;
44 import javax.swing.SwingUtilities JavaDoc;
45
46
47 class ChildSession extends Session {
48
49     private Process JavaDoc 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 JavaDoc userVMArgs, String JavaDoc 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 JavaDoc 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     /*
103      * Create a Thread that will retrieve and display any output.
104      * Needs to be high priority, else debugger may exit before
105      * it can be displayed.
106      */

107
108     //### Rename InputWriter and OutputReader classes
109
//### Thread priorities cribbed from ttydebug. Think about them.
110

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     //### debug
137
//System.out.println("IO after attach: "+ inputWriter + " " + outputReader + " "+ errorReader);
138

139     return true;
140     }
141
142     public void detach() {
143
144     //### debug
145
//System.out.println("IO before detach: "+ inputWriter + " " + outputReader + " "+ errorReader);
146

147     super.detach();
148
149     /*
150     inputWriter.quit();
151     outputReader.quit();
152     errorReader.quit();
153     */

154
155         if (process != null) {
156             process.destroy();
157             process = null;
158         }
159
160     }
161
162     /**
163      * Launch child java interpreter, return host:port
164      */

165
166     static private void dumpStream(OutputListener diagnostics,
167                    InputStream stream) throws IOException {
168         BufferedReader in =
169             new BufferedReader(new InputStreamReader(stream));
170         String JavaDoc line;
171         while ((line = in.readLine()) != null) {
172             diagnostics.putString(line);
173         }
174     }
175
176     static private void dumpFailedLaunchInfo(OutputListener diagnostics,
177                          Process JavaDoc 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 JavaDoc userVMArgs,
189                     String JavaDoc cmdLine) {
190         VirtualMachineManager manager = Bootstrap.virtualMachineManager();
191         LaunchingConnector connector = manager.defaultConnector();
192         Map JavaDoc 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 JavaDoc 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         //### Note small buffer sizes!
223
out = new BufferedReader(new InputStreamReader(process.getInputStream()), 1);
224         err = new BufferedReader(new InputStreamReader(process.getErrorStream()), 1);
225         return true;
226     }
227
228     /**
229      * Threads to handle application input/output.
230      */

231
232     private static class OutputReader extends Thread JavaDoc {
233
234     private String JavaDoc 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 JavaDoc threadName,
242              String JavaDoc 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             // Run in Swing event dispatcher thread.
263
final String JavaDoc chars = new String JavaDoc(buffer, 0, count);
264             SwingUtilities.invokeLater(new Runnable JavaDoc() {
265                 public void run() {
266                 output.putString(chars);
267                 }
268             });
269             }
270             //### Should we sleep briefly here?
271
}
272         } catch (IOException e) {
273         // Run in Swing event dispatcher thread.
274
SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc {
286
287     private PrintWriter stream;
288     private InputListener input;
289     private boolean running = true;
290
291     InputWriter(String JavaDoc 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         //### Won't have much effect if blocked on input!
301
running = false;
302     }
303
304     public void run() {
305         String JavaDoc line;
306         while (running) {
307         line = input.getLine();
308         stream.println(line);
309         // Should not be needed for println above!
310
stream.flush();
311         }
312     }
313     }
314     
315 }
316
317
318
Popular Tags