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