Search in sources :

Example 1 with NamespaceHandlerResolver

use of org.springframework.beans.factory.xml.NamespaceHandlerResolver in project wildfly-camel by wildfly-extras.

the class SpringCamelContextBootstrap method loadBeanDefinitions.

private void loadBeanDefinitions(Resource resource, ClassLoader classLoader) {
    applicationContext = new GenericApplicationContext();
    applicationContext.setClassLoader(classLoader);
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext) {

        @Override
        protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
            NamespaceHandlerResolver defaultResolver = super.createDefaultNamespaceHandlerResolver();
            return new SpringCamelContextBootstrap.CamelNamespaceHandlerResolver(defaultResolver);
        }
    };
    xmlReader.loadBeanDefinitions(resource);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) NamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader)

Example 2 with NamespaceHandlerResolver

use of org.springframework.beans.factory.xml.NamespaceHandlerResolver in project aries by apache.

the class BlueprintNamespaceHandler method createSpringParserContext.

private org.springframework.beans.factory.xml.ParserContext createSpringParserContext(final ParserContext parserContext, DefaultListableBeanFactory registry) {
    try {
        SpringVersionBridgeXmlBeanDefinitionReader xbdr = new SpringVersionBridgeXmlBeanDefinitionReader(registry);
        Resource resource = new UrlResource(parserContext.getSourceNode().getOwnerDocument().getDocumentURI());
        ProblemReporter problemReporter = new FailFastProblemReporter();
        ReaderEventListener listener = new EmptyReaderEventListener();
        SourceExtractor extractor = new NullSourceExtractor();
        NamespaceHandlerResolver resolver = new SpringNamespaceHandlerResolver(parserContext);
        xbdr.setProblemReporter(problemReporter);
        xbdr.setEventListener(listener);
        xbdr.setSourceExtractor(extractor);
        xbdr.setNamespaceHandlerResolver(resolver);
        xbdr.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws IOException {
                for (URI ns : parserContext.getNamespaces()) {
                    NamespaceHandler nh = parserContext.getNamespaceHandler(ns);
                    URL url = null;
                    if (publicId != null) {
                        url = nh.getSchemaLocation(publicId);
                    }
                    if (url == null && systemId != null) {
                        url = nh.getSchemaLocation(systemId);
                    }
                    if (url != null) {
                        InputSource is = new InputSource();
                        is.setPublicId(publicId);
                        is.setSystemId(systemId);
                        is.setByteStream(url.openStream());
                        return is;
                    }
                }
                return null;
            }
        });
        XmlReaderContext xmlReaderContext = xbdr.createReaderContext(resource);
        BeanDefinitionParserDelegate bdpd = new BeanDefinitionParserDelegate(xmlReaderContext);
        return new org.springframework.beans.factory.xml.ParserContext(xmlReaderContext, bdpd);
    } catch (Exception e) {
        throw new RuntimeException("Error creating spring parser context", e);
    }
}
Also used : EmptyReaderEventListener(org.springframework.beans.factory.parsing.EmptyReaderEventListener) InputSource(org.xml.sax.InputSource) NamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver) BeanDefinitionParserDelegate(org.springframework.beans.factory.xml.BeanDefinitionParserDelegate) XmlReaderContext(org.springframework.beans.factory.xml.XmlReaderContext) URI(java.net.URI) URL(java.net.URL) ProblemReporter(org.springframework.beans.factory.parsing.ProblemReporter) FailFastProblemReporter(org.springframework.beans.factory.parsing.FailFastProblemReporter) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) ReaderEventListener(org.springframework.beans.factory.parsing.ReaderEventListener) EmptyReaderEventListener(org.springframework.beans.factory.parsing.EmptyReaderEventListener) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) FailFastProblemReporter(org.springframework.beans.factory.parsing.FailFastProblemReporter) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) UrlResource(org.springframework.core.io.UrlResource) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) ParserContext(org.apache.aries.blueprint.ParserContext) NullSourceExtractor(org.springframework.beans.factory.parsing.NullSourceExtractor) SourceExtractor(org.springframework.beans.factory.parsing.SourceExtractor) NullSourceExtractor(org.springframework.beans.factory.parsing.NullSourceExtractor)

Example 3 with NamespaceHandlerResolver

use of org.springframework.beans.factory.xml.NamespaceHandlerResolver in project spring-security by spring-projects.

the class AuthenticationManagerBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext pc) {
    String id = element.getAttribute("id");
    if (!StringUtils.hasText(id)) {
        if (pc.getRegistry().containsBeanDefinition(BeanIds.AUTHENTICATION_MANAGER)) {
            pc.getReaderContext().warning("Overriding globally registered AuthenticationManager", pc.extractSource(element));
        }
        id = BeanIds.AUTHENTICATION_MANAGER;
    }
    pc.pushContainingComponent(new CompositeComponentDefinition(element.getTagName(), pc.extractSource(element)));
    BeanDefinitionBuilder providerManagerBldr = BeanDefinitionBuilder.rootBeanDefinition(ProviderManager.class);
    String alias = element.getAttribute(ATT_ALIAS);
    List<BeanMetadataElement> providers = new ManagedList<>();
    NamespaceHandlerResolver resolver = pc.getReaderContext().getNamespaceHandlerResolver();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node instanceof Element) {
            providers.add(extracted(element, pc, resolver, (Element) node));
        }
    }
    if (providers.isEmpty()) {
        providers.add(new RootBeanDefinition(NullAuthenticationProvider.class));
    }
    providerManagerBldr.addConstructorArgValue(providers);
    if ("false".equals(element.getAttribute(ATT_ERASE_CREDENTIALS))) {
        providerManagerBldr.addPropertyValue("eraseCredentialsAfterAuthentication", false);
    }
    if (!pc.getRegistry().containsBeanDefinition(AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME)) {
        // Add the default event publisher to the context
        BeanDefinition publisher = new RootBeanDefinition(DefaultAuthenticationEventPublisher.class);
        pc.registerBeanComponent(new BeanComponentDefinition(publisher, AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME));
    }
    providerManagerBldr.addPropertyReference("authenticationEventPublisher", AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME);
    pc.registerBeanComponent(new BeanComponentDefinition(providerManagerBldr.getBeanDefinition(), id));
    if (StringUtils.hasText(alias)) {
        pc.getRegistry().registerAlias(id, alias);
        pc.getReaderContext().fireAliasRegistered(id, alias, pc.extractSource(element));
    }
    if (!BeanIds.AUTHENTICATION_MANAGER.equals(id)) {
        pc.getRegistry().registerAlias(id, BeanIds.AUTHENTICATION_MANAGER);
        pc.getReaderContext().fireAliasRegistered(id, BeanIds.AUTHENTICATION_MANAGER, pc.extractSource(element));
    }
    pc.popAndRegisterContainingComponent();
    return null;
}
Also used : NamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver) CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Example 4 with NamespaceHandlerResolver

use of org.springframework.beans.factory.xml.NamespaceHandlerResolver in project spring-framework by spring-projects.

the class TestNamespaceHandler method setUp.

@BeforeEach
public void setUp() throws Exception {
    NamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(CLASS.getClassLoader(), NS_PROPS);
    this.beanFactory = new GenericApplicationContext();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
    reader.setNamespaceHandlerResolver(resolver);
    reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
    reader.setEntityResolver(new DummySchemaResolver());
    reader.loadBeanDefinitions(getResource());
    this.beanFactory.refresh();
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) NamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver) DefaultNamespaceHandlerResolver(org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultNamespaceHandlerResolver(org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

NamespaceHandlerResolver (org.springframework.beans.factory.xml.NamespaceHandlerResolver)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URL (java.net.URL)1 NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)1 ParserContext (org.apache.aries.blueprint.ParserContext)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)1 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)1 EmptyReaderEventListener (org.springframework.beans.factory.parsing.EmptyReaderEventListener)1 FailFastProblemReporter (org.springframework.beans.factory.parsing.FailFastProblemReporter)1 NullSourceExtractor (org.springframework.beans.factory.parsing.NullSourceExtractor)1 ProblemReporter (org.springframework.beans.factory.parsing.ProblemReporter)1 ReaderEventListener (org.springframework.beans.factory.parsing.ReaderEventListener)1 SourceExtractor (org.springframework.beans.factory.parsing.SourceExtractor)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1