Search in sources :

Example 1 with BlueprintListener

use of org.osgi.service.blueprint.container.BlueprintListener in project camel by apache.

the class CamelBlueprintHelper method waitForBlueprintContainer.

/**
     * Synchronization method to wait for particular state of BlueprintContainer under test.
     */
public static void waitForBlueprintContainer(final Set<Long> eventHistory, BundleContext context, final String symbolicName, final int bpEvent, final Runnable runAndWait) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final Throwable[] pThrowable = new Throwable[] { null };
    ServiceRegistration<BlueprintListener> registration = context.registerService(BlueprintListener.class, new BlueprintListener() {

        @Override
        public void blueprintEvent(BlueprintEvent event) {
            if (event.getBundle().getSymbolicName().equals(symbolicName)) {
                if (event.getType() == bpEvent) {
                    // it works with BP container reloads if next CREATE state is at least 1ms after previous one
                    if (eventHistory == null || eventHistory.add(event.getTimestamp())) {
                        latch.countDown();
                    }
                } else if (event.getType() == BlueprintEvent.FAILURE) {
                    // we didn't wait for FAILURE, but we got it - fail fast then
                    pThrowable[0] = event.getCause();
                    latch.countDown();
                }
            }
        }
    }, null);
    if (runAndWait != null) {
        runAndWait.run();
    }
    boolean found = latch.await(CamelBlueprintHelper.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    registration.unregister();
    if (!found) {
        throw new RuntimeException("Gave up waiting for BlueprintContainer from bundle \"" + symbolicName + "\"");
    }
    if (pThrowable[0] != null) {
        throw new RuntimeException(pThrowable[0].getMessage(), pThrowable[0]);
    }
}
Also used : BlueprintListener(org.osgi.service.blueprint.container.BlueprintListener) CountDownLatch(java.util.concurrent.CountDownLatch) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent)

Example 2 with BlueprintListener

use of org.osgi.service.blueprint.container.BlueprintListener in project aries by apache.

the class ParserServiceImportAndIncludeXSDsTest method waitForConfig.

private boolean waitForConfig() throws InterruptedException {
    final AtomicBoolean ready = new AtomicBoolean();
    @SuppressWarnings("rawtypes") ServiceRegistration reg = context().registerService(BlueprintListener.class, new BlueprintListener() {

        @Override
        public void blueprintEvent(BlueprintEvent event) {
            if (TEST_BUNDLE.equals(event.getBundle().getSymbolicName()) && BlueprintEvent.CREATED == event.getType()) {
                synchronized (ready) {
                    ready.set(true);
                    ready.notify();
                }
            }
        }
    }, null);
    try {
        synchronized (ready) {
            if (!ready.get()) {
                ready.wait(3000);
            }
        }
        return ready.get();
    } finally {
        reg.unregister();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlueprintListener(org.osgi.service.blueprint.container.BlueprintListener) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 3 with BlueprintListener

use of org.osgi.service.blueprint.container.BlueprintListener in project aries by apache.

the class ParserServiceImportCmAndIncorrectNamespaceHandlersTest method waitForConfig.

private void waitForConfig() throws InterruptedException {
    final CountDownLatch ready = new CountDownLatch(2);
    final AtomicBoolean failure = new AtomicBoolean(false);
    @SuppressWarnings("rawtypes") ServiceRegistration reg = context().registerService(BlueprintListener.class, new BlueprintListener() {

        @Override
        public void blueprintEvent(BlueprintEvent event) {
            if (NS_HANDLER_BUNDLE.equals(event.getBundle().getSymbolicName()) && BlueprintEvent.CREATED == event.getType()) {
                ready.countDown();
            } else if (TEST_BUNDLE.equals(event.getBundle().getSymbolicName()) && (BlueprintEvent.CREATED == event.getType() || BlueprintEvent.FAILURE == event.getType())) {
                ready.countDown();
                if (BlueprintEvent.FAILURE == event.getType()) {
                    failure.set(true);
                }
            }
        }
    }, null);
    try {
        assertTrue(ready.await(3000, TimeUnit.MILLISECONDS));
        assertFalse("org.apache.aries.blueprint.aries1503.test bundle should successfully start Blueprint container", failure.get());
    } finally {
        reg.unregister();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlueprintListener(org.osgi.service.blueprint.container.BlueprintListener) CountDownLatch(java.util.concurrent.CountDownLatch) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 4 with BlueprintListener

use of org.osgi.service.blueprint.container.BlueprintListener in project aries by apache.

the class ParserServiceImportXSDsBetweenNamespaceHandlersTest method waitForConfig.

private void waitForConfig() throws InterruptedException {
    final AtomicBoolean ready = new AtomicBoolean();
    @SuppressWarnings("rawtypes") ServiceRegistration reg = context().registerService(BlueprintListener.class, new BlueprintListener() {

        @Override
        public void blueprintEvent(BlueprintEvent event) {
            if ("org.apache.aries.blueprint.aries1503b".equals(event.getBundle().getSymbolicName()) && BlueprintEvent.CREATED == event.getType()) {
                synchronized (ready) {
                    ready.set(true);
                    ready.notify();
                }
            }
        }
    }, null);
    try {
        synchronized (ready) {
            if (!ready.get()) {
                ready.wait(3000);
            }
        }
    } finally {
        reg.unregister();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlueprintListener(org.osgi.service.blueprint.container.BlueprintListener) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 5 with BlueprintListener

use of org.osgi.service.blueprint.container.BlueprintListener in project aries by apache.

the class BlueprintState method postRegister.

public void postRegister(Boolean registrationDone) {
    // reg listener
    if (registrationDone && registrations.incrementAndGet() == 1) {
        BlueprintListener listener = new BlueprintStateListener();
        eventDispatcher = Executors.newSingleThreadExecutor(new JMXThreadFactory("JMX OSGi Blueprint State Event Dispatcher"));
        listenerReg = context.registerService(BlueprintListener.class.getName(), listener, null);
    }
}
Also used : BlueprintListener(org.osgi.service.blueprint.container.BlueprintListener)

Aggregations

BlueprintListener (org.osgi.service.blueprint.container.BlueprintListener)5 BlueprintEvent (org.osgi.service.blueprint.container.BlueprintEvent)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ServiceRegistration (org.osgi.framework.ServiceRegistration)3 CountDownLatch (java.util.concurrent.CountDownLatch)2