Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0096ba9765775c88041a9645d4d4b9c0f7b4a1d6 (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
/****************************************************************************
 * Copyright (c) 2010-2011 Composent, Inc. and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *   Composent, Inc. - initial API and implementation
 *
 * SPDX-License-Identifier: EPL-2.0
 *****************************************************************************/
package org.eclipse.ecf.osgi.services.remoteserviceadmin;

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

import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.remoteservice.IRemoteServiceContainer;
import org.osgi.framework.ServiceReference;

/**
 * Default implementation of {@link IHostContainerSelector} service.
 * 
 */
public class HostContainerSelector extends AbstractHostContainerSelector
		implements IHostContainerSelector {

	private static final boolean reuseExistingContainers = new Boolean(
			System.getProperty(
					"org.eclipse.ecf.osgi.services.remoteserviceadmin.HostContainerSelector.reuseExistingContainers", //$NON-NLS-1$
					"true")).booleanValue(); //$NON-NLS-1$

	private boolean autoCreateContainer = false;

	public HostContainerSelector(String[] defaultConfigTypes,
			boolean autoCreateContainer) {
		super(defaultConfigTypes);
		this.autoCreateContainer = autoCreateContainer;
	}

	// Adding synchronized to make the host container finding
	// thread safe to deal with bug
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=331836
	/**
	 * @see org.eclipse.ecf.osgi.services.remoteserviceadmin.IHostContainerSelector#selectHostContainers(org.osgi.framework.ServiceReference,
	 *      java.util.Map, java.lang.String[], java.lang.String[],
	 *      java.lang.String[])
	 * @since 2.0
	 */
	public synchronized IRemoteServiceContainer[] selectHostContainers(
			ServiceReference serviceReference,
			Map<String, Object> overridingProperties,
			String[] serviceExportedInterfaces,
			String[] serviceExportedConfigs, String[] serviceIntents)
			throws SelectContainerException {
		trace("selectHostContainers", //$NON-NLS-1$
				"serviceReference=" + serviceReference + ",overridingProperties=" + overridingProperties //$NON-NLS-1$ //$NON-NLS-2$
						+ ",exportedInterfaces=" //$NON-NLS-1$
						+ ((serviceExportedInterfaces == null) ? Collections.EMPTY_LIST
								: Arrays.asList(serviceExportedInterfaces))
						+ ",serviceExportedConfigs=" //$NON-NLS-1$
						+ ((serviceExportedConfigs == null) ? Collections.EMPTY_LIST
								: Arrays.asList(serviceExportedConfigs))
						+ ",serviceIntents=" //$NON-NLS-1$
						+ ((serviceIntents == null) ? Collections.EMPTY_LIST : Arrays.asList(serviceIntents)));
		// Find previously created containers that match the given
		// serviceExportedConfigs and serviceIntents
		Collection rsContainers = (reuseExistingContainers) ? selectExistingHostContainers(
				serviceReference, overridingProperties,
				serviceExportedInterfaces, serviceExportedConfigs,
				serviceIntents) : Collections.EMPTY_LIST;

		if (rsContainers.size() == 0 && autoCreateContainer) {
			// If no existing containers are found we'll go through
			// finding/creating/configuring/connecting
			rsContainers = createAndConfigureHostContainers(serviceReference,
					overridingProperties, serviceExportedInterfaces,
					serviceExportedConfigs, serviceIntents);

			// if SERVICE_EXPORTED_CONTAINER_CONNECT_TARGET service property is
			// specified, then
			// connect the host container(s)
			Object target = overridingProperties
					.get(RemoteConstants.ENDPOINT_CONNECTTARGET_ID);
			if (target != null) {
				for (Iterator i = rsContainers.iterator(); i.hasNext();) {
					IContainer container = ((IRemoteServiceContainer) i.next())
							.getContainer();
					try {
						connectHostContainer(serviceReference,
								overridingProperties, container, target);
					} catch (Exception e) {
						logException("doConnectContainer failure containerID=" //$NON-NLS-1$
								+ container.getID() + " target=" + target, e); //$NON-NLS-1$
					}
				}

			}
		}

		trace("selectHostContainers","rsContainers selected="+rsContainers); //$NON-NLS-1$ //$NON-NLS-2$
		// return result
		return (IRemoteServiceContainer[]) rsContainers
				.toArray(new IRemoteServiceContainer[rsContainers.size()]);
	}

	public void close() {
	}
}

Back to the top