Skip to content

Commit

Permalink
Issue #4741 Async ServletMapping
Browse files Browse the repository at this point in the history
More code cleanups

Signed-off-by: Greg Wilkins <[email protected]>
  • Loading branch information
gregw committed May 13, 2020
1 parent 21c0f06 commit 62c067b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public Object getAttribute(String key)
}
}

// If we are forwarded then we hide include attributes
if (key.startsWith(__INCLUDE_PREFIX))
return null;

Expand Down Expand Up @@ -391,9 +392,6 @@ public Object getAttribute(String key)
}
}

if (key.startsWith(__INCLUDE_PREFIX))
return null;

return _attributes.getAttribute(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.EventListener;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
Expand Down Expand Up @@ -93,8 +92,6 @@ public class ServletHandler extends ScopedHandler
{
private static final Logger LOG = LoggerFactory.getLogger(ServletHandler.class);

public static final String __DEFAULT_SERVLET = "default";

private ServletContextHandler _contextHandler;
private ServletContext _servletContext;
private FilterHolder[] _filters = new FilterHolder[0];
Expand Down Expand Up @@ -248,24 +245,16 @@ protected synchronized void doStop()
//remove all of the mappings that were for non-embedded filters
_filterNameMap.remove(filter.getName());
//remove any mappings associated with this filter
ListIterator<FilterMapping> fmitor = filterMappings.listIterator();
while (fmitor.hasNext())
{
FilterMapping fm = fmitor.next();
if (fm.getFilterName().equals(filter.getName()))
fmitor.remove();
}
filterMappings.removeIf(fm -> fm.getFilterName().equals(filter.getName()));
}
else
filterHolders.add(filter); //only retain embedded
}
}

//Retain only filters and mappings that were added using jetty api (ie Source.EMBEDDED)
FilterHolder[] fhs = (FilterHolder[])LazyList.toArray(filterHolders, FilterHolder.class);
_filters = fhs;
FilterMapping[] fms = (FilterMapping[])LazyList.toArray(filterMappings, FilterMapping.class);
_filterMappings = fms;
_filters = (FilterHolder[])LazyList.toArray(filterHolders, FilterHolder.class);
_filterMappings = (FilterMapping[])LazyList.toArray(filterMappings, FilterMapping.class);

_matchAfterIndex = (_filterMappings == null || _filterMappings.length == 0 ? -1 : _filterMappings.length - 1);
_matchBeforeIndex = -1;
Expand All @@ -292,24 +281,16 @@ protected synchronized void doStop()
//remove from servlet name map
_servletNameMap.remove(servlet.getName());
//remove any mappings associated with this servlet
ListIterator<ServletMapping> smitor = servletMappings.listIterator();
while (smitor.hasNext())
{
ServletMapping sm = smitor.next();
if (sm.getServletName().equals(servlet.getName()))
smitor.remove();
}
servletMappings.removeIf(sm -> sm.getServletName().equals(servlet.getName()));
}
else
servletHolders.add(servlet); //only retain embedded
}
}

//Retain only Servlets and mappings added via jetty apis (ie Source.EMBEDDED)
ServletHolder[] shs = (ServletHolder[])LazyList.toArray(servletHolders, ServletHolder.class);
_servlets = shs;
ServletMapping[] sms = (ServletMapping[])LazyList.toArray(servletMappings, ServletMapping.class);
_servletMappings = sms;
_servlets = (ServletHolder[])LazyList.toArray(servletHolders, ServletHolder.class);
_servletMappings = (ServletMapping[])LazyList.toArray(servletMappings, ServletMapping.class);

if (_contextHandler != null)
_contextHandler.contextDestroyed();
Expand All @@ -333,8 +314,7 @@ protected synchronized void doStop()
listenerHolders.add(listener);
}
}
ListenerHolder[] listeners = (ListenerHolder[])LazyList.toArray(listenerHolders, ListenerHolder.class);
_listeners = listeners;
_listeners = (ListenerHolder[])LazyList.toArray(listenerHolders, ListenerHolder.class);

//will be regenerated on next start
_filterPathMappings = null;
Expand Down Expand Up @@ -559,12 +539,12 @@ public MappedServlet getMappedServlet(String target)
return null;
}

protected FilterChain getFilterChain(Request baseRequest, String pathInContext, ServletHolder servletHolder)
private FilterChain getFilterChain(Request baseRequest, String pathInContext, ServletHolder servletHolder)
{
String key = pathInContext == null ? servletHolder.getName() : pathInContext;
int dispatch = FilterMapping.dispatch(baseRequest.getDispatcherType());

if (_filterChainsCached && _chainCache != null)
if (_filterChainsCached)
{
FilterChain chain = _chainCache[dispatch].get(key);
if (chain != null)
Expand Down Expand Up @@ -611,8 +591,7 @@ protected FilterChain getFilterChain(Request baseRequest, String pathInContext,
FilterChain chain = null;
if (_filterChainsCached)
{
if (!filters.isEmpty())
chain = newCachedChain(filters, servletHolder);
chain = newCachedChain(filters, servletHolder);

final Map<String, FilterChain> cache = _chainCache[dispatch];
final Queue<String> lru = _chainLRU[dispatch];
Expand All @@ -635,7 +614,7 @@ protected FilterChain getFilterChain(Request baseRequest, String pathInContext,
cache.put(key, chain);
lru.add(key);
}
else if (!filters.isEmpty())
else
chain = new Chain(baseRequest, filters, servletHolder);

return chain;
Expand Down Expand Up @@ -1120,15 +1099,15 @@ public void addFilterMapping(FilterMapping mapping)
if (mappings == null || mappings.length == 0)
{
setFilterMappings(insertFilterMapping(mapping, 0, false));
if (source != null && source == Source.JAVAX_API)
if (source == Source.JAVAX_API)
_matchAfterIndex = 0;
}
else
{
//there are existing entries. If this is a programmatic filtermapping, it is added at the end of the list.
//If this is a normal filtermapping, it is inserted after all the other filtermappings (matchBefores and normals),
//but before the first matchAfter filtermapping.
if (source != null && Source.JAVAX_API == source)
if (Source.JAVAX_API == source)
{
setFilterMappings(insertFilterMapping(mapping, mappings.length - 1, false));
if (_matchAfterIndex < 0)
Expand Down Expand Up @@ -1164,12 +1143,12 @@ public void prependFilterMapping(FilterMapping mapping)
if (mappings == null || mappings.length == 0)
{
setFilterMappings(insertFilterMapping(mapping, 0, false));
if (source != null && Source.JAVAX_API == source)
if (Source.JAVAX_API == source)
_matchBeforeIndex = 0;
}
else
{
if (source != null && Source.JAVAX_API == source)
if (Source.JAVAX_API == source)
{
//programmatically defined filter mappings are prepended to mapping list in the order
//in which they were defined. In other words, insert this mapping at the tail of the
Expand Down Expand Up @@ -1308,7 +1287,7 @@ protected synchronized void updateMappings()
}

// Map servlet paths to holders
if (_servletMappings == null || _servletNameMap == null)
if (_servletMappings == null)
{
_servletPathMap = null;
}
Expand All @@ -1325,12 +1304,7 @@ protected synchronized void updateMappings()
{
for (String pathSpec : pathSpecs)
{
List<ServletMapping> mappings = sms.get(pathSpec);
if (mappings == null)
{
mappings = new ArrayList<>();
sms.put(pathSpec, mappings);
}
List<ServletMapping> mappings = sms.computeIfAbsent(pathSpec, k -> new ArrayList<>());
mappings.add(servletMapping);
}
}
Expand Down Expand Up @@ -1401,13 +1375,10 @@ else if (isAllowDuplicateMappings())
}

// flush filter chain cache
if (_chainCache != null)
for (int i = _chainCache.length; i-- > 0; )
{
for (int i = _chainCache.length; i-- > 0; )
{
if (_chainCache[i] != null)
_chainCache[i].clear();
}
if (_chainCache[i] != null)
_chainCache[i].clear();
}

if (LOG.isDebugEnabled())
Expand Down Expand Up @@ -1442,28 +1413,26 @@ protected synchronized boolean containsFilterHolder(FilterHolder holder)
{
if (_filters == null)
return false;
boolean found = false;
for (FilterHolder f : _filters)
{
if (f == holder)
found = true;
return true;
}
return found;
return false;
}

protected synchronized boolean containsServletHolder(ServletHolder holder)
{
if (_servlets == null)
return false;
boolean found = false;
for (ServletHolder s : _servlets)
{
@SuppressWarnings("ReferenceEquality")
boolean foundServletHolder = (s == holder);
if (foundServletHolder)
found = true;
return true;
}
return found;
return false;
}

/**
Expand Down Expand Up @@ -1727,7 +1696,7 @@ public static class MappedServlet
{
private final PathSpec _pathSpec;
private final ServletHolder _servletHolder;
private final ServletPathMapping _httpServletMapping;
private final ServletPathMapping _servletPathMapping;

MappedServlet(PathSpec pathSpec, ServletHolder servletHolder)
{
Expand All @@ -1741,16 +1710,16 @@ public static class MappedServlet
{
case EXACT:
case ROOT:
_httpServletMapping = new ServletPathMapping(_pathSpec, _servletHolder.getName(), _pathSpec.getPrefix());
_servletPathMapping = new ServletPathMapping(_pathSpec, _servletHolder.getName(), _pathSpec.getPrefix());
break;
default:
_httpServletMapping = null;
_servletPathMapping = null;
break;
}
}
else
{
_httpServletMapping = null;
_servletPathMapping = null;
}
}

Expand All @@ -1766,8 +1735,8 @@ public ServletHolder getServletHolder()

public ServletPathMapping getServletPathMapping(String pathInContext)
{
if (_httpServletMapping != null)
return _httpServletMapping;
if (_servletPathMapping != null)
return _servletPathMapping;
return new ServletPathMapping(_pathSpec, _servletHolder.getName(), pathInContext);
}

Expand Down

0 comments on commit 62c067b

Please sign in to comment.