Skip to content
Open
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 @@ -45,7 +45,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

/**
Expand All @@ -68,11 +68,7 @@ public class XSLTResult implements Result {
/**
* Cache of all templates.
*/
private static final Map<String, Templates> templatesCache;

static {
templatesCache = new HashMap<>();
}
private static final Map<String, Templates> templatesCache = new ConcurrentHashMap<>();

// Configurable Parameters

Expand Down Expand Up @@ -283,19 +279,23 @@ protected Templates getTemplates(final String path) throws TransformerException,

if (noCache || (templates == null)) {
synchronized (templatesCache) {
URL resource = ServletActionContext.getServletContext().getResource(path);

if (resource == null) {
throw new TransformerException("Stylesheet " + path + " not found in resources.");
// Re-check after acquiring lock to avoid redundant compilation
templates = templatesCache.get(path);
if (noCache || (templates == null)) {
URL resource = ServletActionContext.getServletContext().getResource(path);

if (resource == null) {
throw new TransformerException("Stylesheet " + path + " not found in resources.");
}

LOG.debug("Preparing XSLT stylesheet templates: {}", path);

TransformerFactory factory = createTransformerFactory();
factory.setURIResolver(getURIResolver());
factory.setErrorListener(buildErrorListener());
templates = factory.newTemplates(new StreamSource(resource.openStream()));
templatesCache.put(path, templates);
}

LOG.debug("Preparing XSLT stylesheet templates: {}", path);

TransformerFactory factory = createTransformerFactory();
factory.setURIResolver(getURIResolver());
factory.setErrorListener(buildErrorListener());
templates = factory.newTemplates(new StreamSource(resource.openStream()));
templatesCache.put(path, templates);
}
}

Expand Down