Search in sources :

Example 11 with ServicePermission

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

the class BundleContextImpl method getService.

public <S> S getService(ServiceReference<S> ref) {
    checkValidity();
    if (ref == null) {
        throw new NullPointerException("Specified service reference cannot be null.");
    }
    Object sm = System.getSecurityManager();
    if (sm != null) {
        ((SecurityManager) sm).checkPermission(new ServicePermission(ref, ServicePermission.GET));
    }
    return m_felix.getService(m_bundle, ref, false);
}
Also used : ServicePermission(org.osgi.framework.ServicePermission)

Example 12 with ServicePermission

use of org.osgi.framework.ServicePermission in project rt.equinox.framework by eclipse.

the class PermissionTests method badServicePermission.

protected void badServicePermission(String name, String actions) {
    try {
        ServicePermission p = new ServicePermission(name, actions);
        // $NON-NLS-1$
        fail(p + " created with invalid actions");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : ServicePermission(org.osgi.framework.ServicePermission)

Example 13 with ServicePermission

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

the class Util method findContextClassloader.

private static ClassLoader findContextClassloader(Bundle consumerBundle, String className, String methodName, Class<?> clsArg) {
    BaseActivator activator = BaseActivator.activator;
    String requestedClass;
    Map<Pair<Integer, String>, String> args;
    if (ServiceLoader.class.getName().equals(className) && "load".equals(methodName)) {
        requestedClass = clsArg.getName();
        args = new HashMap<Pair<Integer, String>, String>();
        args.put(new Pair<Integer, String>(0, Class.class.getName()), requestedClass);
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            try {
                sm.checkPermission(new ServicePermission(requestedClass, ServicePermission.GET));
            } catch (AccessControlException ace) {
                // access denied
                activator.log(Level.FINE, "No permission to obtain service of type: " + requestedClass);
                return null;
            }
        }
    } else {
        requestedClass = className;
        // only supported on ServiceLoader.load() at the moment
        args = null;
    }
    Collection<Bundle> bundles = new ArrayList<Bundle>(activator.findProviderBundles(requestedClass));
    activator.log(Level.FINE, "Found bundles providing " + requestedClass + ": " + bundles);
    Collection<Bundle> allowedBundles = activator.findConsumerRestrictions(consumerBundle, className, methodName, args);
    if (allowedBundles != null) {
        for (Iterator<Bundle> it = bundles.iterator(); it.hasNext(); ) {
            if (!allowedBundles.contains(it.next())) {
                it.remove();
            }
        }
    }
    switch(bundles.size()) {
        case 0:
            return null;
        case 1:
            Bundle bundle = bundles.iterator().next();
            return getBundleClassLoader(bundle);
        default:
            List<ClassLoader> loaders = new ArrayList<ClassLoader>();
            for (Bundle b : bundles) {
                loaders.add(getBundleClassLoader(b));
            }
            return new MultiDelegationClassloader(loaders.toArray(new ClassLoader[loaders.size()]));
    }
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) AccessControlException(java.security.AccessControlException) ServicePermission(org.osgi.framework.ServicePermission)

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