From b727406dce4fb39dec65ac71ad709f26b144142b Mon Sep 17 00:00:00 2001 From: Bertrand Lorentz Date: Tue, 5 Nov 2024 14:19:09 +0100 Subject: [PATCH] pure: Store variable values in a ThreadLocal map To ensure that variable values are not overwritten when different XML are validated on separate threads, store the variable values in aborts ThreadLocal map. Fixes issue #182. --- .../pure/xpath/XPathLetVariableResolver.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ph-schematron-pure/src/main/java/com/helger/schematron/pure/xpath/XPathLetVariableResolver.java b/ph-schematron-pure/src/main/java/com/helger/schematron/pure/xpath/XPathLetVariableResolver.java index 91fa32111..df0a36aad 100644 --- a/ph-schematron-pure/src/main/java/com/helger/schematron/pure/xpath/XPathLetVariableResolver.java +++ b/ph-schematron-pure/src/main/java/com/helger/schematron/pure/xpath/XPathLetVariableResolver.java @@ -30,7 +30,14 @@ @NotThreadSafe public class XPathLetVariableResolver implements XPathVariableResolver { - private final ICommonsMap m_aVariables = new CommonsHashMap <> (); + private final ThreadLocal> m_aVariables = new ThreadLocal<> () { + @Override + protected ICommonsMap initialValue() + { + return new CommonsHashMap <> (); + } + }; + private final XPathVariableResolver m_aDelegatedResolver; public XPathLetVariableResolver (@Nullable final XPathVariableResolver aResolver) @@ -41,7 +48,7 @@ public XPathLetVariableResolver (@Nullable final XPathVariableResolver aResolver public void setVariableValue (@Nonnull final QName aVariableName, @Nullable final Object aValue) { ValueEnforcer.notNull (aVariableName, "VariableName"); - m_aVariables.put (aVariableName, aValue); + m_aVariables.get().put (aVariableName, aValue); } /** @@ -53,7 +60,7 @@ public void setVariableValue (@Nonnull final QName aVariableName, @Nullable fina public void removeVariable (@Nullable final QName aVariableName) { if (aVariableName != null) - m_aVariables.remove (aVariableName); + m_aVariables.get().remove (aVariableName); } @Override @@ -62,7 +69,7 @@ public Object resolveVariable (@Nullable final QName aVariableName) if (aVariableName != null) { // 1. variables - final Object result = m_aVariables.get (aVariableName); + final Object result = m_aVariables.get().get (aVariableName); if (result != null) return result;