Search in sources :

Example 1 with ServicePermission

use of org.osgi.framework.ServicePermission 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)

Example 2 with ServicePermission

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

the class BundleContextImpl method registerService.

public ServiceRegistration<?> registerService(String[] clazzes, Object svcObj, Dictionary<String, ?> dict) {
    checkValidity();
    // CONCURRENCY NOTE: This is a NOT a check-then-act situation,
    // because internally the framework acquires the bundle state
    // lock to ensure state consistency.
    Object sm = System.getSecurityManager();
    if (sm != null) {
        if (clazzes != null) {
            for (int i = 0; i < clazzes.length; i++) {
                ((SecurityManager) sm).checkPermission(new ServicePermission(clazzes[i], ServicePermission.REGISTER));
            }
        }
    }
    return m_felix.registerService(this, clazzes, svcObj, dict);
}
Also used : ServicePermission(org.osgi.framework.ServicePermission)

Example 3 with ServicePermission

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

the class BundleContextImpl method getServiceObjects.

/**
 * @see org.osgi.framework.BundleContext#getServiceObjects(org.osgi.framework.ServiceReference)
 */
public <S> ServiceObjects<S> getServiceObjects(final ServiceReference<S> ref) {
    checkValidity();
    Object sm = System.getSecurityManager();
    if (sm != null) {
        ((SecurityManager) sm).checkPermission(new ServicePermission(ref, ServicePermission.GET));
    }
    ServiceRegistrationImpl reg = ((ServiceRegistrationImpl.ServiceReferenceImpl) ref).getRegistration();
    if (reg.isValid()) {
        return new ServiceObjectsImpl(ref);
    }
    return null;
}
Also used : ServicePermission(org.osgi.framework.ServicePermission)

Example 4 with ServicePermission

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

the class BundleImpl method getRegisteredServices.

/**
 * Returns an array of service references corresponding to
 * the bundle's registered services.
 *
 * @return an array of service references or null.
 */
@Override
public ServiceReference[] getRegisteredServices() {
    Object sm = System.getSecurityManager();
    if (sm != null) {
        ServiceReference[] refs = getFramework().getBundleRegisteredServices(this);
        if (refs == null) {
            return refs;
        }
        List result = new ArrayList();
        for (int i = 0; i < refs.length; i++) {
            try {
                ((SecurityManager) sm).checkPermission(new ServicePermission(refs[i], ServicePermission.GET));
                result.add(refs[i]);
            } catch (Exception ex) {
            // Silently ignore.
            }
        }
        if (result.isEmpty()) {
            return null;
        }
        return (ServiceReference[]) result.toArray(new ServiceReference[result.size()]);
    } else {
        return getFramework().getBundleRegisteredServices(this);
    }
}
Also used : ServicePermission(org.osgi.framework.ServicePermission) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with ServicePermission

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

the class BundleImpl method getServicesInUse.

@Override
public ServiceReference[] getServicesInUse() {
    Object sm = System.getSecurityManager();
    if (sm != null) {
        ServiceReference[] refs = getFramework().getBundleServicesInUse(this);
        if (refs == null) {
            return refs;
        }
        List result = new ArrayList();
        for (int i = 0; i < refs.length; i++) {
            try {
                ((SecurityManager) sm).checkPermission(new ServicePermission(refs[i], ServicePermission.GET));
                result.add(refs[i]);
            } catch (Exception ex) {
            // Silently ignore.
            }
        }
        if (result.isEmpty()) {
            return null;
        }
        return (ServiceReference[]) result.toArray(new ServiceReference[result.size()]);
    }
    return getFramework().getBundleServicesInUse(this);
}
Also used : ServicePermission(org.osgi.framework.ServicePermission) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServicePermission (org.osgi.framework.ServicePermission)13 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 ServiceReference (org.osgi.framework.ServiceReference)5 List (java.util.List)4 Bundle (org.osgi.framework.Bundle)3 BundleException (org.osgi.framework.BundleException)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 AccessControlException (java.security.AccessControlException)2 Permission (java.security.Permission)2 FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 PermissionCollection (java.security.PermissionCollection)1 PrivilegedAction (java.security.PrivilegedAction)1 EventObject (java.util.EventObject)1 HashMap (java.util.HashMap)1 PropertyPermission (java.util.PropertyPermission)1 Collectors.toList (java.util.stream.Collectors.toList)1 ServiceMetadata (org.apache.felix.scr.impl.metadata.ServiceMetadata)1