Skip to content

Commit 0736c6f

Browse files
authored
XPath factories and expressions are not thread safe or reentrant (#2129)
1 parent 566b3b3 commit 0736c6f

1 file changed

Lines changed: 8 additions & 30 deletions

File tree

plugins/com.google.cloud.tools.eclipse.dataflow.core/src/com/google/cloud/tools/eclipse/dataflow/core/project/DataflowArtifactRetriever.java

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
import javax.xml.parsers.ParserConfigurationException;
4545
import javax.xml.xpath.XPath;
4646
import javax.xml.xpath.XPathConstants;
47-
import javax.xml.xpath.XPathExpression;
48-
import javax.xml.xpath.XPathExpressionException;
4947
import javax.xml.xpath.XPathFactory;
5048

5149
/**
@@ -57,7 +55,7 @@
5755
* Maven for available versions. Additionally, M2E APIs are internal and unstable, and thus may
5856
* change between versions.
5957
*
60-
* <p>The artifact retriever reads Maven Central metadata xml files to retrieve available and latest
58+
* <p>The artifact retriever reads Maven Central metadata XML files to retrieve available and latest
6159
* versions.
6260
*/
6361
public class DataflowArtifactRetriever {
@@ -68,26 +66,6 @@ public class DataflowArtifactRetriever {
6866
ImmutableSortedSet.<ArtifactVersion>of(
6967
new DefaultArtifactVersion("1.9.0"), new DefaultArtifactVersion("2.0.0-beta1"));
7068

71-
private static final XPath X_PATH = XPathFactory.newInstance().newXPath();
72-
73-
private static XPathExpression createLatestVersionExpression() {
74-
try {
75-
return X_PATH.compile("/metadata/versioning/latest");
76-
} catch (XPathExpressionException e) {
77-
throw new IllegalStateException(
78-
"Could not create constant expression to select latest version", e);
79-
}
80-
}
81-
82-
private static XPathExpression createAllVersionsExpression() {
83-
try {
84-
return X_PATH.compile("/metadata/versioning/versions/version");
85-
} catch (XPathExpressionException e) {
86-
throw new IllegalStateException(
87-
"Could not create constant expression to select all versions", e);
88-
}
89-
}
90-
9169
private static URL getMetadataUrl(String artifactId) {
9270
try {
9371
return new URL(
@@ -110,13 +88,12 @@ private static URL getMetadataUrl(String artifactId) {
11088
.refreshAfterWrite(4, TimeUnit.HOURS)
11189
.build(
11290
new CacheLoader<String, ArtifactVersion>() {
113-
private final XPathExpression latestVersionExpression =
114-
createLatestVersionExpression();
11591

11692
@Override
11793
public ArtifactVersion load(String artifactId) throws Exception {
94+
XPath xpath = XPathFactory.newInstance().newXPath();
11895
Document document = getMetadataDocument(artifactId);
119-
String result = latestVersionExpression.evaluate(document);
96+
String result = xpath.evaluate("/metadata/versioning/latest", document);
12097
return new DefaultArtifactVersion(result);
12198
}
12299
});
@@ -126,14 +103,15 @@ public ArtifactVersion load(String artifactId) throws Exception {
126103
.refreshAfterWrite(4, TimeUnit.HOURS)
127104
.build(
128105
new CacheLoader<String, NavigableSet<ArtifactVersion>>() {
129-
private final XPathExpression allVersionsExpression =
130-
createAllVersionsExpression();
131106

132107
@Override
133108
public NavigableSet<ArtifactVersion> load(String artifactId) throws Exception {
109+
XPath xpath = XPathFactory.newInstance().newXPath();
134110
Document document = getMetadataDocument(artifactId);
135-
NodeList versionNodes =
136-
(NodeList) allVersionsExpression.evaluate(document, XPathConstants.NODESET);
111+
NodeList versionNodes = (NodeList) xpath.evaluate(
112+
"/metadata/versioning/versions/version",
113+
document,
114+
XPathConstants.NODESET);
137115
Builder<ArtifactVersion> versions = ImmutableSortedSet.naturalOrder();
138116
for (int i = 0; i < versionNodes.getLength(); i++) {
139117
String versionString = versionNodes.item(i).getTextContent();

0 commit comments

Comments
 (0)