Issue #5083 - Convert synchronized usages to AutoLock.

Fixes after merge.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2020-08-04 17:59:13 +02:00
parent a79c0cfddd
commit 062878f97b

View File

@ -87,7 +87,6 @@ import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.DumpableCollection; import org.eclipse.jetty.util.component.DumpableCollection;
import org.eclipse.jetty.util.component.Graceful; import org.eclipse.jetty.util.component.Graceful;
import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.thread.AutoLock;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -185,7 +184,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
DESTROYED DESTROYED
} }
private final AutoLock _lock = new AutoLock();
protected ContextStatus _contextStatus = ContextStatus.NOTSET; protected ContextStatus _contextStatus = ContextStatus.NOTSET;
protected Context _scontext; protected Context _scontext;
private final AttributesMap _attributes; private final AttributesMap _attributes;
@ -1846,13 +1844,10 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
if (className == null) if (className == null)
return null; return null;
try (AutoLock l = _lock.lock()) if (_classLoader == null)
{ return Loader.loadClass(className);
if (_classLoader == null)
return Loader.loadClass(className);
return _classLoader.loadClass(className); return _classLoader.loadClass(className);
}
} }
public void addLocaleEncoding(String locale, String encoding) public void addLocaleEncoding(String locale, String encoding)
@ -2084,7 +2079,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
*/ */
public class Context extends StaticContext public class Context extends StaticContext
{ {
private final AutoLock _lock = new AutoLock();
protected boolean _enabled = true; // whether or not the dynamic API is enabled for callers protected boolean _enabled = true; // whether or not the dynamic API is enabled for callers
protected boolean _extendedListenerTypes = false; protected boolean _extendedListenerTypes = false;
@ -2324,60 +2318,51 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
@Override @Override
public Object getAttribute(String name) public Object getAttribute(String name)
{ {
try (AutoLock l = _lock.lock()) Object o = ContextHandler.this.getAttribute(name);
{ if (o == null)
Object o = ContextHandler.this.getAttribute(name); o = super.getAttribute(name);
if (o == null) return o;
o = super.getAttribute(name);
return o;
}
} }
@Override @Override
public Enumeration<String> getAttributeNames() public Enumeration<String> getAttributeNames()
{ {
try (AutoLock l = _lock.lock()) HashSet<String> set = new HashSet<>();
Enumeration<String> e = super.getAttributeNames();
while (e.hasMoreElements())
{ {
HashSet<String> set = new HashSet<>(); set.add(e.nextElement());
Enumeration<String> e = super.getAttributeNames();
while (e.hasMoreElements())
{
set.add(e.nextElement());
}
e = ContextHandler.this.getAttributeNames();
while (e.hasMoreElements())
{
set.add(e.nextElement());
}
return Collections.enumeration(set);
} }
e = ContextHandler.this.getAttributeNames();
while (e.hasMoreElements())
{
set.add(e.nextElement());
}
return Collections.enumeration(set);
} }
@Override @Override
public void setAttribute(String name, Object value) public void setAttribute(String name, Object value)
{ {
try (AutoLock l = _lock.lock()) Object oldValue = super.getAttribute(name);
if (value == null)
super.removeAttribute(name);
else
super.setAttribute(name, value);
if (!_servletContextAttributeListeners.isEmpty())
{ {
Object oldValue = super.getAttribute(name); ServletContextAttributeEvent event = new ServletContextAttributeEvent(_scontext, name, oldValue == null ? value : oldValue);
if (value == null) for (ServletContextAttributeListener listener : _servletContextAttributeListeners)
super.removeAttribute(name);
else
super.setAttribute(name, value);
if (!_servletContextAttributeListeners.isEmpty())
{ {
ServletContextAttributeEvent event = new ServletContextAttributeEvent(_scontext, name, oldValue == null ? value : oldValue); if (oldValue == null)
listener.attributeAdded(event);
for (ServletContextAttributeListener listener : _servletContextAttributeListeners) else if (value == null)
{ listener.attributeRemoved(event);
if (oldValue == null) else
listener.attributeAdded(event); listener.attributeReplaced(event);
else if (value == null)
listener.attributeRemoved(event);
else
listener.attributeReplaced(event);
}
} }
} }
} }
@ -2385,17 +2370,14 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
@Override @Override
public void removeAttribute(String name) public void removeAttribute(String name)
{ {
try (AutoLock l = _lock.lock()) Object oldValue = super.getAttribute(name);
super.removeAttribute(name);
if (oldValue != null && !_servletContextAttributeListeners.isEmpty())
{ {
Object oldValue = super.getAttribute(name); ServletContextAttributeEvent event = new ServletContextAttributeEvent(_scontext, name, oldValue);
super.removeAttribute(name); for (ServletContextAttributeListener listener : _servletContextAttributeListeners)
if (oldValue != null && !_servletContextAttributeListeners.isEmpty())
{ {
ServletContextAttributeEvent event = new ServletContextAttributeEvent(_scontext, name, oldValue); listener.attributeRemoved(event);
for (ServletContextAttributeListener listener : _servletContextAttributeListeners)
{
listener.attributeRemoved(event);
}
} }
} }
} }