Search in sources :

Example 1 with BundleService

use of org.apache.karaf.bundle.core.BundleService in project karaf by apache.

the class JavaSecurityTest method testJavaSecurity.

@Test
public void testJavaSecurity() throws Exception {
    assertNotNull("Karaf should run under a security manager", System.getSecurityManager());
    BundleService service = getOsgiService(BundleService.class);
    long tried = 0;
    while (true) {
        Map<Bundle, BundleState> incorrect = new HashMap<>();
        for (Bundle bundle : bundleContext.getBundles()) {
            BundleInfo info = service.getInfo(bundle);
            BundleState state = info.getState();
            if (state != BundleState.Active && state != BundleState.Resolved) {
                incorrect.put(bundle, state);
            }
        }
        if (incorrect.isEmpty()) {
            break;
        } else {
            if (++tried >= 10) {
                fail("Unable to start bundles correctly: " + incorrect);
            }
            Thread.sleep(100);
        }
    }
}
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) Test(org.junit.Test)

Example 2 with BundleService

use of org.apache.karaf.bundle.core.BundleService 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 3 with BundleService

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

the class TestFederation method testCswDurableSubscription.

@Test
public void testCswDurableSubscription() throws Exception {
    whenHttp(server).match(Condition.post(SUBSCRIBER)).then(success());
    whenHttp(server).match(Condition.get(SUBSCRIBER)).then(success());
    whenHttp(server).match(Condition.delete(SUBSCRIBER)).then(success());
    whenHttp(server).match(Condition.put(SUBSCRIBER)).then(success());
    String wildcardQuery = getCswSubscription("xml", "*", RESTITO_STUB_SERVER.getUrl());
    // CswSubscribe
    String subscriptionId = given().contentType(ContentType.XML).body(wildcardQuery).when().post(CSW_SUBSCRIPTION_PATH.getUrl()).then().assertThat().body(hasXPath("/Acknowledgement/RequestId")).extract().body().xmlPath().get("Acknowledgement.RequestId").toString();
    BundleService bundleService = getServiceManager().getService(BundleService.class);
    Bundle bundle = bundleService.getBundle("spatial-csw-endpoint");
    bundle.stop();
    await("Waiting for bundle to resolve").atMost(1, MINUTES).pollDelay(1, SECONDS).until(() -> bundle.getState() == Bundle.RESOLVED);
    bundle.start();
    await("Waiting for bundle to start").atMost(1, MINUTES).pollDelay(1, SECONDS).until(() -> bundle.getState() == Bundle.ACTIVE);
    getServiceManager().waitForHttpEndpoint(CSW_SUBSCRIPTION_PATH + "?_wadl");
    // get subscription
    given().contentType(ContentType.XML).when().get(CSW_SUBSCRIPTION_PATH.getUrl() + "/" + subscriptionId).then().assertThat().body(hasXPath("/Acknowledgement/RequestId"));
    String metacardId = ingest(getFileContent(JSON_RECORD_RESOURCE_PATH + "/SimpleGeoJsonRecord"), "application/json");
    String[] subscrptionIds = { subscriptionId };
    verifyEvents(Collections.singleton(metacardId), new HashSet<>(0), new HashSet<>(Arrays.asList(subscrptionIds)));
    given().contentType(ContentType.XML).when().delete(CSW_SUBSCRIPTION_PATH.getUrl() + "/" + subscriptionId).then().assertThat().body(hasXPath("/Acknowledgement/RequestId"));
    given().contentType(ContentType.XML).when().get(CSW_SUBSCRIPTION_PATH.getUrl() + "/" + subscriptionId).then().assertThat().statusCode(404);
}
Also used : CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) Bundle(org.osgi.framework.Bundle) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BundleService(org.apache.karaf.bundle.core.BundleService) Test(org.junit.Test) AbstractIntegrationTest(org.codice.ddf.itests.common.AbstractIntegrationTest)

Aggregations

BundleService (org.apache.karaf.bundle.core.BundleService)3 Bundle (org.osgi.framework.Bundle)3 BundleInfo (org.apache.karaf.bundle.core.BundleInfo)2 BundleState (org.apache.karaf.bundle.core.BundleState)2 Test (org.junit.Test)2 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 CoreOptions.mavenBundle (org.ops4j.pax.exam.CoreOptions.mavenBundle)1