Search in sources :

Example 16 with Requirement

use of org.osgi.resource.Requirement in project aries by apache.

the class SubsystemResource method findContent.

private Resource findContent(Requirement requirement) throws BundleException, IOException, InvalidSyntaxException, URISyntaxException {
    Map<Requirement, Collection<Capability>> map;
    // the case of a persisted subsystem.
    if (isUnscoped()) {
        map = Activator.getInstance().getSystemRepository().findProviders(Collections.singleton(requirement));
        if (map.containsKey(requirement)) {
            Collection<Capability> capabilities = map.get(requirement);
            for (Capability capability : capabilities) {
                Resource provider = capability.getResource();
                if (provider instanceof BundleRevision) {
                    if (getRegion().contains(((BundleRevision) provider).getBundle())) {
                        return provider;
                    }
                } else if (provider instanceof BasicSubsystem) {
                    if (getRegion().equals(((BasicSubsystem) provider).getRegion())) {
                        return provider;
                    }
                }
            }
        }
    }
    // First search the local repository.
    map = resource.getLocalRepository().findProviders(Collections.singleton(requirement));
    Collection<Capability> capabilities = map.get(requirement);
    if (capabilities.isEmpty()) {
        // Nothing found in the local repository so search the repository services.
        capabilities = new RepositoryServiceRepository().findProviders(requirement);
    }
    if (capabilities.isEmpty()) {
        // Nothing found period.
        return null;
    }
    for (Capability capability : capabilities) {
        if (!IdentityNamespace.TYPE_FRAGMENT.equals(capability.getAttributes().get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE))) {
            // See ARIES-1425.
            return capability.getResource();
        }
    }
    // Nothing here but fragment bundles. Return the first one.
    return capabilities.iterator().next().getResource();
}
Also used : RequireBundleRequirement(org.apache.aries.subsystem.core.archive.RequireBundleRequirement) Requirement(org.osgi.resource.Requirement) RequireCapabilityRequirement(org.apache.aries.subsystem.core.archive.RequireCapabilityRequirement) ImportPackageRequirement(org.apache.aries.subsystem.core.archive.ImportPackageRequirement) SubsystemImportServiceRequirement(org.apache.aries.subsystem.core.archive.SubsystemImportServiceRequirement) Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) BundleRevision(org.osgi.framework.wiring.BundleRevision) Collection(java.util.Collection)

Example 17 with Requirement

use of org.osgi.resource.Requirement in project aries by apache.

the class SubsystemResource method setImportIsolationPolicy.

private void setImportIsolationPolicy(Map<Resource, List<Wire>> resolution) throws Exception {
    if (!isApplication()) {
        return;
    }
    SubsystemContentHeader contentHeader = getSubsystemManifest().getSubsystemContentHeader();
    // Prepare the regions and filter builder to set the sharing policy.
    Region from = getRegion();
    Region to = ((BasicSubsystem) getParents().iterator().next()).getRegion();
    RegionFilterBuilder builder = from.getRegionDigraph().createRegionFilterBuilder();
    // Always provide visibility to this subsystem's service registration.
    addSubsystemServiceImportToSharingPolicy(builder, to);
    for (Resource resource : resolution.keySet()) {
        if (!contentHeader.contains(resource)) {
            continue;
        }
        // If the resource is content but the wire provider is not,
        // the sharing policy must be updated.
        List<Wire> wires = resolution.get(resource);
        for (Wire wire : wires) {
            Resource provider = wire.getProvider();
            // visible.
            if (contentHeader.contains(provider)) {
                continue;
            }
            // requirements become part of the sharing policy.
            if (!(wire.getCapability() instanceof DependencyCalculator.MissingCapability) && Constants.ResourceTypeSynthesized.equals(ResourceHelper.getTypeAttribute(provider))) {
                continue;
            }
            // The requirement must be added to the sharing policy.
            Requirement requirement = wire.getRequirement();
            List<String> namespaces = new ArrayList<String>(2);
            namespaces.add(requirement.getNamespace());
            if (ServiceNamespace.SERVICE_NAMESPACE.equals(namespaces.get(0))) {
                // Both service capabilities and services must be visible.
                namespaces.add(RegionFilter.VISIBLE_SERVICE_NAMESPACE);
            }
            String filter = requirement.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
            if (filter == null) {
                for (String namespace : namespaces) builder.allowAll(namespace);
            } else {
                for (String namespace : namespaces) builder.allow(namespace, filter);
            }
        }
    }
    // Always add access to osgi.ee and osgi.native namespaces
    setImplicitAccessToNativeAndEECapabilities(builder);
    // Now set the sharing policy, if the regions are different.
    RegionFilter regionFilter = builder.build();
    from.connectRegion(to, regionFilter);
}
Also used : Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) Wire(org.osgi.resource.Wire) SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) RequireBundleRequirement(org.apache.aries.subsystem.core.archive.RequireBundleRequirement) Requirement(org.osgi.resource.Requirement) RequireCapabilityRequirement(org.apache.aries.subsystem.core.archive.RequireCapabilityRequirement) ImportPackageRequirement(org.apache.aries.subsystem.core.archive.ImportPackageRequirement) SubsystemImportServiceRequirement(org.apache.aries.subsystem.core.archive.SubsystemImportServiceRequirement) RegionFilter(org.eclipse.equinox.region.RegionFilter) Region(org.eclipse.equinox.region.Region) RegionFilterBuilder(org.eclipse.equinox.region.RegionFilterBuilder)

Example 18 with Requirement

use of org.osgi.resource.Requirement in project aries by apache.

the class SubsystemResource method computeContentResources.

private void computeContentResources(SubsystemManifest manifest) throws BundleException, IOException, InvalidSyntaxException, URISyntaxException {
    SubsystemContentHeader contentHeader = manifest.getSubsystemContentHeader();
    if (contentHeader == null)
        return;
    for (SubsystemContentHeader.Clause clause : contentHeader.getClauses()) {
        Requirement requirement = clause.toRequirement(this);
        Resource resource = findContent(requirement);
        if (resource == null) {
            if (clause.isMandatory())
                throw new SubsystemException("A required content resource could not be found. This means the resource was either missing or not recognized as a supported resource format due to, for example, an invalid bundle manifest or blueprint XML file. Turn on debug logging for more information. The resource was: " + requirement);
            continue;
        }
        addContentResource(resource);
    }
}
Also used : SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) RequireBundleRequirement(org.apache.aries.subsystem.core.archive.RequireBundleRequirement) Requirement(org.osgi.resource.Requirement) RequireCapabilityRequirement(org.apache.aries.subsystem.core.archive.RequireCapabilityRequirement) ImportPackageRequirement(org.apache.aries.subsystem.core.archive.ImportPackageRequirement) SubsystemImportServiceRequirement(org.apache.aries.subsystem.core.archive.SubsystemImportServiceRequirement) SubsystemException(org.osgi.service.subsystem.SubsystemException) Resource(org.osgi.resource.Resource)

Example 19 with Requirement

use of org.osgi.resource.Requirement in project aries by apache.

the class SubsystemResource method getRequirements.

@Override
public List<Requirement> getRequirements(String namespace) {
    if (isScoped())
        return resource.getRequirements(namespace);
    else {
        ArrayList<Requirement> result = new ArrayList<Requirement>();
        result.addAll(resource.getRequirements(namespace));
        for (Resource r : getContentResources()) result.addAll(r.getRequirements(namespace));
        result.trimToSize();
        return result;
    }
}
Also used : RequireBundleRequirement(org.apache.aries.subsystem.core.archive.RequireBundleRequirement) Requirement(org.osgi.resource.Requirement) RequireCapabilityRequirement(org.apache.aries.subsystem.core.archive.RequireCapabilityRequirement) ImportPackageRequirement(org.apache.aries.subsystem.core.archive.ImportPackageRequirement) SubsystemImportServiceRequirement(org.apache.aries.subsystem.core.archive.SubsystemImportServiceRequirement) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource)

Example 20 with Requirement

use of org.osgi.resource.Requirement in project aries by apache.

the class Aries1425Test method testToRequirement.

@Test
public void testToRequirement() {
    SubsystemContentHeader header = new SubsystemContentHeader(HEADER_1);
    Collection<SubsystemContentHeader.Clause> clauses = header.getClauses();
    assertEquals("Wrong size", 3, clauses.size());
    Map<String, SubsystemContentHeader.Clause> map = new HashMap<String, SubsystemContentHeader.Clause>(3);
    for (SubsystemContentHeader.Clause clause : clauses) {
        map.put(clause.toString(), clause);
    }
    Resource resource = new Resource() {

        @Override
        public List<Capability> getCapabilities(String namespace) {
            return Collections.emptyList();
        }

        @Override
        public List<Requirement> getRequirements(String namespace) {
            return Collections.emptyList();
        }
    };
    SubsystemContentHeader.Clause clause = map.get(BUNDLE_A);
    Requirement requirement = clause.toRequirement(resource);
    assertTrue("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, BUNDLE_A, Version.emptyVersion, IdentityNamespace.TYPE_FRAGMENT)));
    assertTrue("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, BUNDLE_A, Version.emptyVersion, IdentityNamespace.TYPE_BUNDLE)));
    clause = map.get(BUNDLE_B);
    requirement = clause.toRequirement(resource);
    assertFalse("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, "bundle.b", Version.emptyVersion, IdentityNamespace.TYPE_FRAGMENT)));
    assertTrue("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, "bundle.b", Version.emptyVersion, IdentityNamespace.TYPE_BUNDLE)));
    clause = map.get(BUNDLE_C);
    requirement = clause.toRequirement(resource);
    assertTrue("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, "bundle.c", Version.emptyVersion, IdentityNamespace.TYPE_FRAGMENT)));
    assertFalse("Wrong requirement", ResourceHelper.matches(requirement, new OsgiIdentityCapability(resource, "bundle.c", Version.emptyVersion, IdentityNamespace.TYPE_BUNDLE)));
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) OsgiIdentityCapability(org.apache.aries.subsystem.core.internal.OsgiIdentityCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) OsgiIdentityCapability(org.apache.aries.subsystem.core.internal.OsgiIdentityCapability) Test(org.junit.Test)

Aggregations

Requirement (org.osgi.resource.Requirement)193 Capability (org.osgi.resource.Capability)102 Resource (org.osgi.resource.Resource)59 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)53 ArrayList (java.util.ArrayList)46 Collection (java.util.Collection)44 BndEditModel (aQute.bnd.build.model.BndEditModel)35 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)34 HashMap (java.util.HashMap)34 MockRegistry (test.lib.MockRegistry)32 List (java.util.List)19 Repository (org.osgi.service.repository.Repository)19 Test (org.junit.Test)18 File (java.io.File)14 Map (java.util.Map)12 Resolver (org.osgi.service.resolver.Resolver)11 LinkedHashSet (java.util.LinkedHashSet)10 LinkedList (java.util.LinkedList)10 Wire (org.osgi.resource.Wire)10 Parameters (aQute.bnd.header.Parameters)9