JSR-356 - making SessionFactory additive to allow multiple impls at the same time.

This commit is contained in:
Joakim Erdfelt 2013-07-29 09:30:42 -07:00
parent a47b92ac0e
commit b363400c84
5 changed files with 59 additions and 15 deletions

View File

@ -25,6 +25,7 @@ import org.eclipse.jetty.websocket.common.LogicalConnection;
import org.eclipse.jetty.websocket.common.SessionFactory;
import org.eclipse.jetty.websocket.common.WebSocketSession;
import org.eclipse.jetty.websocket.common.events.EventDriver;
import org.eclipse.jetty.websocket.jsr356.endpoints.AbstractJsrEventDriver;
public class JsrSessionFactory implements SessionFactory
{
@ -46,4 +47,10 @@ public class JsrSessionFactory implements SessionFactory
{
return String.format("websocket-%d",idgen.incrementAndGet());
}
@Override
public boolean supports(EventDriver websocket)
{
return (websocket instanceof AbstractJsrEventDriver);
}
}

View File

@ -148,7 +148,7 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
EventDriverFactory eventDriverFactory = this.webSocketServletFactory.getEventDriverFactory();
eventDriverFactory.addImplementation(new JsrServerEndpointImpl());
eventDriverFactory.addImplementation(new JsrEndpointImpl());
this.webSocketServletFactory.setSessionFactory(new JsrSessionFactory(this));
this.webSocketServletFactory.addSessionFactory(new JsrSessionFactory(this));
}
@Override

View File

@ -27,5 +27,7 @@ import org.eclipse.jetty.websocket.common.events.EventDriver;
*/
public interface SessionFactory
{
public boolean supports(EventDriver websocket);
public WebSocketSession createSession(URI requestURI, EventDriver websocket, LogicalConnection connection);
}

View File

@ -21,12 +21,20 @@ package org.eclipse.jetty.websocket.common;
import java.net.URI;
import org.eclipse.jetty.websocket.common.events.EventDriver;
import org.eclipse.jetty.websocket.common.events.JettyAnnotatedEventDriver;
import org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver;
/**
* Default Session factory, creating WebSocketSession objects.
*/
public class WebSocketSessionFactory implements SessionFactory
{
@Override
public boolean supports(EventDriver websocket)
{
return (websocket instanceof JettyAnnotatedEventDriver) || (websocket instanceof JettyListenerEventDriver);
}
@Override
public WebSocketSession createSession(URI requestURI, EventDriver websocket, LogicalConnection connection)
{

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.websocket.server;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
@ -43,6 +44,7 @@ import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.websocket.api.InvalidWebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory;
@ -99,7 +101,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
private final EventDriverFactory eventDriverFactory;
private final WebSocketExtensionFactory extensionFactory;
private List<WebSocketServerFactory.Listener> listeners = new ArrayList<>();
private SessionFactory sessionFactory;
private List<SessionFactory> sessionFactories;
private WebSocketCreator creator;
private List<Class<?>> registeredSocketClasses;
@ -123,7 +125,8 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
this.basePolicy = policy;
this.eventDriverFactory = new EventDriverFactory(basePolicy);
this.extensionFactory = new WebSocketExtensionFactory(basePolicy,bufferPool);
this.sessionFactory = new WebSocketSessionFactory();
this.sessionFactories = new ArrayList<>();
this.sessionFactories.add(new WebSocketSessionFactory());
this.creator = this;
// Create supportedVersions
@ -165,7 +168,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
context = new UpgradeContext();
setActiveUpgradeContext(context);
}
context.setRequest(sockreq);
context.setResponse(sockresp);
@ -199,6 +202,15 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
listeners.add(listener);
}
public void addSessionFactory(SessionFactory sessionFactory)
{
if (sessionFactories.contains(sessionFactory))
{
return;
}
this.sessionFactories.add(sessionFactory);
}
@Override
public void cleanup()
{
@ -227,6 +239,31 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
return new WebSocketServerFactory(policy);
}
private WebSocketSession createSession(URI requestURI, EventDriver websocket, LogicalConnection connection)
{
if (websocket == null)
{
throw new InvalidWebSocketException("Unable to create Session from null websocket");
}
for (SessionFactory impl : sessionFactories)
{
if (impl.supports(websocket))
{
try
{
return impl.createSession(requestURI,websocket,connection);
}
catch (Throwable e)
{
throw new InvalidWebSocketException("Unable to create Session",e);
}
}
}
throw new InvalidWebSocketException("Unable to create Session: unrecognized internal EventDriver type: " + websocket.getClass().getName());
}
/**
* Default Creator logic
*/
@ -303,11 +340,6 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
return basePolicy;
}
public SessionFactory getSessionFactory()
{
return sessionFactory;
}
@Override
public void init() throws Exception
{
@ -415,11 +447,6 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
this.creator = creator;
}
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
/**
* Upgrade the request/response to a WebSocket Connection.
* <p>
@ -488,7 +515,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
}
// Setup Session
WebSocketSession session = sessionFactory.createSession(request.getRequestURI(),driver,connection);
WebSocketSession session = createSession(request.getRequestURI(),driver,connection);
session.setPolicy(getPolicy().clonePolicy());
session.setUpgradeRequest(request);
response.setExtensions(extensionStack.getNegotiatedExtensions());