Created
December 8, 2013 16:02
-
-
Save cescoffier/7859472 to your computer and use it in GitHub Desktop.
An iPOJO interceptor implementing both tracking and ranking interception.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.apache.felix.ipojo.runtime.core.test.interceptors; | |
import org.apache.felix.ipojo.annotations.Component; | |
import org.apache.felix.ipojo.annotations.Provides; | |
import org.apache.felix.ipojo.annotations.ServiceProperty; | |
import org.apache.felix.ipojo.dependency.interceptors.DefaultDependencyInterceptor; | |
import org.apache.felix.ipojo.dependency.interceptors.ServiceRankingInterceptor; | |
import org.apache.felix.ipojo.dependency.interceptors.ServiceTrackingInterceptor; | |
import org.apache.felix.ipojo.dependency.interceptors.TransformedServiceReference; | |
import org.apache.felix.ipojo.runtime.core.test.services.Setter; | |
import org.apache.felix.ipojo.util.DependencyModel; | |
import org.osgi.framework.BundleContext; | |
import org.osgi.framework.ServiceReference; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
/** | |
* An interceptor implements both ranking and tracking interfaces. | |
*/ | |
@Component(immediate = true) | |
@Provides | |
public class TrackerAndRankerInterceptor extends DefaultDependencyInterceptor implements Setter, | |
ServiceTrackingInterceptor, ServiceRankingInterceptor { | |
private static final int HIGH = 1; | |
private static final int LOW = 0; | |
@ServiceProperty | |
private String target; | |
GradeComparator comparator = new GradeComparator(); | |
private int mode = HIGH; | |
private boolean reverse = false; | |
@Override | |
public List<ServiceReference> getServiceReferences(DependencyModel dependency, List<ServiceReference> matching) { | |
List<ServiceReference> references = new ArrayList<ServiceReference>(); | |
for (ServiceReference ref : matching) { | |
if (ref.getProperty("grade") != null) { | |
references.add(ref); | |
} | |
} | |
Collections.sort(references, comparator); | |
if (reverse) { | |
Collections.reverse(references); | |
} | |
return references; | |
} | |
@Override | |
public List<ServiceReference> onServiceArrival(DependencyModel dependency, List<ServiceReference> matching, ServiceReference<?> reference) { | |
return getServiceReferences(dependency, matching); | |
} | |
@Override | |
public List<ServiceReference> onServiceDeparture(DependencyModel dependency, List<ServiceReference> matching, ServiceReference<?> reference) { | |
return getServiceReferences(dependency, matching); | |
} | |
@Override | |
public List<ServiceReference> onServiceModified(DependencyModel dependency, List<ServiceReference> matching, ServiceReference<?> reference) { | |
return getServiceReferences(dependency, matching); | |
} | |
@Override | |
public <S> TransformedServiceReference<S> accept(DependencyModel dependency, BundleContext context, TransformedServiceReference<S> ref) { | |
// Only accept services having a grade in LOW or HIGH according to the mode. | |
if (mode == HIGH) { | |
if (((Integer) ref.get("grade")) > 3) { | |
return ref; | |
} | |
} else { | |
if (((Integer) ref.get("grade")) <= 3) { | |
return ref; | |
} | |
} | |
return null; | |
} | |
@Override | |
public void set(String newValue) { | |
if (newValue.contains("HIGH")) { | |
mode = HIGH; | |
invalidateMatchingServices(); | |
} | |
if (newValue.contains("LOW")) { | |
mode = LOW; | |
invalidateMatchingServices(); | |
} | |
if (newValue.contains("REVERSE")) { | |
reverse = true; | |
invalidateSelectedServices(); | |
} | |
} | |
/** | |
* Notifies the managed dependencies of a change in the set of services selected by this interceptor. | |
* The dependency will call the getServiceReferences method to recompute the set of selected services. | |
*/ | |
public void invalidateSelectedServices() { | |
List<DependencyModel> list = new ArrayList<DependencyModel>(); | |
synchronized (this) { | |
list.addAll(dependencies); | |
} | |
for (DependencyModel dep : list) { | |
dep.invalidateSelectedServices(); | |
} | |
} | |
/** | |
* Notifies the managed dependencies of a change in the set of services accepted by this interceptor. | |
* The dependency will call the accept method to recompute the set of matching services. | |
*/ | |
public void invalidateMatchingServices() { | |
List<DependencyModel> list = new ArrayList<DependencyModel>(); | |
synchronized (this) { | |
list.addAll(dependencies); | |
} | |
for (DependencyModel dep : list) { | |
dep.invalidateMatchingServices(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment