Search in sources :

Example 6 with BundleState

use of org.apache.karaf.bundle.core.BundleState in project ddf by codice.

the class ServiceManagerImpl method waitForRequiredBundles.

@Override
public void waitForRequiredBundles(String symbolicNamePrefix) throws InterruptedException {
    boolean ready = false;
    if (bundleService == null) {
        bundleService = getService(BundleService.class);
    }
    long timeoutLimit = System.currentTimeMillis() + FEATURES_AND_BUNDLES_TIMEOUT;
    while (!ready) {
        List<Bundle> bundles = Arrays.asList(getBundleContext().getBundles());
        ready = true;
        for (Bundle bundle : bundles) {
            if (bundle.getSymbolicName().startsWith(symbolicNamePrefix)) {
                String bundleName = bundle.getHeaders().get(Constants.BUNDLE_NAME);
                BundleInfo bundleInfo = bundleService.getInfo(bundle);
                BundleState bundleState = bundleInfo.getState();
                if (bundleInfo.isFragment()) {
                    if (!BundleState.Resolved.equals(bundleState)) {
                        LOGGER.info("{} bundle not ready yet", bundleName);
                        ready = false;
                    }
                } else if (bundleState != null) {
                    if (BundleState.Failure.equals(bundleState)) {
                        printInactiveBundles();
                        fail("The bundle " + bundleName + " failed.");
                    } else if (!BundleState.Active.equals(bundleState)) {
                        LOGGER.info("{} bundle not ready with state {}", bundleName, bundleState);
                        ready = false;
                    }
                }
            }
        }
        if (!ready) {
            if (System.currentTimeMillis() > timeoutLimit) {
                printInactiveBundles();
                fail(String.format("Bundles and blueprint did not start within %d minutes.", TimeUnit.MILLISECONDS.toMinutes(FEATURES_AND_BUNDLES_TIMEOUT)));
            }
            LOGGER.info("Bundles not up, sleeping...");
            Thread.sleep(1000);
        }
    }
}
Also used : BundleInfo(org.apache.karaf.bundle.core.BundleInfo) Bundle(org.osgi.framework.Bundle) BundleState(org.apache.karaf.bundle.core.BundleState) BundleService(org.apache.karaf.bundle.core.BundleService)

Example 7 with BundleState

use of org.apache.karaf.bundle.core.BundleState 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)

Aggregations

BundleState (org.apache.karaf.bundle.core.BundleState)7 Bundle (org.osgi.framework.Bundle)3 BundleInfo (org.apache.karaf.bundle.core.BundleInfo)2 BundleService (org.apache.karaf.bundle.core.BundleService)2 BundleStateService (org.apache.karaf.bundle.core.BundleStateService)1 BundleInfo (org.apache.karaf.features.BundleInfo)1 Feature (org.apache.karaf.features.Feature)1 Test (org.junit.Test)1 BundleRevision (org.osgi.framework.wiring.BundleRevision)1 BlueprintEvent (org.osgi.service.blueprint.container.BlueprintEvent)1 OsgiBundleApplicationContextEvent (org.springframework.osgi.context.event.OsgiBundleApplicationContextEvent)1