Search in sources :

Example 1 with NamespaceHandler

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

the class SpringOsgiNamespaceHandler method parseInlinedTarget.

private Target parseInlinedTarget(ParserContext context, ComponentMetadata metadata, Element element) {
    Target listenerComponent;
    if (BLUEPRINT_NAMESPACE.equals(element.getNamespaceURI()) && BEAN_ELEMENT.equals(element.getLocalName())) {
        listenerComponent = context.parseElement(BeanMetadata.class, metadata, element);
    } else {
        NamespaceHandler handler = context.getNamespaceHandler(URI.create(element.getNamespaceURI()));
        if (handler == null) {
            throw new IllegalStateException("No NamespaceHandler found for " + element.getNamespaceURI());
        }
        Metadata md = handler.parse(element, context);
        if (!(md instanceof Target)) {
            throw new IllegalStateException("NamespaceHandler did not return a Target instance but " + md);
        }
        listenerComponent = (Target) md;
    }
    return listenerComponent;
}
Also used : Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) MutableReferenceMetadata(org.apache.aries.blueprint.mutable.MutableReferenceMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) MutableServiceMetadata(org.apache.aries.blueprint.mutable.MutableServiceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler)

Example 2 with NamespaceHandler

use of org.apache.aries.blueprint.NamespaceHandler 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 NamespaceHandler

use of org.apache.aries.blueprint.NamespaceHandler 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.newInstance().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 4 with NamespaceHandler

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

the class NamespaceHandlerRegistryImpl method cacheSchema.

private void cacheSchema(Map<URI, NamespaceHandler> handlers, Schema schema) {
    synchronized (schemas) {
        // Remove schemas that are fully included
        for (Iterator<Map<URI, NamespaceHandler>> iterator = schemas.keySet().iterator(); iterator.hasNext(); ) {
            Map<URI, NamespaceHandler> key = iterator.next();
            boolean found = true;
            for (URI uri : key.keySet()) {
                if (!key.get(uri).equals(handlers.get(uri))) {
                    found = false;
                    break;
                }
            }
            if (found) {
                iterator.remove();
                break;
            }
        }
        // Add our new schema
        schemas.put(handlers, new SoftReference<Schema>(schema));
    }
}
Also used : Schema(javax.xml.validation.Schema) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URI(java.net.URI) XML_NS_URI(javax.xml.XMLConstants.XML_NS_URI) W3C_XML_SCHEMA_NS_URI(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)

Example 5 with NamespaceHandler

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

the class NamespaceHandlerRegistryImpl method removedService.

public void removedService(ServiceReference reference, Object service) {
    try {
        LOGGER.debug("Removing NamespaceHandler " + reference.toString());
        NamespaceHandler handler = (NamespaceHandler) service;
        Map<String, Object> props = new HashMap<String, Object>();
        for (String name : reference.getPropertyKeys()) {
            props.put(name, reference.getProperty(name));
        }
        unregisterHandler(handler, props);
    } catch (Exception e) {
        LOGGER.warn("Error unregistering NamespaceHandler", e);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NamespaceHandler(org.apache.aries.blueprint.NamespaceHandler) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

NamespaceHandler (org.apache.aries.blueprint.NamespaceHandler)20 URI (java.net.URI)6 IOException (java.io.IOException)4 Bundle (org.osgi.framework.Bundle)4 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)3 SAXException (org.xml.sax.SAXException)3 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 W3C_XML_SCHEMA_NS_URI (javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)2 XML_NS_URI (javax.xml.XMLConstants.XML_NS_URI)2 ParserContext (org.apache.aries.blueprint.ParserContext)2 SimpleNamespaceHandlerSet (org.apache.aries.blueprint.container.SimpleNamespaceHandlerSet)2