Search in sources :

Example 11 with Feature

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

the class Subsystem method build.

@SuppressWarnings("InfiniteLoopStatement")
public void build(Collection<Feature> features) throws Exception {
    for (Subsystem child : children) {
        child.build(features);
    }
    if (feature != null) {
        for (Dependency dep : feature.getDependencies()) {
            Subsystem ss = this;
            while (!ss.isAcceptDependencies()) {
                ss = ss.getParent();
            }
            ss.requireFeature(dep.getName(), dep.getVersion(), !dep.isDependency());
        }
    }
    List<Requirement> processed = new ArrayList<>();
    while (true) {
        List<Requirement> requirements = getRequirements(IDENTITY_NAMESPACE);
        requirements.addAll(dependentFeatures);
        requirements.removeAll(processed);
        if (requirements.isEmpty()) {
            break;
        }
        for (Requirement requirement : requirements) {
            String name = (String) requirement.getAttributes().get(IDENTITY_NAMESPACE);
            String type = (String) requirement.getAttributes().get(CAPABILITY_TYPE_ATTRIBUTE);
            VersionRange range = (VersionRange) requirement.getAttributes().get(CAPABILITY_VERSION_ATTRIBUTE);
            if (TYPE_FEATURE.equals(type)) {
                for (Feature feature : features) {
                    if (feature.getName().equals(name) && (range == null || range.contains(VersionTable.getVersion(feature.getVersion())))) {
                        if (feature != this.feature) {
                            String ssName = this.name + "#" + (feature.hasVersion() ? feature.getName() + "-" + feature.getVersion() : feature.getName());
                            Subsystem fs = getChild(ssName);
                            if (fs == null) {
                                fs = new Subsystem(ssName, feature, this);
                                fs.build(features);
                                installable.add(fs);
                                children.add(fs);
                            }
                        }
                    }
                }
            }
            processed.add(requirement);
        }
    }
}
Also used : Requirement(org.osgi.resource.Requirement) ResourceUtils.toFeatureRequirement(io.fabric8.agent.resolver.ResourceUtils.toFeatureRequirement) ResourceUtils.addIdentityRequirement(io.fabric8.agent.resolver.ResourceUtils.addIdentityRequirement) ArrayList(java.util.ArrayList) VersionRange(org.apache.felix.utils.version.VersionRange) Dependency(io.fabric8.agent.model.Dependency) Feature(io.fabric8.agent.model.Feature)

Example 12 with Feature

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

the class SubsystemResolveContext method findProviders.

@Override
public List<Capability> findProviders(Requirement requirement) {
    List<Capability> caps = new ArrayList<Capability>();
    Region requirerRegion = getRegion(requirement.getResource());
    if (requirerRegion != null) {
        Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
        Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
        if (res != null && !res.isEmpty()) {
            caps.addAll(res);
        } else if (globalRepository != null) {
            // Only bring in external resources for non optional requirements
            if (!RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(RESOLUTION_DIRECTIVE))) {
                resMap = globalRepository.findProviders(Collections.singleton(requirement));
                res = resMap != null ? resMap.get(requirement) : null;
                if (res != null && !res.isEmpty()) {
                    caps.addAll(res);
                }
            }
        }
        // Use the digraph to prune non visible capabilities
        Visitor visitor = new Visitor(caps);
        requirerRegion.visitSubgraph(visitor);
        Collection<Capability> allowed = visitor.getAllowed();
        caps.retainAll(allowed);
        // the parent one
        if (caps.size() > 1) {
            Map<String, Resource> providers = new HashMap<String, Resource>();
            for (Capability cap : caps) {
                Resource resource = cap.getResource();
                String id = getSymbolicName(resource) + "|" + getVersion(resource);
                Resource prev = providers.get(id);
                if (prev != null && prev != resource) {
                    Region r1 = getRegion(prev);
                    Region r2 = getRegion(resource);
                    boolean r2canSeeR1 = isResourceVisibleFromRegion(prev, r2);
                    boolean r1canSeeR2 = isResourceVisibleFromRegion(resource, r1);
                    if (r1canSeeR2 && r2canSeeR1) {
                        // r1 and r2 can see each other
                        int reqDiff = prev.getRequirements(null).size() - resource.getRequirements(null).size();
                        if (reqDiff == 0) {
                            String r1Name = getRegion(prev).getName();
                            String r2Name = getRegion(resource).getName();
                            int c = r1Name.compareTo(r2Name);
                            if (c == 0) {
                                // One of the resource has to be a bundle, use that one
                                c = (prev instanceof BundleRevision) ? -1 : +1;
                            }
                            resource = c < 0 ? prev : resource;
                        } else {
                            // one of the resource has less requirements, so use this one
                            // This can be the case when one resource has conditionals, which adds further
                            // requirements to the condition feature.
                            resource = reqDiff < 0 ? prev : resource;
                        }
                    } else {
                        // only one region can see the other, grab the correct
                        resource = r1canSeeR2 ? prev : resource;
                    }
                }
                providers.put(id, resource);
            }
            for (Iterator<Capability> it = caps.iterator(); it.hasNext(); ) {
                Capability cap = it.next();
                if (!providers.values().contains(cap.getResource())) {
                    it.remove();
                }
            }
        }
        // Sort caps
        Collections.sort(caps, candidateComparator);
    }
    return caps;
}
Also used : HostedCapability(org.osgi.service.resolver.HostedCapability) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) Requirement(org.osgi.resource.Requirement) ResourceUtils.addIdentityRequirement(io.fabric8.agent.resolver.ResourceUtils.addIdentityRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) Region(org.eclipse.equinox.region.Region) Collection(java.util.Collection)

Example 13 with Feature

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

the class SubsystemResolver method getResourceMapping.

private Map<Resource, String> getResourceMapping(SimpleFilter resourceFilter) {
    Map<String, String> flats = getFlatSubsystemsMap();
    Map<Resource, List<Wire>> wiring = getWiring();
    Map<Resource, String> resources = new HashMap<>();
    SimpleFilter sf = createFilter(IDENTITY_NAMESPACE, "*", CAPABILITY_TYPE_ATTRIBUTE, TYPE_SUBSYSTEM);
    if (wiring != null) {
        for (Resource resource : wiring.keySet()) {
            if (findMatchingCapability(resourceFilter, resource.getCapabilities(null)) != null) {
                // Find the subsystem where this feature is installed
                Wire wire = findMatchingWire(sf, wiring.get(resource));
                if (wire != null) {
                    String region = (String) wire.getCapability().getAttributes().get(IDENTITY_NAMESPACE);
                    region = flats.get(region);
                    resources.put(resource, region);
                }
            }
        }
    }
    return resources;
}
Also used : HashMap(java.util.HashMap) SimpleFilter(io.fabric8.agent.resolver.SimpleFilter) Resource(org.osgi.resource.Resource) List(java.util.List) Wire(org.osgi.resource.Wire)

Example 14 with Feature

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

the class SubsystemResolver method prepare.

public void prepare(Collection<Feature> allFeatures, Map<String, Set<String>> requirements, Map<String, Set<BundleRevision>> system) throws Exception {
    // Build subsystems on the fly
    for (Map.Entry<String, Set<String>> entry : requirements.entrySet()) {
        String[] parts = entry.getKey().split("/");
        if (root == null) {
            root = new Subsystem(parts[0]);
        } else if (!root.getName().equals(parts[0])) {
            throw new IllegalArgumentException("Can not use multiple roots: " + root.getName() + ", " + parts[0]);
        }
        Subsystem ss = root;
        for (int i = 1; i < parts.length; i++) {
            ss = getOrCreateChild(ss, parts[i]);
        }
        for (String requirement : entry.getValue()) {
            ss.require(requirement);
        }
    }
    if (root == null) {
        return;
    }
    // Pre-resolve
    root.build(allFeatures);
    // Add system resources
    BundleRevision sysBundleRev = null;
    boolean hasEeCap = false;
    for (Map.Entry<String, Set<BundleRevision>> entry : system.entrySet()) {
        Subsystem ss = null;
        String[] parts = entry.getKey().split("/");
        String path = parts[0];
        if (path.equals(root.getName())) {
            ss = root;
        }
        for (int i = 1; ss != null && i < parts.length; i++) {
            path += "/" + parts[i];
            ss = ss.getChild(path);
        }
        if (ss != null) {
            ResourceImpl dummy = new ResourceImpl("dummy", "dummy", Version.emptyVersion);
            for (BundleRevision res : entry.getValue()) {
                // We need to explicitely provide service capabilities for bundles
                // We use both actual services and services declared from the headers
                // TODO: use actual services
                Map<String, String> headers = new DictionaryAsMap<>(res.getBundle().getHeaders());
                Resource tmp = ResourceBuilder.build(res.getBundle().getLocation(), headers);
                for (Capability cap : tmp.getCapabilities(ServiceNamespace.SERVICE_NAMESPACE)) {
                    dummy.addCapability(new CapabilityImpl(dummy, cap.getNamespace(), cap.getDirectives(), cap.getAttributes()));
                }
                ss.addSystemResource(res);
                for (Capability cap : res.getCapabilities(null)) {
                    hasEeCap |= cap.getNamespace().equals(EXECUTION_ENVIRONMENT_NAMESPACE);
                }
                if (res.getBundle().getBundleId() == 0) {
                    sysBundleRev = res;
                }
            }
            ss.addSystemResource(dummy);
        }
    }
    // Under Equinox, the osgi.ee capabilities are not provided by the system bundle
    if (!hasEeCap && sysBundleRev != null) {
        String provideCaps = sysBundleRev.getBundle().getHeaders().get(PROVIDE_CAPABILITY);
        environmentResource = new ResourceImpl("environment", "karaf.environment", Version.emptyVersion);
        environmentResource.addCapabilities(ResourceBuilder.parseCapability(environmentResource, provideCaps));
        root.addSystemResource(environmentResource);
    }
}
Also used : CapabilitySet(io.fabric8.agent.resolver.CapabilitySet) Set(java.util.Set) Capability(org.osgi.resource.Capability) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap) Resource(org.osgi.resource.Resource) ResourceImpl(io.fabric8.agent.resolver.ResourceImpl) CapabilityImpl(io.fabric8.agent.resolver.CapabilityImpl) BundleRevision(org.osgi.framework.wiring.BundleRevision) HashMap(java.util.HashMap) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap) Map(java.util.Map)

Example 15 with Feature

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

the class CamelProfileScriptTest method testFeatures.

@Test
public void testFeatures() throws Exception {
    System.out.println(executeCommand("fabric:create -n --wait-for-provisioning"));
    // System.out.println(executeCommand("shell:info"));
    // System.out.println(executeCommand("fabric:info"));
    // System.out.println(executeCommand("fabric:profile-list"));
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(bundleContext, FabricService.class);
    try {
        FabricService fabricService = fabricProxy.getService();
        Set<ContainerProxy> containers = ContainerBuilder.create(fabricProxy).withName("feature-camel").withProfiles("feature-camel").assertProvisioningResult().build();
        try {
            CuratorFramework curator = fabricService.adapt(CuratorFramework.class);
            assertProvisionedFeature(fabricService, curator, containers, "camel-script-javascript", "feature-camel", "scriptengines-javascript");
        } finally {
            ContainerBuilder.destroy(containers);
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) FabricService(io.fabric8.api.FabricService) ContainerProxy(io.fabric8.itests.paxexam.support.ContainerProxy) Test(org.junit.Test) FabricFeaturesTest(io.fabric8.itests.paxexam.support.FabricFeaturesTest)

Aggregations

Test (org.junit.Test)20 HashMap (java.util.HashMap)18 FabricService (io.fabric8.api.FabricService)15 IOException (java.io.IOException)13 Feature (io.fabric8.agent.model.Feature)12 Container (io.fabric8.api.Container)11 Profile (io.fabric8.api.Profile)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 Map (java.util.Map)9 BundleContext (org.osgi.framework.BundleContext)9 HashSet (java.util.HashSet)8 URL (java.net.URL)7 BundleException (org.osgi.framework.BundleException)7 Repository (io.fabric8.agent.model.Repository)6 Version (io.fabric8.api.Version)6 LinkedHashSet (java.util.LinkedHashSet)6 Ignore (org.junit.Ignore)6 BundleInfo (io.fabric8.agent.model.BundleInfo)5 ContainerProxy (io.fabric8.itests.paxexam.support.ContainerProxy)5