Search in sources :

Example 16 with Downloader

use of org.apache.karaf.features.internal.download.Downloader in project karaf by apache.

the class SubsystemResolver method resolve.

@Override
public Map<Resource, List<Wire>> resolve(String featureResolutionRange, FeaturesService.ServiceRequirementsBehavior serviceRequirements, final Repository globalRepository, String outputFile) throws Exception {
    if (root == null) {
        return Collections.emptyMap();
    }
    // Download bundles
    root.downloadBundles(manager, featureResolutionRange, serviceRequirements, new RepositoryManager(), callback);
    // Populate digraph and resolve
    digraph = new StandardRegionDigraph(null, null);
    populateDigraph(digraph, root);
    Downloader downloader = manager.createDownloader();
    SubsystemResolveContext context = new SubsystemResolveContext(root, digraph, globalRepository, downloader, serviceRequirements);
    if (outputFile != null) {
        Map<String, Object> json = new HashMap<>();
        if (globalRepository != null) {
            json.put("globalRepository", toJson(globalRepository));
        }
        json.put("repository", toJson(context.getRepository()));
        try {
            // this is where the magic happens...
            wiring = resolver.resolve(context);
            json.put("success", "true");
            json.put("wiring", toJson(wiring));
        } catch (Exception e) {
            json.put("success", "false");
            json.put("exception", e.toString());
            throw e;
        } finally {
            try (Writer writer = Files.newBufferedWriter(Paths.get(outputFile), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
                JsonWriter.write(writer, json, true);
            }
        }
    } else {
        // this is where the magic happens...
        wiring = resolver.resolve(context);
    }
    downloader.await();
    // Remove wiring to the fake environment resource
    if (environmentResource != null) {
        for (List<Wire> wires : wiring.values()) {
            wires.removeIf(wire -> wire.getProvider() == environmentResource);
        }
    }
    // Fragments are always wired to their host only, so create fake wiring to
    // the subsystem the host is wired to
    associateFragments();
    return wiring;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Downloader(org.apache.karaf.features.internal.download.Downloader) Wire(org.osgi.resource.Wire) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Writer(java.io.Writer) JsonWriter(org.apache.karaf.util.json.JsonWriter) StandardRegionDigraph(org.eclipse.equinox.internal.region.StandardRegionDigraph)

Example 17 with Downloader

use of org.apache.karaf.features.internal.download.Downloader in project karaf by apache.

the class VerifyMojo method loadRepositories.

public Map<String, Features> loadRepositories(DownloadManager manager, Set<String> uris) throws Exception {
    final Map<String, Features> loaded = new HashMap<>();
    final Downloader downloader = manager.createDownloader();
    FeaturesServiceConfig config = null;
    if (featureProcessingInstructions != null) {
        config = new FeaturesServiceConfig(featureProcessingInstructions.toURI().toString(), null);
    } else {
        config = new FeaturesServiceConfig();
    }
    FeaturesProcessorImpl processor = new FeaturesProcessorImpl(config);
    if (blacklistedDescriptors != null) {
        blacklistedDescriptors.forEach(lp -> processor.getInstructions().getBlacklistedRepositoryLocationPatterns().add(new LocationPattern(lp)));
    }
    processor.getInstructions().getBlacklistedRepositoryLocationPatterns().add(new LocationPattern("mvn:" + selfGroupId + "/" + selfArtifactId));
    for (String repository : uris) {
        if (!processor.isRepositoryBlacklisted(repository)) {
            downloader.download(repository, new DownloadCallback() {

                @Override
                public void downloaded(final StreamProvider provider) throws Exception {
                    synchronized (loaded) {
                        // If provider was already loaded, no need to do it again.
                        if (loaded.containsKey(provider.getUrl())) {
                            return;
                        }
                    }
                    try (InputStream is = provider.open()) {
                        Features featuresModel;
                        if (JacksonUtil.isJson(provider.getUrl())) {
                            featuresModel = JacksonUtil.unmarshal(provider.getUrl());
                        } else {
                            featuresModel = JaxbUtil.unmarshal(provider.getUrl(), is, false);
                        }
                        processor.process(featuresModel);
                        synchronized (loaded) {
                            loaded.put(provider.getUrl(), featuresModel);
                            for (String innerRepository : featuresModel.getRepository()) {
                                if (!processor.isRepositoryBlacklisted(innerRepository)) {
                                    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) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) Downloader(org.apache.karaf.features.internal.download.Downloader) MultiException(org.apache.karaf.features.internal.util.MultiException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ResolutionException(org.osgi.service.resolver.ResolutionException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FeaturesProcessorImpl(org.apache.karaf.features.internal.service.FeaturesProcessorImpl) LocationPattern(org.apache.karaf.features.LocationPattern) FeaturesServiceConfig(org.apache.karaf.features.internal.service.FeaturesServiceConfig)

Aggregations

Downloader (org.apache.karaf.features.internal.download.Downloader)17 HashMap (java.util.HashMap)10 LinkedHashMap (java.util.LinkedHashMap)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 Path (java.nio.file.Path)6 Profile (org.apache.karaf.profile.Profile)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 Map (java.util.Map)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Properties (org.apache.felix.utils.properties.Properties)4 Library (org.apache.karaf.features.Library)4 StreamProvider (org.apache.karaf.features.internal.download.StreamProvider)4 Feature (org.apache.karaf.features.internal.model.Feature)4 BundleException (org.osgi.framework.BundleException)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 Resource (org.osgi.resource.Resource)4 File (java.io.File)3 InputStream (java.io.InputStream)3