Search in sources :

Example 81 with Feature

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

the class ApplicationServiceImpl method startApplication.

@Override
public synchronized void startApplication(Application application) throws ApplicationServiceException {
    try {
        LOGGER.debug("Starting Application {} - {}", application.getName(), application.getVersion());
        Set<Feature> autoInstallFeatures = application.getAutoInstallFeatures();
        if (!autoInstallFeatures.isEmpty()) {
            Set<String> autoFeatureNames = autoInstallFeatures.stream().map(Feature::getName).collect(Collectors.toSet());
            for (Feature feature : autoInstallFeatures) {
                if (featuresService.isInstalled(feature)) {
                    autoFeatureNames.remove(feature.getName());
                } else {
                    for (Dependency dependency : feature.getDependencies()) {
                        if (!application.getName().equals(dependency.getName()) && getApplicationNames().contains(dependency.getName())) {
                            if (!isApplicationStarted(getApplication(dependency.getName()))) {
                                startApplication(dependency.getName());
                            }
                            autoFeatureNames.remove(dependency.getName());
                        }
                    }
                }
            }
            if (!autoFeatureNames.isEmpty()) {
                featuresService.installFeatures(autoFeatureNames, EnumSet.of(Option.NoAutoRefreshBundles));
                waitForApplication(application);
            }
        }
    } catch (Exception e) {
        throw new ApplicationServiceException("Could not start application " + application.getName() + " due to errors.", e);
    }
}
Also used : ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Dependency(org.apache.karaf.features.Dependency) Feature(org.apache.karaf.features.Feature) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(ddf.security.service.SecurityServiceException) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException)

Example 82 with Feature

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

the class ApplicationServiceImpl method getNotInstalledFeatures.

/**
     * Given a {@code Set} of {@code Feature}s, returns the subset of
     * {@code Features}s that are not installed
     *
     * @param features The {@code Set} of {@link Feature}s from which to construct
     *                 the subset of {@code Feature}s that are not installed
     * @return A {@code Set} of {@code Feature}s that are not installed that is
     * a sub-set of the <i>features</i> {@code Feature}s {@code Set}
     * parameter
     */
private Set<Feature> getNotInstalledFeatures(Set<Feature> features) {
    Set<Feature> notInstalledFeatures = new HashSet<Feature>();
    for (Feature curFeature : features) {
        if (!featuresService.isInstalled(curFeature)) {
            LOGGER.debug("{} is not installed.", curFeature.getName());
            notInstalledFeatures.add(curFeature);
        }
    }
    return notInstalledFeatures;
}
Also used : Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet)

Example 83 with Feature

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

the class ApplicationStatusImplTest method testAppStatusCreation.

/**
     * Tests that the app status is created properly and returns the correct
     * values.
     */
@Test
public void testAppStatusCreation() {
    Application testApp = mock(Application.class);
    ApplicationState testState = ApplicationState.ACTIVE;
    Set<Feature> testFeatures = new HashSet<Feature>();
    Set<Bundle> testBundles = new HashSet<Bundle>();
    Feature testFeature1 = mock(Feature.class);
    Feature testFeature2 = mock(Feature.class);
    List<Feature> testFeatureList = new ArrayList<Feature>(Arrays.asList(testFeature1, testFeature2));
    testFeatures.addAll(testFeatureList);
    Bundle testBundle1 = mock(Bundle.class);
    Bundle testBundle2 = mock(Bundle.class);
    List<Bundle> testBundleList = new ArrayList<Bundle>(Arrays.asList(testBundle1, testBundle2));
    testBundles.addAll(testBundleList);
    ApplicationStatus testStatus = new ApplicationStatusImpl(testApp, testState, testFeatures, testBundles);
    assertEquals("Sanity check for getApplication()", testApp, testStatus.getApplication());
    assertEquals("Sanity check for getState()", testState, testStatus.getState());
    assertTrue("Sanity check for getErrorFeatures()", testStatus.getErrorFeatures().containsAll(testFeatureList));
    assertTrue("Sanity check for getErrorBundles()", testStatus.getErrorBundles().containsAll(testBundleList));
}
Also used : Bundle(org.osgi.framework.Bundle) ApplicationState(org.codice.ddf.admin.application.service.ApplicationStatus.ApplicationState) ApplicationStatus(org.codice.ddf.admin.application.service.ApplicationStatus) ArrayList(java.util.ArrayList) Application(org.codice.ddf.admin.application.service.Application) Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 84 with Feature

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

the class ApplicationServiceImplTest method testStopApplicationGeneralASE.

/**
     * Tests the {@link ApplicationServiceImpl#stopApplication(Application)} method
     * for the case where an exception is caught
     *
     * @throws Exception
     */
@Test(expected = ApplicationServiceException.class)
public void testStopApplicationGeneralASE() 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 = new ApplicationServiceImpl(bundleStateServices) {

        @Override
        protected BundleContext getContext() {
            return bundleContext;
        }
    };
    Application testApp1 = mock(ApplicationImpl.class);
    Feature testFeature1 = mock(Feature.class);
    when(testApp1.getFeatures()).thenReturn(new HashSet<>(Arrays.asList(testFeature1)));
    when(featuresService.isInstalled(any())).thenReturn(true);
    doThrow(new Exception()).when(featuresService).uninstallFeature(anyString(), anyString(), any());
    appService.stopApplication(testApp1);
}
Also used : Repository(org.apache.karaf.features.Repository) FeaturesService(org.apache.karaf.features.FeaturesService) Application(org.codice.ddf.admin.application.service.Application) Feature(org.apache.karaf.features.Feature) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Example 85 with Feature

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

the class ApplicationServiceImplTest method testStopApplicationNoMainFeature.

/**
     * Tests the {@link ApplicationServiceImpl#stopApplication(Application)} method
     * for the case where there is no main feature, but other features exist
     *
     * @throws Exception
     */
@Test
public void testStopApplicationNoMainFeature() 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();
    Application testApp = mock(ApplicationImpl.class);
    Feature testFeature1 = mock(Feature.class);
    Feature testFeature2 = mock(Feature.class);
    Set<Feature> featureSet = new HashSet<Feature>();
    featureSet.add(testFeature1);
    featureSet.add(testFeature2);
    when(testApp.getName()).thenReturn(TEST_APP_NAME);
    when(testApp.getMainFeature()).thenReturn(null);
    when(testApp.getFeatures()).thenReturn(featureSet);
    when(testFeature1.getName()).thenReturn(TEST_FEATURE_1_NAME);
    when(testFeature2.getName()).thenReturn(TEST_FEATURE_2_NAME);
    when(testFeature1.getInstall()).thenReturn(Feature.DEFAULT_INSTALL_MODE);
    when(testFeature2.getInstall()).thenReturn(Feature.DEFAULT_INSTALL_MODE);
    when(featuresService.isInstalled(testFeature1)).thenReturn(true);
    when(featuresService.isInstalled(testFeature2)).thenReturn(true);
    appService.stopApplication(testApp);
    verify(featuresService, atLeastOnce()).uninstallFeature(TEST_FEATURE_1_NAME, null, EnumSet.of(Option.NoAutoRefreshBundles));
    verify(featuresService, atLeastOnce()).uninstallFeature(TEST_FEATURE_2_NAME, null, EnumSet.of(Option.NoAutoRefreshBundles));
}
Also used : Repository(org.apache.karaf.features.Repository) FeaturesService(org.apache.karaf.features.FeaturesService) Application(org.codice.ddf.admin.application.service.Application) Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Aggregations

Feature (org.apache.karaf.features.Feature)127 Test (org.junit.Test)56 FeaturesService (org.apache.karaf.features.FeaturesService)43 HashSet (java.util.HashSet)41 Repository (org.apache.karaf.features.Repository)39 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)18 Bundle (org.osgi.framework.Bundle)18 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)16 Map (java.util.Map)16 Dependency (org.apache.karaf.features.Dependency)15 BundleInfo (org.apache.karaf.features.BundleInfo)14 Application (org.codice.ddf.admin.application.service.Application)14 URI (java.net.URI)13 IOException (java.io.IOException)12 VersionRange (org.apache.felix.utils.version.VersionRange)12 LinkedHashSet (java.util.LinkedHashSet)11 Version (org.osgi.framework.Version)11 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 EnumSet (java.util.EnumSet)9