1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import java.io.IOException ; 15 import java.net.ConnectException ; 16 import java.net.UnknownHostException ; 17 import com.ibm.icu.text.MessageFormat; 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 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 47 public class SocketAttachConnector implements IVMConnector { 48 49 54 protected static AttachingConnector getAttachingConnector() throws CoreException { 55 AttachingConnector connector= null; 56 Iterator iter= Bootstrap.virtualMachineManager().attachingConnectors().iterator(); 57 while (iter.hasNext()) { 58 AttachingConnector lc= (AttachingConnector) iter.next(); 59 if (lc.name().equals("com.sun.jdi.SocketAttach")) { 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 73 public String getIdentifier() { 74 return IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR; 75 } 76 77 80 public String getName() { 81 return LaunchingMessages.SocketAttachConnector_Standard__Socket_Attach__4; 82 } 83 84 93 protected static void abort(String message, Throwable exception, int code) throws CoreException { 94 throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), code, message, exception)); 95 } 96 97 100 public void connect(Map 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 portNumberString = (String )arguments.get("port"); if (portNumberString == null) { 112 abort(LaunchingMessages.SocketAttachConnector_Port_unspecified_for_remote_connection__2, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_PORT); 113 } 114 String host = (String )arguments.get("hostname"); if (host == null) { 116 abort(LaunchingMessages.SocketAttachConnector_Hostname_unspecified_for_remote_connection__4, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_HOSTNAME); 117 } 118 Map map= connector.defaultArguments(); 119 120 Connector.Argument param= (Connector.Argument) map.get("hostname"); param.setValue(host); 122 param= (Connector.Argument) map.get("port"); param.setValue(portNumberString); 124 125 String timeoutString = (String )arguments.get("timeout"); if (timeoutString != null) { 127 param= (Connector.Argument) map.get("timeout"); 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 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 e) { 148 abort(MessageFormat.format(LaunchingMessages.SocketAttachConnector_Failed_to_connect_to_remote_VM_because_of_unknown_host____0___1, new String []{host}), e, IJavaLaunchConfigurationConstants.ERR_REMOTE_VM_CONNECTION_FAILED); 149 } catch (ConnectException 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 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 161 protected String constructVMLabel(VirtualMachine vm, String host, String port, ILaunchConfiguration configuration) { 162 String name = null; 163 try { 164 name = vm.name(); 165 } catch (TimeoutException e) { 166 } catch (VMDisconnectedException e) { 168 } 170 if (name == null) { 171 if (configuration == null) { 172 name = ""; } else { 174 name = configuration.getName(); 175 } 176 } 177 StringBuffer buffer = new StringBuffer (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 190 public Map getDefaultArguments() throws CoreException { 191 Map def = getAttachingConnector().defaultArguments(); 192 Connector.IntegerArgument arg = (Connector.IntegerArgument)def.get("port"); arg.setValue(8000); 194 return def; 195 } 196 197 200 public List getArgumentOrder() { 201 List list = new ArrayList (2); 202 list.add("hostname"); list.add("port"); return list; 205 } 206 207 } 208 | Popular Tags |