Skip to content

Commit 7dfe4a3

Browse files
committed
SWS-613 - Jaxp13XPathTemplate uses thread-unsafe XPathFactory as field
1 parent ed8a0d3 commit 7dfe4a3

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2007 the original author or authors.
2+
* Copyright 2005-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,13 +34,14 @@
3434
import org.springframework.ws.server.endpoint.MethodEndpoint;
3535
import org.springframework.ws.server.endpoint.annotation.XPathParam;
3636
import org.springframework.xml.namespace.SimpleNamespaceContext;
37+
3738
import org.w3c.dom.Document;
3839
import org.w3c.dom.Element;
3940
import org.w3c.dom.Node;
4041
import org.w3c.dom.NodeList;
4142

4243
/**
43-
* 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:
4445
* <pre>
4546
* void handleMyMessage(@XPathParam("/root/child/text")String param);
4647
* </pre>
@@ -81,7 +82,7 @@ protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
8182
}
8283
Class<?>[] parameterTypes = method.getParameterTypes();
8384
for (int i = 0; i < parameterTypes.length; i++) {
84-
if (getXPathParamAnnotation(method, i) == null || !isSuportedType(parameterTypes[i])) {
85+
if (getXPathParamAnnotation(method, i) == null || !isSupportedType(parameterTypes[i])) {
8586
return false;
8687
}
8788
}
@@ -98,7 +99,7 @@ private XPathParam getXPathParamAnnotation(Method method, int paramIdx) {
9899
return null;
99100
}
100101

101-
private boolean isSuportedType(Class<?> clazz) {
102+
private boolean isSupportedType(Class<?> clazz) {
102103
return Boolean.class.isAssignableFrom(clazz) || Boolean.TYPE.isAssignableFrom(clazz) ||
103104
Double.class.isAssignableFrom(clazz) || Double.TYPE.isAssignableFrom(clazz) ||
104105
Node.class.isAssignableFrom(clazz) || NodeList.class.isAssignableFrom(clazz) ||
@@ -148,7 +149,7 @@ else if (String.class.isAssignableFrom(parameterTypes[i])) {
148149
return args;
149150
}
150151

151-
private XPath createXPath() {
152+
private synchronized XPath createXPath() {
152153
XPath xpath = xpathFactory.newXPath();
153154
if (namespaces != null) {
154155
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
@@ -39,11 +39,7 @@
3939
*/
4040
abstract class Jaxp13XPathExpressionFactory {
4141

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

4844
/**
4945
* Creates a JAXP 1.3 <code>XPathExpression</code> from the given string expression.
@@ -54,7 +50,7 @@ abstract class Jaxp13XPathExpressionFactory {
5450
*/
5551
static XPathExpression createXPathExpression(String expression) {
5652
try {
57-
XPath xpath = xpathFactory.newXPath();
53+
XPath xpath = createXPath();
5854
javax.xml.xpath.XPathExpression xpathExpression = xpath.compile(expression);
5955
return new Jaxp13XPathExpression(xpathExpression);
6056
}
@@ -74,7 +70,7 @@ static XPathExpression createXPathExpression(String expression) {
7470
*/
7571
public static XPathExpression createXPathExpression(String expression, Map namespaces) {
7672
try {
77-
XPath xpath = xpathFactory.newXPath();
73+
XPath xpath = createXPath();
7874
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
7975
namespaceContext.setBindings(namespaces);
8076
xpath.setNamespaceContext(namespaceContext);
@@ -87,6 +83,11 @@ public static XPathExpression createXPathExpression(String expression, Map names
8783
}
8884
}
8985

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

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 List evaluate(String expression, Source context, NodeMapper nodeMapper) t
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)