Search in sources :

Example 11 with Filter

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

the class AbstractIntegrationTest method getOsgiService.

protected <T> T getOsgiService(BundleContext bc, 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(bc == null ? bundleContext : bc, osgiFilter, null);
        tracker.open();
        // add tracker to the list of trackers we close at tear down
        srs.add(tracker);
        Object x = tracker.waitForService(timeout);
        Object svc = type.cast(x);
        if (svc == null) {
            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 : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 12 with Filter

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

the class AbstractJPATransactionTest method getService.

protected <T> T getService(Class<T> clazz, String filter, long timeout) throws InvalidSyntaxException {
    Filter f = FrameworkUtil.createFilter(filter == null ? "(|(foo=bar)(!(foo=bar)))" : filter);
    ServiceTracker<T, T> tracker = new ServiceTracker<T, T>(context, clazz, null) {

        @Override
        public T addingService(ServiceReference<T> reference) {
            return f.match(reference) ? super.addingService(reference) : null;
        }
    };
    tracker.open();
    try {
        T t = tracker.waitForService(timeout);
        if (t == null) {
            throw new NoSuchElementException(clazz.getName());
        }
        return t;
    } catch (InterruptedException e) {
        throw new RuntimeException("Error waiting for service " + clazz.getName(), e);
    } finally {
        trackers.add(tracker);
    }
}
Also used : Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) NoSuchElementException(java.util.NoSuchElementException) ServiceReference(org.osgi.framework.ServiceReference)

Example 13 with Filter

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

the class ImportedServiceImpl method generateAttributeFilter.

private Filter generateAttributeFilter(Map<String, String> attrsToPopulate) throws InvalidAttributeException {
    logger.debug(LOG_ENTRY, "generateAttributeFilter", new Object[] { attrsToPopulate });
    Filter result = null;
    try {
        attrsToPopulate.put(ModellingConstants.OBR_SERVICE, ModellingConstants.OBR_SERVICE);
        if (_blueprintFilter != null) {
            // We may get blueprint filters of the form (&(a=b)(c=d)). We can't put these in 'whole' because we'll 
            // end up generating a filter of the form (&(objectClass=foo)(&(a=b)(c=d)) which subsequent calls to 
            // parseFilter will choke on. So as an interim fix we'll strip off a leading &( and trailing ) if present. 
            String reducedBlueprintFilter;
            if (_blueprintFilter.startsWith("(&")) {
                reducedBlueprintFilter = _blueprintFilter.substring(2, _blueprintFilter.length() - 1);
            } else {
                reducedBlueprintFilter = _blueprintFilter;
            }
            attrsToPopulate.put(ManifestHeaderProcessor.NESTED_FILTER_ATTRIBUTE, reducedBlueprintFilter);
        }
        if (_componentName != null) {
            attrsToPopulate.put("osgi.service.blueprint.compname", _componentName);
        }
        if (_iface != null) {
            attrsToPopulate.put(Constants.OBJECTCLASS, _iface);
        }
        _attribFilterString = ManifestHeaderProcessor.generateFilter(_attributes);
        if (!"".equals(_attribFilterString)) {
            result = FrameworkUtil.createFilter(FilterUtils.removeMandatoryFilterToken(_attribFilterString));
        }
    } catch (InvalidSyntaxException isx) {
        InvalidAttributeException iax = new InvalidAttributeException("A syntax error occurred attempting to parse the blueprint filter string '" + _blueprintFilter + "' for element with id " + _id + ": " + isx.getLocalizedMessage(), isx);
        logger.debug(LOG_EXIT, "generateAttributeFilter", new Object[] { isx });
        throw iax;
    }
    logger.debug(LOG_EXIT, "generateAttributeFilter", new Object[] { result });
    return result;
}
Also used : InvalidAttributeException(org.apache.aries.application.InvalidAttributeException) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 14 with Filter

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

the class ConsumerHeaderProcessor method processRequireCapabilityHeader.

private static Set<WeavingData> processRequireCapabilityHeader(String consumerHeader) throws InvalidSyntaxException {
    Set<WeavingData> weavingData = new HashSet<WeavingData>();
    List<GenericMetadata> requirements = ManifestHeaderProcessor.parseRequirementString(consumerHeader);
    GenericMetadata extenderRequirement = findRequirement(requirements, SpiFlyConstants.EXTENDER_CAPABILITY_NAMESPACE, SpiFlyConstants.PROCESSOR_EXTENDER_NAME);
    Collection<GenericMetadata> serviceLoaderRequirements = findAllMetadata(requirements, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE);
    if (extenderRequirement != null) {
        ArgRestrictions ar = new ArgRestrictions();
        ar.addRestriction(0, Class.class.getName());
        MethodRestriction mr = new MethodRestriction("load", ar);
        List<BundleDescriptor> allowedBundles = new ArrayList<BundleDescriptor>();
        for (GenericMetadata req : serviceLoaderRequirements) {
            String slFilterString = req.getDirectives().get(SpiFlyConstants.FILTER_DIRECTIVE);
            if (slFilterString != null) {
                Filter slFilter = FrameworkUtil.createFilter(slFilterString);
                allowedBundles.add(new BundleDescriptor(slFilter));
            }
        }
        weavingData.add(createWeavingData(ServiceLoader.class.getName(), "load", mr, allowedBundles));
    }
    return weavingData;
}
Also used : ArrayList(java.util.ArrayList) GenericMetadata(org.apache.aries.util.manifest.ManifestHeaderProcessor.GenericMetadata) Filter(org.osgi.framework.Filter) HashSet(java.util.HashSet)

Example 15 with Filter

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

the class ProviderBundleTrackerCustomizer method findRequirement.

private static GenericMetadata findRequirement(List<GenericMetadata> requirements, String namespace, String type) throws InvalidSyntaxException {
    Dictionary<String, String> nsAttr = new Hashtable<String, String>();
    nsAttr.put(namespace, type);
    for (GenericMetadata req : requirements) {
        if (namespace.equals(req.getNamespace())) {
            String filterString = req.getDirectives().get(SpiFlyConstants.FILTER_DIRECTIVE);
            if (filterString != null) {
                Filter filter = FrameworkUtil.createFilter(filterString);
                if (filter.match(nsAttr)) {
                    return req;
                }
            }
        }
    }
    return null;
}
Also used : GenericMetadata(org.apache.aries.util.manifest.ManifestHeaderProcessor.GenericMetadata) Filter(org.osgi.framework.Filter) Hashtable(java.util.Hashtable)

Aggregations

Filter (org.osgi.framework.Filter)81 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)31 ServiceTracker (org.osgi.util.tracker.ServiceTracker)29 ArrayList (java.util.ArrayList)22 ServiceReference (org.osgi.framework.ServiceReference)20 Hashtable (java.util.Hashtable)18 Test (org.junit.Test)14 BundleContext (org.osgi.framework.BundleContext)13 List (java.util.List)12 Bundle (org.osgi.framework.Bundle)11 Dictionary (java.util.Dictionary)9 SharePolicy (org.apache.aries.subsystem.scope.SharePolicy)9 Configuration (org.osgi.service.cm.Configuration)9 HashMap (java.util.HashMap)7 Map (java.util.Map)6 IOException (java.io.IOException)5 URL (java.net.URL)5 InstallInfo (org.apache.aries.subsystem.scope.InstallInfo)5 ScopeUpdate (org.apache.aries.subsystem.scope.ScopeUpdate)5 Iterator (java.util.Iterator)4