Search in sources :

Example 1 with DriverAttributes

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);
}
Also used : DriverAttributes(org.apache.felix.das.DriverAttributes) TreeMap(java.util.TreeMap) ServiceReference(org.osgi.framework.ServiceReference) Match(org.osgi.service.device.Match)

Example 2 with DriverAttributes

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);
}
Also used : DriverAttributes(org.apache.felix.das.DriverAttributes) Bundle(org.osgi.framework.Bundle) Driver(org.osgi.service.device.Driver) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

DriverAttributes (org.apache.felix.das.DriverAttributes)2 ServiceReference (org.osgi.framework.ServiceReference)2 TreeMap (java.util.TreeMap)1 Bundle (org.osgi.framework.Bundle)1 Driver (org.osgi.service.device.Driver)1 Match (org.osgi.service.device.Match)1