Search in sources :

Example 31 with FeaturesService

use of org.apache.karaf.features.FeaturesService in project ddf by codice.

the class ApplicationServiceImplTest method testStopApplicationStringParamASE.

/**
     * Tests the {@link ApplicationServiceImpl#stopApplication(String)} method
     * for the case where the application cannot be found
     */
@Test(expected = ApplicationServiceException.class)
public void testStopApplicationStringParamASE() throws Exception {
    Set<Repository> activeRepos = new HashSet<Repository>(Arrays.asList(mainFeatureRepo, noMainFeatureRepo1, noMainFeatureRepo2));
    FeaturesService featuresService = createMockFeaturesService(activeRepos, null, null);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationService appService = createPermittedApplicationServiceImpl();
    appService.stopApplication("");
}
Also used : Repository(org.apache.karaf.features.Repository) FeaturesService(org.apache.karaf.features.FeaturesService) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Example 32 with FeaturesService

use of org.apache.karaf.features.FeaturesService 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 33 with FeaturesService

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

the class FeaturesServiceImplTest method testRemoveRepo2.

@Test
public void testRemoveRepo2() throws Exception {
    final FeaturesService featureService = createTestFeatureService();
    URI repoA = URI.create("custom:remove/a.xml");
    URI repoB = URI.create("custom:remove/b.xml");
    featureService.addRepository(repoA);
    featureService.addRepository(repoB);
    Feature a1Feature = featureService.getFeature("a1");
    installFeature(featureService, a1Feature);
    Feature b1Feature = featureService.getFeature("b1");
    installFeature(featureService, b1Feature);
    featureService.removeRepository(repoA);
    assertNotInstalled(featureService, a1Feature);
    assertInstalled(featureService, b1Feature);
}
Also used : FeaturesService(org.apache.karaf.features.FeaturesService) Feature(org.apache.karaf.features.Feature) Test(org.junit.Test)

Example 34 with FeaturesService

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

the class Activator method doStart.

@Override
protected void doStart() throws Exception {
    FeaturesService service = getTrackedService(FeaturesService.class);
    if (service == null) {
        return;
    }
    Hashtable<String, Object> props = new Hashtable<>();
    props.put("url.handler.protocol", "feature");
    FeatureURLHandler handler = new FeatureURLHandler();
    register(URLStreamHandlerService.class, handler, props);
    listener = new FeatureDeploymentListener();
    listener.setFeaturesService(service);
    listener.setBundleContext(bundleContext);
    listener.init();
    register(new Class[] { ArtifactUrlTransformer.class, ArtifactListener.class }, listener);
}
Also used : FeatureDeploymentListener(org.apache.karaf.deployer.features.FeatureDeploymentListener) FeatureURLHandler(org.apache.karaf.deployer.features.FeatureURLHandler) Hashtable(java.util.Hashtable) FeaturesService(org.apache.karaf.features.FeaturesService)

Example 35 with FeaturesService

use of org.apache.karaf.features.FeaturesService in project ddf by codice.

the class ProfileInstallCommandTest method testPostInstallWithInstallerInstalled.

@Test
public void testPostInstallWithInstallerInstalled() throws Exception {
    Feature installerFeature = createMockFeature("admin-modules-installer");
    this.featuresService = mock(FeaturesService.class);
    when(featuresService.getFeature(anyString())).thenReturn(installerFeature);
    when(featuresService.isInstalled(installerFeature)).thenReturn(true);
    profileInstallCommand.profileName = "invalidStopBundles";
    profileInstallCommand.doExecute(applicationService, featuresService, bundleService);
    verify(featuresService).uninstallFeature(eq("admin-modules-installer"), eq(NO_AUTO_REFRESH));
}
Also used : FeaturesService(org.apache.karaf.features.FeaturesService) Feature(org.apache.karaf.features.Feature) Test(org.junit.Test)

Aggregations

FeaturesService (org.apache.karaf.features.FeaturesService)71 Test (org.junit.Test)61 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)51 HashSet (java.util.HashSet)48 Repository (org.apache.karaf.features.Repository)43 Feature (org.apache.karaf.features.Feature)29 Application (org.codice.ddf.admin.application.service.Application)19 Mockito.anyString (org.mockito.Mockito.anyString)13 Logger (org.slf4j.Logger)12 Appender (ch.qos.logback.core.Appender)10 URI (java.net.URI)10 ArgumentMatcher (org.mockito.ArgumentMatcher)10 ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)9 EnumSet (java.util.EnumSet)6 Bundle (org.osgi.framework.Bundle)6 URL (java.net.URL)5 ArrayList (java.util.ArrayList)4 BundleInfo (org.apache.karaf.features.BundleInfo)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2