Search in sources :

Example 16 with DownloadManager

use of io.fabric8.agent.download.DownloadManager 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 17 with DownloadManager

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

the class AgentUtils method addFeatures.

/**
 * Adds the set of features to the given set for the given profile
 *
 * @param features
 * @param fabricService
 *@param downloadManager
 * @param profile   @throws Exception
 */
public static void addFeatures(Set<Feature> features, FabricService fabricService, DownloadManager downloadManager, Profile profile) throws Exception {
    List<String> featureNames = profile.getFeatures();
    Map<String, Repository> repositories = getRepositories(fabricService, downloadManager, profile);
    for (String featureName : featureNames) {
        Feature feature = FeatureUtils.search(featureName, repositories.values());
        if (feature == null) {
            LOGGER.warn("Could not find feature " + featureName + " for profile " + profile.getId() + " in repositories " + repositories.keySet());
        } else {
            features.addAll(expandFeature(feature, repositories));
        }
    }
}
Also used : Repository(io.fabric8.agent.model.Repository) Feature(io.fabric8.agent.model.Feature)

Example 18 with DownloadManager

use of io.fabric8.agent.download.DownloadManager 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)

Example 19 with DownloadManager

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

the class DownloadManagerTest method createDownloadManager.

/**
 * Prepares DownloadManager to test
 *
 * @param remoteRepo
 * @param settingsFile
 * @param props
 * @return
 * @throws IOException
 */
private DownloadManager createDownloadManager(String remoteRepo, String settingsFile, Properties props) throws IOException {
    File mavenSettings = new File(karafHome, settingsFile);
    Hashtable<String, String> properties = new Hashtable<>();
    if (props != null) {
        for (Map.Entry<Object, Object> entry : props.entrySet()) {
            properties.put(entry.getKey().toString(), entry.getValue().toString());
        }
    }
    properties.put("org.ops4j.pax.url.mvn.localRepository", systemRepoUri);
    properties.put("org.ops4j.pax.url.mvn.repositories", remoteRepo);
    properties.put("org.ops4j.pax.url.mvn.defaultRepositories", systemRepoUri);
    properties.put("org.ops4j.pax.url.mvn.settings", mavenSettings.toURI().toString());
    MavenResolver resolver = MavenResolvers.createMavenResolver(properties, "org.ops4j.pax.url.mvn");
    return DownloadManagers.createDownloadManager(resolver, Executors.newSingleThreadScheduledExecutor());
}
Also used : Hashtable(java.util.Hashtable) MavenResolver(io.fabric8.maven.MavenResolver) File(java.io.File) Map(java.util.Map)

Example 20 with DownloadManager

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

the class AgentTest method testAgent.

@Test
public void testAgent() throws Exception {
    System.setProperty("karaf.data", new File("target/karaf/data").getAbsolutePath());
    System.setProperty("karaf.home", new File("target/karaf").getAbsolutePath());
    Dictionary<String, String> resolverProps = new Hashtable<>();
    resolverProps.put(ServiceConstants.PROPERTY_REPOSITORIES, "http://repository.jboss.org/nexus/content/repositories/fs-public/@id=jboss.fs.public," + "https://repository.jboss.org/nexus/content/groups/ea/@id=jboss.ea.repo," + "http://repo1.maven.org/maven2@id=maven.central.repo," + "http://download.eng.bos.redhat.com/brewroot/repos/jb-fuse-6.2-build/latest/maven@id=brew");
    MavenResolver mavenResolver = MavenResolvers.createMavenResolver(resolverProps, null);
    DownloadManager manager = DownloadManagers.createDownloadManager(mavenResolver, Executors.newScheduledThreadPool(8));
    BundleContext systemBundleContext = createMock(BundleContext.class);
    TestSystemBundle systemBundle = createTestSystemBundle("/common", "system-bundle");
    systemBundle.setBundleContext(systemBundleContext);
    Bundle serviceBundle = createTestBundle(1l, Bundle.ACTIVE, "/common", "fabric-agent");
    expect(systemBundleContext.getBundle()).andReturn(systemBundle).anyTimes();
    expect(systemBundleContext.getBundles()).andReturn(new Bundle[] { systemBundle }).anyTimes();
    long nextBundleId = 2;
    List<Bundle> mockBundles = new ArrayList<>();
    String karafVersion = System.getProperty("karaf-version");
    String[] bundles = { "mvn:org.apache.aries.blueprint/org.apache.aries.blueprint.api/1.0.1", "mvn:org.apache.aries.blueprint/org.apache.aries.blueprint.cm/1.1.0", "mvn:org.apache.aries.blueprint/org.apache.aries.blueprint.core/1.8.0", "mvn:org.apache.aries.blueprint/org.apache.aries.blueprint.core.compatibility/1.0.0", "mvn:org.apache.aries.proxy/org.apache.aries.proxy/1.1.1", "mvn:org.apache.aries/org.apache.aries.util/1.1.3", "mvn:org.apache.felix/org.apache.felix.configadmin/1.8.12", "mvn:org.apache.karaf.jaas/org.apache.karaf.jaas.command/" + karafVersion, "mvn:org.apache.karaf.jaas/org.apache.karaf.jaas.config/" + karafVersion, "mvn:org.apache.karaf.jaas/org.apache.karaf.jaas.modules/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.commands/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.console/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.dev/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.log/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.osgi/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.packages/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.ssh/" + karafVersion, "mvn:org.apache.mina/mina-core/2.0.13", "mvn:org.apache.sshd/sshd-core/0.14.0.redhat-001", "mvn:org.ow2.asm/asm-all/5.0.4", "mvn:org.ops4j.pax.logging/pax-logging-api/1.9.1", "mvn:org.ops4j.pax.logging/pax-logging-service/1.9.1" };
    for (String bundleUri : bundles) {
        File file = mavenResolver.download(bundleUri);
        Hashtable<String, String> headers = doGetMetadata(file);
        TestBundle bundle = new TestBundle(++nextBundleId, bundleUri, Bundle.INSTALLED, headers) {

            @Override
            public void setStartLevel(int startlevel) {
            }

            @Override
            public void start() throws BundleException {
            }
        };
        expect(systemBundleContext.installBundle(EasyMock.eq(bundleUri), EasyMock.<InputStream>anyObject())).andReturn(bundle);
    }
    ServiceRegistration registration = EasyMock.createMock(ServiceRegistration.class);
    expect(systemBundleContext.registerService(EasyMock.eq(ResolverHookFactory.class), EasyMock.<ResolverHookFactory>anyObject(), EasyMock.<Dictionary>isNull())).andReturn(registration);
    registration.unregister();
    replay(systemBundleContext, registration);
    for (Bundle bundle : mockBundles) {
        replay(bundle);
    }
    Agent agent = new Agent(serviceBundle, systemBundleContext, manager) {

        @Override
        protected <T> void awaitService(Class<T> serviceClass, String filterspec, int timeout, TimeUnit timeUnit) {
        }
    };
    String karafFeaturesUrl = "mvn:org.apache.karaf.assemblies.features/standard/" + System.getProperty("karaf-version") + "/xml/features";
    agent.provision(Collections.singleton(karafFeaturesUrl), Collections.singleton("ssh"), Collections.<String>emptySet(), Collections.<String>emptySet(), Collections.<String>emptySet(), new HashSet<>(Arrays.asList("mvn:org.ops4j.pax.logging/pax-logging-api/1.9.1", "mvn:org.ops4j.pax.logging/pax-logging-service/1.9.1", "mvn:org.apache.felix/org.apache.felix.configadmin/1.8.12")), Collections.<String, Map<VersionRange, Map<String, String>>>emptyMap());
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) Bundle(org.osgi.framework.Bundle) VersionRange(org.apache.felix.utils.version.VersionRange) DownloadManager(io.fabric8.agent.download.DownloadManager) MavenResolver(io.fabric8.maven.MavenResolver) TimeUnit(java.util.concurrent.TimeUnit) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)9 DownloadManager (io.fabric8.agent.download.DownloadManager)8 Profile (io.fabric8.api.Profile)8 File (java.io.File)8 Downloader (io.fabric8.agent.download.Downloader)6 DownloadCallback (io.fabric8.agent.download.DownloadCallback)5 StreamProvider (io.fabric8.agent.download.StreamProvider)5 Feature (io.fabric8.agent.model.Feature)5 ProfileService (io.fabric8.api.ProfileService)5 MavenResolver (io.fabric8.maven.MavenResolver)5 Map (java.util.Map)5 MultiException (io.fabric8.common.util.MultiException)4 Parser (io.fabric8.maven.util.Parser)4 IOException (java.io.IOException)4 Repository (io.fabric8.agent.model.Repository)3 FabricService (io.fabric8.api.FabricService)3 ArrayList (java.util.ArrayList)3 Hashtable (java.util.Hashtable)3 BundleInfo (io.fabric8.agent.model.BundleInfo)2 Agent (io.fabric8.agent.service.Agent)2