1 /*
2 * $Id: JdiRemoteVMConnector.java,v 1.5 2003/08/06 20:50:42 trondandersen Exp $
3 * Copyright 2003 OErjan Nygaard Austvold. All rights reserved.
4 */
5
6 package org.jdiseq;
7
8 import com.sun.jdi.*;
9 import com.sun.jdi.connect.AttachingConnector;
10 import com.sun.jdi.connect.Connector;
11 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
12
13 import java.util.List;
14 import java.util.Iterator;
15 import java.util.Map;
16 import java.io.IOException;
17
18 /***
19 * This class implements a connector to a remote Java virtual machine.
20 * <p>Changelog:
21 * <pre>
22 * $Log: JdiRemoteVMConnector.java,v $
23 * Revision 1.5 2003/08/06 20:50:42 trondandersen
24 * Added properties
25 *
26 * Revision 1.4 2003/07/22 00:07:22 trondandersen
27 * Removed Jython test cases
28 * .
29 *
30 * Revision 1.3 2003/05/05 17:39:04 trondandersen
31 * Inserted initial configuration class and SettingUtil retriever.
32 * Added jython test for these classes. Inserted a logging class.
33 * .
34 *
35 * Revision 1.2 2003/04/02 19:48:50 austvold
36 * Minor updates. Mostly adaptation to new event and eventlistener name.
37 * General brush-up on javadoc comments.
38 * Removal of auto-added revision and date-of-commit members.
39 *
40 * Revision 1.1.1.1 2003/03/28 08:18:23 austvold
41 * Initial import.
42 *
43 * </pre>
44 *
45 * @author Ørjan Nygaard Austvold <austvold@acm.org>
46 * @version $Id: JdiRemoteVMConnector.java,v 1.5 2003/08/06 20:50:42 trondandersen Exp $
47 */
48 class JdiRemoteVMConnector {
49 private String host;
50 private String port;
51 private boolean startSuspended;
52 private VirtualMachine vm;
53 private AttachingConnector connector;
54
55
56 /***
57 *
58 * @param host
59 * @param port
60 * @param startSuspended
61 * @throws JdiConnectionException
62 */
63 public JdiRemoteVMConnector(String host, String port, boolean startSuspended) throws JdiConnectionException {
64 this.host = host;
65 this.port = port;
66 this.startSuspended = startSuspended;
67
68 connector = createAttachingConnector();
69 if (connector == null) {
70 throw new JdiConnectionException("No socket connectors found.");
71 }
72 }
73
74
75 public void connect() throws JdiConnectionException {
76 Map connectArgs = connector.defaultArguments();
77 ((Connector.Argument) connectArgs.get("hostname")).setValue(host);
78 ((Connector.Argument) connectArgs.get("port")).setValue(port);
79
80 // attach to the remote running vm
81 try {
82 vm = connector.attach(connectArgs);
83 } catch (IOException e) {
84 throw new JdiConnectionException("Failed to connect.", e);
85 } catch (IllegalConnectorArgumentsException e) {
86 System.err.println("Bad programmer -- Bad!");
87 e.printStackTrace();
88 System.exit(1);
89 }
90
91 /*
92 if (startSuspended) {
93 vm.suspend();
94 } else {
95 // the remote vm might be started with 'suspend=y'
96 vm.resume();
97 }
98
99 */
100 // hack
101 // vm.suspend();
102 }
103
104
105 public void disconnect() throws JdiConnectionException {
106 if (vm == null) {
107 throw new JdiConnectionException("Not connected.");
108 }
109 vm.dispose();
110 vm = null;
111 }
112
113
114 public void suspendRemoteVM() {
115 vm.suspend();
116 }
117
118
119 public void resumeRemoteVM() {
120 vm.resume();
121 }
122
123
124 public VirtualMachine getVm() {
125 return vm;
126 }
127
128
129 protected AttachingConnector createAttachingConnector() {
130 // get the VM manager and request attaching connectors
131 VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
132 List connectors = vmm.attachingConnectors();
133
134 // Find an attaching connector that uses 'dt_socket'
135 AttachingConnector c = null;
136 Iterator iter = connectors.iterator();
137 while (iter.hasNext()) {
138 Connector conn = (Connector) iter.next();
139 if (conn.transport().name().equals("dt_socket")) {
140 c = (AttachingConnector) conn;
141 break;
142 }
143 }
144 return c;
145 }
146
147 public boolean isConnected() {
148 return vm != null;
149 }
150 }
This page was automatically generated by Maven