aboutsummaryrefslogtreecommitdiffstats
blob: 1e737fa4ee9eea44ab7e5c075cd36122ccc0b5e6 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*******************************************************************************
 * Copyright (c) 2012 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.launch.core.delegates;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
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.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.IStatusHandler;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.te.launch.core.activator.CoreBundleActivator;
import org.eclipse.tcf.te.launch.core.bindings.LaunchConfigTypeBindingsManager;
import org.eclipse.tcf.te.launch.core.lm.interfaces.ICommonLaunchAttributes;
import org.eclipse.tcf.te.launch.core.lm.interfaces.IContextSelectorLaunchAttributes;
import org.eclipse.tcf.te.launch.core.lm.interfaces.ILaunchManagerDelegate;
import org.eclipse.tcf.te.launch.core.nls.Messages;
import org.eclipse.tcf.te.launch.core.persistence.ContextSelectorPersistenceDelegate;
import org.eclipse.tcf.te.launch.core.persistence.DefaultPersistenceDelegate;
import org.eclipse.tcf.te.runtime.concurrent.util.ExecutorsUtil;
import org.eclipse.tcf.te.runtime.interfaces.ISharedConstants;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.model.interfaces.IModelNode;
import org.eclipse.tcf.te.runtime.stepper.FullQualifiedId;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepper;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepperProperties;
import org.eclipse.tcf.te.runtime.stepper.interfaces.tracing.ITraceIds;
import org.eclipse.tcf.te.runtime.stepper.stepper.Stepper;

/**
 * Default launch configuration delegate implementation.
 * <p>
 * The launch configuration delegate implements the bridge between the native Eclipse launch
 * configuration framework and the stepper engine. The delegate is standard for all
 * launch configurations which supports extensible and modularized launching.
 * <p>
 * <b>Implementation Details</b>:<br>
 * <ul>
 * <li>The launch configuration delegate signals the completion of the launch sequence via
 * the custom {@link ILaunch} attribute {@link ICommonLaunchAttributes#ILAUNCH_ATTRIBUTE_LAUNCH_SEQUENCE_COMPLETED}.</li>
 * <li>The launch configuration delegates enforces the removal of the launch from the Eclipse
 * debug platforms launch manager if the progress monitor is set to canceled or an status with
 * severity {@link IStatus#CANCEL} had been thrown by the stepper implementation.</li>
 * <li>The launch configuration delegate creates launches of type {@link Launch}.</li>
 * </ul>
 */
public class LaunchConfigurationDelegate extends AbstractLaunchConfigurationDelegate {

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
		Assert.isNotNull(configuration);
		Assert.isNotNull(mode);
		Assert.isNotNull(launch);

		// Note: This method is typically called within a worker thread from the launch configuration framework.

		long startTime = System.currentTimeMillis();
		ILaunchConfigurationWorkingCopy launchConfig = configuration.getWorkingCopy();
		launchConfig.setAttribute(ICommonLaunchAttributes.ATTR_LAST_LAUNCHED, Long.toString(startTime));
		launchConfig.doSave();

		CoreBundleActivator.getTraceHandler().trace("LaunchConfigurationDelegate#launch: *** ENTERED" //$NON-NLS-1$
						+ " (" + configuration.getName() + ")", //$NON-NLS-1$ //$NON-NLS-2$
						0, ITraceIds.TRACE_STEPPING, IStatus.WARNING, this);
		CoreBundleActivator.getTraceHandler().trace(" [" + ISharedConstants.TIME_FORMAT.format(new Date(startTime)) + "]" //$NON-NLS-1$ //$NON-NLS-2$
						+ " ***", //$NON-NLS-1$
						0, ITraceIds.PROFILE_STEPPING, IStatus.WARNING, this);

		// Reset the attribute to tell if the launch sequence has been completed. Clients cannot
		// use ILaunch.isTerminated() as this is passing through to a possibly associated IProcess
		// object. We need to know when the launch itself has finished the job, not when the process
		// might have died.
		launch.setAttribute(ICommonLaunchAttributes.ILAUNCH_ATTRIBUTE_LAUNCH_SEQUENCE_COMPLETED, Boolean.FALSE.toString());

		// The stepper instance to be used
		IStepper stepper = new Stepper();
		IStatus status = null;

		try {
			// Get the associated stepper for this launch
			IFullQualifiedId fullQualifiedId = new FullQualifiedId(IStepper.ID_TYPE_STEPPER_ID, stepper.getId(), null);
			// Get the launch properties container
			IPropertiesContainer properties = (IPropertiesContainer)launch.getAdapter(IPropertiesContainer.class);
			Assert.isNotNull(properties);

			// Add some information to the stepper data
			properties.setProperty(IStepperProperties.PROP_NAME, launchConfig.getName());
			properties.setProperty(IStepperProperties.PROP_STEP_GROUP_ID,
							LaunchConfigTypeBindingsManager.getInstance().getStepGroupId(
											launchConfig.getType().getIdentifier(),
											launch.getLaunchMode()));

			IModelNode[] contexts = ContextSelectorPersistenceDelegate.decodeLaunchContexts(
							launchConfig.getAttribute(IContextSelectorLaunchAttributes.ATTR_LAUNCH_CONTEXTS, (String)null));
			properties.setProperty(IContextSelectorLaunchAttributes.ATTR_ACTIVE_LAUNCH_CONTEXT, contexts != null && contexts.length > 0 ? contexts[0] : null);

			// Initialize the stepper
			IStepContext context = (IStepContext)launch.getAdapter(IStepContext.class);
			stepper.initialize(context, properties, fullQualifiedId, monitor != null ? monitor : new NullProgressMonitor());

			// Execute
			stepper.execute();

			// Wait for the stepper to be finished
			ExecutorsUtil.waitAndExecute(0, new IStepper.ExecutionFinishedConditionTester(stepper));
		} catch (CoreException e) {
			// We have to catch the CoreException here as we do want to open the
			// launch configurations dialog on ERROR only.
			status = e.getStatus();

			ILaunchManagerDelegate delegate = LaunchConfigTypeBindingsManager.getInstance().getLaunchManagerDelegate(configuration.getType().getIdentifier(), mode);

			if (status == null || (status.getSeverity() == IStatus.ERROR && delegate.showLaunchDialog(ILaunchManagerDelegate.SITUATION_AFTER_LAUNCH_FAILED))) {
				// just pass on the exception as is
				throw e;
			}

			// Try to get a handler for the status
			IStatusHandler handler = DebugPlugin.getDefault().getStatusHandler(status);
			// If we cannot get a handler, pass on the exception
			if (handler == null) {
				throw e;
			}

			// Invoke the handler directly and drop the exception
			handler.handleStatus(status, this);

			// Mimic the original launch configuration behavior if an exception occurred
			// by removing the launch, if empty, and setting the progress monitor canceled.
			// @see LaunchConfiguration#launch(String, IProgressMonitor, boolean, boolean), line #768
			if (!launch.hasChildren()) {
				DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
				if (monitor != null) {
					monitor.setCanceled(true);
				}
			}
		} finally {
			// Mimic the original launch configuration behavior if monitor is set
			// canceled and make sure that the launch get's removed.
			if (monitor == null || monitor.isCanceled()) {
				DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
			}

			// Cleanup the stepper
			stepper.cleanup();

			// Set the launch completed here. Doesn't matter if the launch might completed with error or not.
			launch.setAttribute(ICommonLaunchAttributes.ILAUNCH_ATTRIBUTE_LAUNCH_SEQUENCE_COMPLETED, Boolean.TRUE.toString());

			long endTime = System.currentTimeMillis();
			CoreBundleActivator.getTraceHandler().trace("LaunchConfigurationDelegate#launch: *** DONE" //$NON-NLS-1$
							+ " (" + configuration.getName() + ")", //$NON-NLS-1$ //$NON-NLS-2$
							0, ITraceIds.TRACE_STEPPING, IStatus.WARNING, this);
			CoreBundleActivator.getTraceHandler().trace(" [" + ISharedConstants.TIME_FORMAT.format(new Date(endTime)) //$NON-NLS-1$
							+ " , delay = " + (endTime - startTime) + " ms]" //$NON-NLS-1$ //$NON-NLS-2$
							+ " ***", //$NON-NLS-1$
							0, ITraceIds.PROFILE_STEPPING, IStatus.WARNING, this);

		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getLaunch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
	 */
	@Override
	public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException {
		return new Launch(configuration, mode, null);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getProjectsForProblemSearch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
	 */
	@Override
	protected IProject[] getProjectsForProblemSearch(ILaunchConfiguration configuration, String mode) throws CoreException {
		// Return the same list of projects as we have to build
		return getBuildOrder(configuration, mode);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getBuildOrder(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
	 */
	@Override
	protected IProject[] getBuildOrder(ILaunchConfiguration configuration, String mode) throws CoreException {
		// The result list. Return always an empty list at least. If returning null,
		// the super implementation will perform a incremental workspace build.
		List<IProject> projects = new ArrayList<IProject>();

		// Return the projects to build within the order the user configured within
		// the corresponding UI launch tab.
		List<String> projectNames = (List<String>)DefaultPersistenceDelegate.getAttribute(configuration, ICommonLaunchAttributes.ATTR_PROJECTS_FOR_BUILD, (List<?>)null);
		if (projectNames != null) {
			projects.addAll(findProjectResources(projectNames));
		}

		// If the list of projects is not empty, we have to check for duplicates
		// and possible sub projects. As we cannot pre check if a project must
		// be build, we have to avoid building the same project again and again.
		checkForDuplicatesAndSubProjects(projects);

		return projects.toArray(new IProject[projects.size()]);
	}

	/**
	 * Lookup the corresponding project resources for the given list of project
	 * names. If one of the referenced projects cannot be found or is closed,
	 * a {@link CoreException} will be thrown.
	 *
	 * @param projectNames The list of project names. Must be not <code>null</code>.
	 * @return The list of project resources or an empty list.
	 */
	protected List<IProject> findProjectResources(List<String> projectNames) throws CoreException {
		Assert.isNotNull(projectNames);

		List<IProject> projects = new ArrayList<IProject>();

		// Project resources are stored with the workspace root
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		for (String projectName : projectNames) {
			// Query the project resource from the workspace root.
			IProject project = root.getProject(projectName);
			if (!project.isAccessible()) {
				// Not accessible -> means the project does either not exist or
				// is closed.
				throw new CoreException(new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
								NLS.bind(Messages.LaunchConfigurationDelegate_error_inaccessibleReferencedProject, projectName)
								));
			}

			// add the project resource to the list
			projects.add(project);
		}

		return projects;
	}

	/**
	 * Check for duplicates and sub projects in the given list of project
	 * resources.
	 *
	 * @param projects The list of project resources. Must be not <code>null</code>.
	 */
	protected void checkForDuplicatesAndSubProjects(List<IProject> projects) {
		Assert.isNotNull(projects);

		// The list of already processed project names
		List<String> processedProjectNames = new ArrayList<String>();

		// Loop the project list and determine the duplicates. Use an
		// iterator here as we manipulate the list directly.
		ListIterator<IProject> iterator = projects.listIterator();
		while (iterator.hasNext()) {
			IProject project = iterator.next();
			// If the project name is within the list of already processed
			// projects, the project will be deleted from the list.
			if (processedProjectNames.contains(project.getName())) {
				iterator.remove();
				continue;
			}

			// Add the project name to the list of processed project names
			processedProjectNames.add(project.getName());
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getBreakpoints(org.eclipse.debug.core.ILaunchConfiguration)
	 *
	 * Note: Redefined to be public accessible. Needed to access the breakpoints without
	 *       duplicating the super implementation.
	 */
	@Override
	public IBreakpoint[] getBreakpoints(ILaunchConfiguration configuration) {
		return super.getBreakpoints(configuration);
	}
}