Skip to content

Commit

Permalink
Issue #1100 - ensure init and destroy are always called on JSR356 Enc…
Browse files Browse the repository at this point in the history
…oders

Signed-off-by: Lachlan Roberts <[email protected]>
  • Loading branch information
lachlan-roberts committed Jun 22, 2020
1 parent a03a352 commit 0db2088
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ protected void doStart() throws Exception
protected void doStop() throws Exception
{
ShutdownThread.deregister(this);
this.encoderFactory.destroy();
this.decoderFactory.destroy();
endpointClientMetadataCache.clear();
super.doStop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@
public interface Configurable
{
void init(EndpointConfig config);

void destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public void init(EndpointConfig config)
{
this.decoder.init(config);
}

@Override
public void destroy()
{
this.decoder.destroy();
}
}

private static final Logger LOG = Log.getLogger(DecoderFactory.class);
Expand Down Expand Up @@ -185,6 +191,17 @@ public void init(EndpointConfig config)
}
}

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,21 @@ public void init(EndpointConfig config)
{
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)
{
Expand Down Expand Up @@ -153,31 +160,39 @@ public Wrapper getWrapperFor(Class<?> type)
@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)
{
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ public void init(EndpointConfig config)
decoderFactory.init(config);
}

@Override
public void destroy()
{
encoderFactory.destroy();
decoderFactory.destroy();
}

@Override
public void removeMessageHandler(MessageHandler handler)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public final void onClose(CloseInfo close)
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);
Expand Down

0 comments on commit 0db2088

Please sign in to comment.