Skip to content

Commit

Permalink
Fixes #5498 ServletHolder cleanup (#5514)
Browse files Browse the repository at this point in the history
* Fixes #55498 ServletHolder cleanup

Various cleanups for #5498 including:
 + renaming multiple `_servlet` fields in inner classes to avoid confusion
 + better comments in prepare method to describe why it is needed
 + call prepare from Invoker servlet
 + The `_servlet` field is not set until after the servlet is initialized
 + Consistent wrapping of `SingleThreadedWrapper` now in `initServlet`
 + The `getServlet` method now looks the volatile `_servlet` to avoid locking if possible
 + The `handle` method now calls `getServletInstance` as servlet will have been initialized in `prepare`
 + Found and fixed race with making unavaiable servlet available again
 + fixed nanotime overflow
 + fixed several compiler warnings/suggestions
 + removed while true from unavailable servlet
 + Do not destroy servlets unless init has been called.
 + Added TODOs about calling predestroy on instances not created by the holder.
 + Do not destroy servlets unless init has been called.
 + Added TODOs about calling predestroy on instances not created by the holder.
 + improved dump and toString
  • Loading branch information
gregw committed Oct 28, 2020
1 parent b3186a9 commit 7a0de6a
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 120 deletions.
Expand Up @@ -149,7 +149,6 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory, Welc
private boolean _useFileMappedBuffer = false;
private String _relativeResourceBase;
private ServletHandler _servletHandler;
private ServletHolder _defaultHolder;

public DefaultServlet(ResourceService resourceService)
{
Expand Down Expand Up @@ -303,11 +302,6 @@ public void init()
_resourceService.setGzipEquivalentFileExtensions(gzipEquivalentFileExtensions);

_servletHandler = _contextHandler.getChildHandlerByClass(ServletHandler.class);
for (ServletHolder h : _servletHandler.getServlets())
{
if (h.getServletInstance() == this)
_defaultHolder = h;
}

if (LOG.isDebugEnabled())
LOG.debug("resource base = " + _resourceBase);
Expand Down Expand Up @@ -504,19 +498,17 @@ public String getWelcomeFile(String pathInContext)
return null;

String welcomeServlet = null;
for (int i = 0; i < _welcomes.length; i++)
for (String s : _welcomes)
{
String welcomeInContext = URIUtil.addPaths(pathInContext, _welcomes[i]);
String welcomeInContext = URIUtil.addPaths(pathInContext, s);
Resource welcome = getResource(welcomeInContext);
if (welcome != null && welcome.exists())
return welcomeInContext;

if ((_welcomeServlets || _welcomeExactServlets) && welcomeServlet == null)
{
MappedResource<ServletHolder> entry = _servletHandler.getMappedServlet(welcomeInContext);
@SuppressWarnings("ReferenceEquality")
boolean isDefaultHolder = (entry.getResource() != _defaultHolder);
if (entry != null && isDefaultHolder &&
if (entry != null && entry.getResource().getServletInstance() != this &&
(_welcomeServlets || (_welcomeExactServlets && entry.getPathSpec().getDeclaration().equals(welcomeInContext))))
welcomeServlet = welcomeInContext;
}
Expand Down
Expand Up @@ -222,7 +222,8 @@ public void dump(Appendable out, String indent) throws IOException
@Override
public String toString()
{
return String.format("%s@%x==%s,inst=%b,async=%b", getName(), hashCode(), getClassName(), _filter != null, isAsyncSupported());
return String.format("%s==%s@%x{inst=%b,async=%b,src=%s}",
getName(), getClassName(), hashCode(), _filter != null, isAsyncSupported(), getSource());
}

public FilterRegistration.Dynamic getRegistration()
Expand Down
Expand Up @@ -231,6 +231,7 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
if (holder != null)
{
final Request baseRequest = Request.getBaseRequest(request);
holder.prepare(baseRequest, request, response);
holder.handle(baseRequest,
new InvokedRequest(request, included, servlet, servletPath, pathInfo),
response);
Expand Down
Expand Up @@ -126,7 +126,7 @@ public void doStop() throws Exception
@Override
public String toString()
{
return super.toString() + ": " + getClassName();
return String.format("%s@%x{src=%s}", getClassName(), hashCode(), getSource());
}

/**
Expand Down

0 comments on commit 7a0de6a

Please sign in to comment.