Search in sources :

Example 1 with Match

use of org.osgi.service.device.Match 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 Match

use of org.osgi.service.device.Match in project felix by apache.

the class DriverMatcher method selectBestMatch.

public Match selectBestMatch(ServiceReference deviceRef, DriverSelector selector) {
    // Match[] matches = m_matches.toArray( new Match[0] );
    // (re)check bundle status
    List<Match> activeMatches = new ArrayList<Match>();
    for (Match match : m_matches) {
        if (match.getDriver().getBundle().getState() == Bundle.ACTIVE) {
            activeMatches.add(match);
        } else {
            m_log.debug("skipping: " + match + ", it's bundle is: " + match.getDriver().getBundle().getState());
        }
    }
    try {
        Match[] matches = activeMatches.toArray(new Match[0]);
        int index = selector.select(deviceRef, matches);
        if (index != DriverSelector.SELECT_NONE && index >= 0 && index < matches.length) {
            return matches[index];
        }
    } catch (Exception e) {
        m_log.error("exception thrown in DriverSelector.select()", e);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Match(org.osgi.service.device.Match)

Example 3 with Match

use of org.osgi.service.device.Match in project felix by apache.

the class DriverMatcherTest method GetBestMatchWithNoDriver.

@Test
public void GetBestMatchWithNoDriver() throws Exception {
    Match match = m_matcherImpl.getBestMatch();
    Assert.assertNull(match);
}
Also used : Match(org.osgi.service.device.Match) Test(org.junit.Test)

Example 4 with Match

use of org.osgi.service.device.Match in project felix by apache.

the class DriverMatcherTest method GetBestMatchWithDifferentMatchValue.

@Test
public void GetBestMatchWithDifferentMatchValue() throws Exception {
    add("org.apache.felix.driver.a-1.0", 1);
    add("org.apache.felix.driver.b-1.0", 2);
    add("org.apache.felix.driver.c-1.0", 1);
    Match match = m_matcherImpl.getBestMatch();
    Assert.assertNotNull(match);
    Assert.assertEquals("org.apache.felix.driver.b-1.0", tstDriverId(match));
    Assert.assertEquals(2, match.getMatchValue());
}
Also used : Match(org.osgi.service.device.Match) Test(org.junit.Test)

Example 5 with Match

use of org.osgi.service.device.Match in project felix by apache.

the class DriverMatcherTest method GetBestMatchWithOneDriver.

@Test
public void GetBestMatchWithOneDriver() throws Exception {
    add("org.apache.felix.driver-1.0", 1);
    Match match = m_matcherImpl.getBestMatch();
    Assert.assertNotNull(match);
    Assert.assertEquals("org.apache.felix.driver-1.0", tstDriverId(match));
}
Also used : Match(org.osgi.service.device.Match) Test(org.junit.Test)

Aggregations

Match (org.osgi.service.device.Match)13 Test (org.junit.Test)11 ServiceReference (org.osgi.framework.ServiceReference)5 DriverSelector (org.osgi.service.device.DriverSelector)4 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 DriverAttributes (org.apache.felix.das.DriverAttributes)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1