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;
}
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);
}
}
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;
}
}
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));
}
}
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);
}
}
Aggregations