SWS-613 - Jaxp13XPathTemplate uses thread-unsafe XPathFactory as field

This commit is contained in:
Arjen Poutsma
2010-05-12 09:09:20 +00:00
parent ed8a0d38b5
commit 7dfe4a32de
3 changed files with 20 additions and 13 deletions

View File

@@ -39,11 +39,7 @@ import org.w3c.dom.NodeList;
*/
abstract class Jaxp13XPathExpressionFactory {
private static XPathFactory xpathFactory;
static {
xpathFactory = XPathFactory.newInstance();
}
private static XPathFactory xpathFactory = XPathFactory.newInstance();
/**
* Creates a JAXP 1.3 <code>XPathExpression</code> from the given string expression.
@@ -54,7 +50,7 @@ abstract class Jaxp13XPathExpressionFactory {
*/
static XPathExpression createXPathExpression(String expression) {
try {
XPath xpath = xpathFactory.newXPath();
XPath xpath = createXPath();
javax.xml.xpath.XPathExpression xpathExpression = xpath.compile(expression);
return new Jaxp13XPathExpression(xpathExpression);
}
@@ -74,7 +70,7 @@ abstract class Jaxp13XPathExpressionFactory {
*/
public static XPathExpression createXPathExpression(String expression, Map namespaces) {
try {
XPath xpath = xpathFactory.newXPath();
XPath xpath = createXPath();
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
namespaceContext.setBindings(namespaces);
xpath.setNamespaceContext(namespaceContext);
@@ -87,6 +83,11 @@ abstract class Jaxp13XPathExpressionFactory {
}
}
private static synchronized XPath createXPath() {
return xpathFactory.newXPath();
}
/** JAXP 1.3 implementation of the <code>XPathExpression</code> interface. */
private static class Jaxp13XPathExpression implements XPathExpression {

View File

@@ -121,7 +121,7 @@ public class Jaxp13XPathTemplate extends AbstractXPathTemplate {
}
private Object evaluate(String expression, Source context, QName returnType) throws XPathException {
XPath xpath = xpathFactory.newXPath();
XPath xpath = createXPath();
if (getNamespaces() != null && !getNamespaces().isEmpty()) {
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
namespaceContext.setBindings(getNamespaces());
@@ -166,4 +166,9 @@ public class Jaxp13XPathTemplate extends AbstractXPathTemplate {
}
}
private synchronized XPath createXPath() {
return xpathFactory.newXPath();
}
}