Search in sources :

Example 1 with ParserContext

use of org.apache.aries.blueprint.ParserContext in project aries by apache.

the class NamespaceHandlerRegistryImpl method wrapIfNeeded.

/**
     * Wrap the handler if needed to fix its behavior.
     * When asked for a schema location, some simple handlers always return
     * the same url, whatever the asked location is.  This can lead to lots
     * of problems, so we need to verify and fix those behaviors.
     */
private static NamespaceHandler wrapIfNeeded(final NamespaceHandler handler) {
    URL result = null;
    try {
        result = handler.getSchemaLocation("");
    } catch (Throwable t) {
    // Ignore
    }
    if (result != null) {
        LOGGER.warn("NamespaceHandler " + handler.getClass().getName() + " is behaving badly and should be fixed");
        final URL res = result;
        return new NamespaceHandler() {

            final ConcurrentMap<String, Boolean> cache = new ConcurrentHashMap<String, Boolean>();

            @Override
            public URL getSchemaLocation(String s) {
                URL url = handler.getSchemaLocation(s);
                if (url != null && url.equals(res)) {
                    Boolean v, newValue;
                    Boolean valid = ((v = cache.get(s)) == null && (newValue = isValidSchema(s, url)) != null && (v = cache.putIfAbsent(s, newValue)) == null) ? newValue : v;
                    return valid ? url : null;
                }
                return url;
            }

            @Override
            public Set<Class> getManagedClasses() {
                return handler.getManagedClasses();
            }

            @Override
            public Metadata parse(Element element, ParserContext parserContext) {
                return handler.parse(element, parserContext);
            }

            @Override
            public ComponentMetadata decorate(Node node, ComponentMetadata componentMetadata, ParserContext parserContext) {
                return handler.decorate(node, componentMetadata, parserContext);
            }

            private boolean isValidSchema(String ns, URL url) {
                try {
                    InputStream is = url.openStream();
                    try {
                        XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is);
                        try {
                            reader.nextTag();
                            String nsuri = reader.getNamespaceURI();
                            String name = reader.getLocalName();
                            if ("http://www.w3.org/2001/XMLSchema".equals(nsuri) && "schema".equals(name)) {
                                String target = reader.getAttributeValue(null, "targetNamespace");
                                if (ns.equals(target)) {
                                    return true;
                                }
                            }
                        } finally {
                            reader.close();
                        }
                    } finally {
                        is.close();
                    }
                } catch (Throwable t) {
                // Ignore
                }
                return false;
            }
        };
    } else {
        return handler;
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ConcurrentMap(java.util.concurrent.ConcurrentMap) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) URL(java.net.URL) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) ParserContext(org.apache.aries.blueprint.ParserContext)

Example 2 with ParserContext

use of org.apache.aries.blueprint.ParserContext in project aries by apache.

the class BlueprintNamespaceHandler method createSpringParserContext.

private org.springframework.beans.factory.xml.ParserContext createSpringParserContext(ParserContext parserContext, DefaultListableBeanFactory registry) {
    try {
        XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(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);
        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) ProblemReporter(org.springframework.beans.factory.parsing.ProblemReporter) FailFastProblemReporter(org.springframework.beans.factory.parsing.FailFastProblemReporter) NamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver) BeanDefinitionParserDelegate(org.springframework.beans.factory.xml.BeanDefinitionParserDelegate) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) 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) XmlReaderContext(org.springframework.beans.factory.xml.XmlReaderContext) FailFastProblemReporter(org.springframework.beans.factory.parsing.FailFastProblemReporter) UrlResource(org.springframework.core.io.UrlResource) 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)

Aggregations

ParserContext (org.apache.aries.blueprint.ParserContext)2 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)1 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)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 BeanDefinitionParserDelegate (org.springframework.beans.factory.xml.BeanDefinitionParserDelegate)1 NamespaceHandlerResolver (org.springframework.beans.factory.xml.NamespaceHandlerResolver)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1 XmlReaderContext (org.springframework.beans.factory.xml.XmlReaderContext)1 Resource (org.springframework.core.io.Resource)1 UrlResource (org.springframework.core.io.UrlResource)1 Element (org.w3c.dom.Element)1