Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 353ff01ed82c5ec39d1eec923b0acba80160d1ba (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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*******************************************************************************
 * Copyright (c) 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.core.internal.services;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IPathMap;
import org.eclipse.tcf.services.IPathMap.PathMapRule;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.services.AbstractService;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.utils.StatusHelper;
import org.eclipse.tcf.te.tcf.core.Tcf;
import org.eclipse.tcf.te.tcf.core.interfaces.IPathMapGeneratorService;
import org.eclipse.tcf.te.tcf.core.interfaces.IPathMapService;
import org.eclipse.tcf.te.tcf.launch.core.activator.CoreBundleActivator;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModelProvider;

/**
 * Path map service implementation.
 */
public class PathMapService extends AbstractService implements IPathMapService {
	// Lock to handle multi thread access
	private final Lock lock = new ReentrantLock();

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.core.interfaces.IPathMapService#getPathMap(java.lang.Object)
	 */
    @Override
	public PathMapRule[] getPathMap(Object context) {
    	Assert.isTrue(!Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$
		Assert.isNotNull(context);

		PathMapRule[] rules = null;

		try {
			// Acquire the lock before accessing the path mappings
			lock.lock();

			List<PathMapRule> rulesList = new ArrayList<PathMapRule>();

			// Get the launch configuration for that peer model
			ILaunchConfiguration config = (ILaunchConfiguration) Platform.getAdapterManager().getAdapter(context, ILaunchConfiguration.class);
			if (config == null) {
				config = (ILaunchConfiguration) Platform.getAdapterManager().loadAdapter(context, "org.eclipse.debug.core.ILaunchConfiguration"); //$NON-NLS-1$
			}

			if (config != null) {
				try {
					String path_map_cfg = config.getAttribute(org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.ATTR_PATH_MAP, ""); //$NON-NLS-1$
					rulesList.addAll(org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.parsePathMapAttribute(path_map_cfg));

					path_map_cfg = config.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, ""); //$NON-NLS-1$
					rulesList.addAll(org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.parseSourceLocatorMemento(path_map_cfg));
				} catch (CoreException e) { /* ignored on purpose */ }
			}

			IPathMapGeneratorService generator = ServiceManager.getInstance().getService(context, IPathMapGeneratorService.class);
			if (generator != null) {
				PathMapRule[] generatedRules = generator.getPathMap(context);
				if (generatedRules != null && generatedRules.length > 0) {
					rulesList.addAll(Arrays.asList(generatedRules));
				}
			}

			if (!rulesList.isEmpty()) {
				int cnt = 0;
				String id = getClientID();
				for (PathMapRule r : rulesList) r.getProperties().put(IPathMap.PROP_ID, id + "/" + cnt++); //$NON-NLS-1$
				rules = rulesList.toArray(new PathMapRule[rulesList.size()]);
			}
		} finally {
			// Release the lock
			lock.unlock();
		}

		return rules;
	}

    /* (non-Javadoc)
     * @see org.eclipse.tcf.te.tcf.core.interfaces.IPathMapService#addPathMap(java.lang.Object, java.lang.String, java.lang.String)
     */
    @Override
    public PathMapRule addPathMap(Object context, String source, String destination) {
    	Assert.isTrue(!Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$
		Assert.isNotNull(context);
		Assert.isNotNull(source);
		Assert.isNotNull(destination);

		PathMapRule rule = null;

		try {
			// Acquire the lock before accessing the path mappings
			lock.lock();

			List<PathMapRule> rulesList = new ArrayList<PathMapRule>();

			// Get the launch configuration for that peer model
			ILaunchConfigurationWorkingCopy config = (ILaunchConfigurationWorkingCopy) Platform.getAdapterManager().getAdapter(context, ILaunchConfigurationWorkingCopy.class);
			if (config == null) {
				config = (ILaunchConfigurationWorkingCopy) Platform.getAdapterManager().loadAdapter(context, "org.eclipse.debug.core.ILaunchConfigurationWorkingCopy"); //$NON-NLS-1$
			}

			if (config != null) {
				populatePathMapRulesList(config, rulesList);

				// Find an existing path map rule for the given source and destination
				for (PathMapRule candidate : rulesList) {
					if (source.equals(candidate.getSource()) && destination.equals(candidate.getDestination())) {
						rule = candidate;
						break;
					}
				}

				// If not matching path map rule exist, create a new one
				if (rule == null) {
					Map<String, Object> props = new LinkedHashMap<String, Object>();
					props.put(IPathMap.PROP_SOURCE, source);
					props.put(IPathMap.PROP_DESTINATION, destination);
					rule = new org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.PathMapRule(props);
					rulesList.add(rule);

					// Update the launch configuration
					updateLaunchConfiguration(config, rulesList);

					// Apply the path map
					applyPathMap(context, new Callback() {
						@Override
						protected void internalDone(Object caller, IStatus status) {
							if (status != null && Platform.inDebugMode()) {
								Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(status);
							}
						}
					});
				}
			}
		} finally {
			// Release the lock
			lock.unlock();
		}

		return rule;
    }

    /* (non-Javadoc)
     * @see org.eclipse.tcf.te.tcf.core.interfaces.IPathMapService#removePathMap(java.lang.Object, org.eclipse.tcf.services.IPathMap.PathMapRule)
     */
    @Override
    public void removePathMap(final Object context, final PathMapRule rule) {
    	Assert.isTrue(!Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$
		Assert.isNotNull(context);
		Assert.isNotNull(rule);

		try {
			// Acquire the lock before accessing the path mappings
			lock.lock();

			List<PathMapRule> rulesList = new ArrayList<PathMapRule>();

			// Get the launch configuration for that peer model
			ILaunchConfigurationWorkingCopy config = (ILaunchConfigurationWorkingCopy) Platform.getAdapterManager().getAdapter(context, ILaunchConfigurationWorkingCopy.class);
			if (config == null) {
				config = (ILaunchConfigurationWorkingCopy) Platform.getAdapterManager().loadAdapter(context, "org.eclipse.debug.core.ILaunchConfigurationWorkingCopy"); //$NON-NLS-1$
			}

			if (config != null) {
				populatePathMapRulesList(config, rulesList);

				// Remove the given rule from the list of present
				if (rulesList.remove(rule)) {
					// Update the launch configuration
					updateLaunchConfiguration(config, rulesList);

					// Apply the path map
					applyPathMap(context, new Callback() {
						@Override
						protected void internalDone(Object caller, IStatus status) {
							if (status != null && Platform.inDebugMode()) {
								Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(status);
							}
						}
					});
				}
			}
		} finally {
			// Release the lock
			lock.unlock();
		}
    }

    /**
     * Populate the given path map rules list from the given launch configuration.
     *
     * @param config The launch configuration. Must not be <code>null</code>.
     * @param rulesList The path map rules list. Must not be <code>null</code>.
     */
    private void populatePathMapRulesList(ILaunchConfiguration config, List<PathMapRule> rulesList) {
    	Assert.isNotNull(config);
    	Assert.isNotNull(rulesList);

		try {
			String path_map_cfg = config.getAttribute(org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.ATTR_PATH_MAP, ""); //$NON-NLS-1$
			String path_map_cfgV1 = config.getAttribute(org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.ATTR_PATH_MAP + "V1", ""); //$NON-NLS-1$ //$NON-NLS-2$

			rulesList.addAll(org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.parsePathMapAttribute(path_map_cfgV1));

	        int i = -1;
	        for (PathMapRule candidate : org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.parsePathMapAttribute(path_map_cfg)) {
	            if (rulesList.contains(candidate)) {
	                i = rulesList.indexOf(candidate);
	            } else {
	            	rulesList.add(++i, candidate);
	            }
	        }
		} catch (CoreException e) { /* ignored on purpose */ }
    }

    /**
     * Write back the given path map rules list to the given launch configuration.
     *
     * @param config The launch configuration. Must not be <code>null</code>.
     * @param rulesList The path map rules list. Must not be <code>null</code>.
     */
    private void updateLaunchConfiguration(ILaunchConfigurationWorkingCopy config, List<PathMapRule> rulesList) {
    	Assert.isNotNull(config);
    	Assert.isNotNull(rulesList);

		// Update the launch configuration
        for (PathMapRule candidate : rulesList) {
            candidate.getProperties().remove(IPathMap.PROP_ID);
        }

        StringBuilder bf = new StringBuilder();
        StringBuilder bf1 = new StringBuilder();

        for (PathMapRule candidate : rulesList) {
        	if (!(candidate instanceof org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.PathMapRule)) continue;

            boolean enabled = true;
            if (candidate.getProperties().containsKey("Enabled")) { //$NON-NLS-1$
                enabled = Boolean.parseBoolean(candidate.getProperties().get("Enabled").toString()); //$NON-NLS-1$
            }
            if (enabled) {
                candidate.getProperties().remove("Enabled"); //$NON-NLS-1$
                bf.append(candidate.toString());
            }
            bf1.append(candidate.toString());
        }

        if (bf.length() == 0) {
            config.removeAttribute(TCFLaunchDelegate.ATTR_PATH_MAP);
        } else {
            config.setAttribute(TCFLaunchDelegate.ATTR_PATH_MAP, bf.toString());
        }

        if (bf1.length() == 0) {
            config.removeAttribute(org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.ATTR_PATH_MAP + "V1"); //$NON-NLS-1$
        } else {
            config.setAttribute(org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate.ATTR_PATH_MAP + "V1", bf1.toString()); //$NON-NLS-1$
        }

        try {
	        config.doSave();
        }
        catch (CoreException e) {
    		if (Platform.inDebugMode()) {
    			Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(e.getStatus());
    		}
        }
    }

    /* (non-Javadoc)
     * @see org.eclipse.tcf.te.tcf.core.interfaces.IPathMapService#applyPathMap(java.lang.Object, org.eclipse.tcf.te.runtime.interfaces.callback.ICallback)
     */
    @Override
    public void applyPathMap(final Object context, final ICallback callback) {
    	Assert.isTrue(!Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$
    	Assert.isNotNull(context);
    	Assert.isNotNull(callback);

    	IPeer peer = context instanceof IPeer ? (IPeer)context : null;
    	if (peer == null && context instanceof IPeerModel) peer = ((IPeerModel)context).getPeer();
    	if (peer == null && context instanceof IPeerModelProvider && ((IPeerModelProvider)context).getPeerModel() != null) peer = ((IPeerModelProvider)context).getPeerModel().getPeer();

    	if (peer != null) {
			final IChannel channel = Tcf.getChannelManager().getChannel(peer);
			if (channel != null && IChannel.STATE_OPEN == channel.getState()) {
				// Channel is open -> Have to update the path maps

				// Get the configured path mappings. This must be called from
				// outside the runnable as getPathMap(...) must be called from
				// outside of the TCF dispatch thread.
				final PathMapRule[] configuredMap = getPathMap(context);

				if (configuredMap != null && configuredMap.length > 0) {
					// Create the runnable which set the path map
					Runnable runnable = new Runnable() {
						@Override
						public void run() {
							final IPathMap svc = channel.getRemoteService(IPathMap.class);
							if (svc != null) {
								// Get the old path maps first. Keep path map rules not coming from us
								svc.get(new IPathMap.DoneGet() {
									@Override
									public void doneGet(IToken token, Exception error, PathMapRule[] map) {
										// Merge the maps to a new list
										List<PathMapRule> rules = new ArrayList<PathMapRule>();

										if (map != null && map.length > 0) {
											for (PathMapRule rule : map) {
												if (rule.getID() == null || !rule.getID().startsWith(getClientID())) {
													rules.add(rule);
												}
											}
										}

										rules.addAll(Arrays.asList(configuredMap));

										// Determine if the map has changed
										boolean changed = map != null ? map.length != rules.size() : !rules.isEmpty();
										if (!changed && !rules.isEmpty()) {
											// Make a copy of new map and remove all rules listed
											// by the old map. If not empty at the end, the new map
											// is different from the old map.
											List<PathMapRule> copy = new ArrayList<PathMapRule>(rules);
											for (PathMapRule rule : map) {
												Iterator<PathMapRule> iter = copy.iterator();
												while (iter.hasNext()) {
													PathMapRule r = iter.next();
													if (r.equals(rule)) {
														iter.remove();
														break;
													}
												}
											}

											changed = !copy.isEmpty();
										}

										// If the path map has changed, apply the map
										if (changed) {
											svc.set(rules.toArray(new PathMapRule[rules.size()]), new IPathMap.DoneSet() {
												@Override
												public void doneSet(IToken token, Exception error) {
													callback.done(PathMapService.this, StatusHelper.getStatus(error));
												}
											});
										} else {
											callback.done(PathMapService.this, Status.OK_STATUS);
										}
									}
								});
							} else {
								callback.done(PathMapService.this, Status.OK_STATUS);
							}
						}
					};

					Protocol.invokeLater(runnable);
				} else {
		    		callback.done(PathMapService.this, Status.OK_STATUS);
				}
			} else {
	    		callback.done(PathMapService.this, Status.OK_STATUS);
			}
    	} else {
    		callback.done(PathMapService.this, Status.OK_STATUS);
    	}
    }

    /* (non-Javadoc)
     * @see org.eclipse.tcf.te.tcf.core.interfaces.IPathMapService#getClientID()
     */
    @SuppressWarnings("restriction")
    @Override
    public String getClientID() {
        return org.eclipse.tcf.internal.debug.Activator.getClientID();
    }
}

Back to the top