use of org.apache.felix.das.DriverAttributes in project felix by apache.
the class DriverMatcher method getBestMatch.
public Match getBestMatch() {
if (m_map.isEmpty()) {
return null;
}
int matchValue = m_map.lastKey();
// these are the matches that
// got the highest match value
List<DriverAttributes> das = m_map.get(matchValue);
if (das.size() == 1) {
// a shortcut: there's only one with the highest match
return new MatchImpl(das.get(0).getReference(), matchValue);
}
// get the highest ranking driver
final SortedMap<ServiceReference, Match> matches = new TreeMap<ServiceReference, Match>(new ServicePriority());
for (DriverAttributes da : das) {
matches.put(da.getReference(), new MatchImpl(da.getReference(), matchValue));
}
ServiceReference last = matches.lastKey();
return matches.get(last);
}
use of org.apache.felix.das.DriverAttributes in project felix by apache.
the class DriverMatcherTest method tstCreateDriverAttributes.
private DriverAttributes tstCreateDriverAttributes(String id, int match, int ranking) throws Exception {
Bundle bundle = Mockito.mock(Bundle.class);
ServiceReference ref = Mockito.mock(ServiceReference.class);
Mockito.when(ref.getBundle()).thenReturn(bundle);
Mockito.when(bundle.getLocation()).thenReturn(DriverLoader.DRIVER_LOCATION_PREFIX + "-" + id);
Mockito.when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(m_serviceId++);
Mockito.when(ref.getProperty(org.osgi.service.device.Constants.DRIVER_ID)).thenReturn(id);
if (ranking > 0) {
Mockito.when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(ranking);
} else if (ranking == 0) {
Mockito.when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(null);
} else {
// an invalid ranking object
Mockito.when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(new Object());
}
Driver driver = Mockito.mock(Driver.class);
Mockito.when(driver.match(Mockito.isA(ServiceReference.class))).thenReturn(match);
return new DriverAttributes(ref, driver);
}
Aggregations