Skip to main content
summaryrefslogtreecommitdiffstats
blob: b651668a0f6ad7ed563de6d360763963c73d99f1 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**********************************************************************
 * This file is part of "Object Teams Development Tooling"-Software
 * 
 * Copyright 2008 Technical University Berlin, Germany.
 * 
 * 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
 * $Id: PDELaunchingAdaptor.java 23461 2010-02-04 22:10:39Z stephan $
 * 
 * Please visit http://www.eclipse.org/objectteams for updates and contact.
 * 
 * Contributors:
 * Technical University Berlin - Initial API and implementation
 **********************************************************************/
package org.eclipse.objectteams.otdt.internal.debug.adaptor.launching;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.core.util.Util;
import org.eclipse.objectteams.otdt.debug.OTDebugPlugin;
import org.eclipse.objectteams.otdt.debug.TeamBreakpointInstaller;
import org.eclipse.objectteams.otdt.internal.debug.adaptor.DebugMessages;
import org.eclipse.objectteams.otdt.internal.debug.adaptor.OTDebugAdaptorPlugin;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import base org.eclipse.pde.internal.ui.launcher.JREBlock;
import base org.eclipse.pde.ui.launcher.AbstractLauncherTab;
import base org.eclipse.pde.launching.AbstractPDELaunchConfiguration;
import base org.eclipse.pde.launching.JUnitLaunchConfigurationDelegate;

/**
 * This team adapts all Eclipse and OSGi launches (Launcher) and launch configurations (JREBlock and LauncherTab).
 * 
 * @author stephan
 * @since 1.2.2
 */
@SuppressWarnings("restriction")
public team class PDELaunchingAdaptor {
	
	/** Mediating between LauncherTab and JREBlock: */
	LauncherTab currentTab = null;
	
	static final String OSGI_EXTENSIONS       = "-Dosgi.framework.extensions=org.eclipse.objectteams.otequinox.hook"; //$NON-NLS-1$
	static final String HOOK_CONFIGURATOR     = "-Dosgi.hook.configurators.include=org.eclipse.objectteams.otequinox.hook.HookConfigurator";//$NON-NLS-1$
	static final String CLASSLOADER_LOCKING   = "-Dosgi.classloader.lock=classname"; //$NON-NLS-1$
	static final String ENABLE_OTEQUINOX      = "-Dot.equinox=1"; //$NON-NLS-1$     // this also causes the WORKAROUND_REPOSITORY flag being set to true in OTRE.
	static final String DISABLE_OTEQUINOX     = "-Dot.equinox=false"; //$NON-NLS-1$ // prevents TransformerHook installation and start of TransformerPlugin
	static final String OT_DEBUG_VMARG        = "-Dot.debug"; //$NON-NLS-1$
	static final String[] OT_VM_ARGS          = { OSGI_EXTENSIONS, HOOK_CONFIGURATOR, CLASSLOADER_LOCKING, ENABLE_OTEQUINOX };
	static final String[] OT_VM_DEBUG_ARGS    = { OSGI_EXTENSIONS, HOOK_CONFIGURATOR, CLASSLOADER_LOCKING, ENABLE_OTEQUINOX, OT_DEBUG_VMARG };
	static final String[] VM_ARGS          = { CLASSLOADER_LOCKING, DISABLE_OTEQUINOX };
	static final String[] VM_DEBUG_ARGS    = { CLASSLOADER_LOCKING, DISABLE_OTEQUINOX, OT_DEBUG_VMARG };

	/** 
	 * Extend pre-built vm arguments with OT/Equinox specifics (depending on run/debug mode).
	 */
	static String[] extendVMArguments(String[] args, String mode) {
		String[] otArgs = OT_VM_ARGS;
		if (mode != null && mode.equals(ILaunchManager.DEBUG_MODE))
			otArgs = OT_VM_DEBUG_ARGS;
	
		if (args == null || args.length == 0)
			return otArgs;
	
		String[] combinedArgs = new String[args.length + otArgs.length];
		System.arraycopy(args, 0, combinedArgs, 0, args.length);
		System.arraycopy(otArgs, 0, combinedArgs, args.length, otArgs.length);
		return combinedArgs;
	}
	static String[] addDisableOTEquinoxArgument(String[] args) {
		String[] combinedArgs = new String[args.length + 1];
		System.arraycopy(args, 0, combinedArgs, 0, args.length);
		combinedArgs[args.length] = DISABLE_OTEQUINOX;
		return combinedArgs;		
	}
	/* alternate version for single string signature. */
	static String extendVMArguments(String args, String mode) {
		String otArgs;
		if (mode != null && mode.equals(ILaunchManager.DEBUG_MODE))
			otArgs = Util.concatWith(OT_VM_DEBUG_ARGS, ' ');
		else
			otArgs = Util.concatWith(OT_VM_ARGS, ' ');
	
		if (args == null || args.length() == 0)
			return otArgs;
		
		return args+' '+otArgs;
	}

	/** 
	 * Installs breakpoints needed for the TeamMonitor into org.objectteams.Team.
	 * Needs to find a project with OTJavaNature to do this.
	 */
	static void installOOTBreakpoints(IProject[] projects) throws CoreException
	{
		if (projects != null)
			for (IProject project : projects) 
				// find org.objectteams.Team in any OT/J Project:
				if (project.getNature(JavaCore.OTJ_NATURE_ID) != null) {
					TeamBreakpointInstaller.installTeamBreakpoints(JavaCore.create(project));
					return; // good, done.
				}
		logException(null, Status.WARNING, DebugMessages.OTLaunching_no_OTJ_project_found);
	}
	
	static void logException(CoreException ex, int level, String msg) {
		OTDebugPlugin.getDefault().getLog().log(
						new Status(level, OTDebugAdaptorPlugin.PLUGIN_ID, 0, msg, ex));
	}
	
	// helper needed to protect a guard from exception:
	static boolean isOTLaunch (ILaunchConfiguration configuration) {
		try {
			return configuration.getAttribute(OTDebugPlugin.OT_LAUNCH, false);
		} catch (CoreException ce) {
			return false;
		}
	}
	
	/** Generalizes over normal pde launches and JUnit plugin launches. */
	abstract protected class AbstractLauncher 
	{
		abstract IProject[] getProjectsForProblemSearch(ILaunchConfiguration config, String mode)
			throws CoreException;
		
		String mode;
		
		void prepareLaunch(ILaunchConfiguration configuration, String mode) 
		{
			this.mode = mode;
			if (isOTLaunch(configuration) && ILaunchManager.DEBUG_MODE.equals(mode))
				try {
					PDELaunchingAdaptor.installOOTBreakpoints(getProjectsForProblemSearch(configuration, mode));
				} catch (CoreException ex) {
					logException(ex, Status.WARNING, DebugMessages.OTLaunching_no_OTJ_project_found);
				}
		}
	}
	
	/**
	 * This role adapts any pde-launch where the OT/Equinox flag is set to true:
	 * <ul>
	 * <li>add OT-specific arguments to the vm args (extendVMArguments)</li>
	 * <li>install the breakpoints needed by the TeamMonitor (prepareLaunch)</li>
	 * </ul>
	 */
	protected class Launcher extends AbstractLauncher playedBy AbstractPDELaunchConfiguration 
	{	
		@SuppressWarnings("decapsulation")
		getProjectsForProblemSearch -> getProjectsForProblemSearch;
		
		// Extend VM arguments:
		String[] extendVMArguments(ILaunchConfiguration config) <- replace String[] getVMArguments(ILaunchConfiguration config);
		callin String[] extendVMArguments(ILaunchConfiguration config) throws CoreException 
		{
			String[] args = base.extendVMArguments(config);
			if (isOTLaunch(config))
				return PDELaunchingAdaptor.extendVMArguments(args, this.mode);
			else
				return PDELaunchingAdaptor.addDisableOTEquinoxArgument(args);
		}
			
		// install breakpoints and record launch mode (run/debug):
		prepareLaunch <- before launch;
	}
	
	/** Unfortunately JUnit launches are slightly different (and not related by inheritance) */
	protected class JUnitLauncher extends AbstractLauncher playedBy JUnitLaunchConfigurationDelegate 
	{
		@SuppressWarnings("decapsulation")
		getProjectsForProblemSearch -> getProjectsForProblemSearch;
		
		// Extend VM arguments:
		String extendVMArgument(ILaunchConfiguration config) <- replace String getVMArguments(ILaunchConfiguration config);
		callin String extendVMArgument(ILaunchConfiguration config) throws CoreException 
		{
			String result = base.extendVMArgument(config);
			if (isOTLaunch(config))
				return PDELaunchingAdaptor.extendVMArguments(result, this.mode);
			else
				return result+' '+DISABLE_OTEQUINOX;
		}
			
		// install breakpoints and record launch mode (run/debug):
		prepareLaunch <- before launch;		
	}

	/** 
	 * UI: This role allows us to insert our "Object Teams Runtime" block just after the JREBlock.
	 */
	protected class JREBlock playedBy JREBlock {
		// build the GUI:
		void appendOTOption(Composite parent) <- after void createControl(Composite parent)
			base when (PDELaunchingAdaptor.this.currentTab != null); // only within the LauncherTab#launcherTabCFlow() (see below)

		void appendOTOption(Composite parent) {
			PDELaunchingAdaptor.this.currentTab.createOTRESection(parent, false/*useSWTFactory*/);
		}		
	}
	
	/**
	 * This role manages the UI-part of this team: 
	 * <ul>
	 * <li>insert a new group after the JREBlock.</li>
	 * <li>read (initializeFrom) and apply (performApply) the new flag.</li></ul>
	 */
	protected class LauncherTab extends OTREBlock playedBy AbstractLauncherTab {

		LauncherTab(AbstractLauncherTab b) {
			// different label than default:
			this.enableCheckboxLabel = DebugMessages.OTLaunching_OTEquinox_checkbox_label;
		}
		
		// callout interface:
		@SuppressWarnings("decapsulation")
		Button createCheckButton(Composite parent, String label) -> Button createCheckButton(Composite parent, String label);
		
		@SuppressWarnings("decapsulation")
		void setDirty(boolean dirty) -> void setDirty(boolean dirty);

		void updateLaunchConfigurationDialog() -> void updateLaunchConfigurationDialog();
		
		// CFlow to let the JREBlock trigger building the GUI:
		launcherTabCFlow <- replace createControl;
		callin void launcherTabCFlow(Composite parent) {
			try {
				PDELaunchingAdaptor.this.currentTab = this;
				base.launcherTabCFlow(parent);
			} finally {
				PDELaunchingAdaptor.this.currentTab = null;
			}
		}
		
		// connect triggers to inherited methods:
		void initializeFrom(ILaunchConfiguration config) <- after void initializeFrom(ILaunchConfiguration config)
			when (this._otreToggleButton != null); // i.e.: is this the tab containing the JREBlock?

		@Override
		boolean hasOTJProject(ILaunchConfiguration config) {
			return true; // assume we might have an OT project - even without scanning through all projects; always want to enable our options
		}

		void performApply(ILaunchConfigurationWorkingCopy config) 
				<- after void performApply(ILaunchConfigurationWorkingCopy config);		
	}
}

Back to the top