diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java index be1c5041d5c..3d31408048e 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java @@ -96,7 +96,19 @@ public class ServletContextHandler extends ContextHandler public final static int GZIP=4; public final static int NO_SESSIONS=0; public final static int NO_SECURITY=0; - + + public static ServletContextHandler getServletContextHandler(ServletContext servletContext, String purpose) throws ServletException + { + ContextHandler contextHandler = ContextHandler.getContextHandler(servletContext); + if (contextHandler == null) + throw new ServletException("Not running on Jetty, " + purpose+ " unavailable"); + + if (!(contextHandler instanceof ServletContextHandler)) + throw new ServletException("Not running in Jetty ServletContextHandler, " + purpose + " unavailable"); + + return (ServletContextHandler)contextHandler; + } + public interface ServletContainerInitializerCaller extends LifeCycle {}; protected final DecoratedObjectFactory _objFactory; diff --git a/jetty-websocket/javax-websocket-server/src/main/java/org/eclipse/jetty/websocket/javax/server/JavaxWebSocketServerContainer.java b/jetty-websocket/javax-websocket-server/src/main/java/org/eclipse/jetty/websocket/javax/server/JavaxWebSocketServerContainer.java index 90626fca3e5..18ac6e302c1 100644 --- a/jetty-websocket/javax-websocket-server/src/main/java/org/eclipse/jetty/websocket/javax/server/JavaxWebSocketServerContainer.java +++ b/jetty-websocket/javax-websocket-server/src/main/java/org/eclipse/jetty/websocket/javax/server/JavaxWebSocketServerContainer.java @@ -24,8 +24,6 @@ import java.util.List; import java.util.concurrent.Executor; import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.websocket.DeploymentException; import javax.websocket.EndpointConfig; @@ -80,12 +78,13 @@ public class JavaxWebSocketServerContainer if (!(handler instanceof ServletContextHandler)) return null; - return (javax.websocket.WebSocketContainer)handler.getServletContext().getAttribute(ServerContainer.class.getName()); + return (javax.websocket.WebSocketContainer)handler.getServletContext().getAttribute("javax.websocket.server.ServerContainer"); } public static JavaxWebSocketServerContainer ensureContainer(ServletContext servletContext) throws ServletException { - ContextHandler contextHandler = ContextHandler.getContextHandler(servletContext); + ContextHandler contextHandler = ServletContextHandler.getServletContextHandler(servletContext, "Javax Websocket"); + JavaxWebSocketServerContainer container = contextHandler.getBean(JavaxWebSocketServerContainer.class); if (container==null) { @@ -116,7 +115,7 @@ public class JavaxWebSocketServerContainer return container; } - private final WebSocketMapping _webSocketMapping; + private final WebSocketMapping webSocketMapping; private final JavaxWebSocketServerFrameHandlerFactory frameHandlerFactory; private final Executor executor; private final FrameHandler.ConfigurationCustomizer customizer = new FrameHandler.ConfigurationCustomizer(); @@ -138,7 +137,7 @@ public class JavaxWebSocketServerContainer client.getHttpClient().setExecutor(executor); return client; }); - this._webSocketMapping = webSocketMapping; + this.webSocketMapping = webSocketMapping; this.executor = executor; this.frameHandlerFactory = new JavaxWebSocketServerFrameHandlerFactory(this); } @@ -148,7 +147,7 @@ public class JavaxWebSocketServerContainer { ContextHandler contextHandler = (ContextHandler) context; JavaxWebSocketServerContainer container = contextHandler.getBean(JavaxWebSocketServerContainer.class); - if (container==null) + if (container==this) { contextHandler.removeBean(container); LifeCycle.stop(container); @@ -159,7 +158,7 @@ public class JavaxWebSocketServerContainer @Override public ByteBufferPool getBufferPool() { - return this._webSocketMapping.getBufferPool(); + return this.webSocketMapping.getBufferPool(); } @Override @@ -171,7 +170,7 @@ public class JavaxWebSocketServerContainer @Override public WebSocketExtensionRegistry getExtensionRegistry() { - return this._webSocketMapping.getExtensionRegistry(); + return this.webSocketMapping.getExtensionRegistry(); } @Override @@ -183,7 +182,7 @@ public class JavaxWebSocketServerContainer @Override public DecoratedObjectFactory getObjectFactory() { - return this._webSocketMapping.getObjectFactory(); + return this.webSocketMapping.getObjectFactory(); } @Override @@ -269,10 +268,11 @@ public class JavaxWebSocketServerContainer { frameHandlerFactory.getMetadata(config.getEndpointClass(), config); - JavaxWebSocketCreator creator = new JavaxWebSocketCreator(this, config, this._webSocketMapping + JavaxWebSocketCreator creator = new JavaxWebSocketCreator(this, config, this.webSocketMapping .getExtensionRegistry()); - this._webSocketMapping.addMapping(new UriTemplatePathSpec(config.getPath()), creator, frameHandlerFactory, customizer); + this.webSocketMapping + .addMapping(new UriTemplatePathSpec(config.getPath()), creator, frameHandlerFactory, customizer); } @Override diff --git a/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/JettyWebSocketFrameHandlerFactory.java b/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/JettyWebSocketFrameHandlerFactory.java index e7b94df339e..d9173bce8aa 100644 --- a/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/JettyWebSocketFrameHandlerFactory.java +++ b/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/JettyWebSocketFrameHandlerFactory.java @@ -18,6 +18,23 @@ package org.eclipse.jetty.websocket.common; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.lang.annotation.Annotation; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.nio.ByteBuffer; +import java.time.Duration; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executor; + import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.websocket.api.InvalidWebSocketException; import org.eclipse.jetty.websocket.api.Session; @@ -46,24 +63,21 @@ import org.eclipse.jetty.websocket.common.message.StringMessageSink; import org.eclipse.jetty.websocket.common.util.ReflectUtils; import org.eclipse.jetty.websocket.core.Frame; -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.lang.annotation.Annotation; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.nio.ByteBuffer; -import java.time.Duration; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executor; - /** + * Factory to create {@link JettyWebSocketFrameHandler} instances suitable for + * use with jetty-native websocket API. + *
+ * Will create a {@link FrameHandler} suitable for use with classes/objects that: + *
+ *