From 7dfe4a32ded704fce0d3086fcca9ed79e21bf103 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 12 May 2010 09:09:20 +0000 Subject: [PATCH] SWS-613 - Jaxp13XPathTemplate uses thread-unsafe XPathFactory as field --- ...XPathParamAnnotationMethodEndpointAdapter.java | 11 ++++++----- .../xml/xpath/Jaxp13XPathExpressionFactory.java | 15 ++++++++------- .../xml/xpath/Jaxp13XPathTemplate.java | 7 ++++++- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/core-tiger/src/main/java/org/springframework/ws/server/endpoint/adapter/XPathParamAnnotationMethodEndpointAdapter.java b/core-tiger/src/main/java/org/springframework/ws/server/endpoint/adapter/XPathParamAnnotationMethodEndpointAdapter.java index a851dea1..714a6a84 100644 --- a/core-tiger/src/main/java/org/springframework/ws/server/endpoint/adapter/XPathParamAnnotationMethodEndpointAdapter.java +++ b/core-tiger/src/main/java/org/springframework/ws/server/endpoint/adapter/XPathParamAnnotationMethodEndpointAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2007 the original author or authors. + * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,13 +34,14 @@ import org.springframework.ws.context.MessageContext; import org.springframework.ws.server.endpoint.MethodEndpoint; import org.springframework.ws.server.endpoint.annotation.XPathParam; import org.springframework.xml.namespace.SimpleNamespaceContext; + import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** - * Adapter that supports endpoint methods that use marshalling. Supports methods with the following signature: + * Adapter that supports endpoint methods that use XPath expressions. Supports methods with the following signature: *
  * void handleMyMessage(@XPathParam("/root/child/text")String param);
  * 
@@ -81,7 +82,7 @@ public class XPathParamAnnotationMethodEndpointAdapter extends AbstractMethodEnd } Class[] parameterTypes = method.getParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { - if (getXPathParamAnnotation(method, i) == null || !isSuportedType(parameterTypes[i])) { + if (getXPathParamAnnotation(method, i) == null || !isSupportedType(parameterTypes[i])) { return false; } } @@ -98,7 +99,7 @@ public class XPathParamAnnotationMethodEndpointAdapter extends AbstractMethodEnd return null; } - private boolean isSuportedType(Class clazz) { + private boolean isSupportedType(Class clazz) { return Boolean.class.isAssignableFrom(clazz) || Boolean.TYPE.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz) || Double.TYPE.isAssignableFrom(clazz) || Node.class.isAssignableFrom(clazz) || NodeList.class.isAssignableFrom(clazz) || @@ -148,7 +149,7 @@ public class XPathParamAnnotationMethodEndpointAdapter extends AbstractMethodEnd return args; } - private XPath createXPath() { + private synchronized XPath createXPath() { XPath xpath = xpathFactory.newXPath(); if (namespaces != null) { SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); diff --git a/xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathExpressionFactory.java b/xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathExpressionFactory.java index c797c247..6b5e165d 100644 --- a/xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathExpressionFactory.java +++ b/xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathExpressionFactory.java @@ -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 XPathExpression 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 XPathExpression interface. */ private static class Jaxp13XPathExpression implements XPathExpression { diff --git a/xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathTemplate.java b/xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathTemplate.java index 6f01b0f7..265d6530 100644 --- a/xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathTemplate.java +++ b/xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathTemplate.java @@ -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(); + } + + }