Skip to content

Commit

Permalink
pure: Store variable values in a ThreadLocal map
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bertrand-lorentz committed Nov 5, 2024
1 parent a2b30c0 commit b727406
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@
@NotThreadSafe
public class XPathLetVariableResolver implements XPathVariableResolver
{
private final ICommonsMap <QName, Object> m_aVariables = new CommonsHashMap <> ();
private final ThreadLocal<ICommonsMap <QName, Object>> m_aVariables = new ThreadLocal<> () {
@Override
protected ICommonsMap <QName, Object> initialValue()
{
return new CommonsHashMap <> ();
}
};

private final XPathVariableResolver m_aDelegatedResolver;

public XPathLetVariableResolver (@Nullable final XPathVariableResolver aResolver)
Expand All @@ -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);
}

/**
Expand All @@ -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
Expand All @@ -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;

Expand Down

0 comments on commit b727406

Please sign in to comment.