Search in sources :

Example 16 with BundleInfo

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

the class ApplicationServiceImpl method getCurrentBundleStates.

/**
     * Evaluates the bundles contained in a set of {@link Feature}s and
     * determines if each bundle is currently in an active, inactive, or failed
     * state.
     *
     * @param features
     * @return {@link BundleStateSet} containing information on the state of
     * each bundle
     */
private final BundleStateSet getCurrentBundleStates(Set<Feature> features) {
    BundleStateSet bundleStateSet = new BundleStateSet();
    for (Feature curFeature : features) {
        for (BundleInfo curBundleInfo : curFeature.getBundles()) {
            Bundle curBundle = getContext().getBundle(curBundleInfo.getLocation());
            if (curBundle != null && curBundle.adapt(BundleRevision.class).getTypes() != BundleRevision.TYPE_FRAGMENT) {
                // check if bundle is inactive
                int bundleState = curBundle.getState();
                switch(bundleState) {
                    case Bundle.RESOLVED:
                    case Bundle.STARTING:
                    case Bundle.STOPPING:
                        bundleStateSet.addInactiveBundle(curBundle);
                        break;
                    case Bundle.INSTALLED:
                    case Bundle.UNINSTALLED:
                        bundleStateSet.addFailedBundle(curBundle);
                        break;
                    case Bundle.ACTIVE:
                        // and SpringDM) failed on start
                        for (BundleStateService curStateService : bundleStateServices) {
                            LOGGER.trace("Checking {} for bundle state of {}.", curStateService.getName(), curBundle.getSymbolicName());
                            BundleState curState = curStateService.getState(curBundle);
                            switch(curState) {
                                case Resolved:
                                case Stopping:
                                    LOGGER.trace("{} is in an inactive state. Current State: {}", curBundle.getSymbolicName(), curState.toString());
                                    bundleStateSet.addInactiveBundle(curBundle);
                                    break;
                                case Installed:
                                case Failure:
                                    LOGGER.trace("{} is in a failed state. Current State: {}", curBundle.getSymbolicName(), curState.toString());
                                    bundleStateSet.addFailedBundle(curBundle);
                                    break;
                                case Waiting:
                                case Starting:
                                case GracePeriod:
                                    LOGGER.trace("{} is in a transitional state. Current State: {}", curBundle.getSymbolicName(), curState.toString());
                                    bundleStateSet.addTransitionalBundle(curBundle);
                                    break;
                                case Active:
                                    LOGGER.trace("{} is in an active state. Current State: {}", curBundle.getSymbolicName(), curState.toString());
                                    bundleStateSet.addActiveBundle(curBundle);
                                    break;
                                case Unknown:
                                default:
                                    // Ignore - BundleStateService unaware of this bundle.
                                    break;
                            }
                        }
                        // end case Bundle.Active
                        break;
                    default:
                        bundleStateSet.addActiveBundle(curBundle);
                        break;
                }
            }
        }
    }
    return bundleStateSet;
}
Also used : BundleStateService(org.apache.karaf.bundle.core.BundleStateService) BundleInfo(org.apache.karaf.features.BundleInfo) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleState(org.apache.karaf.bundle.core.BundleState) Feature(org.apache.karaf.features.Feature)

Example 17 with BundleInfo

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

the class ApplicationServiceBeanTest method testGetServicesNotContainsKey.

/**
     * Tests the {@link ApplicationServiceBean#getServices(String)} method
     * for the case where the services do not have the "configurations" key
     *
     * @throws Exception
     */
@Test
public void testGetServicesNotContainsKey() throws Exception {
    ApplicationServiceBean serviceBean = new ApplicationServiceBean(testAppService, testConfigAdminExt, mBeanServer) {

        @Override
        protected BundleContext getContext() {
            return bundleContext;
        }
    };
    Bundle testBundle = mock(Bundle.class);
    Bundle[] bundles = { testBundle };
    when(bundleContext.getBundles()).thenReturn(bundles);
    List<Map<String, Object>> services = new ArrayList<>();
    Map<String, Object> testService2 = mock(HashMap.class);
    Map<String, Object> testService1 = mock(HashMap.class);
    services.add(testService1);
    services.add(testService2);
    when(testService1.get("factory")).thenReturn(true);
    when(testService2.get("factory")).thenReturn(false);
    BundleInfo testBundle1 = mock(BundleInfo.class);
    Set<BundleInfo> testBundles = new HashSet<>();
    testBundles.add(testBundle1);
    when(testApp.getBundles()).thenReturn(testBundles);
    when(testBundle1.getLocation()).thenReturn(TEST_LOCATION);
    when(testAppService.getApplication(TEST_APP_NAME)).thenReturn(testApp);
    when(testConfigAdminExt.listServices(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(services);
    assertThat("Should not find any services.", serviceBean.getServices(TEST_APP_NAME), is(ListUtils.EMPTY_LIST));
}
Also used : BundleInfo(org.apache.karaf.features.BundleInfo) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with BundleInfo

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

the class ApplicationServiceImplTest method createMockFeaturesService.

/**
     * Creates a mock {@code FeaturesService} object consisting of all of the
     * features contained in a {@code Set} of {@code Repository} objects. Each
     * {@code Feature} will be in the <i>installed</i> state unless it is
     * contained in the received set of features that are not to be installed.
     * Each {@code Bundle} will be in the {@code Bundle#ACTIVE} state and the
     * {@code BundleState#Active} extended bundle state (as reported by a
     * dependency injection framework) unless it is contained in the received
     * set of {@code Bundle}s that are not to be active, in which case the
     * {@code Bundle} will be in the {@code Bundle#INSTALLED} state and the
     * {@code BundleState#Installed} extended bundle state.
     * <p>
     * Note that not all of the state and {@code Bundle} information is
     * contained in the {@code FeaturesService}. As such, this method stores
     * some of the required information in the class's {@code #bundleContext}
     * and {@code bundleStateServices}. As such, these objects must be
     * re-instantiated for each test (i.e., they must be instantiated in the
     * {@link #setUp()} method).
     *
     * @param repos                A {@code Set} of {@link Repository} objects from which to
     *                             obtain the {@link Feature}s that are to be included in the
     *                             mock {@code FeaturesService}
     * @param notInstalledFeatures A {@code Set} of {@code Feature}s that the
     *                             {@code FeaturesService} should report as not installed
     * @param inactiveBundles      A {@code Set} of {@link BundleInfo}s containing the locations
     *                             of {@code Bundle}s that should be set to inactive and for
     *                             which the {@link BundleStateService} contained in index 0 of
     *                             {@link #bundleStateServices} should report a
     *                             {@link BundleState#Installed} state.
     * @return A mock {@link FeaturesService} with {@link Feature}s and
     * {@link Bundle}s in the requested states.
     * @throws Exception
     */
private FeaturesService createMockFeaturesService(Set<Repository> repos, Set<Feature> notInstalledFeatures, Set<BundleInfo> inactiveBundles) throws Exception {
    if (LOGGER.isTraceEnabled()) {
        for (Repository repo : repos) {
            for (Feature feature : repo.getFeatures()) {
                LOGGER.trace("Repo Feature: {}", feature);
                LOGGER.trace("Repo Feature name/version: {}/{}", feature.getName(), feature.getVersion());
                LOGGER.trace("Dependencies: ");
                for (Dependency depFeature : feature.getDependencies()) {
                    LOGGER.trace("Dependency Feature: {}", depFeature);
                    LOGGER.trace("Dependency Feature name/version: {}/{}", depFeature.getName(), depFeature.getVersion());
                }
            }
        }
    }
    if (null == notInstalledFeatures) {
        notInstalledFeatures = new HashSet<Feature>();
    }
    if (null == inactiveBundles) {
        inactiveBundles = new HashSet<BundleInfo>();
    }
    Set<String> installedBundleLocations = new HashSet<String>();
    for (BundleInfo bundleInfo : inactiveBundles) {
        installedBundleLocations.add(bundleInfo.getLocation());
    }
    FeaturesService featuresService = mock(FeaturesService.class);
    Set<Feature> featuresSet = new HashSet<Feature>();
    BundleRevision mockBundleRevision = mock(BundleRevision.class);
    when(mockBundleRevision.getTypes()).thenReturn(0);
    for (Repository curRepo : repos) {
        for (Feature curFeature : curRepo.getFeatures()) {
            featuresSet.add(curFeature);
            when(featuresService.getFeature(curFeature.getName())).thenReturn(curFeature);
            when(featuresService.getFeature(curFeature.getName(), curFeature.getVersion())).thenReturn(curFeature);
            // TODO: File Karaf bug that necessitates this, then reference
            // it here.
            when(featuresService.getFeature(curFeature.getName(), "0.0.0")).thenReturn(curFeature);
            when(featuresService.isInstalled(curFeature)).thenReturn(!notInstalledFeatures.contains(curFeature));
            // of that bundle, this logic will need to be modified.
            for (BundleInfo bundleInfo : curFeature.getBundles()) {
                if (installedBundleLocations.contains(bundleInfo.getLocation())) {
                    Bundle mockInstalledBundle = mock(Bundle.class);
                    when(mockInstalledBundle.getState()).thenReturn(Bundle.INSTALLED);
                    when(mockInstalledBundle.adapt(BundleRevision.class)).thenReturn(mockBundleRevision);
                    when(bundleContext.getBundle(bundleInfo.getLocation())).thenReturn(mockInstalledBundle);
                    when(bundleStateServices.get(0).getState(mockInstalledBundle)).thenReturn(BundleState.Installed);
                } else {
                    Bundle mockActiveBundle = mock(Bundle.class);
                    when(mockActiveBundle.getState()).thenReturn(Bundle.ACTIVE);
                    when(mockActiveBundle.adapt(BundleRevision.class)).thenReturn(mockBundleRevision);
                    when(bundleContext.getBundle(bundleInfo.getLocation())).thenReturn(mockActiveBundle);
                    when(bundleStateServices.get(0).getState(mockActiveBundle)).thenReturn(BundleState.Active);
                }
            }
        }
    }
    when(featuresService.listRepositories()).thenReturn(repos.toArray(new Repository[repos.size()]));
    when(featuresService.listFeatures()).thenReturn(featuresSet.toArray(new Feature[] {}));
    return featuresService;
}
Also used : Bundle(org.osgi.framework.Bundle) Dependency(org.apache.karaf.features.Dependency) Mockito.anyString(org.mockito.Mockito.anyString) Feature(org.apache.karaf.features.Feature) Repository(org.apache.karaf.features.Repository) BundleInfo(org.apache.karaf.features.BundleInfo) BundleRevision(org.osgi.framework.wiring.BundleRevision) FeaturesService(org.apache.karaf.features.FeaturesService) HashSet(java.util.HashSet)

Example 19 with BundleInfo

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

the class ApplicationServiceImplTest method testGetApplicationStatusReturnsFailedStatusWhenFeatureDependencyContainsInactiveBundle.

/**
     * Test method for
     * {@link ApplicationService#getApplicationStatus(Application)}
     * <p>
     * Verifies method returns an {@link ApplicationState#FAILED } state for an
     * {@code Application} under the following conditions:
     * <p>
     * <ul>
     * <li>Main feature is installed</li>
     * <li>All dependency features are installed</li>
     * <li>One dependency feature contains a Bundle that is in an inactive state
     * </li>
     * </ul>
     *
     * @throws Exception
     */
@Test
public void testGetApplicationStatusReturnsFailedStatusWhenFeatureDependencyContainsInactiveBundle() throws Exception {
    // Verify the pre-conditions
    int bundleInclusionCount = 0;
    for (Feature feature : mainFeatureRepo2.getFeatures()) {
        for (BundleInfo bundleInfo : feature.getBundles()) {
            if (bundleInfo.getLocation().equals(TEST_MAIN_FEATURES_2_UNIQUE_BUNDLE_LOCATION)) {
                ++bundleInclusionCount;
            }
        }
    }
    assertEquals("Bundle is not included in repository the expected number of times", 1, bundleInclusionCount);
    // Execute test
    Set<String> inactiveBundleNames = new HashSet<String>();
    inactiveBundleNames.add(TEST_MAIN_FEATURES_2_UNIQUE_BUNDLE_LOCATION);
    FeaturesService featuresService = createMockFeaturesService(mainFeatureRepo2, null, inactiveBundleNames);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationService appService = createPermittedApplicationServiceImpl();
    assertEquals(ApplicationService.class.getName() + " does not contain the expected number of Applications", 1, appService.getApplications().size());
    assertEquals(mainFeatureRepo2.getName() + " returned unexpected state", ApplicationState.FAILED, appService.getApplicationStatus(appService.getApplications().toArray(new Application[] {})[0]).getState());
}
Also used : BundleInfo(org.apache.karaf.features.BundleInfo) FeaturesService(org.apache.karaf.features.FeaturesService) Mockito.anyString(org.mockito.Mockito.anyString) Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Aggregations

BundleInfo (org.apache.karaf.features.BundleInfo)19 HashSet (java.util.HashSet)11 Feature (org.apache.karaf.features.Feature)9 Bundle (org.osgi.framework.Bundle)8 HashMap (java.util.HashMap)7 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 Conditional (org.apache.karaf.features.Conditional)4 FeaturesService (org.apache.karaf.features.FeaturesService)4 Repository (org.apache.karaf.features.Repository)4 Dependency (org.apache.karaf.features.Dependency)3 Resource (org.osgi.resource.Resource)3 List (java.util.List)2 Mockito.anyString (org.mockito.Mockito.anyString)2 BundleRevision (org.osgi.framework.wiring.BundleRevision)2 MetaTypeInformation (org.osgi.service.metatype.MetaTypeInformation)2 MetaTypeService (org.osgi.service.metatype.MetaTypeService)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1