DebuggerConnector.java revision e59c27340904879f204f4a702bf91b9554e0f8e2
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.ide.eclipse.pdt.internal; 18 19import com.android.ide.eclipse.ddms.IDebuggerConnector; 20 21import org.eclipse.core.resources.IProject; 22import org.eclipse.core.runtime.CoreException; 23import org.eclipse.debug.core.DebugPlugin; 24import org.eclipse.debug.core.ILaunchConfiguration; 25import org.eclipse.debug.core.ILaunchConfigurationType; 26import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 27import org.eclipse.debug.core.ILaunchManager; 28import org.eclipse.debug.ui.DebugUITools; 29import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 30 31import java.util.HashMap; 32import java.util.Map; 33 34/** 35 * Implementation of the com.android.ide.ddms.debuggerConnector extension point. 36 */ 37public class DebuggerConnector extends DevTreeProjectProvider implements IDebuggerConnector { 38 39 private final static String ATTR_CONNECT_MAP_PORT = "port"; //$NON-NLS-1$ 40 private final static String ATTR_CONNECT_MAP_HOSTNAME = "hostname"; //$NON-NLS-1$ 41 42 public boolean connectDebugger(String appName, int appPort, int selectedPort) { 43 IProject project = getProject(); 44 45 if (project != null) { 46 // get the launch manager 47 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); 48 49 // get the config for the remote launch config. 50 ILaunchConfigurationType configType = manager.getLaunchConfigurationType( 51 IJavaLaunchConfigurationConstants.ID_REMOTE_JAVA_APPLICATION); 52 53 String projectName = project.getName(); 54 55 // look for an existing launch config 56 ILaunchConfiguration config = findConfig(manager, configType, projectName, 57 selectedPort); 58 59 if (config == null) { 60 // Didn't find a matching config, so we make one. 61 // It'll be made in the "working copy" object first. 62 ILaunchConfigurationWorkingCopy wc = null; 63 64 try { 65 // make the working copy object with a unique name 66 wc = configType.newInstance(null, 67 manager.generateUniqueLaunchConfigurationNameFrom(projectName)); 68 69 // set the project name 70 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, 71 projectName); 72 73 // set the connect map info 74 Map<String, String> connectMap = new HashMap<String, String>(); 75 connectMap.put(ATTR_CONNECT_MAP_PORT, Integer.toString(selectedPort)); 76 connectMap.put(ATTR_CONNECT_MAP_HOSTNAME, "localhost"); //$NON-NLS-1$ 77 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, connectMap); 78 79 // set the VM connector ID 80 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_CONNECTOR, 81 IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR); 82 83 // save the working copy to get the launch config object which we return. 84 config = wc.doSave(); 85 86 } catch (CoreException e) { 87 88 } 89 90 } 91 92 if (config != null) { 93 DebugUITools.launch(config, ILaunchManager.DEBUG_MODE); 94 return true; 95 } 96 } 97 98 return false; 99 } 100 101 /** 102 * Looks for and returns an existing {@link ILaunchConfiguration} object for a 103 * specified project and connection port. 104 * @param manager The {@link ILaunchManager}. 105 * @param type The {@link ILaunchConfigurationType}. 106 * @param projectName The name of the project 107 * @param connectionPort the remote connection port. 108 * @return an existing <code>ILaunchConfiguration</code> object matching the project, or 109 * <code>null</code>. 110 */ 111 private static ILaunchConfiguration findConfig(ILaunchManager manager, 112 ILaunchConfigurationType type, 113 String projectName, int connectionPort) { 114 try { 115 ILaunchConfiguration[] configs = manager.getLaunchConfigurations(type); 116 117 // look for one set up for the project with a debug equal to the selected debug port. 118 for (ILaunchConfiguration config : configs) { 119 120 Map<?, ?> attributes = config.getAttributes(); 121 122 String name = (String) attributes.get( 123 IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME); 124 125 if (name == null || name.equals(projectName) == false) { 126 continue; 127 } 128 129 Map<?, ?> connectMap = (Map<?, ?>) attributes.get( 130 IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP); 131 132 if (connectMap != null) { 133 String portStr = (String) connectMap.get(ATTR_CONNECT_MAP_PORT); 134 if (portStr != null) { 135 Integer port = Integer.valueOf(portStr); 136 if (connectionPort == port) { 137 return config; 138 } 139 } 140 } 141 142 } 143 } catch (CoreException e) { 144 } 145 146 // didn't find anything that matches. Return null 147 return null; 148 } 149} 150