Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9cf2ffa15bc23fb635ec23abfc620b64a206d1f2 (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
/*******************************************************************************
 * 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.locator.services.selection;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.runtime.Assert;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNodeProperties;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService;

/**
 * Default context service filter implementation to filter peer model nodes depending on their
 * runtime and offline services.
 */
public class RuntimeServiceContextFilter implements IDefaultContextService.IContextFilter {

	protected final String[] serviceNames;
	protected final boolean useDisconnectedContexts;

	/**
	 * Constructor.
	 * @param serviceName The service name to check.
	 * @param useDisconnectedContexts <code>true</code> if disconnected targets should be selected too.
	 */
	public RuntimeServiceContextFilter(String serviceName, boolean useDisconnectedContexts) {
		this.serviceNames = new String[]{serviceName};
		this.useDisconnectedContexts = useDisconnectedContexts;
	}

	/**
	 * Constructor.
	 * @param serviceNames The list of service names to check.
	 * @param useDisconnectedContexts <code>true</code> if disconnected targets should be selected too.
	 */
	public RuntimeServiceContextFilter(String[] serviceNames, boolean useDisconnectedContexts) {
		this.serviceNames = serviceNames;
		this.useDisconnectedContexts = useDisconnectedContexts;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService.IContextFilter#select(org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode)
	 */
	@Override
	public boolean select(final IPeerNode peerNode) {
		Assert.isNotNull(peerNode);

		final IPeer peer = peerNode.getPeer();
		final AtomicBoolean result = new AtomicBoolean(false);
		Protocol.invokeAndWait(new Runnable() {
			@Override
			public void run() {
				List<String> list;
				String services = peerNode.getStringProperty(IPeerNodeProperties.PROP_REMOTE_SERVICES);
				if (services != null) {
					list = Arrays.asList(services.split(",\\s*")); //$NON-NLS-1$
					boolean containsAll = true;
					for (String serviceName : serviceNames) {
						containsAll &= list.contains(serviceName);
						if (!containsAll) {
							break;
						}
					}
					result.set(containsAll);
					return;
				}

				if (useDisconnectedContexts) {
					services = peer.getAttributes().get(IPeerNodeProperties.PROP_OFFLINE_SERVICES);
					list = services != null ? Arrays.asList(services.split(",\\s*")) : Collections.EMPTY_LIST; //$NON-NLS-1$
					boolean containsAll = true;
					for (String serviceName : serviceNames) {
						containsAll &= list.contains(serviceName);
						if (!containsAll) {
							break;
						}
					}
					result.set(containsAll);
				}
			}
		});
		return result.get();
	}

}

Back to the top