Search in sources :

Example 91 with Filter

use of org.osgi.framework.Filter in project karaf by apache.

the class GuardProxyCatalog method getServiceInvocationRoles.

// Returns what roles can possibly ever invoke this service. Note that not every invocation may be successful
// as there can be different roles for different methods and also roles based on arguments passed in.
Set<String> getServiceInvocationRoles(ServiceReference<?> serviceReference) throws Exception {
    boolean definitionFound = false;
    Set<String> allRoles = new HashSet<>();
    // walking through all of the ones that have 'service.guard'.
    for (Configuration config : getServiceGuardConfigs()) {
        Dictionary<String, Object> properties = config.getProperties();
        Object guardFilter = properties.get(SERVICE_GUARD_KEY);
        if (guardFilter instanceof String) {
            Filter filter = getFilter((String) guardFilter);
            if (filter.match(serviceReference)) {
                definitionFound = true;
                for (Enumeration<String> e = properties.keys(); e.hasMoreElements(); ) {
                    String key = e.nextElement();
                    String bareKey = key;
                    int idx = bareKey.indexOf('(');
                    if (idx >= 0) {
                        bareKey = bareKey.substring(0, idx);
                    }
                    int idx1 = bareKey.indexOf('[');
                    if (idx1 >= 0) {
                        bareKey = bareKey.substring(0, idx1);
                    }
                    int idx2 = bareKey.indexOf('*');
                    if (idx2 >= 0) {
                        bareKey = bareKey.substring(0, idx2);
                    }
                    if (!isValidMethodName(bareKey)) {
                        continue;
                    }
                    Object value = properties.get(key);
                    if (value instanceof String) {
                        allRoles.addAll(ACLConfigurationParser.parseRoles((String) value));
                    }
                }
            }
        }
    }
    return definitionFound ? allRoles : null;
}
Also used : Configuration(org.osgi.service.cm.Configuration) Filter(org.osgi.framework.Filter) HashSet(java.util.HashSet)

Example 92 with Filter

use of org.osgi.framework.Filter in project karaf by apache.

the class KarafTestSupport method getOsgiService.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
    ServiceTracker tracker = null;
    try {
        String flt;
        if (filter != null) {
            if (filter.startsWith("(")) {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
            } else {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
            }
        } else {
            flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
        }
        Filter osgiFilter = FrameworkUtil.createFilter(flt);
        tracker = new ServiceTracker(bundleContext, osgiFilter, null);
        tracker.open(true);
        // Note that the tracker is not closed to keep the reference
        // This is buggy, as the service reference may change i think
        Object svc = type.cast(tracker.waitForService(timeout));
        if (svc == null) {
            Dictionary dic = bundleContext.getBundle().getHeaders();
            System.err.println("Test bundle headers: " + explode(dic));
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
                System.err.println("ServiceReference: " + ref);
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("Filtered ServiceReference: " + ref);
            }
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
        return type.cast(svc);
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid filter", e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : Dictionary(java.util.Dictionary) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 93 with Filter

use of org.osgi.framework.Filter in project karaf by apache.

the class SecuredCommandProcessorImpl method trackCommands.

private ServiceTracker<Object, Object> trackCommands(final BundleContext context, String roleClause) throws InvalidSyntaxException {
    Filter filter = context.createFilter(String.format("(&(%s=*)(%s=*)%s)", CommandProcessor.COMMAND_SCOPE, CommandProcessor.COMMAND_FUNCTION, roleClause));
    return new ServiceTracker<Object, Object>(context, filter, null) {

        @Override
        public Object addingService(ServiceReference<Object> reference) {
            Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
            Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
            List<Object> commands = new ArrayList<>();
            if (scope != null && function != null) {
                if (function.getClass().isArray()) {
                    for (Object f : ((Object[]) function)) {
                        Function target = new CommandProxy(context, reference, f.toString());
                        addCommand(scope.toString(), target, f.toString());
                        commands.add(target);
                    }
                } else {
                    Function target = new CommandProxy(context, reference, function.toString());
                    addCommand(scope.toString(), target, function.toString());
                    commands.add(target);
                }
                return commands;
            }
            return null;
        }

        @Override
        public void removedService(ServiceReference<Object> reference, Object service) {
            Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
            Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
            if (scope != null && function != null) {
                if (!function.getClass().isArray()) {
                    removeCommand(scope.toString(), function.toString());
                } else {
                    for (Object func : (Object[]) function) {
                        removeCommand(scope.toString(), func.toString());
                    }
                }
            }
            super.removedService(reference, service);
        }
    };
}
Also used : Function(org.apache.felix.service.command.Function) CommandProxy(org.apache.felix.gogo.runtime.CommandProxy) Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference)

Example 94 with Filter

use of org.osgi.framework.Filter in project karaf by apache.

the class CommandTracker method init.

public void init() throws Exception {
    Filter filter = context.createFilter(String.format("(&(%s=*)(%s=*)(|(%s=%s)(%s=%s)))", CommandProcessor.COMMAND_SCOPE, CommandProcessor.COMMAND_FUNCTION, Constants.OBJECTCLASS, CommandWithAction.class.getName(), Constants.OBJECTCLASS, org.apache.felix.gogo.commands.CommandWithAction.class.getName()));
    this.tracker = new ServiceTracker<>(context, filter, this);
    this.tracker.open();
}
Also used : Filter(org.osgi.framework.Filter)

Example 95 with Filter

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

the class DeploymentManifestManagerImpl method createFakeBundle.

// create a 'mock' bundle that does nothing but export services required by 
// Application-ImportService
private ModelledResource createFakeBundle(Collection<ServiceDeclaration> appImportServices) throws InvalidAttributeException {
    _logger.debug(LOG_ENTRY, "createFakeBundle", new Object[] { appImportServices });
    Attributes attrs = new Attributes();
    attrs.putValue(Constants.BUNDLE_SYMBOLICNAME, FAKE_BUNDLE_NAME);
    attrs.putValue(Constants.BUNDLE_VERSION_ATTRIBUTE, "1.0");
    attrs.putValue(Constants.BUNDLE_MANIFESTVERSION, "2");
    // Build an ExportedService for every Application-ImportService entry
    Collection<ExportedService> exportedServices = new ArrayList<ExportedService>();
    for (ServiceDeclaration sDec : appImportServices) {
        Collection<String> ifaces = Arrays.asList(sDec.getInterfaceName());
        Filter filter = sDec.getFilter();
        Map<String, String> serviceProperties;
        if (filter != null) {
            serviceProperties = ManifestHeaderProcessor.parseFilter(filter.toString());
        } else {
            serviceProperties = new HashMap<String, String>();
        }
        serviceProperties.put("service.imported", "");
        exportedServices.add(modellingManager.getExportedService("", 0, ifaces, new HashMap<String, Object>(serviceProperties)));
    }
    ModelledResource fakeBundle = modellingManager.getModelledResource(null, attrs, null, exportedServices);
    _logger.debug(LOG_EXIT, "createFakeBundle", new Object[] { fakeBundle });
    return fakeBundle;
}
Also used : Filter(org.osgi.framework.Filter) HashMap(java.util.HashMap) ExportedService(org.apache.aries.application.modelling.ExportedService) ServiceDeclaration(org.apache.aries.application.ServiceDeclaration) Attributes(java.util.jar.Attributes) ArrayList(java.util.ArrayList) ModelledResource(org.apache.aries.application.modelling.ModelledResource)

Aggregations

Filter (org.osgi.framework.Filter)167 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)63 ServiceTracker (org.osgi.util.tracker.ServiceTracker)42 ArrayList (java.util.ArrayList)41 ServiceReference (org.osgi.framework.ServiceReference)36 BundleContext (org.osgi.framework.BundleContext)27 Hashtable (java.util.Hashtable)25 List (java.util.List)23 Bundle (org.osgi.framework.Bundle)21 Dictionary (java.util.Dictionary)20 HashMap (java.util.HashMap)17 Test (org.junit.Test)17 Map (java.util.Map)15 IOException (java.io.IOException)9 Iterator (java.util.Iterator)9 Properties (java.util.Properties)8 SharePolicy (org.apache.aries.subsystem.scope.SharePolicy)7 Configuration (org.osgi.service.cm.Configuration)7 URL (java.net.URL)6 Collection (java.util.Collection)6