Search in sources :

Example 6 with MavenResolver

use of org.ops4j.pax.url.mvn.MavenResolver in project karaf by apache.

the class FeaturesServiceImpl method doProvision.

private // all requirements
void doProvision(// all requirements
Map<String, Set<String>> requirements, // features state changes
Map<String, Map<String, FeatureState>> stateChanges, // current state
State state, // installation options
EnumSet<Option> options, // file to store the resolution or null
String outputFile) throws Exception {
    Dictionary<String, String> props = getMavenConfig();
    MavenResolver resolver = MavenResolvers.createMavenResolver(props, "org.ops4j.pax.url.mvn");
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(cfg.downloadThreads);
    executor.setMaximumPoolSize(cfg.downloadThreads);
    DownloadManager manager = DownloadManagers.createDownloadManager(resolver, executor, cfg.scheduleDelay, cfg.scheduleMaxRun);
    try {
        Set<String> prereqs = new HashSet<>();
        while (true) {
            try {
                Deployer.DeploymentState dstate = getDeploymentState(state);
                Deployer.DeploymentRequest request = getDeploymentRequest(requirements, stateChanges, options, outputFile);
                new Deployer(manager, this.resolver, this.installSupport, this).deploy(dstate, request);
                break;
            } catch (Deployer.PartialDeploymentException e) {
                if (!prereqs.containsAll(e.getMissing())) {
                    prereqs.addAll(e.getMissing());
                    state = copyState();
                } else {
                    throw new Exception("Deployment aborted due to loop in missing prerequisites: " + e.getMissing());
                }
            }
        }
    } finally {
        executor.shutdown();
    }
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) DownloadManager(org.apache.karaf.features.internal.download.DownloadManager) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 7 with MavenResolver

use of org.ops4j.pax.url.mvn.MavenResolver in project opennms by OpenNMS.

the class KarafExtender method init.

public void init() throws InterruptedException {
    Objects.requireNonNull(m_configurationAdmin, "configurationAdmin");
    Objects.requireNonNull(m_mavenResolver, "mavenResolver");
    Objects.requireNonNull(m_featuresService, "featuresService");
    List<Repository> repositories;
    try {
        repositories = getRepositories();
    } catch (IOException e) {
        LOG.error("Failed to retrieve the list of repositories. Aborting.", e);
        return;
    }
    // Prepend the featuresBoot from the repository definitions
    List<Feature> featuresBoot = repositories.stream().flatMap(r -> r.getFeaturesBoot().stream()).collect(Collectors.toList());
    try {
        featuresBoot.addAll(getFeaturesBoot());
    } catch (IOException e) {
        LOG.error("Failed to retrieve the list of features to boot. Aborting.", e);
        return;
    }
    // Filter the list of features
    filterFeatures(featuresBoot);
    // Build a comma separated list of our Maven repositories
    final StringBuilder mavenReposSb = new StringBuilder();
    for (Repository repository : repositories) {
        if (mavenReposSb.length() != 0) {
            mavenReposSb.append(",");
        }
        mavenReposSb.append(repository.toMavenUri());
    }
    final String mavenRepos = mavenReposSb.toString();
    LOG.info("Updating Maven repositories to include: {}", mavenRepos);
    try {
        final Configuration config = m_configurationAdmin.getConfiguration(PAX_MVN_PID);
        if (config == null) {
            throw new IOException("The OSGi configuration (admin) registry was found for pid " + PAX_MVN_PID + ", but a configuration could not be located/generated.  This shouldn't happen.");
        }
        final Dictionary<String, Object> props = config.getProperties();
        if (!mavenRepos.equals(props.get(PAX_MVN_REPOSITORIES))) {
            props.put(PAX_MVN_REPOSITORIES, mavenRepos);
            config.update(props);
        }
    } catch (IOException e) {
        LOG.error("Failed to update the list of Maven repositories to '{}'. Aborting.", mavenRepos, e);
        return;
    }
    // The configuration update is async, we need to wait for the feature URLs to be resolvable before we use them
    LOG.info("Waiting up-to 30 seconds for the Maven repositories to be updated...");
    // Attempting to resolve a missing features writes an exception to the log
    // We sleep fix a fixed amount of time before our first try in order to help minimize the logged
    // exceptions, even if we catch them
    Thread.sleep(2000);
    for (int i = 28; i > 0 && !canResolveAllFeatureUris(repositories); i--) {
        Thread.sleep(1000);
    }
    for (Repository repository : repositories) {
        for (URI featureUri : repository.getFeatureUris()) {
            try {
                LOG.info("Adding feature repository: {}", featureUri);
                m_featuresService.addRepository(featureUri);
            } catch (Exception e) {
                LOG.error("Failed to add feature repository '{}'. Skipping.", featureUri, e);
            }
        }
    }
    final Set<String> featuresToInstall = featuresBoot.stream().map(f -> f.getVersion() != null ? f.getName() + "/" + f.getVersion() : f.getName()).collect(Collectors.toCollection(LinkedHashSet::new));
    // Because of the fix for the following issue, we need to call the
    // feature installation in another thread since this method is invoked
    // during a feature installation itself and feature installations are
    // now single-threaded.
    // 
    // https://issues.apache.org/jira/browse/KARAF-3798
    // https://github.com/apache/karaf/pull/138
    // 
    CompletableFuture.runAsync(new Runnable() {

        @Override
        public void run() {
            try {
                LOG.info("Installing features: {}", featuresToInstall);
                m_featuresService.installFeatures(featuresToInstall, EnumSet.noneOf(Option.class));
            } catch (Exception e) {
                LOG.error("Failed to install one or more features.", e);
            }
        }
    });
}
Also used : Option(org.apache.karaf.features.FeaturesService.Option) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) FeaturesService(org.apache.karaf.features.FeaturesService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DirectoryStream(java.nio.file.DirectoryStream) Matcher(java.util.regex.Matcher) Lists(com.google.common.collect.Lists) Configuration(org.osgi.service.cm.Configuration) URI(java.net.URI) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) LinkedHashSet(java.util.LinkedHashSet) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Files(java.nio.file.Files) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Paths(java.nio.file.Paths) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 8 with MavenResolver

use of org.ops4j.pax.url.mvn.MavenResolver in project components by Talend.

the class JarRuntimeInfoTest method testExtracDependencyFromStream.

/**
 * Test method for
 * {@link org.talend.components.api.component.runtime.DependenciesReader#extractDependenciesFromStream(org.talend.components.api.component.runtime.DependenciesReader, java.lang.String, java.util.jar.JarInputStream)}
 * .
 *
 * @throws IOException
 * @throws MalformedURLException
 */
@Ignore("we can't use components-api-full-example here because it brings cyclic dependency. Should be fixed by using another test module")
@Test
public void testExtracDependencyFromStream() throws MalformedURLException, IOException {
    MavenResolver mavenResolver = MavenResolvers.createMavenResolver(null, "foo");
    File jarWithDeps = mavenResolver.resolve("mvn:org.talend.components/components-api-full-example/0.1.0");
    try (JarInputStream jis = new JarInputStream(new FileInputStream(jarWithDeps))) {
        List<URL> dependencyFromStream = DependenciesReader.extractDependencies(new DependenciesReader(null), DependenciesReader.computeDependenciesFilePath("org.talend.components", "components-full-example"), jis);
        checkFullExampleDependencies(dependencyFromStream);
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) File(java.io.File) FileInputStream(java.io.FileInputStream) URL(java.net.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with MavenResolver

use of org.ops4j.pax.url.mvn.MavenResolver in project karaf by apache.

the class FeaturesServiceImpl method createDownloadManager.

protected DownloadManager createDownloadManager() throws IOException {
    Dictionary<String, String> props = getMavenConfig();
    MavenResolver resolver = MavenResolvers.createMavenResolver(props, "org.ops4j.pax.url.mvn");
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(cfg.downloadThreads, ThreadUtils.namedThreadFactory("downloader"));
    executor.setMaximumPoolSize(cfg.downloadThreads);
    return DownloadManagers.createDownloadManager(resolver, executor, cfg.scheduleDelay, cfg.scheduleMaxRun);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver)

Example 10 with MavenResolver

use of org.ops4j.pax.url.mvn.MavenResolver in project karaf by apache.

the class Builder method createMavenResolver.

private MavenResolver createMavenResolver() {
    Dictionary<String, String> props = new Hashtable<>();
    if (offline) {
        props.put(ORG_OPS4J_PAX_URL_MVN_PID + "offline", "true");
    }
    if (localRepository != null) {
        props.put(ORG_OPS4J_PAX_URL_MVN_PID + ".localRepository", localRepository);
    }
    if (mavenRepositories != null) {
        props.put(ORG_OPS4J_PAX_URL_MVN_PID + ".repositories", mavenRepositories);
    }
    MavenResolver resolver = MavenResolvers.createMavenResolver(props, ORG_OPS4J_PAX_URL_MVN_PID);
    return resolverWrapper.apply(resolver);
}
Also used : Hashtable(java.util.Hashtable) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver)

Aggregations

MavenResolver (org.ops4j.pax.url.mvn.MavenResolver)11 File (java.io.File)7 IOException (java.io.IOException)5 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)3 InputStream (java.io.InputStream)2 URI (java.net.URI)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Dictionary (java.util.Dictionary)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)2 ZipEntry (java.util.zip.ZipEntry)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Lists (com.google.common.collect.Lists)1