Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2a0cbeca2a2965b54336faeb0292b0a4538ced98 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*******************************************************************************
 * Copyright (c) 2014 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.locator.services;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.tcf.core.Command;
import org.eclipse.tcf.core.TransientPeer;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.ILocator;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.model.interfaces.contexts.IAsyncRefreshableCtx;
import org.eclipse.tcf.te.runtime.model.interfaces.contexts.IAsyncRefreshableCtx.QueryState;
import org.eclipse.tcf.te.runtime.model.interfaces.contexts.IAsyncRefreshableCtx.QueryType;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistableNodeProperties;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.tcf.core.Tcf;
import org.eclipse.tcf.te.tcf.core.interfaces.IChannelManager;
import org.eclipse.tcf.te.tcf.core.interfaces.IPeerProperties;
import org.eclipse.tcf.te.tcf.core.peers.Peer;
import org.eclipse.tcf.te.tcf.core.util.persistence.PeerDataHelper;
import org.eclipse.tcf.te.tcf.locator.interfaces.ILocatorModelListener;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.ILocatorModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.ILocatorNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNodeProperties;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelLookupService;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelRefreshService;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelUpdateService;
import org.eclipse.tcf.te.tcf.locator.model.ModelLocationUtil;
import org.eclipse.tcf.te.tcf.locator.model.ModelManager;
import org.eclipse.tcf.te.tcf.locator.nodes.LocatorNode;

/**
 * Default locator model refresh service implementation.
 */
public class LocatorModelRefreshService extends AbstractLocatorModelService implements ILocatorModelRefreshService {

	/**
	 * Constructor.
	 *
	 * @param parentModel The parent locator model instance. Must not be <code>null</code>.
	 */
	public LocatorModelRefreshService(ILocatorModel parentModel) {
		super(parentModel);
	}

	/**
	 * Asynchronously invoke the callback within the TCF dispatch thread.
	 *
	 * @param callback The callback to invoke or <code>null</code>.
	 */
	protected final void invokeCallback(final ICallback callback) {
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		if (callback != null) {
			Protocol.invokeLater(new Runnable() {
				@Override
				public void run() {
					callback.done(LocatorModelRefreshService.this, Status.OK_STATUS);
				}
			});
		}
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.tcf.te.tcf.locator.interfaces.services.IPeerModelRefreshService#refresh(org.eclipse
	 * .tcf.te.runtime.interfaces.callback.ICallback)
	 */
	@Override
	public void refresh(final ICallback callback) {
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		// Get the parent peer model
		ILocatorModel model = getLocatorModel();

		// If the parent model is already disposed, the service will drop out immediately
		if (model.isDisposed()) {
			invokeCallback(callback);
			return;
		}

		// If the TCF framework isn't initialized yet, the service will drop out immediately
		if (!Tcf.isRunning()) {
			invokeCallback(callback);
			return;
		}

		refreshStaticPeers();

		// Get the list of old children (update node instances where possible)
		ILocatorNode[] locatorNodes = model.getLocatorNodes();
		List<IPeer> oldChildren = new ArrayList<IPeer>();
		for (ILocatorNode node : locatorNodes) {
			if (!node.isStatic()) {
				oldChildren.add(node.getPeer());
			}
		}

		// Refresh the static peer definitions
		processPeers(Protocol.getLocator().getPeers(), oldChildren, model);

		// If there are remaining old children, remove them from the model (non-recursive)
		for (IPeer oldChild : oldChildren) {
			model.getService(ILocatorModelUpdateService.class).remove(oldChild);
		}

		ILocatorModelListener[] listeners = model.getListener();
		for (ILocatorModelListener listener : listeners) {
			listener.modelChanged(model, null, false);
		}

		// Invoke the callback
		invokeCallback(callback);
	}

	/**
	 * Process the given map of peers and update the given locator model.
	 *
	 * @param peers The map of peers to process. Must not be <code>null</code>.
	 * @param oldChildren The list of old children. Must not be <code>null</code>.
	 * @param model The locator model. Must not be <code>null</code>.
	 */
	protected void processPeers(Map<String, IPeer> peers, List<IPeer> oldChildren, ILocatorModel model) {
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$
		Assert.isNotNull(peers);
		Assert.isNotNull(oldChildren);
		Assert.isNotNull(model);

		for (Entry<String, IPeer> entry : peers.entrySet()) {
			// Get the peer instance for the current peer id
			IPeer peer = entry.getValue();
			// Check if the peer is filtered
			if (isFiltered(peer)) continue;
			// Try to find an existing peer node first
			IPeer lkupPeer = model.getService(ILocatorModelLookupService.class)
			                .lkupPeerById(entry.getKey());
			// And create a new one if we cannot find it
			if (lkupPeer == null) {
				// Validate peer before adding
				lkupPeer = model.validatePeer(peer);
				if (lkupPeer != null) model.getService(ILocatorModelUpdateService.class)
				                .add(lkupPeer);
			}
			else {
				oldChildren.remove(peer);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelRefreshService#refresh(org
	 * .eclipse.tcf.te.tcf.locator.interfaces.nodes.ILocatorNode,
	 * org.eclipse.tcf.te.runtime.interfaces.callback.ICallback)
	 */
	@Override
	public void refresh(final ILocatorNode locatorNode, ICallback callback) {
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		// Get the parent peer model
		ILocatorModel model = getLocatorModel();

		// If the parent model is already disposed, the service will drop out immediately
		if (model.isDisposed()) {
			invokeCallback(callback);
			return;
		}

		// If the TCF framework isn't initialized yet, the service will drop out immediately
		if (!Tcf.isRunning()) {
			invokeCallback(callback);
			return;
		}

		final IAsyncRefreshableCtx refreshCtx = (IAsyncRefreshableCtx) locatorNode
		                .getAdapter(IAsyncRefreshableCtx.class);
		refreshCtx.setQueryState(QueryType.CONTEXT, QueryState.IN_PROGRESS);
		refreshCtx.setQueryState(QueryType.CHILD_LIST, QueryState.IN_PROGRESS);

		Map<String, Boolean> flags = new HashMap<String, Boolean>();
		flags.put(IChannelManager.FLAG_FORCE_NEW, Boolean.TRUE);
		flags.put(IChannelManager.FLAG_NO_PATH_MAP, Boolean.TRUE);
		flags.put(IChannelManager.FLAG_NO_VALUE_ADD, Boolean.TRUE);

		final ICallback finCb = new Callback(callback) {
			@Override
			protected void internalDone(Object caller, IStatus status) {
				refreshCtx.setQueryState(QueryType.CONTEXT, QueryState.DONE);
				refreshCtx.setQueryState(QueryType.CHILD_LIST, QueryState.DONE);
				final ILocatorModel model = ModelManager.getLocatorModel();
				final ILocatorModelListener[] listeners = model.getListener();
				if (listeners.length > 0) {
					Protocol.invokeLater(new Runnable() {
						@Override
						public void run() {
							for (ILocatorModelListener listener : listeners) {
								listener.modelChanged(model, locatorNode, false);
							}
						}
					});
				}
			}
		};

		Tcf.getChannelManager()
		                .openChannel(locatorNode.getPeer(), flags, new IChannelManager.DoneOpenChannel() {
			                @Override
			                public void doneOpenChannel(Throwable error, final IChannel channel) {
				                if (error != null || channel == null) {
					                locatorNode.removeAll(ILocatorNode.class);
					                refreshCtx.setQueryState(QueryType.CONTEXT, QueryState.DONE);
					                refreshCtx.setQueryState(QueryType.CHILD_LIST, QueryState.DONE);
					                if (channel != null) {
						                Tcf.getChannelManager().closeChannel(channel);
					                }
					                invokeCallback(finCb);
				                }
				                else {
					                onDoneOpenChannelRefreshLocatorNode(channel, locatorNode, new Callback(finCb) {
						                @Override
						                protected void internalDone(Object caller, org.eclipse.core.runtime.IStatus status) {
							                Tcf.getChannelManager().closeChannel(channel);
						                }
					                });
				                }
			                }
		                });
	}

	protected void onDoneOpenChannelRefreshLocatorNode(IChannel channel, ILocatorNode locatorNode, ICallback callback) {
		ILocator locator = channel.getRemoteService(ILocator.class);
		IAsyncRefreshableCtx refreshCtx = (IAsyncRefreshableCtx) locatorNode
		                .getAdapter(IAsyncRefreshableCtx.class);
		if (locator == null) {
			locatorNode.removeAll(ILocatorNode.class);
			refreshCtx.setQueryState(QueryType.CONTEXT, QueryState.DONE);
			refreshCtx.setQueryState(QueryType.CHILD_LIST, QueryState.DONE);
			invokeCallback(callback);
		}
		else {
			refreshCtx.setQueryState(QueryType.CONTEXT, QueryState.DONE);
			getPeers(locator, channel, locatorNode, callback);
		}
	}

	protected void getPeers(final ILocator locator, final IChannel channel, final ILocatorNode locatorNode, final ICallback callback) {
		@SuppressWarnings("unused")
		Command cmd = new Command(channel, locator, "getPeers", null) { //$NON-NLS-1$
			@Override
			public void done(Exception error, Object[] args) {
				if (error == null) {
					Assert.isTrue(args.length == 2);
					error = toError(args[0]);
				}
				// If the error is still null here, process the returned peers
				if (error == null && args[1] != null) {
					// calculate proxies
					String parentProxies = locatorNode.getPeer().getAttributes()
					                .get(IPeerProperties.PROP_PROXIES);
					IPeer[] proxies = PeerDataHelper.decodePeerList(parentProxies);
					List<IPeer> proxiesList = new ArrayList<IPeer>(Arrays.asList(proxies));
					proxiesList.add(locatorNode.getPeer());
					proxies = proxiesList.toArray(new IPeer[proxiesList.size()]);
					String encProxies = PeerDataHelper.encodePeerList(proxies);

					List<ILocatorNode> oldChildren = locatorNode.getChildren(ILocatorNode.class);

					ILocatorModelLookupService lkup = getLocatorModel()
					                .getService(ILocatorModelLookupService.class);

					@SuppressWarnings("unchecked")
					Collection<Map<String, String>> peerAttributesList = (Collection<Map<String, String>>) args[1];
					for (Map<String, String> attributes : peerAttributesList) {

						String agentId = attributes.get(IPeer.ATTR_AGENT_ID);
						String id = attributes.get(IPeer.ATTR_ID);
						ILocatorNode existing = null;
						ILocatorNode[] lkupNodes = agentId != null ? lkup
						                .lkupLocatorNodeByAgentId(locatorNode, agentId) : new ILocatorNode[0];
						if (lkupNodes.length == 0) {
							lkupNodes = id != null ? lkup.lkupLocatorNodeById(locatorNode, id) : new ILocatorNode[0];
						}
						for (ILocatorNode node : lkupNodes) {
							if (node.getPeer().getID().equals(id)) {
								oldChildren.remove(node);
								existing = node;
								break;
							}
						}

						String parentAgentId = locatorNode.getPeer().getAgentID();
						String parentId = locatorNode.getPeer().getID();
						if (agentId != null && !agentId.equals(parentAgentId)) {
							ILocatorNode parent = locatorNode.getParent(ILocatorNode.class);
							ILocatorNode[] parentNodes = lkup
							                .lkupLocatorNodeByAgentId(parent, agentId);
							while (parentNodes.length == 0 && parent != null) {
								parent = parent.getParent(ILocatorNode.class);
								parentNodes = lkup.lkupLocatorNodeByAgentId(parent, agentId);
							}

							attributes = new HashMap<String, String>(attributes);

							if (parentAgentId == null && id.equals(parentId)) {
								attributes.put(IPeerProperties.PROP_PROXIES, parentProxies);
								IPeer peer = new TransientPeer(attributes);
								locatorNode.setProperty(IPeerNodeProperties.PROP_INSTANCE, peer);
							}
							else {
								attributes.put(IPeerProperties.PROP_PROXIES, encProxies);
								IPeer peer = new TransientPeer(attributes);
								if (existing == null) {
									if (parentNodes.length == 0 && !isFiltered(peer)) {
										locatorNode.add(new LocatorNode(peer));
									}
								}
								else {
									existing.setProperty(IPeerNodeProperties.PROP_INSTANCE, peer);
								}
							}
						}
					}
					for (ILocatorNode old : oldChildren) {
						if (!old.isStatic()) {
							locatorNode.remove(old, true);
						}
					}
					if (locatorNode.isStatic() && locatorNode.getPeer().getAgentID() != null) {
						String parentAgentId = locatorNode.getPeer().getAgentID();
						for (ILocatorNode child : locatorNode.getChildren(ILocatorNode.class)) {
							if (parentAgentId.equals(child.getPeer().getAgentID())) {
								locatorNode.remove(child, true);
							}
						}
					}
				}
				else {
					List<ILocatorNode> oldChildren = locatorNode.getChildren(ILocatorNode.class);
					for (ILocatorNode old : oldChildren) {
						if (old.isStatic()) {
							old.setProperty(IPeerNodeProperties.PROP_INSTANCE, locatorNode
							                .getProperty(ILocatorNode.PROPERTY_STATIC_INSTANCE));
						}
						else {
							locatorNode.remove(old, true);
						}
					}
				}
				IAsyncRefreshableCtx refreshCtx = (IAsyncRefreshableCtx) locatorNode
				                .getAdapter(IAsyncRefreshableCtx.class);
				refreshCtx.setQueryState(QueryType.CHILD_LIST, QueryState.DONE);

				// Invoke the callback
				invokeCallback(callback);
			}
		};

	}

	/**
	 * Returns if or if not the given peer is filtered.
	 *
	 * @param peer The peer or <code>null</code>.
	 * @return <code>True</code> if the given peer is filtered, <code>false</code> otherwise.
	 */
	protected boolean isFiltered(IPeer peer) {
		boolean filtered = peer == null;

		if (!filtered) {
			String value = peer.getAttributes().get("ValueAdd"); //$NON-NLS-1$
			boolean isValueAdd = value != null && ("1".equals(value.trim()) || Boolean.parseBoolean(value.trim())); //$NON-NLS-1$

			boolean isCli = peer.getName() != null && (peer.getName().endsWith("Command Server") || peer.getName().endsWith("CLI Server")); //$NON-NLS-1$ //$NON-NLS-2$

			filtered = isValueAdd || isCli;
		}

		return filtered;
	}

	/**
	 * Returns the list of root locations to lookup for static locator definitions.
	 *
	 * @return The list of root locations or an empty list.
	 */
	protected File[] getStaticLocatorsLookupDirectories() {
		// The list defining the root locations
		List<File> rootLocations = new ArrayList<File>();

		// always add default root location
		IPath defaultPath = ModelLocationUtil.getStaticLocatorsRootLocation();
		if (defaultPath != null) {
			File file = defaultPath.toFile();
			if (file.canRead() && file.isDirectory() && !rootLocations.contains(file)) {
				rootLocations.add(file);
			}
		}

		return rootLocations.toArray(new File[rootLocations.size()]);
	}

	/**
	 * Refresh the static locator definitions.
	 */
	protected void refreshStaticPeers() {

		// Get the root locations to lookup the static peer definitions
		File[] roots = getStaticLocatorsLookupDirectories();
		if (roots.length > 0) {
			// The lst of locator peers created from the static definitions
			List<IPeer> peers = new ArrayList<IPeer>();
			// Process the root locations
			for (File root : roots) {
				// List all "*.locator" files within the root location
				File[] candidates = root.listFiles(new FileFilter() {
					@Override
					public boolean accept(File pathname) {
						IPath path = new Path(pathname.getAbsolutePath());
						return path.getFileExtension() != null && path.getFileExtension()
						                .toLowerCase().equals("locator"); //$NON-NLS-1$
					}
				});
				// If there are ini files to read, process them
				if (candidates != null && candidates.length > 0) {

					for (File candidate : candidates) {
						try {
							IURIPersistenceService service = ServiceManager.getInstance()
							                .getService(IURIPersistenceService.class);
							IPeer tempPeer = (IPeer) service.read(IPeer.class, candidate
							                .getAbsoluteFile().toURI());
							Map<String, String> attrs = new HashMap<String, String>(tempPeer.getAttributes());
							// Remember the file path within the properties
							attrs.put(IPersistableNodeProperties.PROPERTY_URI, candidate
							                .getAbsoluteFile().toURI().toString());
							// Construct the peer from the attributes
							IPeer peer = new Peer(attrs);
							peers.add(peer);
						}
						catch (IOException e) {
							/* ignored on purpose */
						}
					}
				}
			}

			for (IPeer peer : peers) {
				ILocatorModelUpdateService update = getLocatorModel()
				                .getService(ILocatorModelUpdateService.class);
				update.add(peer, true);
			}
		}
	}
}

Back to the top