Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

aboutsummaryrefslogtreecommitdiffstats
blob: 41f5667cec763b6998689f48ebac907c9efe1fe1 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/****************************************************************************
 * 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.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.ecf.discovery.IServiceInfo;
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.LogUtility;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.PropertiesUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.remoteserviceadmin.EndpointEventListener;
import org.osgi.service.remoteserviceadmin.EndpointListener;
import org.osgi.service.remoteserviceadmin.ImportRegistration;
import org.osgi.util.tracker.ServiceTracker;

/**
 * Abstract superclass for topology managers. This abstract superclass provides
 * basic functionality for topology managers to reuse. New topology managers can
 * extend this class to get or customize desired functionality. Alternatively,
 * they can use this class as a guide to implementing desired topology manager
 * behavior. For description of the role of topology managers see the
 * <a href="http://www.osgi.org/download/r4v42/r4.enterprise.pdf">OSGI 4.2
 * Remote Service Admin specification (chap 122)</a>.
 * 
 */
public abstract class AbstractTopologyManager {

	public static final String SERVICE_EXPORTED_INTERFACES_WILDCARD = "*"; //$NON-NLS-1$

	private BundleContext context;

	private ServiceTracker<IServiceInfoFactory, IServiceInfoFactory> serviceInfoFactoryTracker;

	private ServiceTracker remoteServiceAdminTracker;
	private Object remoteServiceAdminTrackerLock = new Object();

	class EndpointRegistrationHolder {
		private final org.osgi.service.remoteserviceadmin.EndpointDescription ed;
		private final ServiceRegistration<IServiceInfo> reg;
		
		EndpointRegistrationHolder(org.osgi.service.remoteserviceadmin.EndpointDescription ed, ServiceRegistration<IServiceInfo> reg) {
			this.ed = ed;
			this.reg = reg;
		}
		
		void unadvertise() {
			unadvertiseEndpointDescription(this.ed);
		}

		public void unregister() {
			try {
				this.reg.unregister();
			} catch (Exception e) {
				logError("unregister","Exception in unregistering service info registration",e); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
	}
	
	private final Map<String, List<EndpointRegistrationHolder>> registrations = new HashMap<String, List<EndpointRegistrationHolder>>();
	private final Lock registrationLock;

	private boolean requireServiceExportedConfigs = new Boolean(System.getProperty(
			"org.eclipse.ecf.osgi.services.remoteserviceadmin.AbstractTopologyManager.requireServiceExportedConfigs", //$NON-NLS-1$
			"false")).booleanValue(); //$NON-NLS-1$

	public AbstractTopologyManager(BundleContext context) {
		serviceInfoFactoryTracker = new ServiceTracker(context, createISIFFilter(context), null);
		serviceInfoFactoryTracker.open();
		this.context = context;
		// Use a FAIR lock here to guarantee that an endpoint removed operation
		// for EP x never executes before its corresponding endpoint added op
		// for EP x.
		// This might happen for an unfair lock (e.g. synchronized) because it
		// doesn't maintain ordering of the waiting threads.
		this.registrationLock = new ReentrantLock(true);
	}

	protected BundleContext getContext() {
		return context;
	}

	protected String getFrameworkUUID() {
		Activator a = Activator.getDefault();
		if (a == null)
			return null;
		return a.getFrameworkUUID();
	}

	public void close() {
		registrationLock.lock();
		try {
			registrations.entrySet().stream().forEach(entry -> {
				entry.getValue().forEach(h -> h.unadvertise());
			});
			registrations.clear();
		} finally {
			registrationLock.unlock();
		}
		synchronized (remoteServiceAdminTrackerLock) {
			if (remoteServiceAdminTracker != null) {
				remoteServiceAdminTracker.close();
				remoteServiceAdminTracker = null;
			}
		}
		context = null;
	}

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

	protected Filter createRSAFilter() {
		String filterString = "(&(" //$NON-NLS-1$
				+ org.osgi.framework.Constants.OBJECTCLASS + "=" //$NON-NLS-1$
				+ org.osgi.service.remoteserviceadmin.RemoteServiceAdmin.class.getName() + ")(" //$NON-NLS-1$
				+ org.eclipse.ecf.osgi.services.remoteserviceadmin.RemoteServiceAdmin.SERVICE_PROP + "=*))"; //$NON-NLS-1$
		try {
			return getContext().createFilter(filterString);
		} catch (InvalidSyntaxException doesNotHappen) {
			// Should never happen
			doesNotHappen.printStackTrace();
			return null;
		}
	}

	/**
	 * @param ctx the bundle context
	 * @return Filter the created filter
	 * @since 4.0
	 */
	protected Filter createISIFFilter(BundleContext ctx) {
		String filterString = "(" //$NON-NLS-1$
				+ org.osgi.framework.Constants.OBJECTCLASS + "=" //$NON-NLS-1$
				+ IServiceInfoFactory.class.getName() + ")"; //$NON-NLS-1$
		try {
			return ctx.createFilter(filterString);
		} catch (InvalidSyntaxException doesNotHappen) {
			// Should never happen
			doesNotHappen.printStackTrace();
			return null;
		}
	}

	protected org.osgi.service.remoteserviceadmin.RemoteServiceAdmin getRemoteServiceAdmin() {
		synchronized (remoteServiceAdminTrackerLock) {
			if (remoteServiceAdminTracker == null) {
				remoteServiceAdminTracker = new ServiceTracker(Activator.getContext(), createRSAFilter(), null);
				remoteServiceAdminTracker.open();
			}
		}
		return (org.osgi.service.remoteserviceadmin.RemoteServiceAdmin) remoteServiceAdminTracker.getService();
	}

	private void addRegistration(org.osgi.service.remoteserviceadmin.EndpointDescription ed,
			ServiceRegistration<IServiceInfo> reg) {
		List<EndpointRegistrationHolder> ehs = this.registrations.get(ed.getId());
		if (ehs == null) {
			ehs = new ArrayList<EndpointRegistrationHolder>();
		}
		ehs.add(new EndpointRegistrationHolder(ed,reg));
		this.registrations.put(ed.getId(),ehs);
	}

	private Map<String,Object> removeOSGiProperties(Map<String,Object> m) {
		HashMap<String,Object> result = new HashMap<String,Object>(m);
		for(Iterator<String> i = result.keySet().iterator(); i.hasNext(); ) {
			String key = i.next();
			if (PropertiesUtil.isOSGiProperty(key) || PropertiesUtil.isECFProperty(key)) {
				i.remove();
			}
		}
		return result;
	}
	/**
	 * @since 4.9
	 */
	protected boolean hasDescription(org.osgi.service.remoteserviceadmin.EndpointDescription ed) {
		Map<String, Object> edProps = removeOSGiProperties(ed.getProperties());
		List<EndpointRegistrationHolder> ehs = this.registrations.get(ed.getId());
		if (ehs != null) {
			for(int i = ehs.size()-1; i >= 0; i--) {
				Map<String,Object> ehsProps = removeOSGiProperties(ehs.get(i).ed.getProperties());
				if (edProps.equals(ehsProps)) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * @param endpointDescription endpoint description
	 * @since 4.1
	 */
	protected void advertiseModifyEndpointDescription(
			org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription) {
		this.registrationLock.lock();
		try {
			final IServiceInfoFactory service = serviceInfoFactoryTracker.getService();
			if (service != null) {
				if (!hasDescription(endpointDescription)) {
					final IServiceInfo serviceInfo = service.createServiceInfo(null, endpointDescription);
					if (serviceInfo != null) {
						trace("advertiseModifyEndpointDescription", //$NON-NLS-1$
								"advertising modify endpointDescription=" + endpointDescription + //$NON-NLS-1$
										" and IServiceInfo " + serviceInfo); //$NON-NLS-1$

						final ServiceRegistration<IServiceInfo> registerService = this.context
								.registerService(IServiceInfo.class, serviceInfo, null);

						addRegistration(endpointDescription, registerService);
					} else {
						logError("advertiseModifyEndpointDescription", //$NON-NLS-1$
								"IServiceInfoFactory failed to convert EndpointDescription " + endpointDescription); //$NON-NLS-1$ 1
					}
				}
			} else {
				logError("advertiseModifyEndpointDescription", //$NON-NLS-1$
						"no IServiceInfoFactory service found"); //$NON-NLS-1$
			}
		} finally {
			this.registrationLock.unlock();
		}
	}

	/**
	 * @param endpointDescription endpoint description
	 * @since 3.0
	 */
	protected void advertiseEndpointDescription(
			org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription) {
		this.registrationLock.lock();
		try {
			if (this.registrations.containsKey(endpointDescription.getId())) {
				return;
			}
			final IServiceInfoFactory service = serviceInfoFactoryTracker.getService();
			if (service != null) {
				final IServiceInfo serviceInfo = service.createServiceInfo(null, endpointDescription);
				if (serviceInfo != null) {
					trace("advertiseEndpointDescription", //$NON-NLS-1$
							"advertising endpointDescription=" + endpointDescription + //$NON-NLS-1$
									" and IServiceInfo " + serviceInfo); //$NON-NLS-1$

					final ServiceRegistration<IServiceInfo> registerService = this.context
							.registerService(IServiceInfo.class, serviceInfo, null);
					
					addRegistration(endpointDescription, registerService);

				} else {
					logError("advertiseEndpointDescription", //$NON-NLS-1$
							"IServiceInfoFactory failed to convert EndpointDescription " + endpointDescription); //$NON-NLS-1$ 1
				}
			} else {
				logError("advertiseEndpointDescription", //$NON-NLS-1$
						"no IServiceInfoFactory service found"); //$NON-NLS-1$
			}
		} finally {
			this.registrationLock.unlock();
		}
	}

	/**
	 * @param endpointDescription endpoint description
	 * @since 3.0
	 */
	protected void unadvertiseEndpointDescription(
			org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription) {
		this.registrationLock.lock();
		try {
			final List<EndpointRegistrationHolder> ehs = this.registrations
					.remove(endpointDescription.getId());
			if (ehs != null) {
				ehs.forEach(eh -> eh.unregister());
			}
		} finally {
			this.registrationLock.unlock();
		}
	}

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

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

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

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

	/**
	 * @param endpointDescription endpoint description
	 * @since 3.0
	 */
	protected void handleECFEndpointAdded(EndpointDescription endpointDescription) {
		trace("handleECFEndpointAdded", "endpointDescription=" //$NON-NLS-1$ //$NON-NLS-2$
				+ endpointDescription);
		// Import service
		RemoteServiceAdmin rsa = (RemoteServiceAdmin) getRemoteServiceAdmin();
		if (rsa != null)
			rsa.importService(endpointDescription);
	}

	/**
	 * @param endpointDescription endpoint description
	 * @since 3.0
	 */
	protected void handleECFEndpointRemoved(
			org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription) {
		trace("handleECFEndpointRemoved", "endpointDescription=" //$NON-NLS-1$ //$NON-NLS-2$
				+ endpointDescription);
		RemoteServiceAdmin rsa = (RemoteServiceAdmin) getRemoteServiceAdmin();
		if (rsa != null) {
			List<RemoteServiceAdmin.ImportRegistration> importedRegistrations = rsa.getImportedRegistrations();
			org.eclipse.ecf.osgi.services.remoteserviceadmin.EndpointDescription ed = (org.eclipse.ecf.osgi.services.remoteserviceadmin.EndpointDescription) endpointDescription;
			for (RemoteServiceAdmin.ImportRegistration importedRegistration : importedRegistrations) {
				if (importedRegistration.match(ed)) {
					trace("handleEndpointRemoved", "closing importedRegistration=" //$NON-NLS-1$ //$NON-NLS-2$
							+ importedRegistration);
					importedRegistration.close();
				}
			}
		}
	}

	/**
	 * @param endpoint endpoint description
	 * @since 4.1
	 */
	protected void handleECFEndpointModified(EndpointDescription endpoint) {
		trace("handleECFEndpointModified", "endpointDescription=" //$NON-NLS-1$ //$NON-NLS-2$
				+ endpoint);
		RemoteServiceAdmin rsa = (RemoteServiceAdmin) getRemoteServiceAdmin();
		if (rsa != null) {
			List<RemoteServiceAdmin.ImportRegistration> importedRegistrations = rsa.getImportedRegistrations();
			org.eclipse.ecf.osgi.services.remoteserviceadmin.EndpointDescription ed = (org.eclipse.ecf.osgi.services.remoteserviceadmin.EndpointDescription) endpoint;
			for (RemoteServiceAdmin.ImportRegistration importedRegistration : importedRegistrations) {
				if (importedRegistration.match(ed)) {
					trace("handleECFEndpointModified", "updating importedRegistration=" //$NON-NLS-1$ //$NON-NLS-2$
							+ importedRegistration);
					importedRegistration.update(endpoint);
				}
			}
		}
	}

	/**
	 * @param listener            listener
	 * @param endpointDescription endpoint description
	 * @since 4.9
	 */
	protected void handleNonECFEndpointAdded(EndpointListener listener,
			org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription) {
		trace("handleNonECFEndpointAdded", "ed=" + endpointDescription); //$NON-NLS-1$//$NON-NLS-2$
	}

	/**
	 * @param listener            listener
	 * @param endpointDescription endpoint description
	 * @since 4.9
	 */
	protected void handleNonECFEndpointRemoved(EndpointListener listener,
			org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription) {
		trace("handleNonECFEndpointRemoved", "ed=" + endpointDescription); //$NON-NLS-1$//$NON-NLS-2$
	}

	/**
	 * @param basicTopologyManagerImpl basic topology manager
	 * @param endpointDescription      endpointDescription
	 * @since 4.9
	 */
	protected void handleNonECFEndpointModified(EndpointEventListener basicTopologyManagerImpl,
			org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription) {
		trace("handleNonECFEndpointModified", "ed=" + endpointDescription); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @param endpointDescription endpoint description
	 * @param matchedFilter       matched filter
	 * @since 3.0
	 */
	protected void handleNonECFEndpointRemoved(
			org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription, String matchedFilter) {
		advertiseEndpointDescription(endpointDescription);
	}

	/**
	 * @param result              result
	 * @param endpointDescription endpoint description
	 * @param advertise           advertise
	 * @since 3.0
	 */
	protected void handleAdvertisingResult(IStatus result,
			org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription, boolean advertise) {
		if (!result.isOK())
			logError("handleAdvertisingResult", //$NON-NLS-1$
					(advertise ? "Advertise" : "Unadvertise") + " of endpointDescription=" + endpointDescription //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
							+ " FAILED", //$NON-NLS-1$
					result);
	}

	protected void handleInvalidImportRegistration(ImportRegistration importRegistration, Throwable t) {
		logError("handleInvalidImportRegistration", "importRegistration=" //$NON-NLS-1$ //$NON-NLS-2$
				+ importRegistration, t);
	}

	/**
	 * @param event     the service event
	 * @param listeners map of listeners
	 * @since 3.0
	 */
	protected void handleEvent(ServiceEvent event, Map listeners) {
		switch (event.getType()) {
		case ServiceEvent.MODIFIED:
			handleServiceModifying(event.getServiceReference());
			break;
		case ServiceEvent.REGISTERED:
			handleServiceRegistering(event.getServiceReference());
			break;
		case ServiceEvent.UNREGISTERING:
			handleServiceUnregistering(event.getServiceReference());
			break;
		default:
			break;
		}
	}

	protected void handleServiceRegistering(ServiceReference serviceReference) {
		// Using OSGI 5 Chap 13 Remote Services spec, get the specified remote
		// interfaces for the given service reference
		String[] exportedInterfaces = PropertiesUtil.getExportedInterfaces(serviceReference);
		// If no remote interfaces set, then we don't do anything with it
		if (exportedInterfaces == null)
			return;

		// Get serviceExportedConfigs property
		String[] serviceExportedConfigs = PropertiesUtil.getStringArrayFromPropertyValue(serviceReference
				.getProperty(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_CONFIGS));
		// If requireServiceExportedConfigs is set to true (default is false) then if
		// serviceExportedConfigs
		// is null/not set, then we don't do anything with this service registration
		if (requireServiceExportedConfigs
				&& (serviceExportedConfigs == null || Arrays.asList(serviceExportedConfigs).size() == 0))
			return;
		// If we get this far, then we are going to export it
		// prepare export properties
		Map<String, Object> exportProperties = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
		exportProperties.put(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_INTERFACES,
				exportedInterfaces);
		trace("handleServiceRegistering", "serviceReference=" //$NON-NLS-1$ //$NON-NLS-2$
				+ serviceReference + " exportProperties=" + exportProperties); //$NON-NLS-1$
		org.osgi.service.remoteserviceadmin.RemoteServiceAdmin rsa = getRemoteServiceAdmin();
		// Do the export with RSA
		if (rsa != null)
			rsa.exportService(serviceReference, exportProperties);
	}

	protected void handleServiceModifying(ServiceReference serviceReference) {
		RemoteServiceAdmin rsa = (RemoteServiceAdmin) getRemoteServiceAdmin();
		if (rsa != null) {
			List<RemoteServiceAdmin.ExportRegistration> exportedRegistrations = rsa.getExportedRegistrations();
			for (RemoteServiceAdmin.ExportRegistration exportedRegistration : exportedRegistrations) {
				if (exportedRegistration.match(serviceReference)) {
					trace("handleServiceModifying", "modifying exportRegistration for serviceReference=" //$NON-NLS-1$ //$NON-NLS-2$
							+ serviceReference);
					EndpointDescription updatedED = (EndpointDescription) exportedRegistration.update(null);
					if (updatedED == null)
						logWarning("handleServiceModifying", "ExportRegistration.update failed with exception=" //$NON-NLS-1$//$NON-NLS-2$
								+ exportedRegistration.getException());
				}
			}
		}
	}

	protected void handleServiceUnregistering(ServiceReference serviceReference) {
		RemoteServiceAdmin rsa = (RemoteServiceAdmin) getRemoteServiceAdmin();
		if (rsa != null) {
			List<RemoteServiceAdmin.ExportRegistration> exportedRegistrations = ((RemoteServiceAdmin) getRemoteServiceAdmin())
					.getExportedRegistrations();
			for (RemoteServiceAdmin.ExportRegistration exportedRegistration : exportedRegistrations) {
				if (exportedRegistration.match(serviceReference)) {
					trace("handleServiceUnregistering", "closing exportRegistration for serviceReference=" //$NON-NLS-1$ //$NON-NLS-2$
							+ serviceReference);
					exportedRegistration.close();
				}
			}
		}
	}
}

Back to the top