Search in sources :

Example 1 with BaseRepository

use of org.apache.karaf.features.internal.repository.BaseRepository in project karaf by apache.

the class Builder method resolve.

private Map<String, Integer> resolve(DownloadManager manager, Resolver resolver, Collection<Features> repositories, Collection<String> features, Collection<String> bundles, Collection<String> overrides, Collection<String> optionals) throws Exception {
    BundleRevision systemBundle = getSystemBundle();
    AssemblyDeployCallback callback = new AssemblyDeployCallback(manager, this, systemBundle, repositories);
    Deployer deployer = new Deployer(manager, resolver, callback, callback);
    // Install framework
    Deployer.DeploymentRequest request = createDeploymentRequest();
    // Add overrides
    request.overrides.addAll(overrides);
    // Add optional resources
    final List<Resource> resources = new ArrayList<>();
    Downloader downloader = manager.createDownloader();
    for (String optional : optionals) {
        downloader.download(optional, provider -> {
            Resource resource = ResourceBuilder.build(provider.getUrl(), getHeaders(provider));
            synchronized (resources) {
                resources.add(resource);
            }
        });
    }
    downloader.await();
    request.globalRepository = new BaseRepository(resources);
    // Install features
    for (String feature : features) {
        MapUtils.addToMapSet(request.requirements, FeaturesService.ROOT_REGION, feature);
    }
    for (String bundle : bundles) {
        MapUtils.addToMapSet(request.requirements, FeaturesService.ROOT_REGION, "bundle:" + bundle);
    }
    Set<String> prereqs = new HashSet<>();
    while (true) {
        try {
            deployer.deploy(callback.getDeploymentState(), request);
            break;
        } catch (Deployer.PartialDeploymentException e) {
            if (!prereqs.containsAll(e.getMissing())) {
                prereqs.addAll(e.getMissing());
            } else {
                throw new Exception("Deployment aborted due to loop in missing prerequisites: " + e.getMissing());
            }
        }
    }
    return callback.getStartupBundles();
}
Also used : ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) Downloader(org.apache.karaf.features.internal.download.Downloader) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BundleRevision(org.osgi.framework.wiring.BundleRevision) Deployer(org.apache.karaf.features.internal.service.Deployer) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with BaseRepository

use of org.apache.karaf.features.internal.repository.BaseRepository in project karaf by apache.

the class Subsystem method downloadBundles.

@SuppressWarnings("InfiniteLoopStatement")
public void downloadBundles(DownloadManager manager, Set<String> overrides, String featureResolutionRange, final String serviceRequirements, RepositoryManager repos) throws Exception {
    for (Subsystem child : children) {
        child.downloadBundles(manager, overrides, featureResolutionRange, serviceRequirements, repos);
    }
    final Map<String, ResourceImpl> bundles = new ConcurrentHashMap<>();
    final Downloader downloader = manager.createDownloader();
    final Map<BundleInfo, Conditional> infos = new HashMap<>();
    if (feature != null) {
        for (Conditional cond : feature.getConditional()) {
            for (final BundleInfo bi : cond.getBundles()) {
                infos.put(bi, cond);
            }
        }
        for (BundleInfo bi : feature.getBundles()) {
            infos.put(bi, null);
        }
    }
    boolean removeServiceRequirements;
    if (FeaturesService.SERVICE_REQUIREMENTS_DISABLE.equals(serviceRequirements)) {
        removeServiceRequirements = true;
    } else if (feature != null && FeaturesService.SERVICE_REQUIREMENTS_DEFAULT.equals(serviceRequirements)) {
        removeServiceRequirements = FeaturesNamespaces.URI_1_0_0.equals(feature.getNamespace()) || FeaturesNamespaces.URI_1_1_0.equals(feature.getNamespace()) || FeaturesNamespaces.URI_1_2_0.equals(feature.getNamespace()) || FeaturesNamespaces.URI_1_2_1.equals(feature.getNamespace());
    } else {
        removeServiceRequirements = false;
    }
    for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
        final BundleInfo bi = entry.getKey();
        final String loc = bi.getLocation();
        downloader.download(loc, provider -> {
            bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
        });
    }
    for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
        final String loc = bundle.getName();
        downloader.download(loc, provider -> {
            bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
        });
    }
    for (String override : overrides) {
        final String loc = Overrides.extractUrl(override);
        downloader.download(loc, provider -> {
            bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
        });
    }
    if (feature != null) {
        for (Library library : feature.getLibraries()) {
            if (library.isExport()) {
                final String loc = library.getLocation();
                downloader.download(loc, provider -> {
                    bundles.put(loc, createResource(loc, getMetadata(provider), removeServiceRequirements));
                });
            }
        }
    }
    downloader.await();
    Overrides.override(bundles, overrides);
    if (feature != null) {
        // Add conditionals
        Map<Conditional, Resource> resConds = new HashMap<>();
        for (Conditional cond : feature.getConditional()) {
            FeatureResource resCond = FeatureResource.build(feature, cond, featureResolutionRange, bundles);
            addIdentityRequirement(this, resCond, false);
            addIdentityRequirement(resCond, this, true);
            installable.add(resCond);
            resConds.put(cond, resCond);
        }
        // Add features
        FeatureResource resFeature = FeatureResource.build(feature, featureResolutionRange, bundles);
        addIdentityRequirement(resFeature, this);
        installable.add(resFeature);
        // Add dependencies
        for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
            final BundleInfo bi = entry.getKey();
            final String loc = bi.getLocation();
            final Conditional cond = entry.getValue();
            ResourceImpl res = bundles.get(loc);
            int sl = bi.getStartLevel() <= 0 ? feature.getStartLevel() : bi.getStartLevel();
            if (bi.isDependency()) {
                addDependency(res, false, bi.isStart(), sl);
            } else {
                doAddDependency(res, cond == null, bi.isStart(), sl);
            }
            if (cond != null) {
                addIdentityRequirement(res, resConds.get(cond), true);
            }
        }
        for (Library library : feature.getLibraries()) {
            if (library.isExport()) {
                final String loc = library.getLocation();
                ResourceImpl res = bundles.get(loc);
                addDependency(res, false, false, 0);
            }
        }
        for (String uri : feature.getResourceRepositories()) {
            BaseRepository repo = repos.getRepository(feature.getRepositoryUrl(), uri);
            for (Resource resource : repo.getResources()) {
                ResourceImpl res = cloneResource(resource);
                addDependency(res, false, true, 0);
            }
        }
    }
    for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
        final String loc = bundle.getName();
        boolean dependency = Boolean.parseBoolean(bundle.getAttribute("dependency"));
        boolean start = bundle.getAttribute("start") == null || Boolean.parseBoolean(bundle.getAttribute("start"));
        int startLevel = 0;
        try {
            startLevel = Integer.parseInt(bundle.getAttribute("start-level"));
        } catch (NumberFormatException e) {
        // Ignore
        }
        if (dependency) {
            addDependency(bundles.get(loc), false, start, startLevel);
        } else {
            doAddDependency(bundles.get(loc), true, start, startLevel);
            addIdentityRequirement(this, bundles.get(loc));
        }
    }
    // Compute dependencies
    for (DependencyInfo info : dependencies.values()) {
        installable.add(info.resource);
        addIdentityRequirement(info.resource, this, info.mandatory);
    }
}
Also used : FeatureResource(org.apache.karaf.features.internal.resolver.FeatureResource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FeatureResource(org.apache.karaf.features.internal.resolver.FeatureResource) Resource(org.osgi.resource.Resource) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) Downloader(org.apache.karaf.features.internal.download.Downloader) Conditional(org.apache.karaf.features.Conditional) ResourceImpl(org.apache.karaf.features.internal.resolver.ResourceImpl) BundleInfo(org.apache.karaf.features.BundleInfo) Clause(org.apache.felix.utils.manifest.Clause) Library(org.apache.karaf.features.Library) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

Downloader (org.apache.karaf.features.internal.download.Downloader)2 BaseRepository (org.apache.karaf.features.internal.repository.BaseRepository)2 Resource (org.osgi.resource.Resource)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Clause (org.apache.felix.utils.manifest.Clause)1 BundleInfo (org.apache.karaf.features.BundleInfo)1 Conditional (org.apache.karaf.features.Conditional)1 Library (org.apache.karaf.features.Library)1 FeatureResource (org.apache.karaf.features.internal.resolver.FeatureResource)1 ResourceImpl (org.apache.karaf.features.internal.resolver.ResourceImpl)1 Deployer (org.apache.karaf.features.internal.service.Deployer)1 BundleRevision (org.osgi.framework.wiring.BundleRevision)1