Skip to main content
summaryrefslogtreecommitdiffstats
blob: a99bf22eb910ff60f0d07d07f01152c8b535592e (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
/*******************************************************************************
 * Copyright (c) 2009 EclipseSource 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:
 *   EclipseSource - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.osgi.services.distribution;

import java.util.*;
import org.eclipse.ecf.core.ContainerTypeDescription;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.internal.osgi.services.distribution.Activator;
import org.eclipse.ecf.remoteservice.*;
import org.osgi.framework.ServiceReference;

/**
 * Default implementation of IHostContainerFinder.
 * 
 */
public class DefaultHostContainerFinder extends AbstractContainerFinder
		implements IHostContainerFinder {

	public IRemoteServiceContainer[] findHostContainers(
			ServiceReference serviceReference, String[] remoteInterfaces,
			String[] remoteConfigurationType, String[] remoteRequiresIntents) {
		Collection rsContainers = findRemoteContainersSatisfyingRequiredIntents(remoteRequiresIntents);
		List results = new ArrayList();
		for (Iterator i = rsContainers.iterator(); i.hasNext();) {
			IRemoteServiceContainer rsContainer = (IRemoteServiceContainer) i
					.next();
			if (includeContainer(serviceReference, rsContainer))
				results.add(rsContainer);
		}
		return (IRemoteServiceContainer[]) results
				.toArray(new IRemoteServiceContainer[] {});
	}

	protected Collection findRemoteContainersSatisfyingRequiredIntents(
			String[] remoteRequiresIntents) {
		List results = new ArrayList();
		IContainer[] containers = Activator.getDefault().getContainerManager()
				.getAllContainers();
		if (containers == null || containers.length == 0)
			return null;
		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 = (IRemoteServiceContainerAdapter) containers[i]
					.getAdapter(IRemoteServiceContainerAdapter.class);
			if (adapter == null)
				continue;
			// Get container type description and intents
			ContainerTypeDescription description = Activator.getDefault()
					.getContainerManager().getContainerTypeDescription(
							containers[i].getID());
			// If it has no description continue
			if (description == null)
				continue;
			List supportedIntents = Arrays.asList(description
					.getSupportedIntents());
			boolean hasIntents = true;
			if (remoteRequiresIntents != null) {
				for (int j = 0; j < remoteRequiresIntents.length; j++) {
					if (!supportedIntents.contains(remoteRequiresIntents[j]))
						hasIntents = false;
				}
			}
			if (hasIntents) {
				trace("findRemoteContainersSatisfyingRequiredIntents",
						"include containerID=" + containers[i].getID());
				results.add(new RemoteServiceContainer(containers[i], adapter));
			} else {
				trace("findRemoteContainersSatisfyingRequiredIntents",
						"exclude containerID=" + containers[i].getID()
								+ " supported intents=" + supportedIntents);
			}
		}
		return results;
	}

	protected boolean includeContainer(ServiceReference serviceReference,
			IRemoteServiceContainer rsContainer) {
		IContainer container = rsContainer.getContainer();
		Object cID = serviceReference
				.getProperty(org.eclipse.ecf.remoteservice.Constants.SERVICE_CONTAINER_ID);
		// If the SERVICE_CONTAINER_ID property is not set, then we'll include
		// it by default
		if (cID == null || !(cID instanceof ID)) {
			trace(
					"includeContainer",
					"serviceReference="
							+ serviceReference
							+ " does not set remote service container id service property.  INCLUDING containerID="
							+ container.getID() + " in remote registration");
			return true;
		}
		// Or if the id is specified and it's the same as the containerID under
		// consideration
		// then it's included
		ID containerID = (ID) cID;
		if (container.getID().equals(containerID)) {
			trace("includeContainer", "serviceReference=" + serviceReference
					+ " has MATCHING container id=" + containerID
					+ ".  INCLUDING rsca=" + container.getID()
					+ " in remote registration");
			return true;
		}
		trace("includeContainer", "serviceReference=" + serviceReference
				+ " has non-matching id=" + containerID + ".  EXCLUDING id="
				+ container.getID() + " in remote registration");
		return false;
	}

}

Back to the top