private void doRun()

in blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintContainerImpl.java [309:467]


    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 container for blueprint 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("Blueprint 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 container for blueprint 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("Blueprint 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("Blueprint 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);
                        }
                        LOGGER.info("Blueprint bundle {}/{} has been started", getBundle().getSymbolicName(), getBundle().getVersion());
                        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 container for blueprint 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;
            }
        }
    }