use of org.osgi.framework.ServiceListener in project felix by apache.
the class EventDispatcher method serviceChanged.
/**
* Method called when a {@link ServiceEvent} is
* fired by the OSGi framework.
* According to the event, this method dispatches
* to interested registered listers from
* the {@link EventDispatcher#m_listeners} map.
* @param event the service event
* @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
*/
public void serviceChanged(ServiceEvent event) {
String[] itfs = (String[]) event.getServiceReference().getProperty(Constants.OBJECTCLASS);
for (int s = 0; s < itfs.length; s++) {
List list;
synchronized (this) {
List stored = (List) m_listeners.get(itfs[s]);
if (stored == null) {
// Nothing to do
return;
}
// Creates a new list (stack confinement)
list = new ArrayList(stored);
}
for (int i = 0; i < list.size(); i++) {
((ServiceListener) list.get(i)).serviceChanged(event);
}
}
}
use of org.osgi.framework.ServiceListener in project felix by apache.
the class AdapterFilterIndex method addServiceListener.
public void addServiceListener(ServiceListener listener, String filter) {
FilterData data = getFilterData(null, filter);
if (data != null) {
Long sidObject = Long.valueOf(data.m_serviceId);
synchronized (m_sidToListenersMap) {
List<ServiceListener> listeners = m_sidToListenersMap.get(sidObject);
if (listeners == null) {
listeners = new ArrayList<>();
m_sidToListenersMap.put(sidObject, listeners);
}
listeners.add(listener);
m_listenerToFilterMap.put(listener, filter);
Matcher matcher = PATTERN.matcher(filter);
if (matcher.matches()) {
String objectClass = matcher.group(1);
m_listenerToObjectClassMap.put(listener, objectClass);
} else {
throw new IllegalArgumentException("Filter string does not match index pattern");
}
}
}
}
use of org.osgi.framework.ServiceListener in project felix by apache.
the class AdapterFilterIndex method serviceChanged.
public void serviceChanged(ServiceEvent event) {
ServiceReference reference = event.getServiceReference();
Long sid = ServiceUtil.getServiceIdObject(reference);
List<ServiceListener> notificationList = new ArrayList<>();
synchronized (m_sidToListenersMap) {
List<ServiceListener> list = m_sidToListenersMap.get(sid);
if (list != null) {
Iterator<ServiceListener> iterator = list.iterator();
while (iterator.hasNext()) {
ServiceListener listener = (ServiceListener) iterator.next();
String objectClass = m_listenerToObjectClassMap.get(listener);
if (referenceMatchesObjectClass(reference, objectClass)) {
notificationList.add(listener);
}
}
}
}
// notify
Iterator<ServiceListener> iterator = notificationList.iterator();
while (iterator.hasNext()) {
ServiceListener listener = (ServiceListener) iterator.next();
listener.serviceChanged(event);
}
}
use of org.osgi.framework.ServiceListener in project felix by apache.
the class AspectFilterIndex method serviceChanged.
public void serviceChanged(ServiceEvent event) {
List<ServiceListener> list = new ArrayList<>();
ServiceReference reference = event.getServiceReference();
Long sidObject = ServiceUtil.getServiceIdObject(reference);
int ranking = ServiceUtil.getRanking(reference);
String[] objectClasses = (String[]) reference.getProperty(Constants.OBJECTCLASS);
synchronized (m_sidToObjectClassToRankingToListenersMap) {
for (int i = 0; i < objectClasses.length; i++) {
// handle each of the object classes separately since aspects only work on one object class at a time
String objectClass = objectClasses[i];
Map<String, SortedMap<Integer, List<ServiceListener>>> objectClassToRankingToListenersMap = m_sidToObjectClassToRankingToListenersMap.get(sidObject);
if (objectClassToRankingToListenersMap != null) {
SortedMap<Integer, List<ServiceListener>> rankingToListenersMap = objectClassToRankingToListenersMap.get(objectClass);
if (rankingToListenersMap != null) {
Iterator<Entry<Integer, List<ServiceListener>>> iterator = rankingToListenersMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Integer, List<ServiceListener>> entry = iterator.next();
if (ranking <= ((Integer) entry.getKey()).intValue()) {
list.addAll(entry.getValue());
}
}
}
}
}
}
Iterator<ServiceListener> iterator = list.iterator();
while (iterator.hasNext()) {
ServiceListener listener = iterator.next();
listener.serviceChanged(event);
}
}
use of org.osgi.framework.ServiceListener in project felix by apache.
the class FilterIndexBundleContext method serviceChanged.
public void serviceChanged(ServiceEvent event) {
Entry<ServiceListener, String>[] entries = synchronizeCollection();
for (int i = 0; i < entries.length; i++) {
Entry<ServiceListener, String> serviceListenerFilterEntry = entries[i];
ServiceListener serviceListener = serviceListenerFilterEntry.getKey();
String filter = serviceListenerFilterEntry.getValue();
if (filter == null) {
serviceListener.serviceChanged(event);
} else {
// TODO review if we can be smarter here
try {
if ("(objectClass=*)".equals(filter)) {
serviceListener.serviceChanged(event);
} else {
if (m_context.createFilter(filter).match(event.getServiceReference())) {
serviceListener.serviceChanged(event);
}
}
} catch (InvalidSyntaxException e) {
e.printStackTrace();
}
}
}
}
Aggregations