Search in sources :

Example 1 with Library

use of org.apache.karaf.features.Library 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)

Example 2 with Library

use of org.apache.karaf.features.Library in project karaf by apache.

the class Builder method bootStage.

private Set<Feature> bootStage(Profile bootProfile, Profile startupEffective) throws Exception {
    LOGGER.info("Boot stage");
    //
    // Handle boot profiles
    //
    Profile bootOverlay = Profiles.getOverlay(bootProfile, allProfiles, environment);
    Profile bootEffective = Profiles.getEffective(bootOverlay, false);
    // Load startup repositories
    Map<String, Features> bootRepositories = loadRepositories(manager, bootEffective.getRepositories(), true);
    // Compute startup feature dependencies
    Set<Feature> allBootFeatures = new HashSet<>();
    for (Features repo : bootRepositories.values()) {
        allBootFeatures.addAll(repo.getFeature());
    }
    // Generate a global feature
    Map<String, Dependency> generatedDep = new HashMap<>();
    Feature generated = new Feature();
    generated.setName(UUID.randomUUID().toString());
    // Add feature dependencies
    for (String dependency : bootEffective.getFeatures()) {
        Dependency dep = generatedDep.get(dependency);
        if (dep == null) {
            dep = createDependency(dependency);
            generated.getFeature().add(dep);
            generatedDep.put(dep.getName(), dep);
        }
        dep.setDependency(false);
    }
    // Add bundles
    for (String location : bootEffective.getBundles()) {
        location = location.replace("profile:", "file:etc/");
        Bundle bun = new Bundle();
        bun.setLocation(location);
        generated.getBundle().add(bun);
    }
    Features rep = new Features();
    rep.setName(UUID.randomUUID().toString());
    rep.getRepository().addAll(bootEffective.getRepositories());
    rep.getFeature().add(generated);
    allBootFeatures.add(generated);
    Downloader downloader = manager.createDownloader();
    // Compute startup feature dependencies
    Set<Feature> bootFeatures = new HashSet<>();
    addFeatures(allBootFeatures, generated.getName(), bootFeatures, true);
    for (Feature feature : bootFeatures) {
        // the feature is a startup feature, updating startup.properties file
        LOGGER.info("   Feature " + feature.getId() + " is defined as a boot feature");
        // add the feature in the system folder
        Set<String> locations = new HashSet<>();
        for (Bundle bundle : feature.getBundle()) {
            if (!ignoreDependencyFlag || !bundle.isDependency()) {
                locations.add(bundle.getLocation().trim());
            }
        }
        for (Conditional cond : feature.getConditional()) {
            for (Bundle bundle : cond.getBundle()) {
                if (!ignoreDependencyFlag || !bundle.isDependency()) {
                    locations.add(bundle.getLocation().trim());
                }
            }
        }
        // Build optional features and known prerequisites
        Map<String, List<String>> prereqs = new HashMap<>();
        prereqs.put("blueprint:", Arrays.asList("deployer", "aries-blueprint"));
        prereqs.put("spring:", Arrays.asList("deployer", "spring"));
        prereqs.put("wrap:", Arrays.asList("wrap"));
        prereqs.put("war:", Arrays.asList("war"));
        for (String location : locations) {
            installArtifact(downloader, location);
            for (Map.Entry<String, List<String>> entry : prereqs.entrySet()) {
                if (location.startsWith(entry.getKey())) {
                    for (String prereq : entry.getValue()) {
                        Dependency dep = generatedDep.get(prereq);
                        if (dep == null) {
                            dep = new Dependency();
                            dep.setName(prereq);
                            generated.getFeature().add(dep);
                            generatedDep.put(dep.getName(), dep);
                        }
                        dep.setPrerequisite(true);
                    }
                }
            }
        }
        List<Content> contents = new ArrayList<>();
        contents.add(feature);
        contents.addAll(feature.getConditional());
        for (Content content : contents) {
            // Install config files
            for (Config config : content.getConfig()) {
                if (config.isExternal()) {
                    installArtifact(downloader, config.getValue().trim());
                }
            }
            for (ConfigFile configFile : content.getConfigfile()) {
                installArtifact(downloader, configFile.getLocation().trim());
            }
            // Extract configs
            for (Config config : content.getConfig()) {
                if (pidMatching(config.getName())) {
                    Path configFile = etcDirectory.resolve(config.getName() + ".cfg");
                    LOGGER.info("      adding config file: {}", homeDirectory.relativize(configFile));
                    if (config.isExternal()) {
                        downloader.download(config.getValue().trim(), provider -> {
                            synchronized (provider) {
                                Files.copy(provider.getFile().toPath(), configFile, StandardCopyOption.REPLACE_EXISTING);
                            }
                        });
                    } else {
                        Files.write(configFile, config.getValue().getBytes());
                    }
                }
            }
        }
        // Install libraries
        List<String> libraries = new ArrayList<>();
        for (Library library : feature.getLibraries()) {
            String lib = library.getLocation() + ";type:=" + library.getType() + ";export:=" + library.isExport() + ";delegate:=" + library.isDelegate();
            libraries.add(lib);
        }
        Path configPropertiesPath = etcDirectory.resolve("config.properties");
        Properties configProperties = new Properties(configPropertiesPath.toFile());
        downloadLibraries(downloader, configProperties, libraries, "   ");
        downloader.await();
        // Reformat clauses
        reformatClauses(configProperties, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
        reformatClauses(configProperties, Constants.FRAMEWORK_BOOTDELEGATION);
        configProperties.save();
    }
    // If there are bundles to install, we can't use the boot features only
    // so keep the generated feature
    Path featuresCfgFile = etcDirectory.resolve("org.apache.karaf.features.cfg");
    if (!generated.getBundle().isEmpty()) {
        File output = etcDirectory.resolve(rep.getName() + ".xml").toFile();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JaxbUtil.marshal(rep, baos);
        ByteArrayInputStream bais;
        String repoUrl;
        if (karafVersion == KarafVersion.v24) {
            String str = baos.toString();
            str = str.replace("http://karaf.apache.org/xmlns/features/v1.3.0", "http://karaf.apache.org/xmlns/features/v1.2.0");
            str = str.replaceAll(" dependency=\".*?\"", "");
            str = str.replaceAll(" prerequisite=\".*?\"", "");
            for (Feature f : rep.getFeature()) {
                for (Dependency d : f.getFeature()) {
                    if (d.isPrerequisite()) {
                        if (!startupEffective.getFeatures().contains(d.getName())) {
                            LOGGER.warn("Feature " + d.getName() + " is a prerequisite and should be installed as a startup feature.");
                        }
                    }
                }
            }
            bais = new ByteArrayInputStream(str.getBytes());
            repoUrl = "file:etc/" + output.getName();
        } else {
            bais = new ByteArrayInputStream(baos.toByteArray());
            repoUrl = "file:${karaf.home}/etc/" + output.getName();
        }
        Files.copy(bais, output.toPath());
        Properties featuresProperties = new Properties(featuresCfgFile.toFile());
        featuresProperties.put(FEATURES_REPOSITORIES, repoUrl);
        featuresProperties.put(FEATURES_BOOT, generated.getName());
        featuresProperties.save();
    } else {
        String repos = getRepos(rep);
        String boot = getBootFeatures(generatedDep);
        Properties featuresProperties = new Properties(featuresCfgFile.toFile());
        featuresProperties.put(FEATURES_REPOSITORIES, repos);
        featuresProperties.put(FEATURES_BOOT, boot);
        reformatClauses(featuresProperties, FEATURES_REPOSITORIES);
        reformatClauses(featuresProperties, FEATURES_BOOT);
        featuresProperties.save();
    }
    downloader.await();
    return allBootFeatures;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Config(org.apache.karaf.features.internal.model.Config) ArrayList(java.util.ArrayList) Downloader(org.apache.karaf.features.internal.download.Downloader) Conditional(org.apache.karaf.features.internal.model.Conditional) Properties(org.apache.felix.utils.properties.Properties) Feature(org.apache.karaf.features.internal.model.Feature) Profile(org.apache.karaf.profile.Profile) Features(org.apache.karaf.features.internal.model.Features) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) Bundle(org.apache.karaf.features.internal.model.Bundle) Dependency(org.apache.karaf.features.internal.model.Dependency) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Content(org.apache.karaf.features.internal.model.Content) Library(org.apache.karaf.features.Library) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) File(java.io.File)

Example 3 with Library

use of org.apache.karaf.features.Library in project karaf by apache.

the class FeaturesValidationTest method testNs13.

@Test
public void testNs13() throws Exception {
    Repository features = unmarshalAndValidate("f07.xml");
    assertNotNull(features);
    Feature f0 = features.getFeatures()[0];
    Feature f1 = features.getFeatures()[1];
    assertEquals("2.5.6.SEC02", f0.getVersion());
    assertTrue(f1.isHidden());
    assertNotNull(f1.getLibraries());
    assertEquals(1, f0.getLibraries().size());
    Library lib = f0.getLibraries().get(0);
    assertEquals("my-library", lib.getLocation());
    assertEquals(Library.TYPE_ENDORSED, lib.getType());
    assertFalse(lib.isExport());
    assertTrue(lib.isDelegate());
}
Also used : Repository(org.apache.karaf.features.Repository) Library(org.apache.karaf.features.Library) Feature(org.apache.karaf.features.Feature) Test(org.junit.Test)

Example 4 with Library

use of org.apache.karaf.features.Library in project karaf by apache.

the class Subsystem method downloadBundles.

/**
 * Downloads bundles for all the features in current and child subsystems. But also collects bundles
 * as {@link DependencyInfo}.
 *
 * @param manager The {@link DownloadManager} to use.
 * @param featureResolutionRange The feature resolution range to use.
 * @param serviceRequirements The {@link FeaturesService.ServiceRequirementsBehavior} behavior to use.
 * @param repos The {@link RepositoryManager} to use.
 * @param callback The {@link SubsystemResolverCallback} to use.
 */
@SuppressWarnings("InfiniteLoopStatement")
public void downloadBundles(DownloadManager manager, String featureResolutionRange, final FeaturesService.ServiceRequirementsBehavior serviceRequirements, RepositoryManager repos, SubsystemResolverCallback callback) throws Exception {
    for (Subsystem child : children) {
        child.downloadBundles(manager, featureResolutionRange, serviceRequirements, repos, callback);
    }
    // collect BundleInfos for given feature - both direct <feature>/<bundle>s and <feature>/<conditional>/<bundle>s
    final Map<BundleInfo, Conditional> infos = new HashMap<>();
    final Downloader downloader = manager.createDownloader();
    if (feature != null) {
        for (Conditional cond : feature.getConditional()) {
            if (!cond.isBlacklisted()) {
                for (final BundleInfo bi : cond.getBundles()) {
                    // bundles from conditional features will be added as non-mandatory requirements
                    infos.put(bi, cond);
                }
            }
        }
        for (BundleInfo bi : feature.getBundles()) {
            infos.put(bi, null);
        }
    }
    // infos.keySet().removeIf(Blacklisting::isBlacklisted);
    for (Iterator<BundleInfo> iterator = infos.keySet().iterator(); iterator.hasNext(); ) {
        BundleInfo bi = iterator.next();
        if (bi.isBlacklisted()) {
            iterator.remove();
            if (callback != null) {
                callback.bundleBlacklisted(bi);
            }
        }
    }
    // all downloaded bundles
    final Map<String, ResourceImpl> bundles = new ConcurrentHashMap<>();
    // resources for locations that were overriden in OSGi mode - to check whether the override should actually
    // take place, by checking resource's headers
    final Map<String, ResourceImpl> overrides = new ConcurrentHashMap<>();
    boolean removeServiceRequirements = serviceRequirementsBehavior(feature, serviceRequirements);
    // download collected BundleInfo locations
    for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
        final BundleInfo bi = entry.getKey();
        final String loc = bi.getLocation();
        downloader.download(loc, provider -> {
            // always download location (could be overriden)
            ResourceImpl resource = createResource(loc, getMetadata(provider), removeServiceRequirements);
            bundles.put(loc, resource);
            if (bi.isOverriden() == BundleInfo.BundleOverrideMode.OSGI) {
                // also download original from original bundle URI to check if we should override by comparing
                // symbolic name - requires MANIFEST.MF header access. If there should be no override, we'll get
                // back to original URI
                downloader.download(bi.getOriginalLocation(), provider2 -> {
                    ResourceImpl originalResource = createResource(bi.getOriginalLocation(), getMetadata(provider2), removeServiceRequirements);
                    bundles.put(bi.getOriginalLocation(), originalResource);
                    // an entry in overrides map means that given location was overriden
                    overrides.put(loc, originalResource);
                });
            }
        });
    }
    // download direct bundle: requirements - without consulting overrides
    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)));
    }
    // resolution process
    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();
    // opposite to what we had before. Currently bundles are already overriden at model level, but
    // as we finally have access to headers, we can compare symbolic names and if override mode is OSGi, then
    // we can restore original resource if there should be no override.
    Overrides.override(bundles, overrides);
    if (feature != null) {
        // Add conditionals
        Map<Conditional, Resource> resConds = new HashMap<>();
        for (Conditional cond : feature.getConditional()) {
            if (cond.isBlacklisted()) {
                continue;
            }
            FeatureResource resCond = FeatureResource.build(feature, cond, featureResolutionRange, bundles);
            // feature's subsystem will optionally require conditional feature resource
            addIdentityRequirement(this, resCond, false);
            // but it's a mandatory requirement in other way
            addIdentityRequirement(resCond, this, true);
            installable.add(resCond);
            resConds.put(cond, resCond);
        }
        // Add features and make it require given subsystem that represents logical feature requirement
        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 (cond != null) {
                // bundle of conditional feature will have mandatory requirement on it
                addIdentityRequirement(res, resConds.get(cond), true);
            }
            boolean mandatory = !bi.isDependency() && cond == null;
            if (bi.isDependency()) {
                addDependency(res, mandatory, bi.isStart(), sl, bi.isBlacklisted());
            } else {
                doAddDependency(res, mandatory, bi.isStart(), sl, bi.isBlacklisted());
            }
        }
        for (Library library : feature.getLibraries()) {
            if (library.isExport()) {
                final String loc = library.getLocation();
                ResourceImpl res = bundles.get(loc);
                addDependency(res, false, false, 0, false);
            }
        }
        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, false);
            }
        }
    }
    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"));
        boolean blacklisted = bundle.getAttribute("blacklisted") != null && Boolean.parseBoolean(bundle.getAttribute("blacklisted"));
        int startLevel = 0;
        try {
            startLevel = Integer.parseInt(bundle.getAttribute("start-level"));
        } catch (NumberFormatException e) {
        // Ignore
        }
        if (dependency) {
            addDependency(bundles.get(loc), false, start, startLevel, blacklisted);
        } else {
            doAddDependency(bundles.get(loc), true, start, startLevel, blacklisted);
            // non dependency bundle will be added as osgi.identity req on type=osgi.bundle
            addIdentityRequirement(this, bundles.get(loc));
        }
    }
    // Compute dependencies
    for (DependencyInfo info : dependencies.values()) {
        installable.add(info.resource);
        // bundle resource will have a requirement on its feature's subsystem too
        // when bundle is declared with dependency="true", it will have a requirement on its region's subsystem
        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.felix.utils.repository.BaseRepository) Downloader(org.apache.karaf.features.internal.download.Downloader) Conditional(org.apache.karaf.features.Conditional) BundleInfo(org.apache.karaf.features.BundleInfo) ResourceImpl(org.apache.felix.utils.resource.ResourceImpl) Clause(org.apache.felix.utils.manifest.Clause) Library(org.apache.karaf.features.Library) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StringArrayMap(org.apache.felix.utils.collections.StringArrayMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 5 with Library

use of org.apache.karaf.features.Library in project karaf by apache.

the class Builder method bootStage.

private Set<Feature> bootStage(Profile bootProfile, Profile startupEffective, FeaturesProcessor processor) throws Exception {
    LOGGER.info("Boot stage");
    // 
    // Handle boot profiles
    // 
    Profile bootOverlay = Profiles.getOverlay(bootProfile, allProfiles, environment);
    Profile bootEffective = Profiles.getEffective(bootOverlay, false);
    // Load startup repositories
    LOGGER.info("   Loading boot repositories");
    Map<String, Features> bootRepositories = loadRepositories(manager, bootEffective.getRepositories(), true, processor);
    // Compute startup feature dependencies
    Set<Feature> allBootFeatures = new HashSet<>();
    for (Features repo : bootRepositories.values()) {
        allBootFeatures.addAll(repo.getFeature());
    }
    // Generate a global feature
    Map<String, Dependency> generatedDep = new HashMap<>();
    generatedBootFeatureName = UUID.randomUUID().toString();
    Feature generated = new Feature();
    generated.setName(generatedBootFeatureName);
    // Add feature dependencies
    for (String nameOrPattern : bootEffective.getFeatures()) {
        // KARAF-5273: feature may be a pattern
        for (String dependency : FeatureSelector.getMatchingFeatures(nameOrPattern, bootRepositories.values())) {
            Dependency dep = generatedDep.get(dependency);
            if (dep == null) {
                dep = createDependency(dependency);
                generated.getFeature().add(dep);
                generatedDep.put(dep.getName(), dep);
            }
            dep.setDependency(false);
        }
    }
    // Add bundles
    for (String location : bootEffective.getBundles()) {
        location = location.replace("profile:", "file:etc/");
        int intLevel = -100;
        if (location.contains(START_LEVEL)) {
            // extract start-level for this bundle
            String level = location.substring(location.indexOf(START_LEVEL));
            level = level.substring(START_LEVEL.length() + 1);
            if (level.startsWith("\"")) {
                level = level.substring(1, level.length() - 1);
            }
            intLevel = Integer.parseInt(level);
            LOGGER.debug("bundle start-level: " + level);
            location = location.substring(0, location.indexOf(START_LEVEL) - 1);
            LOGGER.debug("new bundle location after strip start-level: " + location);
        }
        Bundle bun = new Bundle();
        if (intLevel > 0) {
            bun.setStartLevel(intLevel);
        }
        bun.setLocation(location);
        generated.getBundle().add(bun);
    }
    Features rep = new Features();
    rep.setName(UUID.randomUUID().toString());
    rep.getRepository().addAll(bootEffective.getRepositories());
    rep.getFeature().add(generated);
    allBootFeatures.add(generated);
    Downloader downloader = manager.createDownloader();
    // Compute startup feature dependencies
    FeatureSelector selector = new FeatureSelector(allBootFeatures);
    Set<Feature> bootFeatures = selector.getMatching(singletonList(generated.getName()));
    for (Feature feature : bootFeatures) {
        if (feature.isBlacklisted()) {
            LOGGER.info("   Feature " + feature.getId() + " is blacklisted, ignoring");
            continue;
        }
        LOGGER.info("   Feature " + feature.getId() + " is defined as a boot feature");
        // add the feature in the system folder
        Set<BundleInfo> bundleInfos = new HashSet<>();
        for (Bundle bundle : feature.getBundle()) {
            if (!ignoreDependencyFlag || !bundle.isDependency()) {
                bundleInfos.add(bundle);
            }
        }
        for (Conditional cond : feature.getConditional()) {
            if (cond.isBlacklisted()) {
                LOGGER.info("   Conditionial " + cond.getConditionId() + " is blacklisted, ignoring");
            }
            for (Bundle bundle : cond.getBundle()) {
                if (!ignoreDependencyFlag || !bundle.isDependency()) {
                    bundleInfos.add(bundle);
                }
            }
        }
        // Build optional features and known prerequisites
        Map<String, List<String>> prereqs = new HashMap<>();
        prereqs.put("blueprint:", Arrays.asList("deployer", "aries-blueprint"));
        prereqs.put("spring:", Arrays.asList("deployer", "spring"));
        prereqs.put("wrap:", Collections.singletonList("wrap"));
        prereqs.put("war:", Collections.singletonList("war"));
        ArtifactInstaller installer = new ArtifactInstaller(systemDirectory, downloader, blacklist);
        for (BundleInfo bundleInfo : bundleInfos) {
            installer.installArtifact(bundleInfo);
            for (Map.Entry<String, List<String>> entry : prereqs.entrySet()) {
                if (bundleInfo.getLocation().trim().startsWith(entry.getKey())) {
                    for (String prereq : entry.getValue()) {
                        Dependency dep = generatedDep.get(prereq);
                        if (dep == null) {
                            dep = new Dependency();
                            dep.setName(prereq);
                            generated.getFeature().add(dep);
                            generatedDep.put(dep.getName(), dep);
                        }
                        dep.setPrerequisite(true);
                    }
                }
            }
        }
        new ConfigInstaller(etcDirectory, pidsToExtract).installConfigs(feature, downloader, installer);
        // Install libraries
        List<String> libraries = new ArrayList<>();
        for (Library library : feature.getLibraries()) {
            String lib = library.getLocation() + ";type:=" + library.getType() + ";export:=" + library.isExport() + ";delegate:=" + library.isDelegate();
            libraries.add(lib);
        }
        Path configPropertiesPath = etcDirectory.resolve("config.properties");
        Properties configProperties = new Properties(configPropertiesPath.toFile());
        downloadLibraries(downloader, configProperties, libraries, "   ");
        downloader.await();
        // Reformat clauses
        reformatClauses(configProperties, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
        reformatClauses(configProperties, Constants.FRAMEWORK_BOOTDELEGATION);
        configProperties.save();
    }
    // If there are bundles to install, we can't use the boot features only
    // so keep the generated feature
    Path featuresCfgFile = etcDirectory.resolve("org.apache.karaf.features.cfg");
    if (!generated.getBundle().isEmpty()) {
        File output = etcDirectory.resolve(rep.getName() + ".xml").toFile();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JaxbUtil.marshal(rep, baos);
        ByteArrayInputStream bais;
        String repoUrl;
        if (karafVersion == KarafVersion.v24) {
            String str = baos.toString();
            str = str.replace("http://karaf.apache.org/xmlns/features/v1.3.0", "http://karaf.apache.org/xmlns/features/v1.2.0");
            str = str.replaceAll(" dependency=\".*?\"", "");
            str = str.replaceAll(" prerequisite=\".*?\"", "");
            for (Feature f : rep.getFeature()) {
                for (Dependency d : f.getFeature()) {
                    if (d.isPrerequisite()) {
                        if (!startupEffective.getFeatures().contains(d.getName())) {
                            LOGGER.warn("Feature " + d.getName() + " is a prerequisite and should be installed as a startup feature.");
                        }
                    }
                }
            }
            bais = new ByteArrayInputStream(str.getBytes());
            repoUrl = "file:etc/" + output.getName();
        } else {
            bais = new ByteArrayInputStream(baos.toByteArray());
            repoUrl = "file:${karaf.etc}/" + output.getName();
        }
        Files.copy(bais, output.toPath());
        Properties featuresProperties = new Properties(featuresCfgFile.toFile());
        featuresProperties.put(FEATURES_REPOSITORIES, repoUrl);
        featuresProperties.put(FEATURES_BOOT, generated.getName());
        featuresProperties.save();
    } else {
        String repos = getRepos(rep);
        String boot = getBootFeatures(generatedDep);
        Properties featuresProperties = new Properties(featuresCfgFile.toFile());
        featuresProperties.put(FEATURES_REPOSITORIES, repos);
        featuresProperties.put(FEATURES_BOOT, boot);
        reformatClauses(featuresProperties, FEATURES_REPOSITORIES);
        reformatClauses(featuresProperties, FEATURES_BOOT);
        featuresProperties.save();
    }
    downloader.await();
    return allBootFeatures;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Downloader(org.apache.karaf.features.internal.download.Downloader) Properties(org.apache.felix.utils.properties.Properties) Profile(org.apache.karaf.profile.Profile) BundleInfo(org.apache.karaf.features.BundleInfo) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Path(java.nio.file.Path) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Library(org.apache.karaf.features.Library) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) File(java.io.File)

Aggregations

Library (org.apache.karaf.features.Library)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Downloader (org.apache.karaf.features.internal.download.Downloader)4 BundleInfo (org.apache.karaf.features.BundleInfo)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Clause (org.apache.felix.utils.manifest.Clause)2 Properties (org.apache.felix.utils.properties.Properties)2 Conditional (org.apache.karaf.features.Conditional)2 FeatureResource (org.apache.karaf.features.internal.resolver.FeatureResource)2 Profile (org.apache.karaf.profile.Profile)2