Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5c7de0599b32b520d6304510b5ad63ade446a4d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*******************************************************************************
 * Copyright (c) 2011, 2013 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.launch.ui.internal.services;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchListener;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchesListener;
import org.eclipse.debug.core.model.IDisconnect;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.launch.core.lm.LaunchManager;
import org.eclipse.tcf.te.launch.core.lm.interfaces.ILaunchManagerDelegate;
import org.eclipse.tcf.te.launch.core.lm.interfaces.ILaunchSpecification;
import org.eclipse.tcf.te.launch.core.persistence.launchcontext.LaunchContextsPersistenceDelegate;
import org.eclipse.tcf.te.launch.core.selection.LaunchSelection;
import org.eclipse.tcf.te.launch.core.selection.RemoteSelectionContext;
import org.eclipse.tcf.te.launch.core.selection.interfaces.ILaunchSelection;
import org.eclipse.tcf.te.runtime.concurrent.util.ExecutorsUtil;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.model.interfaces.IModelNode;
import org.eclipse.tcf.te.runtime.services.AbstractService;
import org.eclipse.tcf.te.runtime.services.interfaces.IDebugService;
import org.eclipse.tcf.te.runtime.utils.StatusHelper;
import org.eclipse.tcf.te.tcf.launch.core.delegates.Launch;
import org.eclipse.tcf.te.tcf.launch.core.interfaces.ILaunchTypes;
import org.eclipse.tcf.te.ui.swt.DisplayUtil;

/**
 * Debug service implementations for TCF contexts.
 */
public class DebugService extends AbstractService implements IDebugService {
	// Reference to the launches listener
	private final ILaunchesListener listener;

	/**
     * Constructor
     */
    public DebugService() {
    	super();

    	// Create and register the launches listener instance
    	listener = new DebugServicesLaunchesListener();
		DebugPlugin.getDefault().getLaunchManager().addLaunchListener(listener);
    }

    /* (non-Javadoc)
     * @see org.eclipse.tcf.te.runtime.services.interfaces.IDebugService#attach(java.lang.Object, org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer, org.eclipse.core.runtime.IProgressMonitor, org.eclipse.tcf.te.runtime.interfaces.callback.ICallback)
     */
    @Override
    public void attach(final Object context, final IPropertiesContainer data, final IProgressMonitor monitor, final ICallback callback) {
		if (!Protocol.isDispatchThread()) {
			internalAttach(context, data, monitor, callback);
		}
		else {
			ExecutorsUtil.execute(new Runnable() {
				@Override
				public void run() {
					internalAttach(context, data, monitor, callback);
				}
			});
		}
	}

	@SuppressWarnings("restriction")
    protected void internalAttach(final Object context, final IPropertiesContainer data, final IProgressMonitor monitor, final ICallback callback) {
		Assert.isTrue(!Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		Assert.isNotNull(context);
		Assert.isNotNull(data);
		Assert.isNotNull(callback);

		if (context instanceof IModelNode) {
			ILaunchConfigurationType launchConfigType =	DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(ILaunchTypes.ATTACH);
			try {
				ILaunchSelection launchSelection = new LaunchSelection(ILaunchManager.DEBUG_MODE, new RemoteSelectionContext((IModelNode)context, true));
				ILaunchManagerDelegate delegate = LaunchManager.getInstance().getLaunchManagerDelegate(launchConfigType, ILaunchManager.DEBUG_MODE);
				if (delegate != null) {
					// create an empty launch configuration specification to initialize all attributes with their default defaults.
					ILaunchSpecification launchSpec = delegate.getLaunchSpecification(launchConfigType.getIdentifier(), launchSelection);
					for (String key : data.getProperties().keySet()) {
						launchSpec.addAttribute(key, data.getProperty(key));
					}
					delegate.validate(launchSpec);
					if (launchSpec != null && launchSpec.isValid()) {
						ILaunchConfiguration[] launchConfigs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(launchConfigType);
						launchConfigs = delegate.getMatchingLaunchConfigurations(launchSpec, launchConfigs);

						ILaunchConfiguration config = launchConfigs != null && launchConfigs.length > 0 ? launchConfigs[0] : null;

						boolean skip = false;
						ILaunch activeLaunch = null;
						if (config != null) {
							ILaunch[] launches = DebugPlugin.getDefault().getLaunchManager().getLaunches();
							for (ILaunch launch : launches) {
								if (launch.getLaunchConfiguration().getType().getIdentifier().equals(ILaunchTypes.ATTACH) && !launch.isTerminated()) {
									IModelNode[] contexts = LaunchContextsPersistenceDelegate.getLaunchContexts(launch.getLaunchConfiguration());
									if (contexts != null && contexts.length == 1 && contexts[0].equals(context)) {
										callback.setProperty("launch", launch); //$NON-NLS-1$
										activeLaunch = launch;
										skip = true;
										break;
									}
								}
							}
						}

						if (!skip) {
							final ILaunchConfiguration finConfig = LaunchManager.getInstance().createOrUpdateLaunchConfiguration(config, launchSpec);

							delegate.validate(ILaunchManager.DEBUG_MODE, finConfig);

							DebugPlugin.getDefault().getLaunchManager().addLaunchListener(new ILaunchListener() {
								@Override
								public void launchAdded(ILaunch launch) {
									if (launch != null && finConfig.equals(launch.getLaunchConfiguration())) {
										DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(this);
										callback.setProperty("launch", launch); //$NON-NLS-1$
										if (launch instanceof Launch) {
											((Launch)launch).setCallback(callback);
										}
										else {
											callback.done(this, Status.OK_STATUS);
										}
									}
								}

								@Override
								public void launchChanged(ILaunch launch) {
								}

								@Override
								public void launchRemoved(ILaunch launch) {
								}
							});

							// DebugUITools.launch(...) must be called from within the UI thread.
							DisplayUtil.safeAsyncExec(new Runnable() {
								@Override
								public void run() {
									DebugUITools.launch(finConfig, ILaunchManager.DEBUG_MODE);
								}
							});
						} else {
							org.eclipse.debug.internal.ui.DebugUIPlugin.getDefault().getPerspectiveManager().launchAdded(activeLaunch);
							callback.done(this, Status.OK_STATUS);
						}
					}
				}
			}
			catch (Exception e) {
				callback.done(this, StatusHelper.getStatus(e));
			}
		}
		else {
			callback.done(this, Status.OK_STATUS);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.services.interfaces.IDebugService#detach(java.lang.Object, org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer, org.eclipse.core.runtime.IProgressMonitor, org.eclipse.tcf.te.runtime.interfaces.callback.ICallback)
	 */
	@Override
	public void detach(Object context, IPropertiesContainer data, final IProgressMonitor monitor, ICallback callback) {
		Assert.isNotNull(context);
		Assert.isNotNull(data);
		Assert.isNotNull(callback);

		if (context instanceof IModelNode) {
			ILaunch[] launches = DebugPlugin.getDefault().getLaunchManager().getLaunches();
			for (ILaunch launch : launches) {
				try {
					if (launch.getLaunchConfiguration().getType().getIdentifier().equals(ILaunchTypes.ATTACH) && launch instanceof IDisconnect && !((IDisconnect)launch).isDisconnected()) {
						IModelNode[] contexts = LaunchContextsPersistenceDelegate.getLaunchContexts(launch.getLaunchConfiguration());
						if (contexts != null && contexts.length == 1 && contexts[0].equals(context)) {
							((IDisconnect)launch).disconnect();
						}
					}
				} catch (Exception e) {
					callback.done(this, StatusHelper.getStatus(e));
					return;
				}
			}
		}
		callback.done(this, Status.OK_STATUS);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.services.interfaces.IDebugService#isLaunched(java.lang.Object)
	 */
	@Override
	public boolean isLaunched(Object context) {
		Assert.isNotNull(context);

		boolean isLaunched = false;

		if (context instanceof IModelNode) {
			ILaunch[] launches = DebugPlugin.getDefault().getLaunchManager().getLaunches();
			for (ILaunch launch : launches) {
				try {
					if (launch.getLaunchConfiguration().getType().getIdentifier().equals(ILaunchTypes.ATTACH) && !launch.isTerminated()) {
						IModelNode[] contexts = LaunchContextsPersistenceDelegate.getLaunchContexts(launch.getLaunchConfiguration());
						if (contexts != null && contexts.length == 1 && contexts[0].equals(context)) {
							isLaunched = true;
							break;
						}
					}
				} catch (CoreException e) {
					if (Platform.inDebugMode()) e.printStackTrace();
				}
			}
		}

	    return isLaunched;
	}
}

Back to the top