Skip to main content
summaryrefslogtreecommitdiffstats
blob: 53f11a38021fec78bdc18eec5362413a1872ab9b (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
/*******************************************************************************
 * 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.Collection;
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 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) {
		// Find previously created containers that match the given
		// serviceExportedConfigs and serviceIntents
		Collection rsContainers = selectExistingHostContainers(
				serviceReference, overridingProperties,
				serviceExportedInterfaces, serviceExportedConfigs,
				serviceIntents);

		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$
					}
				}

			}
		}

		// return result
		return (IRemoteServiceContainer[]) rsContainers
				.toArray(new IRemoteServiceContainer[] {});
	}

	public void close() {
	}
}

Back to the top