Issue #1100 - ensure init and destroy are always called on JSR356 Encoders

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-06-22 11:40:55 +10:00
parent a03a352d79
commit 0db20886d0
6 changed files with 54 additions and 7 deletions

View File

@ -308,6 +308,8 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
protected void doStop() throws Exception
{
ShutdownThread.deregister(this);
this.encoderFactory.destroy();
this.decoderFactory.destroy();
endpointClientMetadataCache.clear();
super.doStop();
}

View File

@ -26,4 +26,6 @@ import javax.websocket.EndpointConfig;
public interface Configurable
{
void init(EndpointConfig config);
void destroy();
}

View File

@ -69,6 +69,12 @@ public class DecoderFactory implements Configurable
{
this.decoder.init(config);
}
@Override
public void destroy()
{
this.decoder.destroy();
}
}
private static final Logger LOG = Log.getLogger(DecoderFactory.class);
@ -185,6 +191,17 @@ public class DecoderFactory implements Configurable
}
}
@Override
public void destroy()
{
for (Wrapper wrapper : activeWrappers.values())
{
wrapper.decoder.destroy();
}
activeWrappers.clear();
}
public Wrapper newWrapper(DecoderMetadata metadata)
{
Class<? extends Decoder> decoderClass = metadata.getCoderClass();

View File

@ -62,14 +62,21 @@ public class EncoderFactory implements Configurable
{
this.encoder.init(config);
}
@Override
public void destroy()
{
this.encoder.destroy();
}
}
private static final Logger LOG = Log.getLogger(EncoderFactory.class);
private final EncoderMetadataSet metadatas;
private final WebSocketContainerScope containerScope;
private EncoderFactory parentFactory;
private Map<Class<?>, Wrapper> activeWrappers;
private final Map<Class<?>, Wrapper> activeWrappers;
private final EncoderFactory parentFactory;
private EndpointConfig endpointConfig;
public EncoderFactory(WebSocketContainerScope containerScope, EncoderMetadataSet metadatas)
{
@ -153,10 +160,9 @@ public class EncoderFactory implements Configurable
@Override
public void init(EndpointConfig config)
{
this.endpointConfig = config;
if (LOG.isDebugEnabled())
{
LOG.debug("init({})", config);
}
LOG.debug("init({})", endpointConfig);
// Instantiate all declared encoders
for (EncoderMetadata metadata : metadatas)
@ -164,20 +170,29 @@ public class EncoderFactory implements Configurable
Wrapper wrapper = newWrapper(metadata);
activeWrappers.put(metadata.getObjectType(), wrapper);
}
}
// Initialize all encoders
@Override
public void destroy()
{
for (Wrapper wrapper : activeWrappers.values())
{
wrapper.encoder.init(config);
wrapper.encoder.destroy();
}
activeWrappers.clear();
}
private Wrapper newWrapper(EncoderMetadata metadata)
{
if (endpointConfig == null)
throw new IllegalStateException("EndpointConfig not set");
Class<? extends Encoder> encoderClass = metadata.getCoderClass();
try
{
Encoder encoder = containerScope.getObjectFactory().createInstance(encoderClass);
encoder.init(endpointConfig);
return new Wrapper(encoder, metadata);
}
catch (Exception e)

View File

@ -306,6 +306,13 @@ public class JsrSession extends WebSocketSession implements javax.websocket.Sess
decoderFactory.init(config);
}
@Override
public void destroy()
{
encoderFactory.destroy();
decoderFactory.destroy();
}
@Override
public void removeMessageHandler(MessageHandler handler)
{

View File

@ -77,6 +77,10 @@ public abstract class AbstractJsrEventDriver extends AbstractEventDriver
CloseCode closecode = CloseCodes.getCloseCode(close.getStatusCode());
CloseReason closereason = new CloseReason(closecode, close.getReason());
onClose(closereason);
// Destroy the JsrSession.
if (jsrsession != null)
jsrsession.destroy();
}
protected abstract void onClose(CloseReason closereason);