Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5382e02485e852ea16bd4130cb13acb86f9f2ce6 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 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.internal;

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

import org.eclipse.core.expressions.PropertyTester;
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.activator.CoreBundleActivator;
import org.eclipse.tcf.te.tcf.locator.interfaces.ITracing;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.ILocatorModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModelProperties;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelPeerNodeQueryService;
import org.eclipse.tcf.te.tcf.locator.nodes.PeerRedirector;

/**
 * Locator model property tester.
 */
public class LocatorModelPropertyTester extends PropertyTester {

	/* (non-Javadoc)
	 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
	 */
	@Override
	public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
		// The receiver is expected to be a peer model node or a peer
		if (receiver instanceof IPeerModel || receiver instanceof IPeer) {
			final AtomicBoolean result = new AtomicBoolean();

			// If we have to test for local or remote services, we have to handle it special
			if ("hasLocalService".equals(property) || "hasRemoteService".equals(property)) { //$NON-NLS-1$ //$NON-NLS-2$
				// This tests must happen outside the TCF dispatch thread's
				if (!Protocol.isDispatchThread()) {
					result.set(testServices((IPeerModel) receiver, property, args, expectedValue));
				}
			}
			else {
				Runnable runnable = new Runnable() {
					@Override
					public void run() {
						if (receiver instanceof IPeerModel) {
							result.set(testPeerModel((IPeerModel) receiver, property, args, expectedValue));
						} else {
							result.set(testPeer((IPeer) receiver, property, args, expectedValue));
						}
					}
				};

				if (Protocol.isDispatchThread()) {
					runnable.run();
				}
				else {
					Protocol.invokeAndWait(runnable);
				}
			}

			return result.get();
		}
		return false;
	}

	/**
	 * Test the specific peer model node properties.
	 *
	 * @param node The model node. Must not be <code>null</code>.
	 * @param property The property to test.
	 * @param args The property arguments.
	 * @param expectedValue The expected value.
	 *
	 * @return <code>True</code> if the property to test has the expected value, <code>false</code>
	 *         otherwise.
	 */
	protected boolean testPeerModel(IPeerModel node, String property, Object[] args, Object expectedValue) {
		Assert.isNotNull(node);
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		return testPeer(node.getPeer(), property, args, expectedValue);
	}

	/**
	 * Test the specific peer properties.
	 *
	 * @param node The peer. Must not be <code>null</code>.
	 * @param property The property to test.
	 * @param args The property arguments.
	 * @param expectedValue The expected value.
	 *
	 * @return <code>True</code> if the property to test has the expected value, <code>false</code>
	 *         otherwise.
	 */
	protected boolean testPeer(IPeer node, String property, Object[] args, Object expectedValue) {
		Assert.isNotNull(node);
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		if ("name".equals(property)) { //$NON-NLS-1$
			if (node.getName() != null && node.getName().equals(expectedValue)) {
				return true;
			}
		}

		if ("nameRegex".equals(property) && expectedValue instanceof String) { //$NON-NLS-1$
			if (node.getName() != null && node.getName().matches((String)expectedValue)) {
				return true;
			}
		}

		if ("osName".equals(property)) { //$NON-NLS-1$
			if (node.getOSName() != null && node.getOSName().equals(expectedValue)) {
				return true;
			}
		}

		if ("osNameRegex".equals(property) && expectedValue instanceof String) { //$NON-NLS-1$
			if (node.getOSName() != null && node.getOSName().matches((String)expectedValue)) {
				return true;
			}
		}

		if ("isStaticPeer".equals(property)) { //$NON-NLS-1$
			String value = node.getAttributes().get("static.transient"); //$NON-NLS-1$
			boolean isStaticPeer = value != null && Boolean.parseBoolean(value.trim());
			if (expectedValue instanceof Boolean) {
				return ((Boolean) expectedValue).booleanValue() == isStaticPeer;
			}
		}

		if ("isRedirected".equals(property)) { //$NON-NLS-1$
			boolean isRedirected = node instanceof PeerRedirector;
			if (expectedValue instanceof Boolean) {
				return ((Boolean) expectedValue).booleanValue() == isRedirected;
			}
		}

		if ("isProxy".equals(property)) { //$NON-NLS-1$
			boolean isProxy = node.getAttributes().containsKey("Proxy"); //$NON-NLS-1$
			if (expectedValue instanceof Boolean) {
				return ((Boolean) expectedValue).booleanValue() == isProxy;
			}
		}

		if ("isValueAdd".equals(property)) { //$NON-NLS-1$
			String value = node.getAttributes().get("ValueAdd"); //$NON-NLS-1$
			boolean isValueAdd = value != null && ("1".equals(value.trim()) || Boolean.parseBoolean(value.trim())); //$NON-NLS-1$
			if (expectedValue instanceof Boolean) {
				return ((Boolean) expectedValue).booleanValue() == isValueAdd;
			}
		}

		if ("isOfType".equals(property)) { //$NON-NLS-1$
			String value = node.getAttributes().get(IPeerModelProperties.PROP_TYPE);
			if (expectedValue instanceof String) {
				return value != null ? ((String)expectedValue).equals(value) : ((String)expectedValue).equalsIgnoreCase("null"); //$NON-NLS-1$
			}
		}

		if ("hasAttribute".equals(property)) { //$NON-NLS-1$
			String name = args != null && args.length > 0 ? (String)args[0] : null;
			boolean hasAttribute = name != null && !"".equals(name) ? node.getAttributes().containsKey(name) : false; //$NON-NLS-1$
			if (expectedValue instanceof Boolean) {
				return ((Boolean) expectedValue).booleanValue() == hasAttribute;
			}
		}

		if ("isAttribute".equals(property)) { //$NON-NLS-1$
			String name = args != null && args.length > 0 ? (String)args[0] : null;
			String value = name != null && !"".equals(name) ? node.getAttributes().get(name) : null; //$NON-NLS-1$
			if (expectedValue != null) {
				return expectedValue.toString().equals(value);
			}
		}

		if ("hasOfflineService".equals(property)) { //$NON-NLS-1$
			String services = node.getAttributes().get(IPeerModelProperties.PROP_OFFLINE_SERVICES);
			List<String> list = services != null ? Arrays.asList(services.split(",\\s*")) : Collections.EMPTY_LIST; //$NON-NLS-1$
			return list.contains(expectedValue);
		}

		return false;
	}

	/**
	 * Test for the peer model node local or remote services.
	 * <p>
	 * <b>Node:</b> This method cannot be called from within the TCF Dispatch Thread.
	 *
	 * @param node The model node. Must not be <code>null</code>.
	 * @param property The property to test.
	 * @param args The property arguments.
	 * @param expectedValue The expected value.
	 *
	 * @return <code>True</code> if the property to test has the expected value, <code>false</code>
	 *         otherwise.
	 */
	protected boolean testServices(final IPeerModel node, final String property, final Object[] args, final Object expectedValue) {
		Assert.isNotNull(node);
		Assert.isTrue(!Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		String services = null;

		ILocatorModel model = node.getModel();
		ILocatorModelPeerNodeQueryService queryService = model.getService(ILocatorModelPeerNodeQueryService.class);
		if ("hasLocalService".equals(property)) { //$NON-NLS-1$
			services = queryService.queryLocalServices(node);
		} else {
			services = queryService.queryRemoteServices(node);
		}

		if (CoreBundleActivator.getTraceHandler().isSlotEnabled(ITracing.ID_TRACE_PROPERTY_TESTER)) {
			CoreBundleActivator.getTraceHandler().trace("testServices: property = " + property + ", expectedValue = " + expectedValue + ", services = " + services, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
														ITracing.ID_TRACE_PROPERTY_TESTER, LocatorModelPropertyTester.this);
		}

		if (services != null) {
			// Lookup each service individually to avoid "accidental" matching
			for (String service : services.split(",")) { //$NON-NLS-1$
				if (service != null && service.trim().equals(expectedValue)) {
					return true;
				}
			}
		}

		return false;
	}
}

Back to the top