Search in sources :

Example 16 with ServiceListener

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);
        }
    }
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 17 with ServiceListener

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");
            }
        }
    }
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Matcher(java.util.regex.Matcher)

Example 18 with ServiceListener

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);
    }
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference)

Example 19 with ServiceListener

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);
    }
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) Entry(java.util.Map.Entry) SortedMap(java.util.SortedMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 20 with ServiceListener

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();
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) ServiceListener(org.osgi.framework.ServiceListener) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Aggregations

ServiceListener (org.osgi.framework.ServiceListener)28 ServiceEvent (org.osgi.framework.ServiceEvent)21 ServiceReference (org.osgi.framework.ServiceReference)16 Bundle (org.osgi.framework.Bundle)8 Test (org.junit.Test)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 IOException (java.io.IOException)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 ArrayList (java.util.ArrayList)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 BundleContext (org.osgi.framework.BundleContext)4 BundleException (org.osgi.framework.BundleException)4 Hashtable (java.util.Hashtable)3 Dictionary (java.util.Dictionary)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ObjectName (javax.management.ObjectName)2