Skip to main content
summaryrefslogtreecommitdiffstats
blob: 17cceeb9f482997f49a27d8e67c7db4ca21a9ea5 (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
/*******************************************************************************
 * Copyright (c) 2010-2011 Composent, 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:
 *   Composent, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.osgi.services.remoteserviceadmin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.ecf.core.ContainerConnectException;
import org.eclipse.ecf.core.ContainerCreateException;
import org.eclipse.ecf.core.ContainerTypeDescription;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDCreateException;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.IDUtil;
import org.eclipse.ecf.remoteservice.IRemoteServiceContainer;
import org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter;
import org.eclipse.ecf.remoteservice.RemoteServiceContainer;
import org.osgi.framework.ServiceReference;

/**
 * Abstract superclass for host container selectors...i.e. implementers of
 * {@link IHostContainerSelector}.
 * 
 */
public abstract class AbstractHostContainerSelector extends
		AbstractContainerSelector {

	private static final String NODEFAULT = "<<nodefault>>"; //$NON-NLS-1$
	protected String[] defaultConfigTypes;

	public AbstractHostContainerSelector(String[] defaultConfigTypes) {
		this.defaultConfigTypes = defaultConfigTypes;
	}

	/**
	 * @since 2.0
	 */
	protected Collection selectExistingHostContainers(
			ServiceReference serviceReference,
			Map<String, Object> overridingProperties, String[] serviceExportedInterfaces,
			String[] serviceExportedConfigs, String[] serviceIntents) {
		List results = new ArrayList();
		// Get all existing containers
		IContainer[] containers = getContainers();
		// If nothing there, then return empty array
		if (containers == null || containers.length == 0)
			return results;

		for (int i = 0; i < containers.length; i++) {
			// Check to make sure it's a rs container adapter. If it's not go
			// onto next one
			IRemoteServiceContainerAdapter adapter = hasRemoteServiceContainerAdapter(containers[i]);
			if (adapter == null)
				continue;
			// Get container type description and intents
			ContainerTypeDescription description = getContainerTypeDescription(containers[i]);
			// If it has no description go onto next
			if (description == null)
				continue;

			// http://bugs.eclipse.org/331532
			if (!description.isServer()) {
				continue;
			}

			if (matchExistingHostContainer(serviceReference, overridingProperties, containers[i],
					adapter, description, serviceExportedConfigs,
					serviceIntents)) {
				trace("selectExistingHostContainers", "INCLUDING containerID=" //$NON-NLS-1$ //$NON-NLS-2$
						+ containers[i].getID()
						+ " configs=" //$NON-NLS-1$
						+ ((serviceExportedConfigs == null) ? "null" : Arrays //$NON-NLS-1$
								.asList(serviceExportedConfigs).toString())
						+ " intents=" //$NON-NLS-1$
						+ ((serviceIntents == null) ? "null" : Arrays.asList( //$NON-NLS-1$
								serviceIntents).toString()));
				results.add(new RemoteServiceContainer(containers[i], adapter));
			} else {
				trace("selectExistingHostContainers", "EXCLUDING containerID=" //$NON-NLS-1$ //$NON-NLS-2$
						+ containers[i].getID()
						+ " configs=" //$NON-NLS-1$
						+ ((serviceExportedConfigs == null) ? "null" : Arrays //$NON-NLS-1$
								.asList(serviceExportedConfigs).toString())
						+ " intents=" //$NON-NLS-1$
						+ ((serviceIntents == null) ? "null" : Arrays.asList( //$NON-NLS-1$
								serviceIntents).toString()));
			}
		}
		return results;
	}

	/**
	 * @since 2.0
	 */
	protected boolean matchHostContainerToConnectTarget(
			ServiceReference serviceReference,
			Map<String, Object> properties, IContainer container) {
		String target = (String) properties
				.get(RemoteConstants.ENDPOINT_CONNECTTARGET_ID);
		if (target == null)
			return true;
		// If a targetID is specified, make sure it either matches what the
		// container
		// is already connected to, or that we connect an unconnected container
		ID connectedID = container.getConnectedID();
		// If the container is not already connected to anything
		// then we connect it to the given target
		if (connectedID == null) {
			// connect to the target and we have a match
			try {
				connectHostContainer(serviceReference, properties,
						container, target);
			} catch (Exception e) {
				logException("doConnectContainer containerID=" //$NON-NLS-1$
						+ container.getID() + " target=" + target, e); //$NON-NLS-1$
				return false;
			}
			return true;
		} else {
			ID targetID = createTargetID(container, target);
			// We check here if the currently connectedID equals the target.
			// If it does we have a match
			if (connectedID.equals(targetID))
				return true;
		}
		return false;
	}

	/**
	 * @since 2.0
	 */
	protected boolean matchExistingHostContainer(
			ServiceReference serviceReference,
			Map<String, Object> properties, IContainer container,
			IRemoteServiceContainerAdapter adapter,
			ContainerTypeDescription description, String[] requiredConfigTypes,
			String[] requiredServiceIntents) {

		return matchHostSupportedConfigTypes(requiredConfigTypes, description)
				&& matchHostSupportedIntents(requiredServiceIntents,
						description)
				&& matchHostContainerID(serviceReference, properties,
						container)
				&& matchHostContainerToConnectTarget(serviceReference,
						properties, container);
	}

	/**
	 * @since 2.0
	 */
	protected boolean matchHostContainerID(ServiceReference serviceReference,
			Map<String, Object> properties, IContainer container) {

		ID containerID = container.getID();
		// No match if the container has no ID
		if (containerID == null)
			return false;

		// Then get containerid if specified directly by user in properties
		ID requiredContainerID = (ID) properties
				.get(RemoteConstants.SERVICE_EXPORTED_CONTAINER_ID);
		// If the CONTAINER_I
		if (requiredContainerID != null) {
			return requiredContainerID.equals(containerID);
		}
		// Else get the container factory arguments, create an ID from the
		// arguments
		// and check if the ID matches that
		Namespace ns = containerID.getNamespace();
		Object cid = properties
				.get(RemoteConstants.SERVICE_EXPORTED_CONTAINER_FACTORY_ARGS);
		// If no arguments are present, then any container ID should match
		if (cid == null)
			return true;
		ID cID = null;
		if (cid instanceof ID) {
			cID = (ID) cid;
		} else if (cid instanceof String) {
			cID = IDUtil.createID(ns, (String) cid);
		} else if (cid instanceof Object[]) {
			Object cido = ((Object[]) cid)[0];
			cID = IDUtil.createID(ns, new Object[] { cido });
		}
		if (cID == null)
			return true;
		return containerID.equals(cID);
	}

	protected boolean matchHostSupportedConfigTypes(
			String[] requiredConfigTypes,
			ContainerTypeDescription containerTypeDescription) {
		// if no config type is set the spec requires to create a default
		// endpoint (see section 122.5.1)
		if (requiredConfigTypes == null)
			return true;
		// Get supported config types for this description
		String[] supportedConfigTypes = getSupportedConfigTypes(containerTypeDescription);
		// If it doesn't support anything, return false
		if (supportedConfigTypes == null || supportedConfigTypes.length == 0)
			return false;
		// Turn supported config types for this description into list
		List supportedConfigTypesList = Arrays.asList(supportedConfigTypes);
		List requiredConfigTypesList = Arrays.asList(requiredConfigTypes);
		// We check all of the required config types and make sure
		// that they are present in the supportedConfigTypes
		boolean result = true;
		for (Iterator i = requiredConfigTypesList.iterator(); i.hasNext();)
			result &= supportedConfigTypesList.contains(i.next());
		return result;
	}

	/**
	 * @since 2.0
	 */
	protected Collection createAndConfigureHostContainers(
			ServiceReference serviceReference,
			Map<String, Object> properties, String[] serviceExportedInterfaces, String[] requiredConfigs,
			String[] requiredIntents) {

		List results = new ArrayList();
		ContainerTypeDescription[] descriptions = getContainerTypeDescriptions();
		if (descriptions == null)
			return Collections.EMPTY_LIST;
		// If there are no required configs specified, then create any defaults
		if (requiredConfigs == null || requiredConfigs.length == 0) {
			createAndAddDefaultContainers(serviceReference, properties, serviceExportedInterfaces, requiredIntents, descriptions, results);
		} else {
			// See if we have a match
			for (int i = 0; i < descriptions.length; i++) {
				IRemoteServiceContainer matchingContainer = createMatchingContainer(
						descriptions[i], serviceReference, properties,
						serviceExportedInterfaces, requiredConfigs,
						requiredIntents);
				if (matchingContainer != null)
					results.add(matchingContainer);
			}
		}
		return results;
	}

	private void createAndAddDefaultContainers(ServiceReference serviceReference, Map<String, Object> properties, String[] serviceExportedInterfaces, String[] requiredIntents, ContainerTypeDescription[] descriptions, Collection results) {
		ContainerTypeDescription[] ctds = getContainerTypeDescriptionsForDefaultConfigTypes(descriptions);
		if (ctds != null) {
			for (int i = 0; i < ctds.length; i++) {
				IRemoteServiceContainer matchingContainer = createMatchingContainer(ctds[i], serviceReference, properties, serviceExportedInterfaces, null, requiredIntents);
				if (matchingContainer != null)
					results.add(matchingContainer);
			}
		}
	}
	
	protected ContainerTypeDescription[] getContainerTypeDescriptionsForDefaultConfigTypes(
			ContainerTypeDescription[] descriptions) {
		String[] defaultConfigTypes = getDefaultConfigTypes();
		if (defaultConfigTypes == null || defaultConfigTypes.length == 0)
			return null;
		List results = new ArrayList();
		for (int i = 0; i < descriptions.length; i++) {
			// For each description, get supported config types
			String[] supportedConfigTypes = descriptions[i]
					.getSupportedConfigs();
			if (supportedConfigTypes != null
					&& matchDefaultConfigTypes(defaultConfigTypes,
							supportedConfigTypes))
				results.add(descriptions[i]);
		}
		return (ContainerTypeDescription[]) results
				.toArray(new ContainerTypeDescription[] {});
	}

	protected boolean matchDefaultConfigTypes(String[] defaultConfigTypes,
			String[] supportedConfigTypes) {
		List supportedConfigTypesList = Arrays.asList(supportedConfigTypes);
		for (int i = 0; i < defaultConfigTypes.length; i++) {
			if (supportedConfigTypesList.contains(defaultConfigTypes[i]))
				return true;
		}
		return false;
	}

	protected String[] getDefaultConfigTypes() {
		return defaultConfigTypes;
	}

	/**
	 * @since 2.0
	 */
	protected IRemoteServiceContainer createMatchingContainer(
			ContainerTypeDescription containerTypeDescription,
			ServiceReference serviceReference,
			Map<String, Object> properties, String[] serviceExportedInterfaces, String[] requiredConfigs,
			String[] requiredIntents) {

		if (matchHostSupportedConfigTypes(requiredConfigs,
				containerTypeDescription)
				&& matchHostSupportedIntents(requiredIntents,
						containerTypeDescription)) {
			return createRSContainer(serviceReference, properties, containerTypeDescription);
		}
		return null;
	}

	/**
	 * @since 2.0
	 */
	protected IRemoteServiceContainer createRSContainer(
			ServiceReference serviceReference,
			Map<String, Object> properties, ContainerTypeDescription containerTypeDescription) {
		try {
			IContainer container = createContainer(serviceReference, properties,
					containerTypeDescription);
			IRemoteServiceContainerAdapter adapter = (IRemoteServiceContainerAdapter) container
					.getAdapter(IRemoteServiceContainerAdapter.class);
			if (adapter == null)
				throw new ContainerCreateException(
						"Container does not implement IRemoteServiceContainerAdapter"); //$NON-NLS-1$
			return new RemoteServiceContainer(container, adapter);
		} catch (Exception e) {
			logException(
					"Exception creating container from ContainerTypeDescription=" //$NON-NLS-1$
							+ containerTypeDescription, e);
			return null;
		}
	}

	/**
	 * @since 2.0
	 */
	protected void connectHostContainer(ServiceReference serviceReference,
			Map<String, Object> properties, IContainer container, Object target)
			throws ContainerConnectException, IDCreateException {
		ID targetID = (target instanceof String) ? IDUtil.createID(
				container.getConnectNamespace(), (String) target) : IDUtil
				.createID(container.getConnectNamespace(),
						new Object[] { target });
		Object context = properties
				.get(RemoteConstants.SERVICE_EXPORTED_CONTAINER_CONNECT_CONTEXT);
		IConnectContext connectContext = null;
		if (context != null) {
			connectContext = createConnectContext(serviceReference, properties, container,
					context);
		}
		// connect the container
		container.connect(targetID, connectContext);
	}

	protected boolean matchHostSupportedIntents(
			String[] serviceRequiredIntents,
			ContainerTypeDescription containerTypeDescription) {
		// If there are no required intents then we have a match
		if (serviceRequiredIntents == null)
			return true;

		String[] supportedIntents = getSupportedIntents(containerTypeDescription);

		if (supportedIntents == null)
			return false;

		List supportedIntentsList = Arrays.asList(supportedIntents);

		boolean result = true;
		for (int i = 0; i < serviceRequiredIntents.length; i++)
			result = result
					&& supportedIntentsList.contains(serviceRequiredIntents[i]);

		return result;
	}

}

Back to the top