Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 59916440bf9485d33b27e8586e0dc6aceedab0cf (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
/*******************************************************************************
 * 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.List;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
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.IContainerFactory;
import org.eclipse.ecf.core.IContainerManager;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.Activator;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.DebugOptions;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.IDUtil;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.LogUtility;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.PropertiesUtil;
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 both host and consumer container selectors...i.e.
 * implementers of {@link IHostContainerSelector} or
 * {@link IConsumerContainerSelector}.
 * 
 */
public abstract class AbstractContainerSelector {

	public static final IRemoteServiceContainer[] EMPTY_REMOTE_SERVICE_CONTAINER_ARRAY = new IRemoteServiceContainer[] {};

	protected IContainerManager getContainerManager() {
		return Activator.getDefault().getContainerManager();
	}

	protected IContainerFactory getContainerFactory() {
		return getContainerManager().getContainerFactory();
	}

	protected ContainerTypeDescription[] getContainerTypeDescriptions() {
		return (ContainerTypeDescription[]) getContainerFactory()
				.getDescriptions().toArray(new ContainerTypeDescription[] {});
	}

	protected IContainer[] getContainers() {
		return getContainerManager().getAllContainers();
	}

	protected IRemoteServiceContainerAdapter hasRemoteServiceContainerAdapter(
			IContainer container) {
		return (IRemoteServiceContainerAdapter) container
				.getAdapter(IRemoteServiceContainerAdapter.class);
	}

	protected ContainerTypeDescription getContainerTypeDescription(
			IContainer container) {
		return getContainerManager().getContainerTypeDescription(
				container.getID());
	}

	protected IRemoteServiceContainer[] getRemoteServiceContainers(
			IContainer[] containers) {
		List results = new ArrayList();
		for (int i = 0; i < containers.length; i++) {
			IRemoteServiceContainerAdapter adapter = hasRemoteServiceContainerAdapter(containers[i]);
			if (adapter != null)
				results.add(new RemoteServiceContainer(containers[i], adapter));
		}
		return (IRemoteServiceContainer[]) results
				.toArray(new IRemoteServiceContainer[] {});
	}

	protected boolean includeContainerWithConnectNamespace(
			IContainer container, String connectNamespaceName) {
		if (connectNamespaceName != null) {
			Namespace namespace = container.getConnectNamespace();
			if (namespace != null
					&& namespace.getName().equals(connectNamespaceName))
				return true;
		}
		return false;
	}

	protected void connectContainer(IContainer container, ID connectTargetID,
			IConnectContext connectContext) throws ContainerConnectException {
		trace("connectContainer", "Connecting container=" + container.getID() //$NON-NLS-1$ //$NON-NLS-2$
				+ " to connectTargetID=" + connectTargetID); //$NON-NLS-1$
		container.connect(connectTargetID, connectContext);
	}

	protected String[] getSupportedConfigTypes(
			ContainerTypeDescription containerTypeDescription) {
		String[] supportedConfigs = containerTypeDescription
				.getSupportedConfigs();
		return (supportedConfigs == null) ? new String[0] : supportedConfigs;
	}

	protected String[] getSupportedIntents(
			ContainerTypeDescription containerTypeDescription) {
		String[] supportedIntents = containerTypeDescription
				.getSupportedIntents();
		return (supportedIntents == null) ? new String[0] : supportedIntents;
	}

	/**
	 * @param serviceReference serviceReference
	 * @param properties overriding properties
	 * @param containerTypeDescription containerTypeDescription
	 * @return IContainer created container.  Should not be <code>null</code>
	 * @throws SelectContainerException thrown if some create or configure failure
	 * @since 2.0
	 */
	protected IContainer createContainer(ServiceReference serviceReference,
			Map<String, Object> properties,
			ContainerTypeDescription containerTypeDescription)
			throws SelectContainerException {

		IContainerFactory containerFactory = getContainerFactory();

		final Object containerFactoryArguments = getContainerFactoryArguments(
				serviceReference, properties, containerTypeDescription);

		try {
			if (containerFactoryArguments instanceof String) {
				return containerFactory.createContainer(
						containerTypeDescription,
						(String) containerFactoryArguments);
			} else if (containerFactoryArguments instanceof ID) {
				return containerFactory.createContainer(
						containerTypeDescription,
						(ID) containerFactoryArguments);
			} else if (containerFactoryArguments instanceof Object[]) {
				return containerFactory.createContainer(
						containerTypeDescription,
						(Object[]) containerFactoryArguments);
			} else if (containerFactoryArguments instanceof Map) {
				return containerFactory.createContainer(
						containerTypeDescription,
						(Map) containerFactoryArguments);
			}
			return containerFactory.createContainer(containerTypeDescription);
		} catch (ContainerCreateException e) {
			throw new SelectContainerException(
					"Exception creating or configuring container", e, //$NON-NLS-1$
					containerTypeDescription);
		}
	}

	/**
	 * @param serviceReference serviceReference
	 * @param properties overriding properties
	 * @param containerTypeDescription containerTypeDescription
	 * @return Object container factory arguments to use
	 * @since 2.0
	 */
	protected Object getContainerFactoryArguments(
			ServiceReference serviceReference, Map<String, Object> properties,
			ContainerTypeDescription containerTypeDescription) {
		// If the RemoteConstants.SERVICE_EXPORTED_CONTAINER_FACTORY_ARGS is set
		// than use it.
		final Object containerFactoryArguments = properties
				.get(RemoteConstants.SERVICE_EXPORTED_CONTAINER_FACTORY_ARGS);
		if (containerFactoryArguments != null)
			return containerFactoryArguments;

		String exportedConfig = containerTypeDescription.getName();
		// If not, then we look through the properties that start with
		// <containerTypeDescription.name>.
		Map<String, Object> results = PropertiesUtil.getConfigProperties(exportedConfig, properties);
		if (results.size() == 0)
			return null;
		return results;
	}

	protected ID createTargetID(IContainer container, String target) {
		return IDUtil.createID(container.getConnectNamespace(), target);
	}

	protected void disconnectContainer(IContainer container) {
		container.disconnect();
	}

	/**
	 * @param serviceReference serviceReference
	 * @param properties overriding properties
	 * @param container the container to create connect context for
	 * @param context a possible connect context]
	 * @return IConnectContext a connect context or null
	 * @since 2.0
	 */
	protected IConnectContext createConnectContext(
			ServiceReference serviceReference, Map<String, Object> properties,
			IContainer container, Object context) {
		if (context instanceof IConnectContext)
			return (IConnectContext) context;
		return null;
	}

	protected void logException(String string, Exception e) {
		Activator.getDefault().log(
				new Status(IStatus.ERROR, Activator.PLUGIN_ID, string, e));
	}

	protected void trace(String methodName, String message) {
		LogUtility.trace(methodName, DebugOptions.CONTAINER_SELECTOR,
				this.getClass(), message);
	}

	protected void traceException(String methodName, String message, Throwable t) {
		LogUtility.traceException(methodName, DebugOptions.EXCEPTIONS_CATCHING,
				this.getClass(), message, t);
	}

	protected void logError(String methodName, String message, Throwable t) {
		LogUtility.logError(methodName, DebugOptions.CONTAINER_SELECTOR,
				this.getClass(), message, t);
	}

	protected void logError(String methodName, String message) {
		LogUtility.logError(methodName, DebugOptions.CONTAINER_SELECTOR,
				this.getClass(), message);
	}

	protected void logWarning(String methodName, String message) {
		LogUtility.logWarning(methodName, DebugOptions.CONTAINER_SELECTOR,
				this.getClass(), message);
	}

	protected boolean matchConnectNamespace(IContainer container,
			ID endpointID, ID connectTargetID) {
		if (connectTargetID != null) {
			return connectTargetID.getNamespace().getName()
					.equals(container.getConnectNamespace().getName());
		}
		if (endpointID == null)
			return false;
		return endpointID.getNamespace().getName()
				.equals(container.getConnectNamespace().getName());
	}

	protected boolean matchContainerID(IContainer container, ID endpointID) {
		if (endpointID == null)
			return false;
		return endpointID.equals(container.getID());
	}

}

Back to the top