I am just starting with OSGI programming and have come across two ways to listener for services being activated.
The first way, from an EclipseRCP book, uses ServiceReference:
String filter="(objectclass="+IModelCreator.class.getName()+")";
context.addServiceListener(this, filter);
modelCreators = Collections.synchronizedMap(
new HashMap<ModelID, List<IModelCreator>>());
ServiceReference references[] = context.getServiceReferences(null, filter);
if(references==null) return;
for(int i=0;i<references.length;++i) {
this.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
references[i]));
}
The second, from internet examples, uses ServiceTracker:
ServiceTracker logReaderTracker = new ServiceTracker(context,
org.osgi.service.log.LogReaderService.class.getName(), null);
logReaderTracker.open();
Object[] readers = logReaderTracker.getServices();
if (readers != null) {
for (int i = 0; i < readers.length; i++) {
LogReaderService lrs = (LogReaderService) readers[i];
m_readers.add(lrs);
lrs.addLogListener(m_logger);
}
}
logReaderTracker.close();
Which one of these is the correct and/or best way of holding a register of all the services that implement a given Interface? Is there another way of accomplishing this? Why does there seem to be two ways of doing the same thing?