Search in sources :

Example 21 with Features

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

the class GenerateDescriptorMojo method processFeatureArtifact.

private void processFeatureArtifact(Features features, Feature feature, Map<Dependency, Feature> otherFeatures, Map<Feature, String> featureRepositories, Object artifact, Object parent, boolean add) throws MojoExecutionException, XMLStreamException, JAXBException, IOException {
    if (this.dependencyHelper.isArtifactAFeature(artifact) && FEATURE_CLASSIFIER.equals(this.dependencyHelper.getClassifier(artifact))) {
        File featuresFile = this.dependencyHelper.resolve(artifact, getLog());
        if (featuresFile == null || !featuresFile.exists()) {
            throw new MojoExecutionException("Cannot locate file for feature: " + artifact + " at " + featuresFile);
        }
        Features includedFeatures = readFeaturesFile(featuresFile);
        for (String repository : includedFeatures.getRepository()) {
            processFeatureArtifact(features, feature, otherFeatures, featureRepositories, new DefaultArtifact(MavenUtil.mvnToAether(repository)), parent, false);
        }
        for (Feature includedFeature : includedFeatures.getFeature()) {
            Dependency dependency = new Dependency(includedFeature.getName(), includedFeature.getVersion());
            dependency.setPrerequisite(prerequisiteFeatures.contains(dependency.getName()));
            dependency.setDependency(dependencyFeatures.contains(dependency.getName()));
            // Determine what dependency we're actually going to use
            Dependency matchingDependency = findMatchingDependency(feature.getFeature(), dependency);
            if (matchingDependency != null) {
                // The feature already has a matching dependency, merge
                mergeDependencies(matchingDependency, dependency);
                dependency = matchingDependency;
            }
            // We mustn't de-duplicate here, we may have seen a feature in !add mode
            otherFeatures.put(dependency, includedFeature);
            if (add) {
                if (!feature.getFeature().contains(dependency)) {
                    feature.getFeature().add(dependency);
                }
                if (aggregateFeatures) {
                    features.getFeature().add(includedFeature);
                }
            }
            if (!featureRepositories.containsKey(includedFeature)) {
                featureRepositories.put(includedFeature, this.dependencyHelper.artifactToMvn(artifact, getVersionOrRange(parent, artifact)));
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Features(org.apache.karaf.features.internal.model.Features) Dependency(org.apache.karaf.features.internal.model.Dependency) LocalDependency(org.apache.karaf.tooling.utils.LocalDependency) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) File(java.io.File) Feature(org.apache.karaf.features.internal.model.Feature) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 22 with Features

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

the class GenerateDescriptorMojoTest method testReadXml100.

@Test
public void testReadXml100() throws JAXBException, SAXException, ParserConfigurationException, XMLStreamException {
    URL url = getClass().getClassLoader().getResource("input-features-1.0.0.xml");
    Features featuresRoot = JaxbUtil.unmarshal(url.toExternalForm(), false);
    assertEquals(featuresRoot.getRepository().size(), 1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JaxbUtil.marshal(featuresRoot, baos);
    String text = new String(baos.toByteArray());
    assertTrue(text.contains("repository"));
    assertTrue(text.contains(FeaturesNamespaces.URI_CURRENT));
}
Also used : Features(org.apache.karaf.features.internal.model.Features) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) Test(org.junit.Test)

Example 23 with Features

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

the class Builder method loadRepositories.

private Map<String, Features> loadRepositories(DownloadManager manager, Collection<String> repositories, final boolean install) throws Exception {
    final Map<String, Features> loaded = new HashMap<>();
    final Downloader downloader = manager.createDownloader();
    final List<String> blacklist = new ArrayList<>();
    blacklist.addAll(blacklistedBundles);
    blacklist.addAll(blacklistedFeatures);
    final List<String> blacklistRepos = new ArrayList<>();
    blacklistRepos.addAll(blacklistedRepositories);
    final Clause[] clauses = org.apache.felix.utils.manifest.Parser.parseClauses(blacklist.toArray(new String[blacklist.size()]));
    final Clause[] clausesRepos = org.apache.felix.utils.manifest.Parser.parseClauses(blacklistRepos.toArray(new String[blacklistRepos.size()]));
    for (String repository : repositories) {
        downloader.download(repository, new DownloadCallback() {

            @Override
            public void downloaded(final StreamProvider provider) throws Exception {
                String url = provider.getUrl();
                if (Blacklist.isBlacklisted(clausesRepos, url, TYPE_REPOSITORY)) {
                    return;
                }
                synchronized (loaded) {
                    if (!loaded.containsKey(provider.getUrl())) {
                        if (install) {
                            synchronized (provider) {
                                Path path = pathFromProviderUrl(url);
                                Files.createDirectories(path.getParent());
                                Files.copy(provider.getFile().toPath(), path, StandardCopyOption.REPLACE_EXISTING);
                            }
                        }
                        try (InputStream is = provider.open()) {
                            Features featuresModel = JaxbUtil.unmarshal(url, is, false);
                            if (blacklistPolicy == BlacklistPolicy.Discard) {
                                Blacklist.blacklist(featuresModel, clauses);
                            }
                            loaded.put(provider.getUrl(), featuresModel);
                            for (String innerRepository : featuresModel.getRepository()) {
                                downloader.download(innerRepository, this);
                            }
                        }
                    }
                }
            }
        });
    }
    downloader.await();
    return loaded;
}
Also used : StreamProvider(org.apache.karaf.features.internal.download.StreamProvider) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DownloadCallback(org.apache.karaf.features.internal.download.DownloadCallback) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Downloader(org.apache.karaf.features.internal.download.Downloader) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Features(org.apache.karaf.features.internal.model.Features) Clause(org.apache.felix.utils.manifest.Clause)

Example 24 with Features

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

the class Builder method addFeatures.

private void addFeatures(Set<Feature> allFeatures, String feature, Set<Feature> features, boolean mandatory) {
    String name;
    VersionRange range;
    int idx = feature.indexOf('/');
    if (idx > 0) {
        name = feature.substring(0, idx);
        String version = feature.substring(idx + 1);
        version = version.trim();
        if (version.equals(org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION)) {
            range = new VersionRange(Version.emptyVersion);
        } else {
            range = new VersionRange(version, true, true);
        }
    } else {
        name = feature;
        range = new VersionRange(Version.emptyVersion);
    }
    Set<Feature> set = allFeatures.stream().filter(f -> f.getName().equals(name) && range.contains(VersionTable.getVersion(f.getVersion()))).collect(Collectors.toSet());
    if (mandatory && set.isEmpty()) {
        throw new IllegalStateException("Could not find matching feature for " + feature);
    }
    for (Feature f : set) {
        features.add(f);
        for (Dependency dep : f.getFeature()) {
            addFeatures(allFeatures, dep.toString(), features, !dep.isDependency() && !dep.isPrerequisite());
        }
    }
}
Also used : Manifest(java.util.jar.Manifest) Dependency(org.apache.karaf.features.internal.model.Dependency) Profile(org.apache.karaf.profile.Profile) Arrays(java.util.Arrays) Constants(org.osgi.framework.Constants) KarafPropertiesEditor(org.apache.karaf.tools.utils.KarafPropertiesEditor) LoggerFactory(org.slf4j.LoggerFactory) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) FeaturesService(org.apache.karaf.features.FeaturesService) java.nio.file(java.nio.file) DownloadManagerHelper(org.apache.karaf.features.internal.download.impl.DownloadManagerHelper) Clause(org.apache.felix.utils.manifest.Clause) ByteArrayInputStream(java.io.ByteArrayInputStream) JaxbUtil(org.apache.karaf.features.internal.model.JaxbUtil) Map(java.util.Map) URI(java.net.URI) VersionTable(org.apache.felix.utils.version.VersionTable) ZipEntry(java.util.zip.ZipEntry) BundleRevision(org.osgi.framework.wiring.BundleRevision) EnumSet(java.util.EnumSet) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) Blacklist(org.apache.karaf.features.internal.service.Blacklist) Deployer(org.apache.karaf.features.internal.service.Deployer) MapUtils(org.apache.karaf.features.internal.util.MapUtils) Collection(java.util.Collection) Set(java.util.Set) MANIFEST_NAME(java.util.jar.JarFile.MANIFEST_NAME) UUID(java.util.UUID) Library(org.apache.karaf.features.Library) Version(org.osgi.framework.Version) Attributes(java.util.jar.Attributes) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) VersionRange(org.apache.felix.utils.version.VersionRange) List(java.util.List) PropertiesLoader(org.apache.karaf.util.config.PropertiesLoader) Properties(org.apache.felix.utils.properties.Properties) Pattern(java.util.regex.Pattern) DownloadManager(org.apache.karaf.features.internal.download.DownloadManager) Features(org.apache.karaf.features.internal.model.Features) Dictionary(java.util.Dictionary) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DownloadCallback(org.apache.karaf.features.internal.download.DownloadCallback) HashMap(java.util.HashMap) ResourceBuilder(org.apache.karaf.features.internal.resolver.ResourceBuilder) Kar(org.apache.karaf.kar.internal.Kar) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) StreamProvider(org.apache.karaf.features.internal.download.StreamProvider) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) Parser(org.apache.karaf.util.maven.Parser) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Bundle(org.apache.karaf.features.internal.model.Bundle) MavenResolvers(org.ops4j.pax.url.mvn.MavenResolvers) VersionUtils(org.apache.karaf.profile.versioning.VersionUtils) Downloader(org.apache.karaf.features.internal.download.Downloader) TYPE_REPOSITORY(org.apache.karaf.features.internal.service.Blacklist.TYPE_REPOSITORY) Content(org.apache.karaf.features.internal.model.Content) Hashtable(java.util.Hashtable) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) Profiles(org.apache.karaf.profile.impl.Profiles) MalformedURLException(java.net.MalformedURLException) Conditional(org.apache.karaf.features.internal.model.Conditional) Resource(org.osgi.resource.Resource) IOException(java.io.IOException) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Resolver(org.osgi.service.resolver.Resolver) KarafPropertyEdits(org.apache.karaf.tools.utils.model.KarafPropertyEdits) File(java.io.File) TreeMap(java.util.TreeMap) Feature(org.apache.karaf.features.internal.model.Feature) Config(org.apache.karaf.features.internal.model.Config) Startup(org.apache.karaf.profile.assembly.Builder.Stage.Startup) Collections(java.util.Collections) InputStream(java.io.InputStream) VersionRange(org.apache.felix.utils.version.VersionRange) Dependency(org.apache.karaf.features.internal.model.Dependency) Feature(org.apache.karaf.features.internal.model.Feature)

Example 25 with Features

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

the class Builder method installStage.

private void installStage(Profile installedProfile, Set<Feature> allBootFeatures) throws Exception {
    LOGGER.info("Install stage");
    //
    // Handle installed profiles
    //
    Profile installedOverlay = Profiles.getOverlay(installedProfile, allProfiles, environment);
    Profile installedEffective = Profiles.getEffective(installedOverlay, false);
    Downloader downloader = manager.createDownloader();
    // Load startup repositories
    Map<String, Features> installedRepositories = loadRepositories(manager, installedEffective.getRepositories(), true);
    // Compute startup feature dependencies
    Set<Feature> allInstalledFeatures = new HashSet<>();
    for (Features repo : installedRepositories.values()) {
        allInstalledFeatures.addAll(repo.getFeature());
    }
    Set<Feature> installedFeatures = new LinkedHashSet<>();
    // Add boot features for search
    allInstalledFeatures.addAll(allBootFeatures);
    for (String feature : installedEffective.getFeatures()) {
        addFeatures(allInstalledFeatures, feature, installedFeatures, true);
    }
    for (Feature feature : installedFeatures) {
        LOGGER.info("   Feature {} is defined as an installed feature", feature.getId());
        for (Bundle bundle : feature.getBundle()) {
            if (!ignoreDependencyFlag || !bundle.isDependency()) {
                installArtifact(downloader, bundle.getLocation().trim());
            }
        }
        // Install config files
        for (ConfigFile configFile : feature.getConfigfile()) {
            installArtifact(downloader, configFile.getLocation().trim());
        }
        for (Conditional cond : feature.getConditional()) {
            for (Bundle bundle : cond.getBundle()) {
                if (!ignoreDependencyFlag || !bundle.isDependency()) {
                    installArtifact(downloader, bundle.getLocation().trim());
                }
            }
        }
    }
    for (String location : installedEffective.getBundles()) {
        installArtifact(downloader, location);
    }
    downloader.await();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) Bundle(org.apache.karaf.features.internal.model.Bundle) Downloader(org.apache.karaf.features.internal.download.Downloader) Features(org.apache.karaf.features.internal.model.Features) Conditional(org.apache.karaf.features.internal.model.Conditional) Feature(org.apache.karaf.features.internal.model.Feature) Profile(org.apache.karaf.profile.Profile) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Features (org.apache.karaf.features.internal.model.Features)37 Feature (org.apache.karaf.features.internal.model.Feature)21 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 File (java.io.File)12 LinkedHashMap (java.util.LinkedHashMap)12 IOException (java.io.IOException)11 LinkedHashSet (java.util.LinkedHashSet)11 URL (java.net.URL)10 HashSet (java.util.HashSet)10 ConfigFile (org.apache.karaf.features.internal.model.ConfigFile)10 Test (org.junit.Test)10 Map (java.util.Map)9 TreeMap (java.util.TreeMap)9 Downloader (org.apache.karaf.features.internal.download.Downloader)8 InputStream (java.io.InputStream)7 List (java.util.List)7 Bundle (org.apache.karaf.features.internal.model.Bundle)7 Conditional (org.apache.karaf.features.internal.model.Conditional)7 Dependency (org.apache.karaf.features.internal.model.Dependency)7