Search in sources :

Example 1 with UnfilteredServiceListener

use of org.osgi.framework.UnfilteredServiceListener in project felix by apache.

the class EventDispatcher method invokeServiceListenerCallback.

private static void invokeServiceListenerCallback(Bundle bundle, final EventListener l, Filter filter, Object acc, final EventObject event, final Dictionary oldProps) {
    // STOPPING, and ACTIVE bundles.
    if ((bundle.getState() != Bundle.STARTING) && (bundle.getState() != Bundle.STOPPING) && (bundle.getState() != Bundle.ACTIVE)) {
        return;
    }
    // Check that the bundle has permission to get at least
    // one of the service interfaces; the objectClass property
    // of the service stores its service interfaces.
    ServiceReference ref = ((ServiceEvent) event).getServiceReference();
    boolean hasPermission = true;
    Object sm = System.getSecurityManager();
    if ((acc != null) && (sm != null)) {
        try {
            ServicePermission perm = new ServicePermission(ref, ServicePermission.GET);
            ((SecurityManager) sm).checkPermission(perm, acc);
        } catch (Exception ex) {
            hasPermission = false;
        }
    }
    if (hasPermission) {
        // Dispatch according to the filter.
        boolean matched;
        if (l instanceof UnfilteredServiceListener) {
            // An UnfilteredServiceListener always matches, regardless of the filter.
            // The filter is still passed on to the Service Registry Hooks.
            matched = true;
        } else {
            matched = (filter == null) || filter.match(((ServiceEvent) event).getServiceReference());
        }
        if (matched) {
            if ((l instanceof AllServiceListener) || Util.isServiceAssignable(bundle, ((ServiceEvent) event).getServiceReference())) {
                if (System.getSecurityManager() != null) {
                    AccessController.doPrivileged(new PrivilegedAction() {

                        @Override
                        public Object run() {
                            ((ServiceListener) l).serviceChanged((ServiceEvent) event);
                            return null;
                        }
                    });
                } else {
                    ((ServiceListener) l).serviceChanged((ServiceEvent) event);
                }
            }
        } else // matched previously.
        if (((ServiceEvent) event).getType() == ServiceEvent.MODIFIED) {
            if (filter.match(oldProps)) {
                final ServiceEvent se = new ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, ((ServiceEvent) event).getServiceReference());
                if (System.getSecurityManager() != null) {
                    AccessController.doPrivileged(new PrivilegedAction() {

                        @Override
                        public Object run() {
                            ((ServiceListener) l).serviceChanged(se);
                            return null;
                        }
                    });
                } else {
                    ((ServiceListener) l).serviceChanged(se);
                }
            }
        }
    }
}
Also used : AllServiceListener(org.osgi.framework.AllServiceListener) UnfilteredServiceListener(org.osgi.framework.UnfilteredServiceListener) ServiceListener(org.osgi.framework.ServiceListener) PrivilegedAction(java.security.PrivilegedAction) ServiceEvent(org.osgi.framework.ServiceEvent) ServicePermission(org.osgi.framework.ServicePermission) EventObject(java.util.EventObject) ServiceReference(org.osgi.framework.ServiceReference) UnfilteredServiceListener(org.osgi.framework.UnfilteredServiceListener) AllServiceListener(org.osgi.framework.AllServiceListener)

Aggregations

PrivilegedAction (java.security.PrivilegedAction)1 EventObject (java.util.EventObject)1 AllServiceListener (org.osgi.framework.AllServiceListener)1 ServiceEvent (org.osgi.framework.ServiceEvent)1 ServiceListener (org.osgi.framework.ServiceListener)1 ServicePermission (org.osgi.framework.ServicePermission)1 ServiceReference (org.osgi.framework.ServiceReference)1 UnfilteredServiceListener (org.osgi.framework.UnfilteredServiceListener)1