Search in sources :

Example 1 with BlueprintEvent

use of org.osgi.service.blueprint.container.BlueprintEvent 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 BlueprintEvent

use of org.osgi.service.blueprint.container.BlueprintEvent 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 BlueprintEvent

use of org.osgi.service.blueprint.container.BlueprintEvent 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 BlueprintEvent

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

the class BlueprintExtender method getBlueprintPaths.

private List<URL> getBlueprintPaths(Bundle bundle) {
    LOGGER.debug("Scanning bundle {}/{} for blueprint application", bundle.getSymbolicName(), bundle.getVersion());
    try {
        List<URL> pathList = new ArrayList<URL>();
        String blueprintHeader = bundle.getHeaders().get(BlueprintConstants.BUNDLE_BLUEPRINT_HEADER);
        String blueprintHeaderAnnotation = bundle.getHeaders().get(BlueprintConstants.BUNDLE_BLUEPRINT_ANNOTATION_HEADER);
        if (blueprintHeader == null) {
            blueprintHeader = "OSGI-INF/blueprint/";
        }
        List<PathElement> paths = HeaderParser.parseHeader(blueprintHeader);
        for (PathElement path : paths) {
            String name = path.getName();
            if (name.endsWith("/")) {
                addEntries(bundle, name, "*.xml", pathList);
            } else {
                String baseName;
                String filePattern;
                int pos = name.lastIndexOf('/');
                if (pos < 0) {
                    baseName = "/";
                    filePattern = name;
                } else {
                    baseName = name.substring(0, pos + 1);
                    filePattern = name.substring(pos + 1);
                }
                addEntries(bundle, baseName, filePattern, pathList);
            }
        }
        // Check annotations
        if (blueprintHeaderAnnotation != null && blueprintHeaderAnnotation.trim().equalsIgnoreCase("true")) {
            LOGGER.debug("Scanning bundle {}/{} for blueprint annotations", bundle.getSymbolicName(), bundle.getVersion());
            ServiceReference sr = this.context.getServiceReference(BlueprintAnnotationScanner.class.getName());
            if (sr != null) {
                BlueprintAnnotationScanner bas = (BlueprintAnnotationScanner) this.context.getService(sr);
                try {
                    // try to generate the blueprint definition XML
                    URL url = bas.createBlueprintModel(bundle);
                    if (url != null) {
                        pathList.add(url);
                    }
                } finally {
                    this.context.ungetService(sr);
                }
            }
        }
        if (!pathList.isEmpty()) {
            LOGGER.debug("Found blueprint application in bundle {}/{} with paths: {}", bundle.getSymbolicName(), bundle.getVersion(), pathList);
            // ServiceReference, or just not do this check, which could be quite harmful.
            if (isCompatible(bundle)) {
                return pathList;
            } else {
                LOGGER.info("Bundle {}/{} is not compatible with this blueprint extender", bundle.getSymbolicName(), bundle.getVersion());
            }
        } else {
            LOGGER.debug("No blueprint application found in bundle {}/{}", bundle.getSymbolicName(), bundle.getVersion());
        }
    } catch (Throwable t) {
        if (!stopping) {
            LOGGER.warn("Error creating blueprint container for bundle {}/{}", bundle.getSymbolicName(), bundle.getVersion(), t);
            eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.FAILURE, bundle, context.getBundle(), t));
        }
    }
    return null;
}
Also used : BlueprintAnnotationScanner(org.apache.aries.blueprint.annotation.service.BlueprintAnnotationScanner) PathElement(org.apache.aries.blueprint.utils.HeaderParser.PathElement) ArrayList(java.util.ArrayList) URL(java.net.URL) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with BlueprintEvent

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

the class BlueprintContainerImpl method destroy.

public void destroy() {
    synchronized (scheduled) {
        destroyed.set(true);
    }
    cancelFutureIfPresent();
    eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.DESTROYING, getBundle(), getExtenderBundle()));
    executors.shutdownNow();
    if (handlerSet != null) {
        handlerSet.removeListener(this);
        handlerSet.destroy();
    }
    try {
        executors.awaitTermination(5 * 60, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        LOGGER.debug("Interrupted waiting for executor to shut down");
    }
    tidyupComponents();
    eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.DESTROYED, getBundle(), getExtenderBundle()));
    LOGGER.debug("Blueprint container {} destroyed", getBundle().getSymbolicName(), getBundle().getVersion());
}
Also used : BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent)

Aggregations

BlueprintEvent (org.osgi.service.blueprint.container.BlueprintEvent)10 BlueprintListener (org.osgi.service.blueprint.container.BlueprintListener)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ServiceRegistration (org.osgi.framework.ServiceRegistration)3 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 URI (java.net.URI)1 URL (java.net.URL)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 Properties (java.util.Properties)1 TimeoutException (java.util.concurrent.TimeoutException)1 NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)1 NamespaceHandler2 (org.apache.aries.blueprint.NamespaceHandler2)1 BlueprintAnnotationScanner (org.apache.aries.blueprint.annotation.service.BlueprintAnnotationScanner)1 MissingNamespaceException (org.apache.aries.blueprint.namespace.MissingNamespaceException)1 Parser (org.apache.aries.blueprint.parser.Parser)1 HeaderParser (org.apache.aries.blueprint.utils.HeaderParser)1 PathElement (org.apache.aries.blueprint.utils.HeaderParser.PathElement)1