Search in sources :

Example 11 with Requirement

use of io.fabric8.agent.model.Requirement in project fabric8 by jboss-fuse.

the class ServiceImplTest method testCheckPrerequisitesMultiplePatches.

@Test
public void testCheckPrerequisitesMultiplePatches() throws IOException {
    ServiceImpl service = createMockServiceImpl(getDirectoryForResource("prereq/patch1.patch"));
    Collection<Patch> patches = new LinkedList<Patch>();
    patches.add(service.getPatch("patch3"));
    // this should not throw a PatchException
    service.checkPrerequisites(patches);
    patches.add(service.getPatch("patch2"));
    try {
        service.checkPrerequisites(patches);
        fail("Should not pass check if one of the patches is missing a requirement");
    } catch (PatchException e) {
    // graciously do nothing, this is OK
    }
}
Also used : GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) PatchException(io.fabric8.patch.management.PatchException) Patch(io.fabric8.patch.management.Patch) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 12 with Requirement

use of io.fabric8.agent.model.Requirement in project fabric8 by jboss-fuse.

the class Deployer method computeBundlesToRefresh.

private void computeBundlesToRefresh(Map<Bundle, String> toRefresh, Collection<Bundle> bundles, Map<Resource, Bundle> resources, Map<Resource, List<Wire>> resolution) {
    // Compute the new list of fragments
    Map<Bundle, Set<Resource>> newFragments = new HashMap<>();
    for (Bundle bundle : bundles) {
        newFragments.put(bundle, new HashSet<Resource>());
    }
    if (resolution != null) {
        for (Resource res : resolution.keySet()) {
            for (Wire wire : resolution.get(res)) {
                if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
                    Bundle bundle = resources.get(wire.getProvider());
                    if (bundle != null) {
                        Bundle b = resources.get(wire.getRequirer());
                        Resource r = b != null ? b.adapt(BundleRevision.class) : wire.getRequirer();
                        newFragments.get(bundle).add(r);
                    }
                }
            }
        }
    }
    // Main loop
    int size;
    Map<Bundle, Resource> bndToRes = new HashMap<>();
    for (Map.Entry<Resource, Bundle> entry : resources.entrySet()) {
        bndToRes.put(entry.getValue(), entry.getKey());
    }
    do {
        size = toRefresh.size();
        main: for (Bundle bundle : bundles) {
            Resource resource = bndToRes.get(bundle);
            // This bundle is not managed
            if (resource == null) {
                continue;
            }
            // Continue if we already know about this bundle
            if (toRefresh.containsKey(bundle)) {
                continue;
            }
            // Ignore non resolved bundle
            BundleWiring wiring = bundle.adapt(BundleWiring.class);
            if (wiring == null) {
                continue;
            }
            // Ignore bundles that won't be wired
            List<Wire> newWires = resolution.get(resource);
            if (newWires == null) {
                continue;
            }
            // Check if this bundle is a host and its fragments changed
            Set<Resource> oldFragments = new HashSet<>();
            for (BundleWire wire : wiring.getProvidedWires(null)) {
                if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
                    oldFragments.add(wire.getRequirer());
                }
            }
            if (!oldFragments.equals(newFragments.get(bundle))) {
                toRefresh.put(bundle, "Attached fragments changed: " + new ArrayList<>(newFragments.get(bundle)));
                break;
            }
            // Compare the old and new resolutions
            Set<Resource> wiredBundles = new HashSet<>();
            for (BundleWire wire : wiring.getRequiredWires(null)) {
                BundleRevision rev = wire.getProvider();
                Bundle provider = rev.getBundle();
                if (toRefresh.containsKey(provider)) {
                    // The bundle is wired to a bundle being refreshed,
                    // so we need to refresh it too
                    toRefresh.put(bundle, "Wired to " + provider.getSymbolicName() + "/" + provider.getVersion() + " which is being refreshed");
                    continue main;
                }
                Resource res = bndToRes.get(provider);
                wiredBundles.add(res != null ? res : rev);
            }
            Map<Resource, Requirement> wiredResources = new HashMap<>();
            for (Wire wire : newWires) {
                // Handle only packages, hosts, and required bundles
                String namespace = wire.getRequirement().getNamespace();
                if (!namespace.equals(BundleNamespace.BUNDLE_NAMESPACE) && !namespace.equals(PackageNamespace.PACKAGE_NAMESPACE) && !namespace.equals(HostNamespace.HOST_NAMESPACE)) {
                    continue;
                }
                // Ignore non-resolution time requirements
                String effective = wire.getRequirement().getDirectives().get(Namespace.CAPABILITY_EFFECTIVE_DIRECTIVE);
                if (effective != null && !Namespace.EFFECTIVE_RESOLVE.equals(effective)) {
                    continue;
                }
                // Ignore non bundle resources
                if (!isBundle(wire.getProvider())) {
                    continue;
                }
                if (!wiredResources.containsKey(wire.getProvider())) {
                    wiredResources.put(wire.getProvider(), wire.getRequirement());
                }
            }
            if (!wiredBundles.containsAll(wiredResources.keySet())) {
                Map<Resource, Requirement> newResources = new HashMap<>(wiredResources);
                newResources.keySet().removeAll(wiredBundles);
                StringBuilder sb = new StringBuilder();
                sb.append("Should be wired to: ");
                boolean first = true;
                for (Map.Entry<Resource, Requirement> entry : newResources.entrySet()) {
                    if (!first) {
                        sb.append(", ");
                    } else {
                        first = false;
                    }
                    Resource res = entry.getKey();
                    Requirement req = entry.getValue();
                    sb.append(getSymbolicName(res)).append("/").append(getVersion(res));
                    sb.append(" (through ");
                    sb.append(req);
                    sb.append(")");
                }
                toRefresh.put(bundle, sb.toString());
            }
        }
    } while (toRefresh.size() > size);
}
Also used : MapUtils.addToMapSet(io.fabric8.agent.internal.MapUtils.addToMapSet) MapUtils.removeFromMapSet(io.fabric8.agent.internal.MapUtils.removeFromMapSet) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) Resource(org.osgi.resource.Resource) FeatureResource(io.fabric8.agent.resolver.FeatureResource) Wire(org.osgi.resource.Wire) BundleWire(org.osgi.framework.wiring.BundleWire) BundleWire(org.osgi.framework.wiring.BundleWire) Requirement(org.osgi.resource.Requirement) ZipEntry(java.util.zip.ZipEntry) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Aggregations

Requirement (org.osgi.resource.Requirement)7 Resource (org.osgi.resource.Resource)6 Capability (org.osgi.resource.Capability)5 ResourceUtils.addIdentityRequirement (io.fabric8.agent.resolver.ResourceUtils.addIdentityRequirement)4 HashMap (java.util.HashMap)4 BundleRevision (org.osgi.framework.wiring.BundleRevision)4 SimpleFilter (io.fabric8.agent.resolver.SimpleFilter)3 VersionRange (org.apache.felix.utils.version.VersionRange)3 Wire (org.osgi.resource.Wire)3 MapUtils.addToMapSet (io.fabric8.agent.internal.MapUtils.addToMapSet)2 MapUtils.removeFromMapSet (io.fabric8.agent.internal.MapUtils.removeFromMapSet)2 BundleInfo (io.fabric8.agent.model.BundleInfo)2 Dependency (io.fabric8.agent.model.Dependency)2 Feature (io.fabric8.agent.model.Feature)2 CapabilitySet (io.fabric8.agent.resolver.CapabilitySet)2 FeatureResource (io.fabric8.agent.resolver.FeatureResource)2 ResourceUtils.toFeatureRequirement (io.fabric8.agent.resolver.ResourceUtils.toFeatureRequirement)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Map (java.util.Map)2