Making WebSocketClient connection stuff pluggable via factory methods

This commit is contained in:
Joakim Erdfelt 2013-02-22 10:43:40 -07:00
parent d1cac944b8
commit 12814b02ed
3 changed files with 31 additions and 8 deletions

View File

@ -188,7 +188,7 @@ public class WebSocketClient extends ContainerLifeCycle
cookieStore = new HttpCookieStore.Empty();
}
this.connectionManager = new ConnectionManager(this);
this.connectionManager = newConnectionManager(this);
addBean(this.connectionManager);
super.doStart();
@ -300,6 +300,18 @@ public class WebSocketClient extends ContainerLifeCycle
return extensions;
}
/**
* Factory method for new ConnectionManager (used by other projects like cometd)
*
* @param client
* the client used to create the {@link ConnectionManager}
* @return the ConnectionManager instance to use
*/
protected ConnectionManager newConnectionManager(WebSocketClient client)
{
return new ConnectionManager(this);
}
public void setBindAdddress(SocketAddress bindAddress)
{
this.bindAddress = bindAddress;

View File

@ -176,7 +176,7 @@ public class ConnectionManager extends ContainerLifeCycle
@Override
protected void doStart() throws Exception
{
selector = new WebSocketClientSelectorManager(client.getBufferPool(),client.getExecutor(),client.getScheduler(),client.getPolicy());
selector = new WebSocketClientSelectorManager(client);
selector.setSslContextFactory(client.getSslContextFactory());
selector.setConnectTimeout(client.getConnectTimeout());
addBean(selector);
@ -209,6 +209,18 @@ public class ConnectionManager extends ContainerLifeCycle
return false;
}
/**
* Factory method for new WebSocketClientSelectorManager (used by other projects like cometd)
*
* @param client
* the client used to create the WebSocketClientSelectorManager
* @return the new WebSocketClientSelectorManager
*/
protected WebSocketClientSelectorManager newWebSocketClientSelectorManager(WebSocketClient client)
{
return new WebSocketClientSelectorManager(client);
}
public void removeSession(WebSocketSession session)
{
sessions.remove(session);

View File

@ -34,7 +34,6 @@ import org.eclipse.jetty.io.ssl.SslConnection;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.client.WebSocketClient;
@ -45,17 +44,17 @@ public class WebSocketClientSelectorManager extends SelectorManager
private final ByteBufferPool bufferPool;
private SslContextFactory sslContextFactory;
public WebSocketClientSelectorManager(ByteBufferPool bufferPool, Executor executor, Scheduler scheduler, WebSocketPolicy policy)
public WebSocketClientSelectorManager(WebSocketClient client)
{
super(executor,scheduler);
this.bufferPool = bufferPool;
this.policy = policy;
super(client.getExecutor(),client.getScheduler());
this.bufferPool = client.getBufferPool();
this.policy = client.getPolicy();
}
@Override
protected void connectionFailed(SocketChannel channel, Throwable ex, Object attachment)
{
LOG.info("Connection Failed",ex);
LOG.debug("Connection Failed",ex);
ConnectPromise connect = (ConnectPromise)attachment;
connect.failed(ex);
}