in blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/namespace/NamespaceHandlerRegistryImpl.java [747:813]
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;
}
}