Search in sources :

Example 96 with Feature

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

the class FeaturesDumpProvider method writeDump.

/**
 * {@inheritDoc}
 */
protected void writeDump(OutputStreamWriter outputStreamWriter) throws Exception {
    // creates header
    outputStreamWriter.write("Repositories:\n");
    // list repositories
    for (Repository repo : features.listRepositories()) {
        outputStreamWriter.write(repo.getURI() + " (" + repo.getName() + ")\n");
    }
    // list features
    outputStreamWriter.write("\nfeatures:\n");
    for (Feature feature : features.listFeatures()) {
        outputStreamWriter.write(feature.getName() + " " + feature.getVersion());
        outputStreamWriter.write(" installed: " + features.isInstalled(feature));
        outputStreamWriter.write("\nBundles:\n");
        for (BundleInfo bundle : feature.getBundles()) {
            outputStreamWriter.write("\t" + bundle.getLocation());
            if (bundle.getStartLevel() != 0) {
                outputStreamWriter.write(" start level " + bundle.getStartLevel());
            }
            outputStreamWriter.write("\n\n");
        }
    }
    // flush & close stream
    outputStreamWriter.close();
}
Also used : Repository(org.apache.karaf.features.Repository) BundleInfo(org.apache.karaf.features.BundleInfo) Feature(org.apache.karaf.features.Feature)

Example 97 with Feature

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

the class FeatureReq method getMatchingFeatures.

public Stream<Feature> getMatchingFeatures(Map<String, Map<String, Feature>> allFeatures) {
    Pattern pattern = Pattern.compile(name);
    Function<String, Optional<Feature>> func = featureName -> {
        Feature matchingFeature = null;
        if (pattern.matcher(featureName).matches()) {
            Map<String, Feature> versions = allFeatures.get(featureName);
            matchingFeature = getLatestFeature(versions, versionRange);
        }
        return Optional.ofNullable(matchingFeature);
    };
    return allFeatures.keySet().stream().map(func).filter(Optional::isPresent).map(Optional::get);
}
Also used : Feature(org.apache.karaf.features.Feature) Set(java.util.Set) Version(org.osgi.framework.Version) ANY_VERSION(org.apache.felix.utils.version.VersionRange.ANY_VERSION) MapUtils.filter(org.apache.karaf.features.internal.util.MapUtils.filter) Function(java.util.function.Function) VersionRange(org.apache.felix.utils.version.VersionRange) Objects(java.util.Objects) Stream(java.util.stream.Stream) Map(java.util.Map) Optional(java.util.Optional) VersionTable(org.apache.felix.utils.version.VersionTable) Pattern(java.util.regex.Pattern) Pattern(java.util.regex.Pattern) Optional(java.util.Optional) Feature(org.apache.karaf.features.Feature) Map(java.util.Map)

Example 98 with Feature

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

the class FeatureReq method getLatestFeature.

private static Feature getLatestFeature(Map<String, Feature> versions, VersionRange versionRange) {
    Feature feature = null;
    if (versions != null && !versions.isEmpty()) {
        Version latest = Version.emptyVersion;
        for (String available : versions.keySet()) {
            Version availableVersion = VersionTable.getVersion(available);
            if (availableVersion.compareTo(latest) >= 0 && versionRange.contains(availableVersion)) {
                Feature possiblyBlacklisted = versions.get(available);
                // return only if there are no more non-blaclisted features
                if (feature == null || !possiblyBlacklisted.isBlacklisted()) {
                    feature = possiblyBlacklisted;
                }
                latest = availableVersion;
            }
        }
    }
    return feature;
}
Also used : Version(org.osgi.framework.Version) Feature(org.apache.karaf.features.Feature)

Example 99 with Feature

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

the class FeaturesServiceImpl method getFeatureCache.

/**
 * Should not be called while holding a lock.
 * @return map from feature name to map from feature version to Feature
 */
protected Map<String, Map<String, Feature>> getFeatureCache() throws Exception {
    Set<String> uris;
    synchronized (lock) {
        if (featureCache != null) {
            return featureCache;
        }
        uris = new TreeSet<>(state.repositories);
    }
    // the outer map's key is feature name, the inner map's key is feature version
    Map<String, Map<String, Feature>> map = new HashMap<>();
    // Two phase load:
    // * first load dependent repositories
    Set<String> loaded = new HashSet<>();
    Queue<String> toLoad = new ArrayDeque<>(uris);
    while (!toLoad.isEmpty()) {
        String uri = toLoad.remove();
        Repository repo;
        synchronized (lock) {
            repo = repositories.getRepository(uri);
        }
        try {
            if (repo == null) {
                repo = repositories.create(URI.create(uri), false);
                synchronized (lock) {
                    repositories.addRepository(repo);
                }
            }
            if (loaded.add(uri)) {
                for (URI u : repo.getRepositories()) {
                    toLoad.add(u.toString());
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Can't load features repository {}", uri, e);
        }
    }
    List<Repository> repos;
    synchronized (lock) {
        repos = Arrays.asList(repositories.listRepositories());
    }
    // * then load all features
    for (Repository repo : repos) {
        for (Feature f : repo.getFeatures()) {
            Map<String, Feature> versionMap = map.computeIfAbsent(f.getName(), key -> new HashMap<>());
            versionMap.put(f.getVersion(), f);
        }
    }
    synchronized (lock) {
        if (uris.equals(state.repositories)) {
            featureCache = map;
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) URI(java.net.URI) Feature(org.apache.karaf.features.Feature) ArrayDeque(java.util.ArrayDeque) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Repository(org.apache.karaf.features.Repository) StateStorage.toStringStringSetMap(org.apache.karaf.features.internal.service.StateStorage.toStringStringSetMap) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 100 with Feature

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

the class FeaturesServiceImpl method addRepository.

@Override
public void addRepository(URI uri, boolean install) throws Exception {
    Repository repository = repositories.create(uri, true);
    synchronized (lock) {
        repositories.addRepository(repository);
        featureCache = null;
        // Add repo
        if (!state.repositories.add(uri.toString())) {
            return;
        }
        saveState();
    }
    callListeners(new RepositoryEvent(repository, RepositoryEvent.EventType.RepositoryAdded, false));
    // install the features in the repo
    if (install) {
        HashSet<String> features = new HashSet<>();
        for (Feature feature : repository.getFeatures()) {
            features.add(feature.getId());
        }
        installFeatures(features, EnumSet.noneOf(FeaturesService.Option.class));
    }
}
Also used : Repository(org.apache.karaf.features.Repository) RepositoryEvent(org.apache.karaf.features.RepositoryEvent) Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Feature (org.apache.karaf.features.Feature)127 Test (org.junit.Test)56 FeaturesService (org.apache.karaf.features.FeaturesService)43 HashSet (java.util.HashSet)41 Repository (org.apache.karaf.features.Repository)39 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)18 Bundle (org.osgi.framework.Bundle)18 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)16 Map (java.util.Map)16 Dependency (org.apache.karaf.features.Dependency)15 BundleInfo (org.apache.karaf.features.BundleInfo)14 Application (org.codice.ddf.admin.application.service.Application)14 URI (java.net.URI)13 IOException (java.io.IOException)12 VersionRange (org.apache.felix.utils.version.VersionRange)12 LinkedHashSet (java.util.LinkedHashSet)11 Version (org.osgi.framework.Version)11 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 EnumSet (java.util.EnumSet)9