Skip to content

Commit

Permalink
Fix #5835 Durable Filters, Servlets and Listeners
Browse files Browse the repository at this point in the history
Update WebSocketUpgradeFilter to have durable configuration.
  • Loading branch information
gregw committed Dec 23, 2020
1 parent 4b6930f commit 176e58d
Showing 1 changed file with 22 additions and 18 deletions.
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.util.EnumSet;
import java.util.Objects;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand Down Expand Up @@ -131,12 +132,12 @@ public static WebSocketUpgradeFilter configureContext(ServletContext context) th

private NativeWebSocketConfiguration configuration;
private String instanceKey;
private boolean localConfiguration = false;
private boolean durableConfiguration = false;
private boolean managedConfiguration = false;
private boolean alreadySetToAttribute = false;

public WebSocketUpgradeFilter()
{
// do nothing
}

public WebSocketUpgradeFilter(WebSocketServerFactory factory)
Expand All @@ -146,6 +147,7 @@ public WebSocketUpgradeFilter(WebSocketServerFactory factory)

public WebSocketUpgradeFilter(NativeWebSocketConfiguration configuration)
{
durableConfiguration = configuration != null;
this.configuration = configuration;
}

Expand Down Expand Up @@ -182,14 +184,17 @@ public void destroy()
{
try
{
// We must clear out the configuration as the configuration is cleared on restart.
NativeWebSocketConfiguration config = configuration;
boolean localConfig = localConfiguration;
configuration = null;
localConfiguration = false;
alreadySetToAttribute = false;
if (localConfig)
if (!durableConfiguration)
configuration = null;

boolean managed = managedConfiguration;
managedConfiguration = false;
if (managed)
{
config.stop();
}
alreadySetToAttribute = false;
}
catch (Exception e)
{
Expand Down Expand Up @@ -332,7 +337,14 @@ public void init(FilterConfig config) throws ServletException
configurationKey = NativeWebSocketConfiguration.class.getName();
}

if (configuration == null)
if (durableConfiguration)
{
Objects.requireNonNull(this.configuration);
// We have a NativeWebSocketConfiguration already present, make sure it exists on the ServletContext
config.getServletContext().setAttribute(configurationKey, this.configuration);

}
else
{
this.configuration = (NativeWebSocketConfiguration)config.getServletContext().getAttribute(configurationKey);
if (this.configuration == null)
Expand All @@ -342,18 +354,10 @@ public void init(FilterConfig config) throws ServletException
NativeWebSocketConfiguration.class.getName() + " at ServletContext attribute '" + configurationKey + "'");
}
}
else
{
// We have a NativeWebSocketConfiguration already present, make sure it exists on the ServletContext
if (config.getServletContext().getAttribute(configurationKey) == null)
{
config.getServletContext().setAttribute(configurationKey, this.configuration);
}
}

if (!this.configuration.isRunning())
{
localConfiguration = true;
managedConfiguration = true;
this.configuration.start();
}

Expand Down

0 comments on commit 176e58d

Please sign in to comment.