Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
Expand Down Expand Up @@ -135,10 +135,15 @@ public void setSystemId(String systemId) {
}
}

/** Hashmap to hold the entities. */
private Map<String, byte[]> cachedEntities = new HashMap<>();
/** Thread-safe cache of resolved entities keyed by systemId. */
private Map<String, byte[]> cachedEntities = new ConcurrentHashMap<>();

private ProblemHandler handler;
/**
* Per-thread problem handler so that concurrent callers do not overwrite
* each other's handler when this resolver is shared (e.g. as an
* {@link XMLCatalogResolver} on the singleton {@code LabelValidator}).
*/
private final ThreadLocal<ProblemHandler> handlerRef = new ThreadLocal<>();

/**
* Constructor.
Expand All @@ -150,11 +155,11 @@ public CachedLSResourceResolver() {
/**
* Constructor.
*
* @param container A container to hold messages.
* @param handler A handler to receive problems during resource resolution.
*/
public CachedLSResourceResolver(ProblemHandler handler) {
cachedEntities = new HashMap<>();
this.handler = handler;
cachedEntities = new ConcurrentHashMap<>();
this.handlerRef.set(handler);
}

@Override
Expand All @@ -163,8 +168,8 @@ public LSInput resolveResource(String type, String namespaceURI, String publicId
if (systemId == null) {
return null;
}
byte[] entity = cachedEntities.get(systemId);
LSInputImpl input = new LSInputImpl();
byte[] entity = cachedEntities.get(systemId);
if (entity == null) {
InputStream in = null;
URLConnection conn = null;
Expand Down Expand Up @@ -194,16 +199,17 @@ public LSInput resolveResource(String type, String namespaceURI, String publicId
conn = url.openConnection();
in = Utility.openConnection(conn);
entity = IOUtils.toByteArray(in);
cachedEntities.put(systemId, entity);
cachedEntities.putIfAbsent(systemId, entity);
} catch (Exception e) {
if (handler != null) {
ProblemHandler h = handlerRef.get();
if (h != null) {
URL u = null;
try {
u = new URL(systemId);
} catch (MalformedURLException mu) {
u = url;
}
handler.addProblem(new ValidationProblem(new ProblemDefinition(ExceptionType.FATAL,
h.addProblem(new ValidationProblem(new ProblemDefinition(ExceptionType.FATAL,
ProblemType.LABEL_UNRESOLVABLE_RESOURCE, e.getMessage()), u));
} else {
e.printStackTrace();
Expand Down Expand Up @@ -233,10 +239,10 @@ public void addCachedEntities(Map<String, byte[]> entities) {
}

public void setProblemHandler(ProblemHandler handler) {
this.handler = handler;
this.handlerRef.set(handler);
}

public ProblemHandler getProblemHandler() {
return this.handler;
return this.handlerRef.get();
}
}
Loading