KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > SocketAttachConnector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.launching;
12
13
14 import java.io.IOException JavaDoc;
15 import java.net.ConnectException JavaDoc;
16 import java.net.UnknownHostException JavaDoc;
17 import com.ibm.icu.text.MessageFormat;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.NullProgressMonitor;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.SubProgressMonitor;
29 import org.eclipse.debug.core.ILaunch;
30 import org.eclipse.debug.core.ILaunchConfiguration;
31 import org.eclipse.debug.core.model.IDebugTarget;
32 import org.eclipse.jdi.Bootstrap;
33 import org.eclipse.jdi.TimeoutException;
34 import org.eclipse.jdt.debug.core.JDIDebugModel;
35 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
36 import org.eclipse.jdt.launching.IVMConnector;
37
38 import com.sun.jdi.VMDisconnectedException;
39 import com.sun.jdi.VirtualMachine;
40 import com.sun.jdi.connect.AttachingConnector;
41 import com.sun.jdi.connect.Connector;
42 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
43
44 /**
45  * A standard socket attaching connector
46  */

47 public class SocketAttachConnector implements IVMConnector {
48         
49     /**
50      * Return the socket transport attaching connector
51      *
52      * @exception CoreException if unable to locate the connector
53      */

54     protected static AttachingConnector getAttachingConnector() throws CoreException {
55         AttachingConnector connector= null;
56         Iterator JavaDoc iter= Bootstrap.virtualMachineManager().attachingConnectors().iterator();
57         while (iter.hasNext()) {
58             AttachingConnector lc= (AttachingConnector) iter.next();
59             if (lc.name().equals("com.sun.jdi.SocketAttach")) { //$NON-NLS-1$
60
connector= lc;
61                 break;
62             }
63         }
64         if (connector == null) {
65             abort(LaunchingMessages.SocketAttachConnector_Socket_attaching_connector_not_available_3, null, IJavaLaunchConfigurationConstants.ERR_SHARED_MEMORY_CONNECTOR_UNAVAILABLE);
66         }
67         return connector;
68     }
69
70     /**
71      * @see IVMConnector#getIdentifier()
72      */

73     public String JavaDoc getIdentifier() {
74         return IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR;
75     }
76
77     /**
78      * @see IVMConnector#getName()
79      */

80     public String JavaDoc getName() {
81         return LaunchingMessages.SocketAttachConnector_Standard__Socket_Attach__4;
82     }
83     
84     /**
85      * Throws a core exception with an error status object built from
86      * the given message, lower level exception, and error code.
87      *
88      * @param message the status message
89      * @param exception lower level exception associated with the
90      * error, or <code>null</code> if none
91      * @param code error code
92      */

93     protected static void abort(String JavaDoc message, Throwable JavaDoc exception, int code) throws CoreException {
94         throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), code, message, exception));
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.jdt.launching.IVMConnector#connect(java.util.Map, org.eclipse.core.runtime.IProgressMonitor, org.eclipse.debug.core.ILaunch)
99      */

100     public void connect(Map JavaDoc arguments, IProgressMonitor monitor, ILaunch launch) throws CoreException {
101         if (monitor == null) {
102             monitor = new NullProgressMonitor();
103         }
104         
105         IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
106         subMonitor.beginTask(LaunchingMessages.SocketAttachConnector_Connecting____1, 2);
107         subMonitor.subTask(LaunchingMessages.SocketAttachConnector_Configuring_connection____1);
108         
109         AttachingConnector connector= getAttachingConnector();
110         String JavaDoc portNumberString = (String JavaDoc)arguments.get("port"); //$NON-NLS-1$
111
if (portNumberString == null) {
112             abort(LaunchingMessages.SocketAttachConnector_Port_unspecified_for_remote_connection__2, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_PORT);
113         }
114         String JavaDoc host = (String JavaDoc)arguments.get("hostname"); //$NON-NLS-1$
115
if (host == null) {
116             abort(LaunchingMessages.SocketAttachConnector_Hostname_unspecified_for_remote_connection__4, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_HOSTNAME);
117         }
118         Map JavaDoc map= connector.defaultArguments();
119         
120         Connector.Argument param= (Connector.Argument) map.get("hostname"); //$NON-NLS-1$
121
param.setValue(host);
122         param= (Connector.Argument) map.get("port"); //$NON-NLS-1$
123
param.setValue(portNumberString);
124         
125         String JavaDoc timeoutString = (String JavaDoc)arguments.get("timeout"); //$NON-NLS-1$
126
if (timeoutString != null) {
127             param= (Connector.Argument) map.get("timeout"); //$NON-NLS-1$
128
param.setValue(timeoutString);
129         }
130         
131         ILaunchConfiguration configuration = launch.getLaunchConfiguration();
132         boolean allowTerminate = false;
133         if (configuration != null) {
134             allowTerminate = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_ALLOW_TERMINATE, false);
135         }
136         subMonitor.worked(1);
137         subMonitor.subTask(LaunchingMessages.SocketAttachConnector_Establishing_connection____2);
138         try {
139             VirtualMachine vm = connector.attach(map);
140             String JavaDoc vmLabel = constructVMLabel(vm, host, portNumberString, configuration);
141             IDebugTarget debugTarget= JDIDebugModel.newDebugTarget(launch, vm, vmLabel, null, allowTerminate, true);
142             launch.addDebugTarget(debugTarget);
143             subMonitor.worked(1);
144             subMonitor.done();
145         } catch (TimeoutException e) {
146             abort(LaunchingMessages.SocketAttachConnector_0, e, IJavaLaunchConfigurationConstants.ERR_REMOTE_VM_CONNECTION_FAILED);
147         } catch (UnknownHostException JavaDoc e) {
148             abort(MessageFormat.format(LaunchingMessages.SocketAttachConnector_Failed_to_connect_to_remote_VM_because_of_unknown_host____0___1, new String JavaDoc[]{host}), e, IJavaLaunchConfigurationConstants.ERR_REMOTE_VM_CONNECTION_FAILED);
149         } catch (ConnectException JavaDoc e) {
150             abort(LaunchingMessages.SocketAttachConnector_Failed_to_connect_to_remote_VM_as_connection_was_refused_2, e, IJavaLaunchConfigurationConstants.ERR_REMOTE_VM_CONNECTION_FAILED);
151         } catch (IOException JavaDoc e) {
152             abort(LaunchingMessages.SocketAttachConnector_Failed_to_connect_to_remote_VM_1, e, IJavaLaunchConfigurationConstants.ERR_REMOTE_VM_CONNECTION_FAILED);
153         } catch (IllegalConnectorArgumentsException e) {
154             abort(LaunchingMessages.SocketAttachConnector_Failed_to_connect_to_remote_VM_1, e, IJavaLaunchConfigurationConstants.ERR_REMOTE_VM_CONNECTION_FAILED);
155         }
156     }
157
158     /**
159      * Helper method that constructs a human-readable label for a remote VM.
160      */

161     protected String JavaDoc constructVMLabel(VirtualMachine vm, String JavaDoc host, String JavaDoc port, ILaunchConfiguration configuration) {
162         String JavaDoc name = null;
163         try {
164             name = vm.name();
165         } catch (TimeoutException e) {
166             // do nothing
167
} catch (VMDisconnectedException e) {
168             // do nothing
169
}
170         if (name == null) {
171             if (configuration == null) {
172                 name = ""; //$NON-NLS-1$
173
} else {
174                 name = configuration.getName();
175             }
176         }
177         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(name);
178         buffer.append('[');
179         buffer.append(host);
180         buffer.append(':');
181         buffer.append(port);
182         buffer.append(']');
183         return buffer.toString();
184     }
185         
186
187     /**
188      * @see IVMConnector#getDefaultArguments()
189      */

190     public Map JavaDoc getDefaultArguments() throws CoreException {
191         Map JavaDoc def = getAttachingConnector().defaultArguments();
192         Connector.IntegerArgument arg = (Connector.IntegerArgument)def.get("port"); //$NON-NLS-1$
193
arg.setValue(8000);
194         return def;
195     }
196
197     /**
198      * @see IVMConnector#getArgumentOrder()
199      */

200     public List JavaDoc getArgumentOrder() {
201         List JavaDoc list = new ArrayList JavaDoc(2);
202         list.add("hostname"); //$NON-NLS-1$
203
list.add("port"); //$NON-NLS-1$
204
return list;
205     }
206
207 }
208
Popular Tags