Search in sources :

Example 1 with Parser

use of org.apache.aries.blueprint.parser.Parser in project aries by apache.

the class BaseNameSpaceHandlerSetup method parseCDR.

protected ComponentDefinitionRegistry parseCDR(String name) throws Exception {
    Parser p = new Parser();
    URL bpxml = this.getClass().getResource(name);
    p.parse(Arrays.asList(bpxml));
    Set<URI> nsuris = p.getNamespaces();
    NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
    ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
    cdr.registerComponentDefinition(new PassThroughMetadataImpl("blueprintBundle", b));
    p.populate(nshandlers, cdr);
    return cdr;
}
Also used : NamespaceHandlerSet(org.apache.aries.blueprint.parser.NamespaceHandlerSet) ComponentDefinitionRegistry(org.apache.aries.blueprint.ComponentDefinitionRegistry) PassThroughMetadataImpl(org.apache.aries.blueprint.reflect.PassThroughMetadataImpl) URI(java.net.URI) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl) URL(java.net.URL) Parser(org.apache.aries.blueprint.parser.Parser)

Example 2 with Parser

use of org.apache.aries.blueprint.parser.Parser in project aries by apache.

the class AbstractBlueprintTest method parse.

protected ComponentDefinitionRegistryImpl parse(String name, NamespaceHandlerSet handlers) throws Exception {
    ComponentDefinitionRegistryImpl registry = new ComponentDefinitionRegistryImpl();
    Parser parser = new Parser();
    parser.parse(Collections.singletonList(getClass().getResource(name)));
    parser.populate(handlers, registry);
    return registry;
}
Also used : ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl) Parser(org.apache.aries.blueprint.parser.Parser)

Example 3 with Parser

use of org.apache.aries.blueprint.parser.Parser in project aries by apache.

the class BlueprintContainerImpl method init.

public void init(boolean validate) throws Exception {
    // Parse xml resources
    Parser parser = new Parser();
    parser.parse(getResources());
    // Check namespaces
    Set<URI> namespaces = parser.getNamespaces();
    // Create handler set
    NamespaceHandlerSet handlerSet = createNamespaceHandlerSet(namespaces);
    // Add predefined beans
    componentDefinitionRegistry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintContainer", this));
    if (validate) {
        // Validate
        parser.validate(handlerSet.getSchema());
    }
    // Populate
    parser.populate(handlerSet, componentDefinitionRegistry);
    // Create repository
    repository = new NoOsgiRecipeBuilder(this, tempRecipeIdSpace).createRepository();
    // Processors handling
    processTypeConverters();
    processProcessors();
    // Instantiate eager singletons
    instantiateEagerComponents();
}
Also used : NamespaceHandlerSet(org.apache.aries.blueprint.parser.NamespaceHandlerSet) PassThroughMetadataImpl(org.apache.aries.blueprint.reflect.PassThroughMetadataImpl) URI(java.net.URI) Parser(org.apache.aries.blueprint.parser.Parser)

Example 4 with Parser

use of org.apache.aries.blueprint.parser.Parser 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 5 with Parser

use of org.apache.aries.blueprint.parser.Parser in project aries by apache.

the class ParserServiceImpl method parse.

public ComponentDefinitionRegistry parse(List<URL> urls, Bundle clientBundle, boolean validate) throws Exception {
    Parser parser = new Parser(null, _ignoreUnknownNamespaceHandlers);
    parser.parse(urls);
    return validateAndPopulate(parser, clientBundle, validate);
}
Also used : Parser(org.apache.aries.blueprint.parser.Parser)

Aggregations

Parser (org.apache.aries.blueprint.parser.Parser)6 URI (java.net.URI)3 ComponentDefinitionRegistryImpl (org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl)2 NamespaceHandlerSet (org.apache.aries.blueprint.parser.NamespaceHandlerSet)2 PassThroughMetadataImpl (org.apache.aries.blueprint.reflect.PassThroughMetadataImpl)2 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1 TimeoutException (java.util.concurrent.TimeoutException)1 ComponentDefinitionRegistry (org.apache.aries.blueprint.ComponentDefinitionRegistry)1 NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)1 NamespaceHandler2 (org.apache.aries.blueprint.NamespaceHandler2)1 MissingNamespaceException (org.apache.aries.blueprint.namespace.MissingNamespaceException)1 HeaderParser (org.apache.aries.blueprint.utils.HeaderParser)1 BlueprintEvent (org.osgi.service.blueprint.container.BlueprintEvent)1