KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > jdb > VMConnection


1 /*
2  * VMConnection.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: VMConnection.java,v 1.4 2003/05/18 01:27:55 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.jdb;
23
24 import com.sun.jdi.Bootstrap;
25 import com.sun.jdi.VMDisconnectedException;
26 import com.sun.jdi.VirtualMachine;
27 import com.sun.jdi.VirtualMachineManager;
28 import com.sun.jdi.connect.Connector.Argument;
29 import com.sun.jdi.connect.Connector;
30 import com.sun.jdi.connect.LaunchingConnector;
31 import java.util.Map JavaDoc;
32 import org.armedbear.j.Debug;
33 import org.armedbear.j.FastStringBuffer;
34 import org.armedbear.j.Log;
35
36 public final class VMConnection
37 {
38     private final Connector connector;
39     private final Map JavaDoc map;
40
41     public VMConnection(Connector connector, Map JavaDoc map)
42     {
43         this.connector = connector;
44         this.map = map;
45     }
46
47     public static VMConnection getConnection(Jdb jdb)
48     {
49         VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
50         LaunchingConnector connector = vmm.defaultConnector();
51         Map JavaDoc map = connector.defaultArguments();
52         String JavaDoc javaHome = jdb.getJavaHome();
53         ((Connector.Argument)map.get("home")).setValue(javaHome);
54         String JavaDoc javaExecutable = jdb.getJavaExecutable();
55         ((Connector.Argument)map.get("vmexec")).setValue(javaExecutable);
56
57         // Command line.
58
FastStringBuffer sb = new FastStringBuffer(jdb.getMainClass());
59         String JavaDoc mainClassArgs = jdb.getMainClassArgs();
60         if (mainClassArgs != null && mainClassArgs.length() > 0) {
61             sb.append(' ');
62             sb.append(mainClassArgs);
63         }
64         ((Connector.Argument)map.get("main")).setValue(sb.toString());
65
66         // CLASSPATH and VM options.
67
sb.setLength(0);
68         String JavaDoc vmArgs = jdb.getVMArgs();
69         if (vmArgs != null) {
70             vmArgs = vmArgs.trim();
71             if (vmArgs.length() > 0) {
72                 sb.append(vmArgs);
73                 sb.append(' ');
74             }
75         }
76         String JavaDoc classPath = jdb.getClassPath();
77         if (classPath != null) {
78             classPath = classPath.trim();
79             if (classPath.length() > 0) {
80                 sb.append("-classpath ");
81                 sb.append(classPath);
82             }
83         }
84         ((Connector.Argument)map.get("options")).setValue(sb.toString());
85
86         ((Connector.Argument)map.get("suspend")).setValue("true");
87         return new VMConnection(connector, map);
88     }
89
90     public VirtualMachine open(Jdb jdb)
91     {
92         if (connector instanceof LaunchingConnector)
93             return launchTarget(jdb);
94         // Otherwise...
95
Debug.bug();
96         return null;
97     }
98
99     private VirtualMachine launchTarget(Jdb jdb)
100     {
101         VirtualMachine vm = null;
102         try {
103             vm = ((LaunchingConnector)connector).launch(map);
104         }
105         catch (VMDisconnectedException disconnected) {
106             return null;
107         }
108         catch (Exception JavaDoc e) {
109             Log.error(e);
110             return null;
111         }
112         Process JavaDoc process = vm.process();
113         jdb.displayRemoteOutput(process.getErrorStream());
114         jdb.displayRemoteOutput(process.getInputStream());
115         vm.suspend();
116         jdb.setSuspended(true);
117         jdb.setVM(vm);
118         new EventHandler(jdb);
119         jdb.fireContextChanged();
120         return vm;
121     }
122 }
123
Popular Tags