com.effectiveosgi.lib.osgi.SingletonServiceTracker Maven / Gradle / Ivy
The newest version!
package com.effectiveosgi.lib.osgi;
import java.util.SortedSet;
import java.util.TreeSet;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
public class SingletonServiceTracker extends ServiceTracker {
private final SortedSet> rankedServices = new TreeSet<>();
private final ServiceTrackerCustomizer customizer;
private ServiceReference currentRef = null;
private T currentService = null;
public SingletonServiceTracker(BundleContext context, Class clazz, ServiceTrackerCustomizer customizer) {
super(context, clazz, null);
if (customizer == null) throw new IllegalArgumentException("Customizer may not be null");
this.customizer = customizer;
}
@Override
public T addingService(ServiceReference reference) {
synchronized (rankedServices) {
rankedServices.add(reference);
if (currentRef == null) {
currentRef = reference;
currentService = customizer.addingService(currentRef);
}
}
return currentService;
}
@Override
public void modifiedService(ServiceReference reference, T service) {
if (reference.equals(currentRef)) {
customizer.modifiedService(currentRef, service);
}
}
@Override
public void removedService(ServiceReference reference, T ignore) {
synchronized (rankedServices) {
rankedServices.remove(reference);
if (currentRef != null && currentRef.equals(reference)) {
ServiceReference oldRef = currentRef;
T oldService = currentService;
if (rankedServices.isEmpty()) {
currentRef = null;
currentService = null;
customizer.removedService(oldRef, oldService);
} else {
currentRef = rankedServices.last();
currentService = customizer.addingService(currentRef);
customizer.removedService(oldRef, oldService);
}
}
}
}
}