Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e6dddf937197c8a51ce7d12eadf381602d51e70d (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
255
256
257
258
259
260
261
262
263
264
265
/*******************************************************************************
 * Copyright (c) 2006, 2013 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchDelegate;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;

import com.ibm.icu.text.MessageFormat;

/**
 * Proxy to a launch delegate extension
 * Clients can contribute launch delegates through the <code>launchDelegates</code> extension point
 *
 * Example contribution of the local java launch delegate
 * <pre>
 * <extension point="org.eclipse.debug.core.launchDelegates">
      <launchDelegate
            delegate="org.eclipse.jdt.launching.JavaLaunchDelegate"
            id="org.eclipse.jdt.launching.localJavaApplicationDelegate"
            modes="run, debug"
            name="%localJavaApplication"
            type="org.eclipse.jdt.launching.localJavaApplication">
          <modeCombination
    		modes="run, profile">
    		perspective="com.example.Perspective">
   		  </modeCombination>
      </launchDelegate>
 * </pre>
 *
 * Clients are NOT intended to subclass this class
 *
 * @see IConfigurationElementConstants
 *
 * @since 3.3
 */
public final class LaunchDelegate implements ILaunchDelegate {

	/**
	 * The configuration element for this delegate
	 */
	private IConfigurationElement fElement = null;

	/**
	 * The cached delegate. Remains null until asked for, then persisted
	 */
	private ILaunchConfigurationDelegate fDelegate = null;

	//a listing of sets of
	private List<Set<String>> fLaunchModes = null;
	private String fType = null;
	private HashMap<Set<String>, String> fPerspectiveIds = null;

	/**
	 * Constructor
	 * @param element the configuration element to associate with this launch delegate
	 */
	public LaunchDelegate(IConfigurationElement element) {
		fElement = element;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchDelegateProxy#getDelegate()
	 */
	@Override
	public ILaunchConfigurationDelegate getDelegate() throws CoreException {
		if(fDelegate == null) {
			Object obj = fElement.createExecutableExtension(IConfigurationElementConstants.DELEGATE);
			if(obj instanceof ILaunchConfigurationDelegate) {
				fDelegate = (ILaunchConfigurationDelegate)obj;
			} else {
				throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.ERROR, MessageFormat.format(DebugCoreMessages.LaunchDelegate_3, new Object[] { getId() }), null));
			}
		}
		return fDelegate;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchDelegateProxy#getId()
	 */
	@Override
	public String getId() {
		return fElement.getAttribute(IConfigurationElementConstants.ID);
	}

	/**
	 * Returns the id of the associated <code>ILaunchConfigurationType</code> or <code>null</code> if none provided
	 * @return the id of the <code>ILaunchConfigurationType</code> associated with this delegate
	 */
	public String getLaunchConfigurationTypeId() {
		if(fType == null) {
			//fall back to single association if no appliesTo
			fType = fElement.getAttribute(IConfigurationElementConstants.TYPE);
			if(fType == null) {
				//the case when we have passed a launch configuration type to the launch delegate
				fType = fElement.getAttribute(IConfigurationElementConstants.ID);
			}
		}
		return fType;
	}

	/**
	 * Simple method to parse mode strings (separated by commas)
	 * @param element the config element to read the mode string from
	 * @return a set of the parsed strings or an empty collection
	 * @since 3.3
	 */
	private Set<String> parseModes(IConfigurationElement element) {
		HashSet<String> set = new HashSet<String>();
		String modes = element.getAttribute(IConfigurationElementConstants.MODES);
		if (modes != null) {
			String[] strings = modes.split(","); //$NON-NLS-1$
			for (int i = 0; i < strings.length; i++) {
				set.add(strings[i].trim());
			}
		}
		return set;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchDelegateProxy#getModes()
	 */
	@Override
	public List<Set<String>> getModes() {
		if(fLaunchModes == null) {
			fLaunchModes = new ArrayList<Set<String>>();
			fPerspectiveIds = new HashMap<Set<String>, String>();
			IConfigurationElement[] children = fElement.getChildren(IConfigurationElementConstants.MODE_COMBINATION);
			Set<String> modeset = null;
			for (int i = 0; i < children.length; i++) {
				modeset = parseModes(children[i]);
				fLaunchModes.add(modeset);
				fPerspectiveIds.put(modeset, children[i].getAttribute(IConfigurationElementConstants.PERSPECTIVE));
			}
			//try to get the modes from the old definition and make each one
			//a separate set of one element
			modeset = null;
			String modes = fElement.getAttribute(IConfigurationElementConstants.MODES);
			if (modes != null) {
				String[] strings = modes.split(","); //$NON-NLS-1$
				for (int i = 0; i < strings.length; i++) {
					modeset = new HashSet<String>();
					modeset.add(strings[i].trim());
					fLaunchModes.add(modeset);
				}
			}
		}
		return fLaunchModes;
	}

	/**
	 * Returns the human readable name for this launch delegate
	 * @return the human readable name for this launch delegate, or <code>null</code> if none
	 */
	@Override
	public String getName() {
		//try a delegateName attribute first, in the event this delegate was made from an ILaunchConfigurationType
		String name = fElement.getAttribute(IConfigurationElementConstants.DELEGATE_NAME);
		if(name == null) {
			name = fElement.getAttribute(IConfigurationElementConstants.NAME);
			if (name == null) {
				name = getContributorName();
			}
			name = name.trim();
			if (Character.isUpperCase(name.charAt(0))) {
				name = MessageFormat.format(DebugCoreMessages.LaunchDelegate_1, new Object[] { name });
			} else {
				name = MessageFormat.format(DebugCoreMessages.LaunchDelegate_2, new Object[] { name });
			}
		}
		return name;
	}

	/**
	 * Returns the contributor name of this delegate (plug-in name).
	 *
	 * @return contributor name
	 */
	@Override
	public String getContributorName() {
		return fElement.getContributor().getName();
	}

	/**
	 * Returns the associated source locator id or <code>null</code>
	 * @return the associated source locator id or <code>null</code> if not provided
	 */
	public String getSourceLocatorId() {
		return fElement.getAttribute(IConfigurationElementConstants.SOURCE_LOCATOR);
	}

	/**
	 * Returns the associated source path computer id or <code>null</code>
	 * @return the associated source path computer id or <code>null</code> if not provided
	 */
	public String getSourcePathComputerId() {
		return fElement.getAttribute(IConfigurationElementConstants.SOURCE_PATH_COMPUTER);
	}

	/**
	 * @see org.eclipse.debug.core.ILaunchDelegate#getDescription()
	 */
	@Override
	public String getDescription() {
		String desc = fElement.getAttribute(IConfigurationElementConstants.DELEGATE_DESCRIPTION);
		if(desc == null) {
			return DebugCoreMessages.LaunchDelegate_0;
		}
		return desc;
	}

	/**
	 * @see org.eclipse.debug.core.ILaunchDelegate#getPluginIdentifier()
	 */
	@Override
	public String getPluginIdentifier() {
		return fElement.getContributor().getName();
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		return obj instanceof ILaunchDelegate && getId() != null && getId().equals(((ILaunchDelegate)obj).getId());
	}

	@Override
	public int hashCode() {
		String id = getId();
		return id == null ? 0 : id.hashCode();
	}

	/**
	 * @see org.eclipse.debug.core.ILaunchDelegate#getPerspectiveId(java.util.Set)
	 */
	@Override
	public String getPerspectiveId(Set<String> modes) {
		if(fPerspectiveIds == null) {
			getModes();
		}
		return fPerspectiveIds.get(modes);
	}
}

Back to the top