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);
}
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;
}
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);
}
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());
}
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));
}
Aggregations