Adding back ENABLE attribute for JSR-356

This commit is contained in:
Joakim Erdfelt 2013-09-09 18:51:12 -07:00
parent c0f5c5a659
commit d3421070cb
2 changed files with 56 additions and 14 deletions

View File

@ -30,50 +30,87 @@ import org.eclipse.jetty.websocket.jsr356.server.deploy.ServerEndpointAnnotation
import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter;
/** /**
* WebSocket Server Configuration component * WebSocket Server Configuration component. This configuration will configure a context for JSR356 Websockets if the attribute
* "org.eclipse.jetty.websocket.jsr356" is set to true. This attribute may be set on an individual context or on the server to affect all deployed contexts.
*/ */
public class WebSocketConfiguration extends AbstractConfiguration public class WebSocketConfiguration extends AbstractConfiguration
{ {
public static final String ENABLE = "org.eclipse.jetty.websocket.jsr356";
private static final Logger LOG = Log.getLogger(WebSocketConfiguration.class); private static final Logger LOG = Log.getLogger(WebSocketConfiguration.class);
/**
* Create a ServerContainer properly, useful for embedded application use.
* <p>
* Notably, the cometd3 project uses this.
*
* @param context
* the context to enable javax.websocket support filters on
* @return the ServerContainer that was created
*/
public static ServerContainer configureContext(ServletContextHandler context) public static ServerContainer configureContext(ServletContextHandler context)
{ {
LOG.debug("Configure javax.websocket for WebApp {}",context);
WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configureContext(context); WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configureContext(context);
// Create the Jetty ServerContainer implementation // Create the Jetty ServerContainer implementation
ServerContainer jettyContainer = new ServerContainer(filter,filter.getFactory()); ServerContainer jettyContainer = new ServerContainer(filter,filter.getFactory());
context.addBean(jettyContainer); context.addBean(jettyContainer);
// Store a reference to the ServerContainer per javax.websocket spec 1.0 final section 6.4 Programmatic Server Deployment // Store a reference to the ServerContainer per javax.websocket spec 1.0 final section 6.4 Programmatic Server Deployment
context.setAttribute(javax.websocket.server.ServerContainer.class.getName(),jettyContainer); context.setAttribute(javax.websocket.server.ServerContainer.class.getName(),jettyContainer);
// Store reference to DiscoveredEndpoints // Store reference to DiscoveredEndpoints
context.setAttribute(DiscoveredEndpoints.class.getName(),new DiscoveredEndpoints()); context.setAttribute(DiscoveredEndpoints.class.getName(),new DiscoveredEndpoints());
return jettyContainer; return jettyContainer;
} }
public static boolean isJSR356Context(WebAppContext context)
{
Object enable = context.getAttribute(ENABLE);
if (enable instanceof Boolean)
{
}
enable = context.getServer().getAttribute(ENABLE);
if (enable instanceof Boolean)
{
return ((Boolean)enable).booleanValue();
}
return true;
}
@Override @Override
public void configure(WebAppContext context) throws Exception public void configure(WebAppContext context) throws Exception
{ {
LOG.debug("Configure javax.websocket for WebApp {}",context); if (isJSR356Context(context))
WebSocketConfiguration.configureContext(context); {
WebSocketConfiguration.configureContext(context);
}
else
{
LOG.debug("JSR-356 support disabled for WebApp {}",context);
}
} }
@Override @Override
public void preConfigure(WebAppContext context) throws Exception public void preConfigure(WebAppContext context) throws Exception
{ {
boolean scanningAdded = false; if (isJSR356Context(context))
// Add the annotation scanning handlers (if annotation scanning enabled)
for (Configuration config : context.getConfigurations())
{ {
if (config instanceof AnnotationConfiguration) boolean scanningAdded = false;
// Add the annotation scanning handlers (if annotation scanning enabled)
for (Configuration config : context.getConfigurations())
{ {
AnnotationConfiguration annocfg = (AnnotationConfiguration)config; if (config instanceof AnnotationConfiguration)
annocfg.addDiscoverableAnnotationHandler(new ServerEndpointAnnotationHandler(context)); {
scanningAdded = true; AnnotationConfiguration annocfg = (AnnotationConfiguration)config;
annocfg.addDiscoverableAnnotationHandler(new ServerEndpointAnnotationHandler(context));
scanningAdded = true;
}
} }
LOG.debug("@ServerEndpoint scanning added: {}",scanningAdded);
} }
LOG.debug("@ServerEndpoint scanning added: {}", scanningAdded);
} }
} }

View File

@ -35,7 +35,9 @@ import javax.websocket.server.ServerEndpointConfig;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.jsr356.server.ServerContainer; import org.eclipse.jetty.websocket.jsr356.server.ServerContainer;
import org.eclipse.jetty.websocket.jsr356.server.WebSocketConfiguration;
import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter;
@HandlesTypes( @HandlesTypes(
@ -47,6 +49,9 @@ public class ServerApplicationConfigListener implements ServletContainerInitiali
@Override @Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException
{ {
if (!WebSocketConfiguration.isJSR356Context(WebAppContext.getCurrentWebAppContext()))
return;
WebSocketUpgradeFilter filter = (WebSocketUpgradeFilter)ctx.getAttribute(WebSocketUpgradeFilter.class.getName()); WebSocketUpgradeFilter filter = (WebSocketUpgradeFilter)ctx.getAttribute(WebSocketUpgradeFilter.class.getName());
if (filter == null) if (filter == null)
{ {