DebuggerConnector.java revision 4f830e0754e925eb33bae188ade858d10dd9f2d3
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 isWorkspaceApp(String appName) {
43        return getProject() != null;
44    }
45
46    public boolean connectDebugger(String appName, int appPort, int selectedPort) {
47        IProject project = getProject();
48
49        if (project != null) {
50            // get the launch manager
51            ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
52
53            // get the config for the remote launch config.
54            ILaunchConfigurationType configType = manager.getLaunchConfigurationType(
55                            IJavaLaunchConfigurationConstants.ID_REMOTE_JAVA_APPLICATION);
56
57            String projectName = project.getName();
58
59            // look for an existing launch config
60            ILaunchConfiguration config = findConfig(manager, configType, projectName,
61                    selectedPort);
62
63            if (config == null) {
64                // Didn't find a matching config, so we make one.
65                // It'll be made in the "working copy" object first.
66                ILaunchConfigurationWorkingCopy wc = null;
67
68                try {
69                    // make the working copy object with a unique name
70                    wc = configType.newInstance(null,
71                            manager.generateUniqueLaunchConfigurationNameFrom(projectName));
72
73                    // set the project name
74                    wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
75                            projectName);
76
77                    // set the connect map info
78                    Map<String, String> connectMap = new HashMap<String, String>();
79                    connectMap.put(ATTR_CONNECT_MAP_PORT, Integer.toString(selectedPort));
80                    connectMap.put(ATTR_CONNECT_MAP_HOSTNAME, "localhost"); //$NON-NLS-1$
81                    wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, connectMap);
82
83                    // set the VM connector ID
84                    wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_CONNECTOR,
85                            IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR);
86
87                    // save the working copy to get the launch config object which we return.
88                    config = wc.doSave();
89
90                } catch (CoreException e) {
91
92                }
93
94            }
95
96            if (config != null) {
97                DebugUITools.launch(config, ILaunchManager.DEBUG_MODE);
98                return true;
99            }
100        }
101
102        return false;
103    }
104
105    /**
106     * Looks for and returns an existing {@link ILaunchConfiguration} object for a
107     * specified project and connection port.
108     * @param manager The {@link ILaunchManager}.
109     * @param type The {@link ILaunchConfigurationType}.
110     * @param projectName The name of the project
111     * @param connectionPort the remote connection port.
112     * @return an existing <code>ILaunchConfiguration</code> object matching the project, or
113     *      <code>null</code>.
114     */
115    private static ILaunchConfiguration findConfig(ILaunchManager manager,
116            ILaunchConfigurationType type,
117            String projectName, int connectionPort) {
118        try {
119            ILaunchConfiguration[] configs = manager.getLaunchConfigurations(type);
120
121            // look for one set up for the project with a debug equal to the selected debug port.
122            for (ILaunchConfiguration config : configs) {
123
124                Map<?, ?> attributes = config.getAttributes();
125
126                String name = (String) attributes.get(
127                        IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME);
128
129                if (name == null || name.equals(projectName) == false) {
130                    continue;
131                }
132
133                Map<?, ?> connectMap = (Map<?, ?>) attributes.get(
134                        IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP);
135
136                if (connectMap != null) {
137                    String portStr = (String) connectMap.get(ATTR_CONNECT_MAP_PORT);
138                    if (portStr != null) {
139                        Integer port = Integer.valueOf(portStr);
140                        if (connectionPort == port) {
141                            return config;
142                        }
143                    }
144                }
145
146            }
147        } catch (CoreException e) {
148        }
149
150        // didn't find anything that matches. Return null
151        return null;
152    }
153}
154