Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2dab30d9eba9cf3e925d96a70497f52307c8ad20 (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
/*****************************************************************************
 * Copyright (c) 2016 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.tools.util;

import java.lang.reflect.Array;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BinaryOperator;
import java.util.stream.Stream;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

/**
 * A service tracker that provides a single service as a composite of
 * registered service implementations.
 */
public class CompositeServiceTracker<S> extends ServiceTracker<S, S> {
	private final AtomicReference<S> delegate = new AtomicReference<>();

	private final Class<S> serviceType;
	private final S identity;
	private final BinaryOperator<S> composer;

	/**
	 * Initializes me with the bundle context in which I track resolver services,
	 * an identity service that generally performs trivially (e.g., no-ops or default behaviour),
	 * and an operator that composes two service instances.
	 * 
	 * @param context
	 *            the bundle context
	 * @param serviceType
	 *            the service protocol type
	 * @param identity
	 *            the basic no-op or default service instance
	 * @param composer
	 *            an operator that composes two services instances into one
	 */
	public CompositeServiceTracker(BundleContext context, Class<S> serviceType, S identity, BinaryOperator<S> composer) {
		super(context, serviceType, null);

		this.serviceType = serviceType;
		this.identity = identity;
		this.composer = composer;
	}

	@Override
	public final S getService() {
		S result = this.delegate.get();
		if (result == null) {
			// Recompute
			@SuppressWarnings("unchecked")
			S[] services = (S[]) Array.newInstance(serviceType, getTrackingCount());
			result = Stream.of(getServices(services))
					.filter(Objects::nonNull) // If the array has more slots than we have services
					.reduce(identity, composer);
			this.delegate.set(result);
		}

		return result;
	}

	@Override
	public S addingService(ServiceReference<S> reference) {
		S result = super.addingService(reference);

		// We will have to recompute our delegates
		delegate.set(null);

		return result;
	}

	@Override
	public void removedService(ServiceReference<S> reference, S service) {
		super.removedService(reference, service);

		// We will have to recompute our delegates
		delegate.set(null);
	}
}

Back to the top