Search in sources :

Example 6 with BlueprintEvent

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

the class BlueprintStateService method getDiag.

@Override
public String getDiag(Bundle bundle) {
    BlueprintEvent event = states.get(bundle.getBundleId());
    if (event == null) {
        return null;
    }
    if (event.getType() != BlueprintEvent.FAILURE && event.getType() != BlueprintEvent.GRACE_PERIOD && event.getType() != BlueprintEvent.WAITING) {
        return null;
    }
    StringBuilder message = new StringBuilder();
    Date date = new Date(event.getTimestamp());
    SimpleDateFormat df = new SimpleDateFormat();
    message.append(df.format(date) + "\n");
    if (event.getCause() != null) {
        message.append("Exception: \n");
        addMessages(message, event.getCause());
    }
    if (event.getDependencies() != null) {
        message.append("Missing dependencies: \n");
        for (String dep : event.getDependencies()) {
            message.append(dep + " ");
        }
        message.append("\n");
    }
    return message.toString();
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent) Date(java.util.Date)

Example 7 with BlueprintEvent

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

the class BlueprintStateService method getState.

@Override
public BundleState getState(Bundle bundle) {
    BlueprintEvent event = states.get(bundle.getBundleId());
    BundleState state = getState(event);
    return (bundle.getState() != Bundle.ACTIVE) ? BundleState.Unknown : state;
}
Also used : BundleState(org.apache.karaf.bundle.core.BundleState) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent)

Example 8 with BlueprintEvent

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

the class BlueprintContainerImpl method doRun.

/**
     * This method must be called inside a synchronized block to ensure this method is not run concurrently
     */
private void doRun() {
    try {
        for (; ; ) {
            if (destroyed.get()) {
                return;
            }
            if (bundle.getState() != Bundle.ACTIVE && bundle.getState() != Bundle.STARTING) {
                return;
            }
            if (bundle.getBundleContext() != bundleContext) {
                return;
            }
            LOGGER.debug("Running blueprint container for bundle {}/{} in state {}", getBundle().getSymbolicName(), getBundle().getVersion(), state);
            switch(state) {
                case Unknown:
                    readDirectives();
                    eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.CREATING, getBundle(), getExtenderBundle()));
                    parser = new Parser();
                    parser.parse(pathList);
                    namespaces = parser.getNamespaces();
                    if (additionalNamespaces != null) {
                        namespaces.addAll(additionalNamespaces);
                    }
                    handlerSet = handlers.getNamespaceHandlers(namespaces, getBundle());
                    handlerSet.addListener(this);
                    state = State.WaitForNamespaceHandlers;
                    break;
                case WaitForNamespaceHandlers:
                    {
                        List<String> missing = new ArrayList<String>();
                        List<URI> missingURIs = new ArrayList<URI>();
                        for (URI ns : handlerSet.getNamespaces()) {
                            if (handlerSet.getNamespaceHandler(ns) == null) {
                                missing.add("(&(" + Constants.OBJECTCLASS + "=" + NamespaceHandler.class.getName() + ")(" + NamespaceHandlerRegistryImpl.NAMESPACE + "=" + ns + "))");
                                missingURIs.add(ns);
                            }
                        }
                        if (missing.size() > 0) {
                            LOGGER.info("Bundle {}/{} is waiting for namespace handlers {}", getBundle().getSymbolicName(), getBundle().getVersion(), missingURIs);
                            eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.GRACE_PERIOD, getBundle(), getExtenderBundle(), missing.toArray(new String[missing.size()])));
                            return;
                        }
                        resetComponentDefinitionRegistry();
                        if (xmlValidation == null || "true".equals(xmlValidation)) {
                            for (URI ns : handlerSet.getNamespaces()) {
                                NamespaceHandler handler = handlerSet.getNamespaceHandler(ns);
                                if (handler instanceof NamespaceHandler2) {
                                    if (((NamespaceHandler2) handler).usePsvi()) {
                                        xmlValidation = "psvi";
                                        break;
                                    }
                                }
                            }
                        }
                        try {
                            if (xmlValidation == null || "true".equals(xmlValidation)) {
                                parser.validate(handlerSet.getSchema(parser.getSchemaLocations()));
                            } else if ("structure".equals(xmlValidation)) {
                                parser.validate(handlerSet.getSchema(parser.getSchemaLocations()), new ValidationHandler());
                            } else if ("psvi".equals(xmlValidation)) {
                                parser.validatePsvi(handlerSet.getSchema(parser.getSchemaLocations()));
                            }
                            parser.populate(handlerSet, componentDefinitionRegistry);
                            state = State.Populated;
                        } catch (MissingNamespaceException e) {
                            // If we found a missing namespace when parsing the schema,
                            // we remain in the current state
                            handlerSet.getNamespaces().add(e.getNamespace());
                        }
                        break;
                    }
                case Populated:
                    getRepository();
                    trackServiceReferences();
                    Runnable r = new Runnable() {

                        public void run() {
                            synchronized (scheduled) {
                                if (destroyed.get()) {
                                    return;
                                }
                                String[] missingDependecies = getMissingDependencies();
                                if (missingDependecies.length == 0) {
                                    return;
                                }
                                Throwable t = new TimeoutException();
                                state = State.Failed;
                                tidyupComponents();
                                LOGGER.error("Unable to start blueprint container for bundle {}/{} due to unresolved dependencies {}", getBundle().getSymbolicName(), getBundle().getVersion(), Arrays.asList(missingDependecies), t);
                                eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.FAILURE, getBundle(), getExtenderBundle(), missingDependecies, t));
                            }
                        }
                    };
                    timeoutFuture = timer.schedule(r, timeout, TimeUnit.MILLISECONDS);
                    state = State.WaitForInitialReferences;
                    break;
                case WaitForInitialReferences:
                    if (waitForDependencies) {
                        String[] missingDependencies = getMissingDependencies();
                        if (missingDependencies.length > 0) {
                            LOGGER.info("Bundle {}/{} is waiting for dependencies {}", getBundle().getSymbolicName(), getBundle().getVersion(), Arrays.asList(missingDependencies));
                            eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.GRACE_PERIOD, getBundle(), getExtenderBundle(), missingDependencies));
                            return;
                        }
                    }
                    state = State.InitialReferencesSatisfied;
                    break;
                case InitialReferencesSatisfied:
                    processTypeConverters();
                    processProcessors();
                    state = State.WaitForInitialReferences2;
                    break;
                case WaitForInitialReferences2:
                    if (waitForDependencies) {
                        String[] missingDependencies = getMissingDependencies();
                        if (missingDependencies.length > 0) {
                            LOGGER.info("Bundle {}/{} is waiting for dependencies {}", getBundle().getSymbolicName(), getBundle().getVersion(), Arrays.asList(missingDependencies));
                            eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.GRACE_PERIOD, getBundle(), getExtenderBundle(), missingDependencies));
                            return;
                        }
                    }
                    state = State.Create;
                    break;
                case Create:
                    cancelFutureIfPresent();
                    instantiateEagerComponents();
                    //Register the services after the eager components are ready, as per 121.6
                    registerServices();
                    // Register the BlueprintContainer in the OSGi registry
                    int bs = bundle.getState();
                    if (registration == null && (bs == Bundle.ACTIVE || bs == Bundle.STARTING)) {
                        Properties props = new Properties();
                        props.put(BlueprintConstants.CONTAINER_SYMBOLIC_NAME_PROPERTY, bundle.getSymbolicName());
                        props.put(BlueprintConstants.CONTAINER_VERSION_PROPERTY, JavaUtils.getBundleVersion(bundle));
                        registration = registerService(new String[] { BlueprintContainer.class.getName() }, this, props);
                    }
                    eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.CREATED, getBundle(), getExtenderBundle()));
                    state = State.Created;
                    break;
                case Created:
                case Failed:
                    return;
            }
        }
    } catch (Throwable t) {
        try {
            state = State.Failed;
            cancelFutureIfPresent();
            tidyupComponents();
            LOGGER.error("Unable to start blueprint container for bundle {}/{}", getBundle().getSymbolicName(), getBundle().getVersion(), t);
            eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.FAILURE, getBundle(), getExtenderBundle(), t));
        } catch (RuntimeException re) {
            LOGGER.debug("Tidying up components failed. ", re);
            throw re;
        }
    }
}
Also used : Properties(java.util.Properties) URI(java.net.URI) HeaderParser(org.apache.aries.blueprint.utils.HeaderParser) Parser(org.apache.aries.blueprint.parser.Parser) NamespaceHandler2(org.apache.aries.blueprint.NamespaceHandler2) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) List(java.util.List) ArrayList(java.util.ArrayList) MissingNamespaceException(org.apache.aries.blueprint.namespace.MissingNamespaceException) BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent) TimeoutException(java.util.concurrent.TimeoutException)

Example 9 with BlueprintEvent

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

the class BlueprintContainerImpl method quiesce.

protected void quiesce() {
    destroyed.set(true);
    eventDispatcher.blueprintEvent(new BlueprintEvent(BlueprintEvent.DESTROYING, getBundle(), getExtenderBundle()));
    cancelFutureIfPresent();
    ServiceUtil.safeUnregisterService(registration);
    if (handlerSet != null) {
        handlerSet.removeListener(this);
        handlerSet.destroy();
    }
    LOGGER.debug("Blueprint container {} quiesced", getBundle().getSymbolicName(), getBundle().getVersion());
}
Also used : BlueprintEvent(org.osgi.service.blueprint.container.BlueprintEvent)

Example 10 with BlueprintEvent

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

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