Skip to content

Commit 5ec05e2

Browse files
committed
SWS-613 - Jaxp13XPathTemplate uses thread-unsafe XPathFactory as field
1 parent 3556ee7 commit 5ec05e2

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

core/src/main/java/org/springframework/ws/server/endpoint/adapter/XPathParamAnnotationMethodEndpointAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import org.w3c.dom.NodeList;
4242

4343
/**
44-
* Adapter that supports endpoint methods that use marshalling. Supports methods with the following signature:
44+
* Adapter that supports endpoint methods that use XPath expressions. Supports methods with the following signature:
4545
* <pre>
4646
* void handleMyMessage(@XPathParam("/root/child/text")String param);
4747
* </pre>
@@ -83,7 +83,7 @@ protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
8383
}
8484
Class<?>[] parameterTypes = method.getParameterTypes();
8585
for (int i = 0; i < parameterTypes.length; i++) {
86-
if (getXPathParamAnnotation(method, i) == null || !isSuportedType(parameterTypes[i])) {
86+
if (getXPathParamAnnotation(method, i) == null || !isSupportedType(parameterTypes[i])) {
8787
return false;
8888
}
8989
}
@@ -100,7 +100,7 @@ private XPathParam getXPathParamAnnotation(Method method, int paramIdx) {
100100
return null;
101101
}
102102

103-
private boolean isSuportedType(Class<?> clazz) {
103+
private boolean isSupportedType(Class<?> clazz) {
104104
return Boolean.class.isAssignableFrom(clazz) || Boolean.TYPE.isAssignableFrom(clazz) ||
105105
Double.class.isAssignableFrom(clazz) || Double.TYPE.isAssignableFrom(clazz) ||
106106
Node.class.isAssignableFrom(clazz) || NodeList.class.isAssignableFrom(clazz) ||
@@ -151,7 +151,7 @@ else if (String.class.isAssignableFrom(parameterTypes[i])) {
151151
return args;
152152
}
153153

154-
private XPath createXPath() {
154+
private synchronized XPath createXPath() {
155155
XPath xpath = xpathFactory.newXPath();
156156
if (namespaces != null) {
157157
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();

xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathExpressionFactory.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@
4040
*/
4141
abstract class Jaxp13XPathExpressionFactory {
4242

43-
private static XPathFactory xpathFactory;
44-
45-
static {
46-
xpathFactory = XPathFactory.newInstance();
47-
}
43+
private static XPathFactory xpathFactory = XPathFactory.newInstance();
4844

4945
/**
5046
* Creates a JAXP 1.3 <code>XPathExpression</code> from the given string expression.
@@ -55,7 +51,7 @@ abstract class Jaxp13XPathExpressionFactory {
5551
*/
5652
static XPathExpression createXPathExpression(String expression) {
5753
try {
58-
XPath xpath = xpathFactory.newXPath();
54+
XPath xpath = createXPath();
5955
javax.xml.xpath.XPathExpression xpathExpression = xpath.compile(expression);
6056
return new Jaxp13XPathExpression(xpathExpression);
6157
}
@@ -75,7 +71,7 @@ static XPathExpression createXPathExpression(String expression) {
7571
*/
7672
public static XPathExpression createXPathExpression(String expression, Map<String, String> namespaces) {
7773
try {
78-
XPath xpath = xpathFactory.newXPath();
74+
XPath xpath = createXPath();
7975
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
8076
namespaceContext.setBindings(namespaces);
8177
xpath.setNamespaceContext(namespaceContext);
@@ -88,6 +84,11 @@ public static XPathExpression createXPathExpression(String expression, Map<Strin
8884
}
8985
}
9086

87+
private static synchronized XPath createXPath() {
88+
return xpathFactory.newXPath();
89+
}
90+
91+
9192
/** JAXP 1.3 implementation of the <code>XPathExpression</code> interface. */
9293
private static class Jaxp13XPathExpression implements XPathExpression {
9394

xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathTemplate.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public <T> List<T> evaluate(String expression, Source context, NodeMapper<T> nod
121121
}
122122

123123
private Object evaluate(String expression, Source context, QName returnType) throws XPathException {
124-
XPath xpath = xpathFactory.newXPath();
124+
XPath xpath = createXPath();
125125
if (getNamespaces() != null && !getNamespaces().isEmpty()) {
126126
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
127127
namespaceContext.setBindings(getNamespaces());
@@ -166,4 +166,9 @@ else if (streamSource.getReader() != null) {
166166
}
167167
}
168168

169+
private synchronized XPath createXPath() {
170+
return xpathFactory.newXPath();
171+
}
172+
173+
169174
}

0 commit comments

Comments
 (0)