Skip to content

Commit

Permalink
Issue jetty#2340 ServletContext cleanup (jetty#4307)
Browse files Browse the repository at this point in the history
* Issue jetty#2340 ServletContext cleanup

A minor cleanup of the ServletContext implementation classes.

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2340 ServletContext cleanup

cleanup after review

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw authored Nov 14, 2019
1 parent 89a4f92 commit b24c6ce
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
};

public static final int DEFAULT_LISTENER_TYPE_INDEX = 1;

public static final int EXTENDED_LISTENER_TYPE_INDEX = 0;

private static final String UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER = "Unimplemented {} - use org.eclipse.jetty.servlet.ServletContextHandler";

private static final Logger LOG = Log.getLogger(ContextHandler.class);

private static final ThreadLocal<Context> __context = new ThreadLocal<Context>();
private static final ThreadLocal<Context> __context = new ThreadLocal<>();

private static String __serverInfo = "jetty/" + Server.getVersion();

Expand Down Expand Up @@ -212,7 +213,7 @@ public static void setServerInfo(String serverInfo)
private final List<ContextScopeListener> _contextListeners = new CopyOnWriteArrayList<>();
private final List<EventListener> _durableListeners = new CopyOnWriteArrayList<>();
private String[] _protectedTargets;
private final CopyOnWriteArrayList<AliasCheck> _aliasChecks = new CopyOnWriteArrayList<ContextHandler.AliasCheck>();
private final CopyOnWriteArrayList<AliasCheck> _aliasChecks = new CopyOnWriteArrayList<>();

public enum Availability
{
Expand Down Expand Up @@ -245,7 +246,7 @@ private ContextHandler(Context context, HandlerContainer parent, String contextP
{
_scontext = context == null ? new Context() : context;
_attributes = new AttributesMap();
_initParams = new HashMap<String, String>();
_initParams = new HashMap<>();
addAliasCheck(new ApproveNonExistentDirectoryAliases());
if (File.separatorChar == '/')
addAliasCheck(new AllowSymLinkAliasChecker());
Expand Down Expand Up @@ -355,7 +356,7 @@ public void setVirtualHosts(String[] vhosts)
if (connectorIndex == 0)
{
if (connectorOnlyIndexes == null)
connectorOnlyIndexes = new ArrayList<Integer>();
connectorOnlyIndexes = new ArrayList<>();
connectorOnlyIndexes.add(i);
}
}
Expand Down Expand Up @@ -414,7 +415,7 @@ public void addVirtualHosts(String[] virtualHosts)
}
else
{
Set<String> currentVirtualHosts = new HashSet<String>(Arrays.asList(getVirtualHosts()));
Set<String> currentVirtualHosts = new HashSet<>(Arrays.asList(getVirtualHosts()));
for (String vh : virtualHosts)
{
currentVirtualHosts.add(normalizeHostname(vh));
Expand All @@ -439,7 +440,7 @@ public void removeVirtualHosts(String[] virtualHosts)
if (virtualHosts == null || virtualHosts.length == 0 || _vhosts == null || _vhosts.length == 0)
return; // do nothing

Set<String> existingVirtualHosts = new HashSet<String>(Arrays.asList(getVirtualHosts()));
Set<String> existingVirtualHosts = new HashSet<>(Arrays.asList(getVirtualHosts()));
for (String vh : virtualHosts)
{
existingVirtualHosts.remove(normalizeHostname(vh));
Expand Down Expand Up @@ -612,7 +613,7 @@ public String getDisplayName()

public EventListener[] getEventListeners()
{
return _eventListeners.toArray(new EventListener[_eventListeners.size()]);
return _eventListeners.toArray(new EventListener[0]);
}

/**
Expand Down Expand Up @@ -979,7 +980,7 @@ protected void doStop() throws Exception
stopContext();

// retain only durable listeners
setEventListeners(_durableListeners.toArray(new EventListener[_durableListeners.size()]));
setEventListeners(_durableListeners.toArray(new EventListener[0]));
_durableListeners.clear();

if (_errorHandler != null)
Expand Down Expand Up @@ -1133,7 +1134,7 @@ public void doScope(String target, Request baseRequest, HttpServletRequest reque
if (LOG.isDebugEnabled())
LOG.debug("scope {}|{}|{} @ {}", baseRequest.getContextPath(), baseRequest.getServletPath(), baseRequest.getPathInfo(), this);

Context oldContext = null;
Context oldContext;
String oldContextPath = null;
String oldServletPath = null;
String oldPathInfo = null;
Expand All @@ -1151,7 +1152,7 @@ public void doScope(String target, Request baseRequest, HttpServletRequest reque
// check the target.
if (DispatcherType.REQUEST.equals(dispatch) || DispatcherType.ASYNC.equals(dispatch))
{
if (_compactPath)
if (isCompactPath())
target = URIUtil.compactPath(target);
if (!checkContext(target, baseRequest, response))
return;
Expand Down Expand Up @@ -1197,7 +1198,7 @@ else if (_contextPath.length() == 1)
if (_contextPath.length() == 1)
baseRequest.setContextPath("");
else
baseRequest.setContextPath(_contextPathEncoded);
baseRequest.setContextPath(getContextPathEncoded());
baseRequest.setServletPath(null);
baseRequest.setPathInfo(pathInfo);
}
Expand Down Expand Up @@ -1756,7 +1757,7 @@ public synchronized Class<?> loadClass(String className) throws ClassNotFoundExc
public void addLocaleEncoding(String locale, String encoding)
{
if (_localeEncodingMap == null)
_localeEncodingMap = new HashMap<String, String>();
_localeEncodingMap = new HashMap<>();
_localeEncodingMap.put(locale, encoding);
}

Expand Down Expand Up @@ -1836,7 +1837,7 @@ public boolean checkAlias(String path, Resource resource)
LOG.debug("Aliased resource: " + resource + "~=" + resource.getAlias());

// alias checks
for (Iterator<AliasCheck> i = _aliasChecks.iterator(); i.hasNext(); )
for (Iterator<AliasCheck> i = getAliasChecks().iterator(); i.hasNext(); )
{
AliasCheck check = i.next();
if (check.check(path, resource))
Expand Down Expand Up @@ -1902,7 +1903,7 @@ public Set<String> getResourcePaths(String path)
String[] l = resource.list();
if (l != null)
{
HashSet<String> set = new HashSet<String>();
HashSet<String> set = new HashSet<>();
for (int i = 0; i < l.length; i++)
{
set.add(path + l[i]);
Expand Down Expand Up @@ -1945,7 +1946,7 @@ private String normalizeHostname(String host)
*/
public void addAliasCheck(AliasCheck check)
{
_aliasChecks.add(check);
getAliasChecks().add(check);
}

/**
Expand All @@ -1961,22 +1962,23 @@ public List<AliasCheck> getAliasChecks()
*/
public void setAliasChecks(List<AliasCheck> checks)
{
_aliasChecks.clear();
_aliasChecks.addAll(checks);
getAliasChecks().clear();
getAliasChecks().addAll(checks);
}

/**
* clear the list of AliasChecks
*/
public void clearAliasChecks()
{
_aliasChecks.clear();
getAliasChecks().clear();
}

/**
* Context.
* <p>
* A partial implementation of {@link javax.servlet.ServletContext}. A complete implementation is provided by the derived {@link ContextHandler}.
* A partial implementation of {@link javax.servlet.ServletContext}. A complete implementation is provided by the
* derived {@link ContextHandler} implementations.
* </p>
*/
public class Context extends StaticContext
Expand All @@ -1999,7 +2001,7 @@ public ContextHandler getContextHandler()
@Override
public ServletContext getContext(String uripath)
{
List<ContextHandler> contexts = new ArrayList<ContextHandler>();
List<ContextHandler> contexts = new ArrayList<>();
Handler[] handlers = getServer().getChildHandlersByClass(ContextHandler.class);
String matchedPath = null;

Expand Down Expand Up @@ -2074,7 +2076,7 @@ public ServletContext getContext(String uripath)
matchedPath = contextPath;
}

if (matchedPath != null && matchedPath.equals(contextPath))
if (matchedPath.equals(contextPath))
contexts.add(ch);
}
}
Expand Down Expand Up @@ -2266,7 +2268,7 @@ public synchronized Object getAttribute(String name)
@Override
public synchronized Enumeration<String> getAttributeNames()
{
HashSet<String> set = new HashSet<String>();
HashSet<String> set = new HashSet<>();
Enumeration<String> e = super.getAttributeNames();
while (e.hasMoreElements())
{
Expand Down Expand Up @@ -2413,19 +2415,6 @@ public void addListener(Class<? extends EventListener> listenerClass)
}
}

@Override
public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException
{
try
{
return createInstance(clazz);
}
catch (Exception e)
{
throw new ServletException(e);
}
}

public void checkListener(Class<? extends EventListener> listener) throws IllegalStateException
{
boolean ok = false;
Expand Down Expand Up @@ -2459,7 +2448,7 @@ public ClassLoader getClassLoader()
throw new UnsupportedOperationException();

// no security manager just return the classloader
if (!_usingSecurityManager)
if (!isUsingSecurityManager())
{
return _classLoader;
}
Expand Down Expand Up @@ -2515,12 +2504,6 @@ public boolean isEnabled()
return _enabled;
}

public <T> T createInstance(Class<T> clazz) throws Exception
{
T o = clazz.getDeclaredConstructor().newInstance();
return o;
}

@Override
public String getVirtualServerName()
{
Expand All @@ -2531,15 +2514,16 @@ public String getVirtualServerName()
}
}

/**
* A simple implementation of ServletContext that is used when there is no
* ContextHandler. This is also used as the base for all other ServletContext
* implementations.
*/
public static class StaticContext extends AttributesMap implements ServletContext
{
private int _effectiveMajorVersion = SERVLET_MAJOR_VERSION;
private int _effectiveMinorVersion = SERVLET_MINOR_VERSION;

public StaticContext()
{
}

@Override
public ServletContext getContext(String uripath)
{
Expand Down Expand Up @@ -2603,7 +2587,7 @@ public Set<String> getResourcePaths(String path)
@Override
public String getServerInfo()
{
return __serverInfo;
return ContextHandler.getServerInfo();
}

@Override
Expand Down Expand Up @@ -2720,20 +2704,6 @@ public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName,
return null;
}

@Override
public <T extends Filter> T createFilter(Class<T> c) throws ServletException
{
LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "createFilter(Class)");
return null;
}

@Override
public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
{
LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "createServlet(Class)");
return null;
}

@Override
public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
{
Expand Down Expand Up @@ -2807,8 +2777,7 @@ public void addListener(Class<? extends EventListener> listenerClass)
LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addListener(Class)");
}

@Override
public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException
protected <T> T createInstance(Class<T> clazz) throws ServletException
{
try
{
Expand All @@ -2820,6 +2789,24 @@ public <T extends EventListener> T createListener(Class<T> clazz) throws Servlet
}
}

@Override
public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException
{
return createInstance(clazz);
}

@Override
public <T extends Servlet> T createServlet(Class<T> clazz) throws ServletException
{
return createInstance(clazz);
}

@Override
public <T extends Filter> T createFilter(Class<T> clazz) throws ServletException
{
return createInstance(clazz);
}

@Override
public ClassLoader getClassLoader()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void initialize() throws Exception
try
{
ServletContext context = getServletHandler().getServletContext();
_filter = (context instanceof ServletContextHandler.Context)
_filter = (context != null)
? context.createFilter(getHeldClass())
: getHeldClass().getDeclaredConstructor().newInstance();
}
Expand Down Expand Up @@ -234,7 +234,7 @@ public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, bo
public Collection<String> getServletNameMappings()
{
FilterMapping[] mappings = getServletHandler().getFilterMappings();
List<String> names = new ArrayList<String>();
List<String> names = new ArrayList<>();
for (FilterMapping mapping : mappings)
{
if (mapping.getFilterHolder() != FilterHolder.this)
Expand All @@ -250,7 +250,7 @@ public Collection<String> getServletNameMappings()
public Collection<String> getUrlPatternMappings()
{
FilterMapping[] mappings = getServletHandler().getFilterMappings();
List<String> patterns = new ArrayList<String>();
List<String> patterns = new ArrayList<>();
for (FilterMapping mapping : mappings)
{
if (mapping.getFilterHolder() != FilterHolder.this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public void doStart() throws Exception
//create an instance of the listener and decorate it
try
{
ServletContext scontext = contextHandler.getServletContext();
_listener = (scontext instanceof ServletContextHandler.Context)
? scontext.createListener(getHeldClass())
ServletContext context = contextHandler.getServletContext();
_listener = (context != null)
? context.createListener(getHeldClass())
: getHeldClass().getDeclaredConstructor().newInstance();
}
catch (ServletException ex)
Expand Down
Loading

0 comments on commit b24c6ce

Please sign in to comment.