Search in sources :

Example 1 with Feature

use of org.apache.karaf.features.internal.model.Feature in project karaf by apache.

the class Builder method doGenerateAssembly.

private void doGenerateAssembly() throws Exception {
    systemDirectory = homeDirectory.resolve("system");
    etcDirectory = homeDirectory.resolve("etc");
    LOGGER.info("Generating karaf assembly: " + homeDirectory);
    //
    // Create download manager
    //
    Dictionary<String, String> props = new Hashtable<>();
    if (offline) {
        props.put(ORG_OPS4J_PAX_URL_MVN_PID + "offline", "true");
    }
    if (localRepository != null) {
        props.put(Builder.ORG_OPS4J_PAX_URL_MVN_PID + ".localRepository", localRepository);
    }
    if (mavenRepositories != null) {
        props.put(Builder.ORG_OPS4J_PAX_URL_MVN_PID + ".repositories", mavenRepositories);
    }
    MavenResolver resolver = MavenResolvers.createMavenResolver(props, ORG_OPS4J_PAX_URL_MVN_PID);
    executor = Executors.newScheduledThreadPool(8);
    manager = new CustomDownloadManager(resolver, executor, null, translatedUrls);
    this.resolver = new ResolverImpl(new Slf4jResolverLog(LOGGER));
    //
    // Unzip kars
    //
    LOGGER.info("Unzipping kars");
    Map<String, RepositoryInfo> repositories = new LinkedHashMap<>(this.repositories);
    Downloader downloader = manager.createDownloader();
    for (String kar : kars.keySet()) {
        downloader.download(kar, null);
    }
    downloader.await();
    for (String karUri : kars.keySet()) {
        Kar kar = new Kar(manager.getProviders().get(karUri).getFile().toURI());
        kar.extract(systemDirectory.toFile(), homeDirectory.toFile());
        RepositoryInfo info = kars.get(karUri);
        for (URI repositoryUri : kar.getFeatureRepos()) {
            repositories.put(repositoryUri.toString(), info);
        }
    }
    //
    // Propagate feature installation from repositories
    //
    Map<String, Stage> features = new LinkedHashMap<>(this.features);
    Map<String, Features> karRepositories = loadRepositories(manager, repositories.keySet(), false);
    for (String repo : repositories.keySet()) {
        RepositoryInfo info = repositories.get(repo);
        if (info.addAll) {
            for (Feature feature : karRepositories.get(repo).getFeature()) {
                features.put(feature.getId(), info.stage);
            }
        }
    }
    //
    // Load profiles
    //
    LOGGER.info("Loading profiles");
    allProfiles = new HashMap<>();
    for (String profilesUri : profilesUris) {
        String uri = profilesUri;
        if (uri.startsWith("jar:") && uri.contains("!/")) {
            uri = uri.substring("jar:".length(), uri.indexOf("!/"));
        }
        if (!uri.startsWith("file:")) {
            downloader = manager.createDownloader();
            downloader.download(uri, null);
            downloader.await();
            StreamProvider provider = manager.getProviders().get(uri);
            profilesUri = profilesUri.replace(uri, provider.getFile().toURI().toString());
        }
        URI profileURI = URI.create(profilesUri);
        Path profilePath;
        try {
            profilePath = Paths.get(profileURI);
        } catch (FileSystemNotFoundException e) {
            // file system does not exist, try to create it
            FileSystem fs = FileSystems.newFileSystem(profileURI, new HashMap<>(), Builder.class.getClassLoader());
            profilePath = fs.provider().getPath(profileURI);
        }
        allProfiles.putAll(Profiles.loadProfiles(profilePath));
        // Handle blacklisted profiles
        if (!blacklistedProfiles.isEmpty()) {
            if (blacklistPolicy == BlacklistPolicy.Discard) {
                // Override blacklisted profiles with empty ones
                for (String profile : blacklistedProfiles) {
                    allProfiles.put(profile, ProfileBuilder.Factory.create(profile).getProfile());
                }
            } else {
                // Remove profiles completely
                allProfiles.keySet().removeAll(blacklistedProfiles);
            }
        }
    }
    // Generate profiles
    Profile startupProfile = generateProfile(Stage.Startup, profiles, repositories, features, bundles);
    profiles.put(startupProfile.getId(), Stage.Boot);
    Profile bootProfile = generateProfile(Stage.Boot, profiles, repositories, features, bundles);
    Profile installedProfile = generateProfile(Stage.Installed, profiles, repositories, features, bundles);
    //
    // Compute overall profile
    //
    ProfileBuilder builder = ProfileBuilder.Factory.create(UUID.randomUUID().toString()).setParents(Arrays.asList(startupProfile.getId(), bootProfile.getId(), installedProfile.getId()));
    config.forEach((k, v) -> builder.addConfiguration(Profile.INTERNAL_PID, Profile.CONFIG_PREFIX + k, v));
    system.forEach((k, v) -> builder.addConfiguration(Profile.INTERNAL_PID, Profile.SYSTEM_PREFIX + k, v));
    Profile overallProfile = builder.getProfile();
    Profile overallOverlay = Profiles.getOverlay(overallProfile, allProfiles, environment);
    Profile overallEffective = Profiles.getEffective(overallOverlay, false);
    manager = new CustomDownloadManager(resolver, executor, overallEffective, translatedUrls);
    //        Hashtable<String, String> agentProps = new Hashtable<>(overallEffective.getConfiguration(ORG_OPS4J_PAX_URL_MVN_PID));
    //        final Map<String, String> properties = new HashMap<>();
    //        properties.put("karaf.default.repository", "system");
    //        InterpolationHelper.performSubstitution(agentProps, properties::get, false, false, true);
    //
    // Write config and system properties
    //
    Path configPropertiesPath = etcDirectory.resolve("config.properties");
    Properties configProperties = new Properties(configPropertiesPath.toFile());
    configProperties.putAll(overallEffective.getConfig());
    configProperties.save();
    Path systemPropertiesPath = etcDirectory.resolve("system.properties");
    Properties systemProperties = new Properties(systemPropertiesPath.toFile());
    systemProperties.putAll(overallEffective.getSystem());
    systemProperties.save();
    //
    // Download libraries
    //
    // TODO: handle karaf 2.x and 3.x libraries
    LOGGER.info("Downloading libraries");
    downloader = manager.createDownloader();
    downloadLibraries(downloader, configProperties, overallEffective.getLibraries(), "");
    downloadLibraries(downloader, configProperties, libraries, "");
    downloader.await();
    // Reformat clauses
    reformatClauses(configProperties, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
    reformatClauses(configProperties, Constants.FRAMEWORK_BOOTDELEGATION);
    configProperties.save();
    //
    // Write all configuration files
    //
    LOGGER.info("Writing configurations");
    for (Map.Entry<String, byte[]> config : overallEffective.getFileConfigurations().entrySet()) {
        Path configFile = etcDirectory.resolve(config.getKey());
        LOGGER.info("   adding config file: {}", homeDirectory.relativize(configFile));
        Files.createDirectories(configFile.getParent());
        Files.write(configFile, config.getValue());
    }
    // 'improve' configuration files.
    if (propertyEdits != null) {
        KarafPropertiesEditor editor = new KarafPropertiesEditor();
        editor.setInputEtc(etcDirectory.toFile()).setOutputEtc(etcDirectory.toFile()).setEdits(propertyEdits);
        editor.run();
    }
    //
    if (!overallEffective.getOverrides().isEmpty()) {
        Path overrides = etcDirectory.resolve("override.properties");
        List<String> lines = new ArrayList<>();
        lines.add("#");
        lines.add("# Generated by the karaf assembly builder");
        lines.add("#");
        lines.addAll(overallEffective.getOverrides());
        LOGGER.info("Generating {}", homeDirectory.relativize(overrides));
        Files.write(overrides, lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
    //
    if (!blacklistedFeatures.isEmpty() || !blacklistedBundles.isEmpty()) {
        Path blacklist = etcDirectory.resolve("blacklisted.properties");
        List<String> lines = new ArrayList<>();
        lines.add("#");
        lines.add("# Generated by the karaf assembly builder");
        lines.add("#");
        if (!blacklistedFeatures.isEmpty()) {
            lines.add("");
            lines.add("# Features");
            lines.addAll(blacklistedFeatures);
        }
        if (!blacklistedBundles.isEmpty()) {
            lines.add("");
            lines.add("# Bundles");
            lines.addAll(blacklistedBundles);
        }
        LOGGER.info("Generating {}", homeDirectory.relativize(blacklist));
        Files.write(blacklist, lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
    //
    // Startup stage
    //
    Profile startupEffective = startupStage(startupProfile);
    //
    // Boot stage
    //
    Set<Feature> allBootFeatures = bootStage(bootProfile, startupEffective);
    //
    // Installed stage
    //
    installStage(installedProfile, allBootFeatures);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Downloader(org.apache.karaf.features.internal.download.Downloader) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Properties(org.apache.felix.utils.properties.Properties) URI(java.net.URI) Feature(org.apache.karaf.features.internal.model.Feature) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) Profile(org.apache.karaf.profile.Profile) LinkedHashMap(java.util.LinkedHashMap) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) Features(org.apache.karaf.features.internal.model.Features) StreamProvider(org.apache.karaf.features.internal.download.StreamProvider) Hashtable(java.util.Hashtable) KarafPropertiesEditor(org.apache.karaf.tools.utils.KarafPropertiesEditor) Kar(org.apache.karaf.kar.internal.Kar) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 2 with Feature

use of org.apache.karaf.features.internal.model.Feature in project karaf by apache.

the class GenerateDescriptorMojoTest method testReadXml1.

@Test
public void testReadXml1() throws Exception {
    URL url = getClass().getClassLoader().getResource("input-features-1.1.0.xml");
    Features featuresRoot = JaxbUtil.unmarshal(url.toExternalForm(), false);
    List<Feature> featuresList = featuresRoot.getFeature();
    assertEquals(featuresList.size(), 1);
    Feature feature = featuresList.get(0);
    assertEquals(feature.getInstall(), "auto");
}
Also used : Features(org.apache.karaf.features.internal.model.Features) Feature(org.apache.karaf.features.internal.model.Feature) URL(java.net.URL) Test(org.junit.Test)

Example 3 with Feature

use of org.apache.karaf.features.internal.model.Feature in project karaf by apache.

the class GenerateDescriptorMojo method checkChanges.

private void checkChanges(Features newFeatures, ObjectFactory objectFactory) throws Exception {
    if (checkDependencyChange) {
        //combine all the dependencies to one feature and strip out versions
        Features features = objectFactory.createFeaturesRoot();
        features.setName(newFeatures.getName());
        Feature feature = objectFactory.createFeature();
        features.getFeature().add(feature);
        for (Feature f : newFeatures.getFeature()) {
            for (Bundle b : f.getBundle()) {
                Bundle bundle = objectFactory.createBundle();
                bundle.setLocation(b.getLocation());
                feature.getBundle().add(bundle);
            }
            for (Dependency d : f.getFeature()) {
                Dependency dependency = objectFactory.createDependency();
                dependency.setName(d.getName());
                feature.getFeature().add(dependency);
            }
        }
        feature.getBundle().sort(Comparator.comparing(Bundle::getLocation));
        feature.getFeature().sort(Comparator.comparing(Dependency::getName));
        if (dependencyCache.exists()) {
            //filter dependencies file
            filter(dependencyCache, filteredDependencyCache);
            //read dependency types, convert to dependencies, compare.
            Features oldfeatures = readFeaturesFile(filteredDependencyCache);
            Feature oldFeature = oldfeatures.getFeature().get(0);
            List<Bundle> addedBundles = new ArrayList<>(feature.getBundle());
            List<Bundle> removedBundles = new ArrayList<>();
            for (Bundle test : oldFeature.getBundle()) {
                boolean t1 = addedBundles.contains(test);
                int s1 = addedBundles.size();
                boolean t2 = addedBundles.remove(test);
                int s2 = addedBundles.size();
                if (t1 != t2) {
                    getLog().warn("dependencies.contains: " + t1 + ", dependencies.remove(test): " + t2);
                }
                if (t1 == (s1 == s2)) {
                    getLog().warn("dependencies.contains: " + t1 + ", size before: " + s1 + ", size after: " + s2);
                }
                if (!t2) {
                    removedBundles.add(test);
                }
            }
            List<Dependency> addedDependencys = new ArrayList<>(feature.getFeature());
            List<Dependency> removedDependencys = new ArrayList<>();
            for (Dependency test : oldFeature.getFeature()) {
                boolean t1 = addedDependencys.contains(test);
                int s1 = addedDependencys.size();
                boolean t2 = addedDependencys.remove(test);
                int s2 = addedDependencys.size();
                if (t1 != t2) {
                    getLog().warn("dependencies.contains: " + t1 + ", dependencies.remove(test): " + t2);
                }
                if (t1 == (s1 == s2)) {
                    getLog().warn("dependencies.contains: " + t1 + ", size before: " + s1 + ", size after: " + s2);
                }
                if (!t2) {
                    removedDependencys.add(test);
                }
            }
            if (!addedBundles.isEmpty() || !removedBundles.isEmpty() || !addedDependencys.isEmpty() || !removedDependencys.isEmpty()) {
                saveDependencyChanges(addedBundles, removedBundles, addedDependencys, removedDependencys, objectFactory);
                if (overwriteChangedDependencies) {
                    writeDependencies(features, dependencyCache);
                }
            } else {
                getLog().info(saveTreeListing());
            }
        } else {
            writeDependencies(features, dependencyCache);
        }
    }
}
Also used : Bundle(org.apache.karaf.features.internal.model.Bundle) ArrayList(java.util.ArrayList) Features(org.apache.karaf.features.internal.model.Features) Dependency(org.apache.karaf.features.internal.model.Dependency) LocalDependency(org.apache.karaf.tooling.utils.LocalDependency) Feature(org.apache.karaf.features.internal.model.Feature)

Example 4 with Feature

use of org.apache.karaf.features.internal.model.Feature in project karaf by apache.

the class AbstractFeatureMojo method addFeatures.

/**
     * Populate the features by traversing the listed features and their
     * dependencies if transitive is true
     *  
     * @param featureNames The {@link List} of feature names.
     * @param features The {@link Set} of features.
     * @param featuresMap The {@link Map} of features.
     * @param transitive True to add transitive features, false else.
     */
protected void addFeatures(List<String> featureNames, Set<Feature> features, Map<String, Feature> featuresMap, boolean transitive) {
    for (String feature : featureNames) {
        String[] split = feature.split("/");
        Feature f = getMatchingFeature(featuresMap, split[0], split.length > 1 ? split[1] : null);
        features.add(f);
        if (transitive) {
            addFeaturesDependencies(f.getFeature(), features, featuresMap, true);
        }
    }
}
Also used : Feature(org.apache.karaf.features.internal.model.Feature)

Example 5 with Feature

use of org.apache.karaf.features.internal.model.Feature in project karaf by apache.

the class AbstractFeatureMojo method resolveFeatures.

protected Set<Feature> resolveFeatures() throws MojoExecutionException {
    Set<Feature> featuresSet = new HashSet<>();
    try {
        Set<String> artifactsToCopy = new HashSet<>();
        Map<String, Feature> featuresMap = new HashMap<>();
        for (String uri : descriptors) {
            retrieveDescriptorsRecursively(uri, artifactsToCopy, featuresMap);
        }
        // no features specified, handle all of them
        if (features == null) {
            features = new ArrayList<>(featuresMap.keySet());
        }
        addFeatures(features, featuresSet, featuresMap, addTransitiveFeatures);
        getLog().info("Using local repository at: " + localRepo.getUrl());
        for (Feature feature : featuresSet) {
            try {
                for (Bundle bundle : feature.getBundle()) {
                    resolveArtifact(bundle.getLocation());
                }
                for (Conditional conditional : feature.getConditional()) {
                    for (BundleInfo bundle : conditional.getBundles()) {
                        if (ignoreDependencyFlag || (!ignoreDependencyFlag && !bundle.isDependency())) {
                            resolveArtifact(bundle.getLocation());
                        }
                    }
                }
                for (ConfigFile configfile : feature.getConfigfile()) {
                    resolveArtifact(configfile.getLocation());
                }
            } catch (RuntimeException e) {
                throw new RuntimeException("Error resolving feature " + feature.getName() + "/" + feature.getVersion(), e);
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error populating repository", e);
    }
    return featuresSet;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) Bundle(org.apache.karaf.features.internal.model.Bundle) Conditional(org.apache.karaf.features.Conditional) Feature(org.apache.karaf.features.internal.model.Feature) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) BundleInfo(org.apache.karaf.features.BundleInfo) HashSet(java.util.HashSet)

Aggregations

Feature (org.apache.karaf.features.internal.model.Feature)25 Features (org.apache.karaf.features.internal.model.Features)16 ArrayList (java.util.ArrayList)10 Bundle (org.apache.karaf.features.internal.model.Bundle)9 ConfigFile (org.apache.karaf.features.internal.model.ConfigFile)8 HashMap (java.util.HashMap)7 Downloader (org.apache.karaf.features.internal.download.Downloader)6 Dependency (org.apache.karaf.features.internal.model.Dependency)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 IOException (java.io.IOException)5 HashSet (java.util.HashSet)5 File (java.io.File)4 URL (java.net.URL)4 LinkedHashSet (java.util.LinkedHashSet)4 Properties (org.apache.felix.utils.properties.Properties)4 Conditional (org.apache.karaf.features.internal.model.Conditional)4 Profile (org.apache.karaf.profile.Profile)4 Test (org.junit.Test)4 Hashtable (java.util.Hashtable)3 LinkedHashMap (java.util.LinkedHashMap)3