001: /*
002: * VMConnection.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: VMConnection.java,v 1.4 2003/05/18 01:27:55 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.jdb;
023:
024: import com.sun.jdi.Bootstrap;
025: import com.sun.jdi.VMDisconnectedException;
026: import com.sun.jdi.VirtualMachine;
027: import com.sun.jdi.VirtualMachineManager;
028: import com.sun.jdi.connect.Connector.Argument;
029: import com.sun.jdi.connect.Connector;
030: import com.sun.jdi.connect.LaunchingConnector;
031: import java.util.Map;
032: import org.armedbear.j.Debug;
033: import org.armedbear.j.FastStringBuffer;
034: import org.armedbear.j.Log;
035:
036: public final class VMConnection {
037: private final Connector connector;
038: private final Map map;
039:
040: public VMConnection(Connector connector, Map map) {
041: this .connector = connector;
042: this .map = map;
043: }
044:
045: public static VMConnection getConnection(Jdb jdb) {
046: VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
047: LaunchingConnector connector = vmm.defaultConnector();
048: Map map = connector.defaultArguments();
049: String javaHome = jdb.getJavaHome();
050: ((Connector.Argument) map.get("home")).setValue(javaHome);
051: String javaExecutable = jdb.getJavaExecutable();
052: ((Connector.Argument) map.get("vmexec"))
053: .setValue(javaExecutable);
054:
055: // Command line.
056: FastStringBuffer sb = new FastStringBuffer(jdb.getMainClass());
057: String mainClassArgs = jdb.getMainClassArgs();
058: if (mainClassArgs != null && mainClassArgs.length() > 0) {
059: sb.append(' ');
060: sb.append(mainClassArgs);
061: }
062: ((Connector.Argument) map.get("main")).setValue(sb.toString());
063:
064: // CLASSPATH and VM options.
065: sb.setLength(0);
066: String vmArgs = jdb.getVMArgs();
067: if (vmArgs != null) {
068: vmArgs = vmArgs.trim();
069: if (vmArgs.length() > 0) {
070: sb.append(vmArgs);
071: sb.append(' ');
072: }
073: }
074: String classPath = jdb.getClassPath();
075: if (classPath != null) {
076: classPath = classPath.trim();
077: if (classPath.length() > 0) {
078: sb.append("-classpath ");
079: sb.append(classPath);
080: }
081: }
082: ((Connector.Argument) map.get("options")).setValue(sb
083: .toString());
084:
085: ((Connector.Argument) map.get("suspend")).setValue("true");
086: return new VMConnection(connector, map);
087: }
088:
089: public VirtualMachine open(Jdb jdb) {
090: if (connector instanceof LaunchingConnector)
091: return launchTarget(jdb);
092: // Otherwise...
093: Debug.bug();
094: return null;
095: }
096:
097: private VirtualMachine launchTarget(Jdb jdb) {
098: VirtualMachine vm = null;
099: try {
100: vm = ((LaunchingConnector) connector).launch(map);
101: } catch (VMDisconnectedException disconnected) {
102: return null;
103: } catch (Exception e) {
104: Log.error(e);
105: return null;
106: }
107: Process process = vm.process();
108: jdb.displayRemoteOutput(process.getErrorStream());
109: jdb.displayRemoteOutput(process.getInputStream());
110: vm.suspend();
111: jdb.setSuspended(true);
112: jdb.setVM(vm);
113: new EventHandler(jdb);
114: jdb.fireContextChanged();
115: return vm;
116: }
117: }
|