Search in sources :

Example 6 with StreamProvider

use of io.fabric8.agent.download.StreamProvider in project fabric8 by jboss-fuse.

the class ArchetypeGenerateAction method fetchArchetype.

/**
 * Fetches archetype from the configured repositories
 * TODO: make this code available to hawt.io/JMX too
 */
private File fetchArchetype(Archetype archetype) throws IOException {
    MavenResolver resolver = MavenResolvers.createMavenResolver(new Hashtable<String, String>(), "org.ops4j.pax.url.mvn");
    DownloadManager dm = DownloadManagers.createDownloadManager(resolver, Executors.newSingleThreadScheduledExecutor());
    final AtomicReference<File> file = new AtomicReference<>();
    String url = String.format("mvn:%s/%s/%s", archetype.groupId, archetype.artifactId, archetype.version);
    Downloader downloader = dm.createDownloader();
    downloader.download(url, new DownloadCallback() {

        @Override
        public void downloaded(StreamProvider provider) throws Exception {
            file.set(provider.getFile());
        }
    });
    // wait for download
    try {
        boolean init = false;
        for (int i = 0; i < 2 * 60 && file.get() == null; i++) {
            // dont do anything in the first 3 seconds as we likely can download it faster
            if (i > 3) {
                if (!init) {
                    System.out.print("Downloading archetype in progress: ");
                    init = true;
                }
                System.out.print(".");
            }
            // only sleep 0.5 sec so we can react faster
            Thread.sleep(500);
        }
    } catch (InterruptedException e) {
        System.err.println("\nFailed to download " + archetype);
        throw new IOException(e.getMessage(), e);
    }
    try {
        downloader.await();
        return file.get();
    } catch (Exception e) {
        System.err.println("\nFailed to download archetype within 60 seconds: " + archetype);
        throw new IOException("Failed to download archetype within 60 seconds: " + archetype);
    }
}
Also used : StreamProvider(io.fabric8.agent.download.StreamProvider) DownloadCallback(io.fabric8.agent.download.DownloadCallback) Downloader(io.fabric8.agent.download.Downloader) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) DownloadManager(io.fabric8.agent.download.DownloadManager) IOException(java.io.IOException) MavenResolver(io.fabric8.maven.MavenResolver) File(java.io.File)

Example 7 with StreamProvider

use of io.fabric8.agent.download.StreamProvider in project fabric8 by jboss-fuse.

the class Deployer method getBundleInputStream.

protected InputStream getBundleInputStream(Resource resource, Map<String, StreamProvider> providers) throws IOException {
    String uri = getUri(resource);
    if (uri == null) {
        throw new IllegalStateException("Resource has no uri");
    }
    StreamProvider provider = providers.get(uri);
    if (provider == null) {
        throw new IllegalStateException("Resource " + uri + " has no StreamProvider");
    }
    return new FileInputStream(provider.getFile());
}
Also used : StreamProvider(io.fabric8.agent.download.StreamProvider) FileInputStream(java.io.FileInputStream)

Example 8 with StreamProvider

use of io.fabric8.agent.download.StreamProvider in project fabric8 by jboss-fuse.

the class AgentUtils method downloadRepositories.

public static Callable<Map<String, Repository>> downloadRepositories(DownloadManager manager, Set<String> uris) throws MultiException, InterruptedException, MalformedURLException {
    final Map<String, Repository> repositories = new HashMap<>();
    final Downloader downloader = manager.createDownloader();
    final File targetLocation = getDefaultKarafRepository();
    for (String uri : uris) {
        downloader.download(uri, new DownloadCallback() {

            @Override
            public void downloaded(StreamProvider provider) throws Exception {
                String uri = provider.getUrl();
                Repository repository = new Repository(URI.create(uri));
                repository.load(new FileInputStream(provider.getFile()), true);
                synchronized (repositories) {
                    repositories.put(uri, repository);
                }
                for (URI repo : repository.getRepositories()) {
                    downloader.download(repo.toASCIIString(), this);
                }
                Artifact artifact = Utils.mvnurlToArtifact(uri, true);
                if (artifact == null || artifact.getVersion() == null || !artifact.getVersion().endsWith("-SNAPSHOT")) {
                    // we need a feature repository to be available in ${karaf.home}/${karaf.default.repository}
                    // it makes patching much easier
                    // ENTESB-6931: don't store SNAPSHOT feature repositories in ${karaf.home}/${karaf.default.repository}
                    storeInDefaultKarafRepository(targetLocation, provider.getFile(), uri);
                }
            }
        });
    }
    return new Callable<Map<String, Repository>>() {

        @Override
        public Map<String, Repository> call() throws Exception {
            downloader.await();
            return repositories;
        }
    };
}
Also used : StreamProvider(io.fabric8.agent.download.StreamProvider) HashMap(java.util.HashMap) DownloadCallback(io.fabric8.agent.download.DownloadCallback) Downloader(io.fabric8.agent.download.Downloader) URI(java.net.URI) MalformedURLException(java.net.MalformedURLException) MultiException(io.fabric8.common.util.MultiException) FileInputStream(java.io.FileInputStream) Artifact(io.fabric8.patch.management.Artifact) Callable(java.util.concurrent.Callable) Repository(io.fabric8.agent.model.Repository) File(java.io.File)

Aggregations

StreamProvider (io.fabric8.agent.download.StreamProvider)7 Downloader (io.fabric8.agent.download.Downloader)6 DownloadCallback (io.fabric8.agent.download.DownloadCallback)5 File (java.io.File)5 MultiException (io.fabric8.common.util.MultiException)4 FileInputStream (java.io.FileInputStream)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 URI (java.net.URI)3 HashMap (java.util.HashMap)3 DownloadManager (io.fabric8.agent.download.DownloadManager)2 MavenResolver (io.fabric8.maven.MavenResolver)2 Callable (java.util.concurrent.Callable)2 BundleException (org.osgi.framework.BundleException)2 Resource (org.osgi.resource.Resource)2 MapUtils.addToMapSet (io.fabric8.agent.internal.MapUtils.addToMapSet)1 MapUtils.removeFromMapSet (io.fabric8.agent.internal.MapUtils.removeFromMapSet)1 BundleInfo (io.fabric8.agent.model.BundleInfo)1 ConfigFile (io.fabric8.agent.model.ConfigFile)1 Feature (io.fabric8.agent.model.Feature)1